- Timestamp:
- Dec 8, 2005, 8:13:22 AM (19 years ago)
- Location:
- branches/network/src
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/defs/class_id.h
r5968 r5987 224 224 CL_NETWORK_PROTOCOL = 0x00000b02, 225 225 CL_NETWORK_SOCKET = 0x00000b03, 226 CL_CONNECTION_MONITOR = 0x00000b04, 226 CL_SERVER_SOCKET = 0X00000b04, 227 CL_CONNECTION_MONITOR = 0x00000b05, 227 228 228 229 -
branches/network/src/lib/network/Makefile.am
r5822 r5987 10 10 network_stream.cc \ 11 11 data_stream.cc \ 12 network_protocol.cc 12 network_protocol.cc \ 13 server_socket.cc 13 14 14 15 … … 20 21 network_stream.h \ 21 22 data_stream.h \ 22 network_protocol.h 23 network_protocol.h \ 24 server_socket.h 23 25 -
branches/network/src/lib/network/network_socket.cc
r5829 r5987 47 47 48 48 49 NetworkSocket::NetworkSocket( TCPsocket sock ) 50 { 51 this->init(); 52 this->tcpSocket = sock; 53 54 SDL_CreateThread(thread_read, (void*)this); 55 SDL_CreateThread(thread_write, (void*)this); 56 } 57 49 58 void NetworkSocket::init() 50 59 { … … 75 84 } 76 85 86 87 77 88 /** 78 89 * Default destructor … … 85 96 PRINTF(5)("SDL_net shutdown\n"); 86 97 87 _isListening = false;88 89 98 SDL_DestroyMutex(incomingBufferMutex); 90 99 SDL_DestroyMutex(outgoingBufferMutex); … … 113 122 } 114 123 115 _isListening = false;116 117 124 SDL_CreateThread(thread_read, (void*)this); 118 125 SDL_CreateThread(thread_write, (void*)this); 119 126 } 120 127 121 /**122 * Tells the NetworkSocket to listen on a specific port for incoming connections.123 * NetworkSocket::writeBytes(...) will have no effect until there is a valuable connection.124 * @param port125 */126 void NetworkSocket::listen(unsigned int port)127 {128 _isListening = true;129 //check if not already connected or listening130 if (tcpSocket)131 {132 PRINTF(1)("NetworkSocket::listen: tcpSocket!=NULL! maybe you already called listen or connectToServer or did not call disconnectServer()!\n");133 _isListening = false;134 return;135 }136 137 IPaddress ip;138 139 if (SDLNet_ResolveHost(&ip, NULL, port)==-1)140 {141 PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError());142 _isListening = false;143 return;144 }145 146 tcpSocket = SDLNet_TCP_Open(&ip);147 148 if (!tcpSocket)149 {150 PRINTF(1)("SDLNet_TCP_Open: %s\n", SDLNet_GetError());151 _isListening = false;152 return;153 }154 155 SDL_CreateThread(thread_listen, (void*)this);156 SDL_CreateThread(thread_write, (void*)this);157 }158 128 159 129 /** … … 184 154 PRINTF(0)("NetworkSocket::writeBytes()\n"); 185 155 #ifdef _USE_OUTGOING_BUFFER 186 187 //printf("length=%d, bufsize=%d\n", length, _OUTGOING_BUFFER_SIZE);188 // if (length>_OUTGOING_BUFFER_SIZE)189 // {190 // int res = 0;191 // int n = length / _OUTGOING_BUFFER_SIZE;192 // if (length % _OUTGOING_BUFFER_SIZE != 0)193 // n++;194 // // printf("n=%d\n", n);195 // SDL_Delay(500);196 // for (int i = 0; i<n; i++)197 // {198 // // printf("i=%d\n", i);199 // if (i==n-1)200 // {201 // res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, length-i*_OUTGOING_BUFFER_SIZE);202 // // printf("res = %d\n", res);203 // }204 // else205 // {206 // res += writeBytes(data + i*_OUTGOING_BUFFER_SIZE, _OUTGOING_BUFFER_SIZE);207 // // printf("res = %d\n", res);208 // }209 // }210 // return res;211 // }212 156 213 157 #define min(a,b) (a<b)?a:b … … 256 200 int NetworkSocket::readBytes(byte * data, int length) 257 201 { 202 PRINTF(0)("NetworkSocket::readBytes()\n"); 258 203 if (data==NULL) 259 204 return 0; … … 299 244 } 300 245 301 /**302 * used to create a thread to listen303 * will call thrad_read when established connection304 * @param data: pointer to NetwortSocket305 */306 int NetworkSocket::thread_listen( void * data )307 {308 NetworkSocket * self = (NetworkSocket*)data;309 self->_isListening = true;310 TCPsocket tempsocket;311 312 tempsocket = SDLNet_TCP_Accept(self->tcpSocket);313 314 while (!tempsocket && !self->terminateThread)315 {316 tempsocket = SDLNet_TCP_Accept(self->tcpSocket);317 SDL_Delay(_MSECONDS_SLEEP_LISTEN);318 }319 320 SDL_mutexP(self->socketMutex);321 SDLNet_TCP_Close(self->tcpSocket);322 self->tcpSocket = NULL;323 324 if (!tempsocket)325 {326 printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());327 //printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());328 SDL_mutexV(self->socketMutex);329 self->_isListening = false;330 return -1;331 }332 333 self->tcpSocket = tempsocket;334 335 SDL_mutexV(self->socketMutex);336 337 self->_isListening = false;338 return thread_read(data);339 }340 246 341 247 /** … … 357 263 358 264 //if buffer is full 359 if (nbytestoread<=0 || self->_isListening)265 if (nbytestoread<=0) 360 266 { 361 267 SDL_Delay(_MSECONDS_SLEEP_FULL_BUFFER); … … 408 314 409 315 //if buffer is full 410 if (nbytestowrite<=0 || self->_isListening)316 if (nbytestowrite<=0) 411 317 { 412 318 SDL_Delay(_MSECONDS_SLEEP_EMPTY_BUFFER); … … 445 351 } 446 352 353 bool NetworkSocket::writePacket( byte * data, int length ) 354 { 355 PRINTF(0)("NetworkSocket::writePacket()\n"); 356 if (length>255) 357 { 358 PRINTF(1)("Packet length > 255!\n"); 359 return false; 360 } 361 362 byte blen = length; 363 364 writeBytes(&blen, 1); 365 writeBytes(data, length); 366 } 367 368 int NetworkSocket::readPacket( byte * data, int maxLength ) 369 { 370 PRINTF(0)("NetworkSocket::readPacket()\n"); 371 if (incomingBufferLength<1) 372 { 373 return 0; 374 } 375 376 byte blen = incomingBuffer[0]; 377 378 if (blen>maxLength) 379 { 380 PRINTF(1)("Buffersize is too small (%d) for packet (%d)\n", maxLength, blen); 381 return 0; 382 } 383 384 if (blen>incomingBufferLength) 385 { 386 return 0; 387 } 388 389 readBytes(&blen, 1); 390 int res = readBytes(data, blen); 391 392 if (res!=blen) 393 return -1; 394 else 395 return blen; 396 397 } 398 399 -
branches/network/src/lib/network/network_socket.h
r5829 r5987 18 18 //sleep if outgoing buffer is empty 19 19 #define _MSECONDS_SLEEP_EMPTY_BUFFER 10 20 //sleep when waiting for connections 21 #define _MSECONDS_SLEEP_LISTEN 100 20 22 21 23 22 /* contains memmove and memcpy */ … … 64 63 bool terminateThread; 65 64 66 static int thread_listen(void * data);67 65 static int thread_read(void * data); 68 66 #ifdef _USE_OUTGOING_BUFFER … … 70 68 #endif 71 69 72 bool _isListening; 70 int writeBytes(byte * data, int length); 71 int readBytes(byte * data, int length); 72 int readBlock(byte * data, int length); 73 74 void init(); 73 75 74 76 public: … … 76 78 NetworkSocket(); 77 79 NetworkSocket(IPaddress ip); 80 NetworkSocket(TCPsocket sock); 78 81 ~NetworkSocket(); 79 82 80 83 void connectToServer(IPaddress ip); 81 void listen(unsigned int port);82 84 void disconnectServer(); 83 int writeBytes(byte * data, int length);84 int readBytes(byte * data, int length);85 int readBlock(byte * data, int length);86 85 87 private: 88 void init();86 bool writePacket(byte * data, int length); 87 int readPacket(byte * data, int maxLength); 89 88 90 89 }; -
branches/network/src/lib/network/network_stream.cc
r5833 r5987 67 67 this->init(); 68 68 this->networkSocket = new NetworkSocket(); 69 this->networkSocket->listen(port);69 // this->networkSocket->listen(port); 70 70 this->networkProtocol = new NetworkProtocol(); 71 71 this->synchronizeables = NULL; … … 93 93 this->init(); 94 94 this->networkSocket = new NetworkSocket(); 95 this->networkSocket->listen(port);95 // this->networkSocket->listen(port); 96 96 this->networkProtocol = new NetworkProtocol(); 97 97 this->synchronizeables = &sync; … … 151 151 152 152 /* pass the data to the network socket */ 153 dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength);153 // dataLength = this->networkSocket->writeBytes((byte*)downBuffer, dataLength); 154 154 /* check if there was an error */ 155 155 if( dataLength == -1) … … 167 167 if( this->state == NET_REC_HEADER) 168 168 { 169 dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header));169 // dataLength = this->networkSocket->readBlock((byte*)upBuffer, sizeof(Header)); 170 170 if( dataLength == sizeof(Header)) 171 171 { … … 182 182 { 183 183 /* now read the data */ 184 dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length);184 // dataLength = this->networkSocket->readBlock((byte*)upBuffer, this->packetHeader.length); 185 185 /* check if the data is available and process it if so */ 186 186 if( dataLength == this->packetHeader.length) -
branches/network/src/subprojects/network/network_unit_test.cc
r5831 r5987 8 8 #include "network_manager.h" 9 9 #include "network_socket.h" 10 #include "server_socket.h" 10 11 #include "network_stream.h" 11 12 #include "synchronizeable.h" … … 36 37 IPaddress ip; 37 38 SDLNet_ResolveHost(&ip, "127.0.0.1", 9999); 38 NetworkSocket server;39 ServerSocket server; 39 40 server.listen(9999); 40 SDL_Delay(20); 41 42 NetworkSocket client(ip); 41 42 NetworkSocket client1(ip); 43 44 NetworkSocket server1 = server.getNewSocket(); 45 46 NetworkSocket client2(ip); 47 48 NetworkSocket server2 = server.getNewSocket(); 43 49 44 50 char buf[1024]; 45 51 46 printf("read from client before sending data\n");47 printf("result: %d bytes\n", client .readBytes((byte*)buf, 1024));48 49 printf("read from serverbefore sending data\n");50 printf("result: %d bytes\n", server.readBytes((byte*)buf, 1024));52 printf("read from client1 before sending data\n"); 53 printf("result: %d bytes\n", client1.readPacket((byte*)buf, 1024)); 54 55 printf("read from client2 before sending data\n"); 56 printf("result: %d bytes\n", client2.readPacket((byte*)buf, 1024)); 51 57 52 58 int n; 53 char * str1 = "client to server"; 54 char * str2 = "server to client"; 55 n = client.writeBytes((byte*)str1, strlen(str1)+1); 56 printf("%d bytes send from client\n", n); 57 n = server.writeBytes((byte*)str2, strlen(str2)+1); 58 printf("%d bytes send from server\n", n); 59 char * str1 = "client1 to server"; 60 char * str2 = "server1 to client"; 61 char * str3 = "client2 to server"; 62 char * str4 = "server2 to client"; 63 n = client1.writePacket((byte*)str1, strlen(str1)+1); 64 printf("%d bytes send from client1\n", n); 65 n = server1.writePacket((byte*)str2, strlen(str2)+1); 66 printf("%d bytes send from server1\n", n); 67 n = client2.writePacket((byte*)str3, strlen(str3)+1); 68 printf("%d bytes send from client2\n", n); 69 n = server2.writePacket((byte*)str4, strlen(str4)+1); 70 printf("%d bytes send from server2\n", n); 59 71 SDL_Delay(1000); 60 72 61 printf("read from server \n");62 n = server .readBytes((byte*)buf, 1024);73 printf("read from server1\n"); 74 n = server1.readPacket((byte*)buf, 1024); 63 75 printf("read %d bytes\n", n); 64 76 if (n<0) 65 77 return -1; 66 78 67 printf("data: %s\n", buf);68 printf("read from client\n"); 69 n = client.readBytes((byte*)buf, 1024);70 79 printf("data: '%s'\n", buf); 80 81 printf("read from server2\n"); 82 n = server2.readPacket((byte*)buf, 1024); 71 83 printf("read %d bytes\n", n); 72 84 if (n<0) 73 85 return -1; 74 86 75 printf("data: %s\n", buf); 76 77 printf("testing a bigger amount of data\n"); 78 79 #define _N_ELEMENTS 212994 80 81 char sendbuf[_N_ELEMENTS+1]; 82 char recvbuf[_N_ELEMENTS+1]; 83 sendbuf[_N_ELEMENTS] = '\0'; 84 recvbuf[_N_ELEMENTS] = '\0'; 85 86 for (int i = 0; i<_N_ELEMENTS; i++) 87 sendbuf[i] = i%26 + 65; 88 89 printf("write\n"); 90 printf("result = %d\n", client.writeBytes((byte*)sendbuf, _N_ELEMENTS)); 91 92 SDL_Delay(50); 93 94 printf("read\n"); 95 int nbytes = server.readBytes((byte*)recvbuf, _N_ELEMENTS); 96 int offset = nbytes; 97 98 while (nbytes>0) 99 { 100 SDL_Delay(10); 101 //printf("read\n"); 102 nbytes = server.readBytes((byte*)recvbuf+offset, _N_ELEMENTS-offset); 103 offset += nbytes; 104 //printf("nbytes=%d, offset=%d\n", nbytes, offset); 105 } 106 107 printf("strcmp = %d (0 is good :D not 0 is evil)\noffset = %d\n", strncmp(sendbuf, recvbuf, _N_ELEMENTS), offset); 108 109 //printf("%s\n%s\n", sendbuf, recvbuf); 110 111 for (int i = 0; i<_N_ELEMENTS; i++) 112 { 113 if (sendbuf[i]!=recvbuf[i]) 114 { 115 printf("byte %d is the first difference\n", i+1); 116 break; 117 } 118 } 87 printf("data: '%s'\n", buf); 88 89 printf("read from client1\n"); 90 n = client1.readPacket((byte*)buf, 1024); 91 printf("read %d bytes\n", n); 92 if (n<0) 93 return -1; 94 95 printf("data: '%s'\n", buf); 96 97 printf("read from client2\n"); 98 n = client2.readPacket((byte*)buf, 1024); 99 printf("read %d bytes\n", n); 100 if (n<0) 101 return -1; 102 103 printf("data: '%s'\n", buf); 104 105 //sending bigger packets than 255 is not supported 106 107 printf("try to send more than 255 bytes\n"); 108 printf("result: %d\n", client1.writePacket((byte*)buf, 1000)); 109 110 server1.writePacket((byte*)str1, strlen(str1)+1); 111 SDL_Delay(500); 112 printf("try to read with a too small buffer\n"); 113 printf("result: %d\n", client1.readPacket((byte*)buf, strlen(str1))); 119 114 120 115 return 0;
Note: See TracChangeset
for help on using the changeset viewer.