Changeset 5624 in orxonox.OLD for branches/network/src/lib
- Timestamp:
- Nov 17, 2005, 9:06:47 AM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/netdefs.h
r5605 r5624 7 7 */ 8 8 9 #ifndef _NETWORK_MANAGER 10 #define _NETWORK_MANAGER 11 12 9 #ifndef _NETDEFS 10 #define _NETDEFS 13 11 14 12 #ifdef HAVE_SDL_NET_H -
branches/network/src/lib/network/network_socket.cc
r5614 r5624 37 37 this->setClassID(CL_NETWORK_SOCKET, "NetworkSocket"); 38 38 39 tcpSocket = NULL; 40 bufferlength = 0; 41 42 mutex = SDL_CreateMutex(); 43 terminateThread = false; 44 39 45 /* Init SDL_net */ 46 //NOTE: do we need to call SDLNet_Init for all instances? 40 47 if(SDLNet_Init()==-1) 41 48 { … … 55 62 NetworkSocket::~ NetworkSocket( ) 56 63 { 64 SDL_DestroyMutex(mutex); 57 65 58 66 /* Quit SDL_net */ 67 // NOTE: what if other instances of NetworkSocket running? 59 68 SDLNet_Quit(); 60 69 PRINTF(5)("SDL_net shutdown\n"); … … 62 71 63 72 /** 64 * This function establishes a TCP/UDP connection to a given server (function argument). 65 * It is called by the NetworkStream. It creates a TCP/UDP socket for the connection. 66 */ 73 * This function establishes a TCP/UDP connection to a given server (function argument). 74 * It is called by the NetworkStream. It creates a TCP/UDP socket for the connection. 75 * @param ip 76 * @param port 77 */ 67 78 void NetworkSocket::connectToServer(IPaddress ip, unsigned int port) 68 79 { 80 //check if not already connected or listening 81 if (tcpSocket) 82 { 83 PRINTF(1)("NetworkSocket::listen: tcpSocket!=NULL! maybe you already called listen or connectToServer or did not call disconnectServer()!"); 84 } 69 85 70 86 /* Connect to the host and port contained in ip using a TCP connection. */ … … 76 92 } 77 93 78 } 79 80 /** 81 * Tells the NetworkSocket to listen on a specific port for incoming connections. 82 * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection. 83 */ 84 void listen(unsigned int port) 85 { 94 SDL_CreateThread(thread_read, (void*)this); 95 } 96 97 /** 98 * Tells the NetworkSocket to listen on a specific port for incoming connections. 99 * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection. 100 * @param port 101 */ 102 void NetworkSocket::listen(unsigned int port) 103 { 104 //check if not already connected or listening 105 if (tcpSocket) 106 { 107 PRINTF(1)("NetworkSocket::listen: tcpSocket!=NULL! maybe you already called listen or connectToServer or did not call disconnectServer()!\n"); 108 return; 109 } 110 111 IPaddress ip; 112 113 if (SDLNet_ResolveHost(&ip, NULL, port)==-1) 114 { 115 PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); 116 return; 117 } 118 119 tcpSocket = SDLNet_TCP_Open(&ip); 120 121 if (!tcpSocket) 122 { 123 PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); 124 return; 125 } 126 127 SDL_CreateThread(thread_listen, (void*)this); 86 128 } 87 129 … … 91 133 void NetworkSocket::disconnectServer( ) 92 134 { 93 135 terminateThread = true; 94 136 /* Close the connection */ 95 137 SDLNet_TCP_Close(tcpSocket); 96 97 } 98 99 /** 100 * This function writes some bytes (data) to the network connection (if the connection is already 101 * estabilhed) otherwise it just does nothing (silently discarding the data). And writes some 102 * warnings 103 */ 104 int NetworkSocket::writeBytes(byte* data, int length) 105 { 106 } 107 108 /** 109 * Reads in the bytes from the network interface and passes it to the NetworkStream. 110 * This function must internaly be implemented/connected as a thread, since the read 111 * functions of many network libraries are blocking an would therefore block the whole 112 * program. 113 * From outside, the thread shouldn't be accessible at all. 114 */ 138 tcpSocket = NULL; 139 140 } 141 142 143 /** 144 * This function writes some bytes (data) to the network connection (if the connection is already 145 * estabilhed) otherwise it just does nothing (silently discarding the data). And writes some 146 * warnings 147 * @param data: pointer to the data to send 148 * @param length: n bytes to send 149 * @return the number successfully written bytes 150 */ 151 int NetworkSocket::writeBytes(byte * data, int length) 152 { 153 if (!tcpSocket || data==NULL) 154 return 0; 155 156 int res = SDLNet_TCP_Send(tcpSocket, data, length); 157 158 if (res<length) 159 { 160 PRINTF(1)("SDLNet_TCP_Send: %s\n", SDLNet_GetError()); 161 return res; 162 } 163 } 164 165 /** 166 * Reads in the bytes from the network interface and passes it to the NetworkStream. 167 * This function must internaly be implemented/connected as a thread, since the read 168 * functions of many network libraries are blocking an would therefore block the whole 169 * program. 170 * From outside, the thread shouldn't be accessible at all. 171 * @param data: pointer to memory, big enough to store length bytes 172 * @param length: n bytes to read 173 * @return the number successfully read bytes. -1 on error. may be less than length! 174 */ 115 175 int NetworkSocket::readBytes(byte * data, int length) 116 176 { 117 } 177 if (!tcpSocket || data==NULL) 178 return 0; 179 180 int nbytes = (length<bufferlength) ? length : bufferlength; 181 182 // just in case ... 183 if (nbytes<0) 184 return -1; 185 186 if (nbytes==0) 187 return 0; 188 189 SDL_mutexP(mutex); 190 191 memcpy(data, buf, nbytes); 192 193 //important: use memmove because the memory areas may overlap 194 memmove(buf, buf+nbytes, bufferlength-nbytes); 195 bufferlength -= nbytes; 196 197 SDL_mutexV(mutex); 198 199 return nbytes; 200 } 201 202 /** 203 * used to create a thread to listen 204 * will call thrad_read when established connection 205 * @param data: pointer to NetwortSocket 206 */ 207 int NetworkSocket::thread_listen( void * data ) 208 { 209 NetworkSocket * self = (NetworkSocket*)data; 210 TCPsocket tempsocket; 211 212 tempsocket = SDLNet_TCP_Accept(self->tcpSocket); 213 214 SDLNet_TCP_Close(self->tcpSocket); 215 self->tcpSocket = NULL; 216 217 if (!tempsocket) 218 { 219 PRINTF(1)("SDLNet_TCP_Accept: %s\n", SDLNet_GetError()); 220 return -1; 221 } 222 223 self->tcpSocket = tempsocket; 224 225 return thread_read(data); 226 } 227 228 /** 229 * used to create a thread to read from socket 230 * @param data: pointer to NetworkSocket 231 */ 232 int NetworkSocket::thread_read( void * data ) 233 { 234 int nbytesread = 0; 235 int nbytestoread = 0; 236 char buffer[_LOCAL_BUFFER_SIZE]; 237 NetworkSocket * self = (NetworkSocket*)data; 238 239 while (!self->terminateThread) 240 { 241 #define min(a,b) (a<b)?a:b 242 nbytestoread = min(_INCOMING_BUFFER_SIZE - self->bufferlength, _LOCAL_BUFFER_SIZE); 243 244 nbytesread = SDLNet_TCP_Recv(self->tcpSocket, buffer, nbytestoread); 245 246 SDL_mutexP(self->mutex); 247 248 if (nbytesread<=0) 249 { 250 PRINTF(1)("SDLNet_TCP_Recv: %s\n", SDLNet_GetError()); 251 SDLNet_TCP_Close(self->tcpSocket); 252 self->tcpSocket = NULL; 253 SDL_mutexV(self->mutex); 254 return -1; 255 } 256 257 memcpy(self->buf+self->bufferlength, buffer, nbytesread); 258 self->bufferlength += nbytesread; 259 260 SDL_mutexV(self->mutex); 261 } 262 263 return 0; 264 } 265 266 -
branches/network/src/lib/network/network_socket.h
r5614 r5624 7 7 #ifndef _NETWORK_SOCKET 8 8 #define _NETWORK_SOCKET 9 10 #define _INCOMING_BUFFER_SIZE 10240 11 #define _LOCAL_BUFFER_SIZE 1024 12 13 /* contains memmove and memcpy */ 14 #include <string.h> 15 16 #include <SDL_thread.h> 9 17 10 18 /* include this file, it contains some default definitions */ … … 22 30 23 31 private: 24 IPaddress serverAddress;25 unsigned int port;32 // IPaddress serverAddress; 33 // unsigned int port; 26 34 TCPsocket tcpSocket; 27 UDPsocket udpSocket; 35 // UDPsocket udpSocket; 36 37 byte buf[_INCOMING_BUFFER_SIZE]; 38 int bufferlength; 39 40 SDL_mutex * mutex; 41 bool terminateThread; 42 43 static int thread_listen(void * data); 44 static int thread_read(void * data); 28 45 29 46 public:
Note: See TracChangeset
for help on using the changeset viewer.