1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Christoph Renner |
---|
13 | co-programmer: |
---|
14 | */ |
---|
15 | |
---|
16 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
17 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
18 | */ |
---|
19 | #define DEBUG_MODULE_NETWORK |
---|
20 | |
---|
21 | |
---|
22 | /* include your own header */ |
---|
23 | #include "server_socket.h" |
---|
24 | |
---|
25 | /* header for debug output */ |
---|
26 | #include "debug.h" |
---|
27 | |
---|
28 | |
---|
29 | ServerSocket::ServerSocket( ConnectionType connectionType ) |
---|
30 | { |
---|
31 | this->connectionType = connectionType; |
---|
32 | init(); |
---|
33 | } |
---|
34 | |
---|
35 | |
---|
36 | ServerSocket::ServerSocket( ConnectionType connectionType, unsigned int port ) |
---|
37 | { |
---|
38 | this->connectionType = connectionType; |
---|
39 | init(); |
---|
40 | listen(port); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | ServerSocket::ServerSocket() |
---|
45 | { |
---|
46 | this->connectionType == NET_TCP; |
---|
47 | init(); |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | ServerSocket::ServerSocket( unsigned int port ) |
---|
52 | { |
---|
53 | this->connectionType == NET_TCP; |
---|
54 | init(); |
---|
55 | listen(port); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | /** |
---|
60 | * Default destructor |
---|
61 | */ |
---|
62 | ServerSocket::~ServerSocket( ) |
---|
63 | { |
---|
64 | /* Quit SDL_net */ |
---|
65 | // NOTE: what if other instances of NetworkSocket running? |
---|
66 | SDLNet_Quit(); |
---|
67 | PRINTF(5)("SDL_net shutdown\n"); |
---|
68 | |
---|
69 | _isListening = false; |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | void ServerSocket::init( ) |
---|
76 | { |
---|
77 | /* set the class id for the base object */ |
---|
78 | this->setClassID(CL_SERVER_SOCKET, "ServerSocket"); |
---|
79 | |
---|
80 | terminateThread = false; |
---|
81 | listenSocket = NULL; |
---|
82 | _isListening = false; |
---|
83 | |
---|
84 | if(SDLNet_Init()==-1) |
---|
85 | { |
---|
86 | PRINTF(1)("SDLNet_Init: %s\n", SDLNet_GetError()); |
---|
87 | return; |
---|
88 | } |
---|
89 | else |
---|
90 | PRINTF(5)("SDL_net initialized\n"); |
---|
91 | |
---|
92 | PRINTF(0)("ServerSocket created\n"); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | /** |
---|
97 | * Tells the NetworkSocket to listen on a specific port for incoming connections. |
---|
98 | * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection. |
---|
99 | * @param port |
---|
100 | */ |
---|
101 | bool ServerSocket::listen(unsigned int port) |
---|
102 | { |
---|
103 | PRINTF(0)("ServerSocket::listen()\n"); |
---|
104 | _isListening = true; |
---|
105 | //check if not already connected or listening |
---|
106 | if (listenSocket) |
---|
107 | { |
---|
108 | PRINTF(1)("ServerSocket::listen: tcpSocket!=NULL! maybe you already called listen or did not call close()!\n"); |
---|
109 | _isListening = false; |
---|
110 | return false; |
---|
111 | } |
---|
112 | |
---|
113 | IPaddress ip; |
---|
114 | |
---|
115 | if (SDLNet_ResolveHost(&ip, NULL, port)==-1) |
---|
116 | { |
---|
117 | PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
---|
118 | _isListening = false; |
---|
119 | return false; |
---|
120 | } |
---|
121 | |
---|
122 | if( this->connectionType == NET_TCP) |
---|
123 | { |
---|
124 | listenSocketTCP = SDLNet_TCP_Open(&ip); |
---|
125 | if( !listenSocketTCP) { |
---|
126 | PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); |
---|
127 | _isListening = false; |
---|
128 | return false; |
---|
129 | } |
---|
130 | } |
---|
131 | else |
---|
132 | { |
---|
133 | listenSocketUDP = SDLNet_UDP_Open(port); |
---|
134 | PRINTF(1)("SDLNet_UDP_Open: %s\n", SDLNet_GetError()); |
---|
135 | _isListening = false; |
---|
136 | return false; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | return true; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | /** |
---|
145 | * returns a new NetworkSocket |
---|
146 | * @return new socket |
---|
147 | */ |
---|
148 | NetworkSocket* ServerSocket::getNewSocket( ) |
---|
149 | { |
---|
150 | if ( !listenSocket ) |
---|
151 | { |
---|
152 | PRINTF(1)("listenSocket == NULL! Maybe you forgot to call listen()\n"); |
---|
153 | close(); |
---|
154 | return NULL; |
---|
155 | } |
---|
156 | |
---|
157 | if( this->connectionType == NET_TCP) |
---|
158 | { |
---|
159 | TCPsocket sockTCP = SDLNet_TCP_Accept(this->listenSocketTCP); |
---|
160 | if( !sockTCP) |
---|
161 | return NULL; |
---|
162 | else |
---|
163 | return new NetworkSocket(sockTCP); |
---|
164 | } |
---|
165 | else |
---|
166 | { |
---|
167 | #warning UDP getNewSocket incomplet |
---|
168 | // UDPsocket sockUDP = SDLNet_UDP_Accept(this->listenSocketUDP); |
---|
169 | // if( !sockUDP) |
---|
170 | // return NULL; |
---|
171 | |
---|
172 | // else |
---|
173 | // return new NetworkSocket(); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | /** |
---|
179 | * closes the ServerSocket |
---|
180 | */ |
---|
181 | void ServerSocket::close( ) |
---|
182 | { |
---|
183 | if ( listenSocket ) |
---|
184 | { |
---|
185 | SDLNet_TCP_Close( listenSocket ); |
---|
186 | listenSocket = NULL; |
---|
187 | } |
---|
188 | |
---|
189 | _isListening = false; |
---|
190 | } |
---|
191 | |
---|