Changeset 5996 in orxonox.OLD for trunk/src/subprojects
- Timestamp:
- Dec 9, 2005, 12:31:01 PM (19 years ago)
- Location:
- trunk/src/subprojects/network
- Files:
-
- 4 edited
- 4 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/subprojects/network/Makefile.am
r5822 r5996 15 15 network_SOURCES= network_unit_test.cc \ 16 16 simple_sync.cc \ 17 read_sync.cc \ 18 write_sync.cc \ 17 19 \ 18 20 \ … … 21 23 $(MAINSRCDIR)/util/loading/load_param.cc \ 22 24 $(MAINSRCDIR)/lib/util/substring.cc \ 23 $(MAINSRCDIR)/lib/util/helper_functions.cc 25 $(MAINSRCDIR)/util/loading/load_param_description.cc \ 26 $(MAINSRCDIR)/lib/util/helper_functions.cc \ 27 $(MAINSRCDIR)/lib/util/executor/executor.cc \ 28 $(MAINSRCDIR)/lib/util/multi_type.cc 24 29 25 30 26 31 27 32 noinst_HEADERS = network_unit_test.h \ 28 simple_sync.h 33 simple_sync.h \ 34 read_sync.h \ 35 write_sync.h 29 36 -
trunk/src/subprojects/network/network_unit_test.cc
r5822 r5996 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" 12 13 13 14 #include "simple_sync.h" 15 #include "read_sync.h" 14 16 15 17 int verbose = 4; … … 25 27 printf(" --server [port number] creates a test server\n"); 26 28 printf(" --client [address] [port] connects to a server\n"); 29 printf(" --listen [address] [port] just listens to this connection"); 27 30 printf("\n"); 28 31 } … … 34 37 IPaddress ip; 35 38 SDLNet_ResolveHost(&ip, "127.0.0.1", 9999); 36 NetworkSocket server;39 ServerSocket server; 37 40 server.listen(9999); 38 SDL_Delay(20); 39 40 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(); 41 49 42 50 char buf[1024]; 43 51 44 printf("read from client before sending data\n");45 printf("result: %d bytes\n", client .readBytes((byte*)buf, 1024));46 47 printf("read from serverbefore sending data\n");48 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)); 49 57 50 58 int n; 51 char * str1 = "client to server"; 52 char * str2 = "server to client"; 53 n = client.writeBytes((byte*)str1, strlen(str1)+1); 54 printf("%d bytes send from client\n", n); 55 n = server.writeBytes((byte*)str2, strlen(str2)+1); 56 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); 57 71 SDL_Delay(1000); 58 72 59 printf("read from server \n");60 n = server .readBytes((byte*)buf, 1024);73 printf("read from server1\n"); 74 n = server1.readPacket((byte*)buf, 1024); 61 75 printf("read %d bytes\n", n); 62 76 if (n<0) 63 77 return -1; 64 78 65 printf("data: %s\n", buf);66 printf("read from client\n"); 67 n = client.readBytes((byte*)buf, 1024);68 79 printf("data: '%s'\n", buf); 80 81 printf("read from server2\n"); 82 n = server2.readPacket((byte*)buf, 1024); 69 83 printf("read %d bytes\n", n); 70 84 if (n<0) 71 85 return -1; 72 86 73 printf("data: %s\n", buf); 74 75 printf("testing a bigger amount of data\n"); 76 77 #define _N_ELEMENTS 212994 78 char sendbuf[_N_ELEMENTS+1]; 79 char recvbuf[_N_ELEMENTS+1]; 80 sendbuf[_N_ELEMENTS] = '\0'; 81 recvbuf[_N_ELEMENTS] = '\0'; 82 83 for (int i = 0; i<_N_ELEMENTS; i++) 84 sendbuf[i] = i%26 + 65; 85 86 printf("write\n"); 87 printf("result = %d\n", client.writeBytes((byte*)sendbuf, _N_ELEMENTS)); 88 89 SDL_Delay(50); 90 91 printf("read\n"); 92 int nbytes = server.readBytes((byte*)recvbuf, _N_ELEMENTS); 93 int offset = nbytes; 94 95 while (nbytes>0) 96 { 97 SDL_Delay(10); 98 //printf("read\n"); 99 nbytes = server.readBytes((byte*)recvbuf+offset, _N_ELEMENTS-offset); 100 offset += nbytes; 101 //printf("nbytes=%d, offset=%d\n", nbytes, offset); 102 } 103 104 printf("strcmp = %d (0 is good :D not 0 is evil)\noffset = %d\n", strncmp(sendbuf, recvbuf, _N_ELEMENTS), offset); 105 106 //printf("%s\n%s\n", sendbuf, recvbuf); 107 108 for (int i = 0; i<_N_ELEMENTS; i++) 109 { 110 if (sendbuf[i]!=recvbuf[i]) 111 { 112 printf("byte %d is the first difference\n", i+1); 113 break; 114 } 115 } 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))); 116 114 117 115 return 0; … … 131 129 132 130 /* create the network manager */ 133 NetworkManager* nm = new NetworkManager();131 NetworkManager* nm = NetworkManager::getInstance(); 134 132 135 133 /* initialize the network manager */ … … 143 141 int error = SDLNet_ResolveHost(&ip, "127.0.0.1", port); 144 142 //SDLNet_ResolveHost(&ip, "localhost", port); 145 if(error == -1) printf("\n\nerror on address resolution, program inconsistancy\n\n"); 143 if(error == -1) 144 printf("\n\nerror on address resolution, program inconsistancy\n\n"); 146 145 nm->establishConnection(ip, *clientSync); 147 146 /* adding some break for connection setup */ … … 149 148 150 149 /* synchronize the data 1 time (increment for longer tests) */ 151 for( int i = 0; i < 3; i++) { 150 for( int i = 0; i < 3; i++) 151 { 152 152 nm->synchronize(); 153 153 /* simulate the network delay */ … … 177 177 int startServer(int argc, char** argv) 178 178 { 179 if( argc <= 2) { 179 if( argc <= 2) 180 { 180 181 printf(" Wrong arguments try following notations:\n"); 181 182 printf(" --server [port number]\n"); … … 186 187 printf("Starting Server on port %i\n", port); 187 188 188 NetworkManager* netMan = new NetworkManager();189 NetworkManager* netMan = NetworkManager::getInstance(); 189 190 Synchronizeable* ss = new SimpleSync("Server\0"); 190 191 191 //NetworkStream* server = new NetworkStream(port, ss, NET_SERVER);192 192 netMan->createServer(*ss, port); 193 193 SDL_Delay(20); 194 194 195 for(;;) { 195 for(;;) 196 { 196 197 netMan->synchronize(); 197 198 SDL_Delay(500); 198 199 } 200 201 delete netMan; 202 delete ss; 203 204 199 205 return 0; 200 206 } … … 203 209 int startClient(int argc, char** argv) 204 210 { 205 if( argc < 3) { 211 if( argc < 3) 212 { 206 213 printf(" Wrong arguments try following notations:\n"); 207 214 printf(" --client [server ip] [port number]\n"); 208 printf(" -- server[server name] [port number]\n");215 printf(" --client [server name] [port number]\n"); 209 216 return 0; 210 217 } … … 216 223 IPaddress ip; 217 224 int error = SDLNet_ResolveHost(&ip, name, port); 218 //SDLNet_ResolveHost(&ip, "localhost", port);219 if(error == -1)printf("\n\nerror on address resolution, program inconsistancy\n\n");220 221 NetworkManager* netMan = new NetworkManager();225 if(error == -1) 226 printf("\n\nerror on address resolution, program inconsistancy\n\n"); 227 228 NetworkManager* netMan = NetworkManager::getInstance(); 222 229 Synchronizeable* ss = new SimpleSync("Client\0"); 223 230 224 231 netMan->establishConnection(ip, *ss); 225 232 226 for(;;) { 233 for(;;) 234 { 227 235 netMan->synchronize(); 228 236 SDL_Delay(500); 229 237 } 230 238 231 //NetworkStream* client = new NetworkStream(ip, ss, NET_CLIENT); 239 240 delete netMan; 241 delete ss; 242 243 return 0; 244 } 245 246 247 248 int startListen(int argc, char** argv) 249 { 250 if( argc < 3) 251 { 252 printf(" Wrong arguments try following notations:\n"); 253 printf(" --listen [server ip] [port number]\n"); 254 printf(" --listen [server name] [port number]\n"); 255 return 0; 256 } 257 258 char* name = argv[2]; 259 int port = atoi(argv[3]); 260 printf("Connecting to %s, on port %i\n", name, port); 261 262 IPaddress ip; 263 int error = SDLNet_ResolveHost(&ip, name, port); 264 if(error == -1) 265 printf("\n\nerror on address resolution, program inconsistancy\n\n"); 266 267 NetworkManager* netMan = NetworkManager::getInstance(); 268 Synchronizeable* ss = new ReadSync("WriteSync\0"); 269 270 netMan->establishConnection(ip, *ss); 271 272 for(;;) 273 { 274 netMan->synchronize(); 275 SDL_Delay(10); 276 } 232 277 233 278 … … 253 298 { 254 299 //else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; 255 if (! strcmp( "--sockettest", argv[i]) || !strcmp("-st", argv[i])) return testSocket(argc, argv); 256 else if (! strcmp( "--frameworktest", argv[i]) || !strcmp("-ft", argv[i])) return testFramework(argc, argv); 257 else if (! strcmp( "--server", argv[i]) || !strcmp("-s", argv[i])) return startServer(argc, argv); 258 else if (! strcmp( "--client", argv[i]) || !strcmp("-c", argv[i])) return startClient(argc, argv); 300 if (! strcmp( "--sockettest", argv[i]) || !strcmp("-st", argv[i])) 301 return testSocket(argc, argv); 302 else if (! strcmp( "--frameworktest", argv[i]) || !strcmp("-ft", argv[i])) 303 return testFramework(argc, argv); 304 else if (! strcmp( "--server", argv[i]) || !strcmp("-s", argv[i])) 305 return startServer(argc, argv); 306 else if (! strcmp( "--client", argv[i]) || !strcmp("-c", argv[i])) 307 return startClient(argc, argv); 308 else if (! strcmp( "--listen", argv[i]) || !strcmp("-l", argv[i])) 309 return startListen(argc, argv); 259 310 } 260 311 … … 270 321 //vprintf(line, arguments); 271 322 printf("%s", line); 272 273 } 323 } -
trunk/src/subprojects/network/simple_sync.cc
r5822 r5996 86 86 * read data from Synchronizeable 87 87 */ 88 int SimpleSync::readBytes(byte* data) const88 int SimpleSync::readBytes(byte* data) 89 89 { 90 90 PRINTF(0)("SimpleSync: sent %i bytes of data\n", this->outLength); -
trunk/src/subprojects/network/simple_sync.h
r5822 r5996 17 17 18 18 virtual void writeBytes(const byte* data, int length); 19 virtual int readBytes(byte* data) const;19 virtual int readBytes(byte* data); 20 20 21 21
Note: See TracChangeset
for help on using the changeset viewer.