Changeset 5630 in orxonox.OLD for branches/network
- Timestamp:
- Nov 18, 2005, 2:21:30 PM (19 years ago)
- Location:
- branches/network/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/network_socket.cc
r5628 r5630 38 38 39 39 tcpSocket = NULL; 40 bufferlength = 0; 41 42 mutex = SDL_CreateMutex(); 43 socketmutex = SDL_CreateMutex(); 40 incomingBufferLength = 0; 41 outgoingBufferLength = 0; 42 43 incomingBufferMutex = SDL_CreateMutex(); 44 outgoingBufferMutex = SDL_CreateMutex(); 45 socketMutex = SDL_CreateMutex(); 44 46 terminateThread = false; 45 47 … … 68 70 PRINTF(5)("SDL_net shutdown\n"); 69 71 70 SDL_DestroyMutex(mutex); 71 SDL_DestroyMutex(socketmutex); 72 _isListening = false; 73 74 SDL_DestroyMutex(incomingBufferMutex); 75 SDL_DestroyMutex(outgoingBufferMutex); 76 SDL_DestroyMutex(socketMutex); 72 77 } 73 78 … … 94 99 } 95 100 101 _isListening = false; 102 96 103 SDL_CreateThread(thread_read, (void*)this); 104 SDL_CreateThread(thread_write, (void*)this); 97 105 } 98 106 … … 104 112 void NetworkSocket::listen(unsigned int port) 105 113 { 114 _isListening = true; 106 115 //check if not already connected or listening 107 116 if (tcpSocket) 108 117 { 109 118 PRINTF(1)("NetworkSocket::listen: tcpSocket!=NULL! maybe you already called listen or connectToServer or did not call disconnectServer()!\n"); 119 _isListening = false; 110 120 return; 111 121 } … … 116 126 { 117 127 PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); 128 _isListening = false; 118 129 return; 119 130 } … … 124 135 { 125 136 PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); 137 _isListening = false; 126 138 return; 127 139 } 128 140 129 141 SDL_CreateThread(thread_listen, (void*)this); 142 SDL_CreateThread(thread_write, (void*)this); 130 143 } 131 144 … … 138 151 /* Close the connection */ 139 152 140 SDL_mutexP(socket mutex);153 SDL_mutexP(socketMutex); 141 154 SDLNet_TCP_Close(tcpSocket); 142 155 tcpSocket = NULL; 143 SDL_mutexV(socket mutex);156 SDL_mutexV(socketMutex); 144 157 145 158 } … … 156 169 int NetworkSocket::writeBytes(byte * data, int length) 157 170 { 158 SDL_mutexP(socketmutex); 171 #ifdef _USE_OUTGOING_BUFFER 172 173 //printf("length=%d, bufsize=%d\n", length, _OUTGOING_BUFFER_SIZE); 174 if (length>_OUTGOING_BUFFER_SIZE) 175 { 176 int res = 0; 177 int n = length / _OUTGOING_BUFFER_SIZE; 178 if (length % _OUTGOING_BUFFER_SIZE != 0) 179 n++; 180 // printf("n=%d\n", n); 181 SDL_Delay(500); 182 for (int i = 0; i<n; i++) 183 { 184 // printf("i=%d\n", i); 185 if (i==n-1) 186 { 187 res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, length-i*_OUTGOING_BUFFER_SIZE); 188 // printf("res = %d\n", res); 189 } 190 else 191 { 192 res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, _OUTGOING_BUFFER_SIZE); 193 // printf("res = %d\n", res); 194 } 195 } 196 return res; 197 } 198 199 #define min(a,b) (a<b)?a:b 200 int nbytes = min(_OUTGOING_BUFFER_SIZE - outgoingBufferLength, length); 201 #undef min 202 203 if (!tcpSocket || data==NULL || nbytes<=0) 204 return 0; 205 206 SDL_mutexP(outgoingBufferMutex); 207 208 memcpy(outgoingBuffer + outgoingBufferLength, data, nbytes); 209 outgoingBufferLength += nbytes; 210 211 SDL_mutexV(outgoingBufferMutex); 212 213 return nbytes; 214 #else 215 SDL_mutexP(socketMutex); 159 216 160 217 if (!tcpSocket || data==NULL) … … 163 220 int res = SDLNet_TCP_Send(tcpSocket, data, length); 164 221 165 SDL_mutexV(socket mutex);222 SDL_mutexV(socketMutex); 166 223 167 224 if (res<length) … … 169 226 170 227 return res; 228 #endif 171 229 } 172 230 … … 186 244 return 0; 187 245 188 int nbytes = (length<bufferlength) ? length : bufferlength; 246 int nbytes = (length<incomingBufferLength) ? length : incomingBufferLength; 247 248 //printf("readBytes: nbytes = %d; length=%d; incomingBufferLength=%d\n", nbytes, length, incomingBufferLength); 189 249 190 250 // just in case ... … … 195 255 return 0; 196 256 197 SDL_mutexP( mutex);198 199 memcpy(data, buf, nbytes);257 SDL_mutexP(incomingBufferMutex); 258 259 memcpy(data, incomingBuffer, nbytes); 200 260 201 261 //important: use memmove because the memory areas may overlap 202 memmove( buf, buf+nbytes, bufferlength-nbytes);203 bufferlength -= nbytes;204 205 SDL_mutexV( mutex);262 memmove(incomingBuffer, incomingBuffer+nbytes, incomingBufferLength-nbytes); 263 incomingBufferLength -= nbytes; 264 265 SDL_mutexV(incomingBufferMutex); 206 266 207 267 return nbytes; … … 216 276 { 217 277 NetworkSocket * self = (NetworkSocket*)data; 278 self->_isListening = true; 218 279 TCPsocket tempsocket; 219 280 220 281 tempsocket = SDLNet_TCP_Accept(self->tcpSocket); 221 282 222 SDL_mutexP(self->socketmutex); 283 while (!tempsocket && !self->terminateThread) 284 tempsocket = SDLNet_TCP_Accept(self->tcpSocket); 285 286 SDL_mutexP(self->socketMutex); 223 287 SDLNet_TCP_Close(self->tcpSocket); 224 288 self->tcpSocket = NULL; … … 227 291 { 228 292 PRINTF(1)("SDLNet_TCP_Accept: %s\n", SDLNet_GetError()); 229 SDL_mutexV(self->socketmutex); 293 //printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError()); 294 SDL_mutexV(self->socketMutex); 295 self->_isListening = false; 230 296 return -1; 231 297 } … … 233 299 self->tcpSocket = tempsocket; 234 300 235 SDL_mutexV(self->socketmutex); 236 301 SDL_mutexV(self->socketMutex); 302 303 self->_isListening = false; 237 304 return thread_read(data); 238 305 } … … 252 319 { 253 320 #define min(a,b) (a<b)?a:b 254 nbytestoread = min(_INCOMING_BUFFER_SIZE - self->bufferlength, _LOCAL_BUFFER_SIZE); 321 nbytestoread = min(_INCOMING_BUFFER_SIZE - self->incomingBufferLength, _LOCAL_BUFFER_SIZE); 322 #undef min 255 323 256 324 //if buffer is full 257 if (nbytestoread<=0 )325 if (nbytestoread<=0 || self->_isListening) 258 326 { 259 327 SDL_Delay(_MSECONDS_SLEEP_FULL_BUFFER); … … 263 331 nbytesread = SDLNet_TCP_Recv(self->tcpSocket, buffer, nbytestoread); 264 332 265 SDL_mutexP(self-> mutex);333 SDL_mutexP(self->incomingBufferMutex); 266 334 267 335 if (nbytesread<=0) … … 269 337 PRINTF(1)("SDLNet_TCP_Recv: %s\n", SDLNet_GetError()); 270 338 271 SDL_mutexP(self->socket mutex);339 SDL_mutexP(self->socketMutex); 272 340 273 341 SDLNet_TCP_Close(self->tcpSocket); 274 342 self->tcpSocket = NULL; 275 343 276 SDL_mutexV(self->socket mutex);277 SDL_mutexV(self-> mutex);344 SDL_mutexV(self->socketMutex); 345 SDL_mutexV(self->incomingBufferMutex); 278 346 return -1; 279 347 } 280 348 281 memcpy(self->buf+self->bufferlength, buffer, nbytesread); 282 self->bufferlength += nbytesread; 283 284 SDL_mutexV(self->mutex); 349 //printf("thread_read: nbytesread=%d\n", nbytesread); 350 351 memcpy(self->incomingBuffer+self->incomingBufferLength, buffer, nbytesread); 352 self->incomingBufferLength += nbytesread; 353 354 SDL_mutexV(self->incomingBufferMutex); 285 355 } 286 356 … … 288 358 } 289 359 290 291 360 int NetworkSocket::thread_write( void * data ) 361 { 362 int nbyteswrite = 0; 363 int nbytestowrite = 0; 364 char buffer[_LOCAL_BUFFER_SIZE]; 365 NetworkSocket * self = (NetworkSocket*)data; 366 367 while (!self->terminateThread) 368 { 369 #define min(a,b) (a<b)?a:b 370 nbytestowrite = min(self->outgoingBufferLength, _LOCAL_BUFFER_SIZE); 371 #undef min 372 373 //printf("thread_write nbytes=%d listening=%d\n", nbytestowrite, (int)self->_isListening); 374 375 //if buffer is full 376 if (nbytestowrite<=0 || self->_isListening) 377 { 378 SDL_Delay(_MSECONDS_SLEEP_EMPTY_BUFFER); 379 continue; 380 } 381 382 SDL_mutexP(self->outgoingBufferMutex); 383 384 //printf("a\n"); 385 386 memcpy(buffer, self->outgoingBuffer, nbytestowrite); 387 self->outgoingBufferLength -= nbytestowrite; 388 memmove(self->outgoingBuffer, self->outgoingBuffer+nbytestowrite, self->outgoingBufferLength); 389 390 SDL_mutexV(self->outgoingBufferMutex); 391 392 nbyteswrite = SDLNet_TCP_Send(self->tcpSocket, buffer, nbytestowrite); 393 394 if (nbyteswrite<=0) 395 { 396 PRINTF(1)("SDLNet_TCP_Recv: %s\n", SDLNet_GetError()); 397 398 SDL_mutexP(self->socketMutex); 399 400 SDLNet_TCP_Close(self->tcpSocket); 401 self->tcpSocket = NULL; 402 403 SDL_mutexV(self->socketMutex); 404 return -1; 405 } 406 407 } 408 409 return 0; 410 } 411 -
branches/network/src/lib/network/network_socket.h
r5628 r5630 8 8 #define _NETWORK_SOCKET 9 9 10 //if you want to use outgoing buffer define _USE_OUTGOING_BUFFER 11 #define _USE_OUTGOING_BUFFER 12 10 13 #define _INCOMING_BUFFER_SIZE 10240 14 #define _OUTGOING_BUFFER_SIZE 302400 11 15 #define _LOCAL_BUFFER_SIZE 1024 16 //sleep if incoming buffer is full 12 17 #define _MSECONDS_SLEEP_FULL_BUFFER 10 18 //sleep if outgoing buffer is empty 19 #define _MSECONDS_SLEEP_EMPTY_BUFFER 10 13 20 14 21 /* contains memmove and memcpy */ … … 36 43 // UDPsocket udpSocket; 37 44 38 byte buf[_INCOMING_BUFFER_SIZE]; 39 int bufferlength; 45 byte incomingBuffer[_INCOMING_BUFFER_SIZE]; 46 #ifdef _USE_OUTGOING_BUFFER 47 byte outgoingBuffer[_OUTGOING_BUFFER_SIZE]; 48 #endif 49 int incomingBufferLength; 50 #ifdef _USE_OUTGOING_BUFFER 51 int outgoingBufferLength; 52 #endif 40 53 41 SDL_mutex * mutex; 42 SDL_mutex * socketmutex; 54 SDL_mutex * incomingBufferMutex; 55 #ifdef _USE_OUTGOING_BUFFER 56 SDL_mutex * outgoingBufferMutex; 57 #endif 58 SDL_mutex * socketMutex; 43 59 bool terminateThread; 44 60 45 61 static int thread_listen(void * data); 46 62 static int thread_read(void * data); 63 #ifdef _USE_OUTGOING_BUFFER 64 static int thread_write(void * data); 65 #endif 66 67 bool _isListening; 47 68 48 69 public: -
branches/network/src/subprojects/network/network_unit_test.cc
r5628 r5630 27 27 SDLNet_ResolveHost(&ip, "127.0.0.1", 9999); 28 28 server.listen(9999); 29 SDL_Delay(20); 29 30 client.connectToServer(ip, 9999); 30 31 char buf[1024]; … … 37 38 38 39 int n; 39 40 char * str1 = "client to server"; 40 41 char * str2 = "server to client"; 41 42 n = client.writeBytes((byte*)str1, strlen(str1)+1); 42 43 printf("%d bytes send from client\n", n); 43 44 n = server.writeBytes((byte*)str2, strlen(str2)+1); 44 45 printf("%d bytes send from server\n", n); 45 46 SDL_Delay(1000); 46 47 47 48 printf("read from server\n"); 48 49 n = server.readBytes((byte*)buf, 1024); … … 63 64 printf("testing a bigger amount of data\n"); 64 65 65 #define _N_ELEMENTS 212992 66 char sendbuf[_N_ELEMENTS]; 67 char recvbuf[_N_ELEMENTS]; 66 #define _N_ELEMENTS 212994 67 char sendbuf[_N_ELEMENTS+1]; 68 char recvbuf[_N_ELEMENTS+1]; 69 sendbuf[_N_ELEMENTS] = '\0'; 70 recvbuf[_N_ELEMENTS] = '\0'; 68 71 69 72 for (int i = 0; i<_N_ELEMENTS; i++) 70 sendbuf[i] = i% 30 + 30;73 sendbuf[i] = i%26 + 65; 71 74 72 75 printf("write\n"); 73 client.writeBytes((byte*)sendbuf, _N_ELEMENTS);76 printf("result = %d\n", client.writeBytes((byte*)sendbuf, _N_ELEMENTS)); 74 77 75 SDL_Delay(50 0);78 SDL_Delay(50); 76 79 77 80 printf("read\n"); … … 82 85 { 83 86 SDL_Delay(10); 84 printf("read\n");87 //printf("read\n"); 85 88 nbytes = server.readBytes((byte*)recvbuf+offset, _N_ELEMENTS-offset); 86 89 offset += nbytes; 87 printf("nbytes=%d, offset=%d\n", nbytes, offset);90 //printf("nbytes=%d, offset=%d\n", nbytes, offset); 88 91 } 89 92 90 93 printf("strcmp = %d (0 is good :D not 0 is evil)\noffset = %d\n", strncmp(sendbuf, recvbuf, _N_ELEMENTS), offset); 94 95 //printf("%s\n%s\n", sendbuf, recvbuf); 96 97 for (int i = 0; i<_N_ELEMENTS; i++) 98 { 99 if (sendbuf[i]!=recvbuf[i]) 100 { 101 printf("byte %d is the first difference\n", i+1); 102 break; 103 } 104 } 91 105 92 106 return 0;
Note: See TracChangeset
for help on using the changeset viewer.