| 1 | |
|---|
| 2 | #include "stdlibincl.h" |
|---|
| 3 | #include <stdarg.h> |
|---|
| 4 | #include <stdio.h> |
|---|
| 5 | #include "shell_buffer.h" |
|---|
| 6 | #include "class_list.h" |
|---|
| 7 | |
|---|
| 8 | #include "network_manager.h" |
|---|
| 9 | #include "network_socket.h" |
|---|
| 10 | #include "server_socket.h" |
|---|
| 11 | #include "network_stream.h" |
|---|
| 12 | #include "synchronizeable.h" |
|---|
| 13 | |
|---|
| 14 | #include "simple_sync.h" |
|---|
| 15 | #include "read_sync.h" |
|---|
| 16 | |
|---|
| 17 | int verbose = 4; |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | /* outputs the help */ |
|---|
| 21 | int startHelp(int argc, char** argv) |
|---|
| 22 | { |
|---|
| 23 | printf("Network is a network unit test\n"); |
|---|
| 24 | printf(" --help this output\n"); |
|---|
| 25 | printf(" -st, --sockettest test network_socket\n"); |
|---|
| 26 | printf(" -ft, --frameworktest test the network module\n"); |
|---|
| 27 | printf(" --server [port number] creates a test server\n"); |
|---|
| 28 | printf(" --client [address] [port] connects to a server\n"); |
|---|
| 29 | printf(" --listen [address] [port] just listens to this connection"); |
|---|
| 30 | printf("\n"); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | /* test SDL network socket */ |
|---|
| 35 | int testSocket(int argc, char** argv) |
|---|
| 36 | { |
|---|
| 37 | IPaddress ip; |
|---|
| 38 | SDLNet_ResolveHost(&ip, "127.0.0.1", 9999); |
|---|
| 39 | ServerSocket server; |
|---|
| 40 | server.listen(9999); |
|---|
| 41 | |
|---|
| 42 | NetworkSocket client1(ip); |
|---|
| 43 | |
|---|
| 44 | NetworkSocket server1 = server.getNewSocket(); |
|---|
| 45 | |
|---|
| 46 | NetworkSocket client2(ip); |
|---|
| 47 | |
|---|
| 48 | NetworkSocket server2 = server.getNewSocket(); |
|---|
| 49 | |
|---|
| 50 | char buf[1024]; |
|---|
| 51 | |
|---|
| 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)); |
|---|
| 57 | |
|---|
| 58 | int 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); |
|---|
| 71 | SDL_Delay(1000); |
|---|
| 72 | |
|---|
| 73 | printf("read from server1\n"); |
|---|
| 74 | n = server1.readPacket((byte*)buf, 1024); |
|---|
| 75 | printf("read %d bytes\n", n); |
|---|
| 76 | if (n<0) |
|---|
| 77 | return -1; |
|---|
| 78 | |
|---|
| 79 | printf("data: '%s'\n", buf); |
|---|
| 80 | |
|---|
| 81 | printf("read from server2\n"); |
|---|
| 82 | n = server2.readPacket((byte*)buf, 1024); |
|---|
| 83 | printf("read %d bytes\n", n); |
|---|
| 84 | if (n<0) |
|---|
| 85 | return -1; |
|---|
| 86 | |
|---|
| 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))); |
|---|
| 114 | |
|---|
| 115 | return 0; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | int testFramework(int argc, char** argv) |
|---|
| 120 | { |
|---|
| 121 | printf("=================\n"); |
|---|
| 122 | printf("TestFramework\n"); |
|---|
| 123 | printf("=================\n"); |
|---|
| 124 | |
|---|
| 125 | Synchronizeable* clientSync = new SimpleSync("Client\0"); |
|---|
| 126 | Synchronizeable* serverSync = new SimpleSync("Server\0"); |
|---|
| 127 | |
|---|
| 128 | unsigned int port = 9999; |
|---|
| 129 | |
|---|
| 130 | /* create the network manager */ |
|---|
| 131 | NetworkManager* nm = NetworkManager::getInstance(); |
|---|
| 132 | |
|---|
| 133 | /* initialize the network manager */ |
|---|
| 134 | nm->initialize(); |
|---|
| 135 | |
|---|
| 136 | /* create a server stream */ |
|---|
| 137 | nm->createServer(*serverSync, port); |
|---|
| 138 | |
|---|
| 139 | /* esatblish a connection */ |
|---|
| 140 | IPaddress ip; |
|---|
| 141 | int error = SDLNet_ResolveHost(&ip, "127.0.0.1", port); |
|---|
| 142 | //SDLNet_ResolveHost(&ip, "localhost", port); |
|---|
| 143 | if(error == -1) |
|---|
| 144 | printf("\n\nerror on address resolution, program inconsistancy\n\n"); |
|---|
| 145 | nm->establishConnection(ip, *clientSync); |
|---|
| 146 | /* adding some break for connection setup */ |
|---|
| 147 | SDL_Delay(20); |
|---|
| 148 | |
|---|
| 149 | /* synchronize the data 1 time (increment for longer tests) */ |
|---|
| 150 | for( int i = 0; i < 3; i++) |
|---|
| 151 | { |
|---|
| 152 | nm->synchronize(); |
|---|
| 153 | /* simulate the network delay */ |
|---|
| 154 | SDL_Delay(50); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | printf("Test finished\n"); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | /* delete the network manager again */ |
|---|
| 161 | delete nm; |
|---|
| 162 | |
|---|
| 163 | delete clientSync; |
|---|
| 164 | delete serverSync; |
|---|
| 165 | |
|---|
| 166 | return 0; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * |
|---|
| 173 | * @param argc |
|---|
| 174 | * @param argv |
|---|
| 175 | * @return |
|---|
| 176 | */ |
|---|
| 177 | int startServer(int argc, char** argv) |
|---|
| 178 | { |
|---|
| 179 | if( argc <= 2) |
|---|
| 180 | { |
|---|
| 181 | printf(" Wrong arguments try following notations:\n"); |
|---|
| 182 | printf(" --server [port number]\n"); |
|---|
| 183 | return 0; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | int port = atoi(argv[2]); |
|---|
| 187 | printf("Starting Server on port %i\n", port); |
|---|
| 188 | |
|---|
| 189 | NetworkManager* netMan = NetworkManager::getInstance(); |
|---|
| 190 | Synchronizeable* ss = new SimpleSync("Server\0"); |
|---|
| 191 | |
|---|
| 192 | netMan->createServer(*ss, port); |
|---|
| 193 | SDL_Delay(20); |
|---|
| 194 | |
|---|
| 195 | for(;;) |
|---|
| 196 | { |
|---|
| 197 | netMan->synchronize(); |
|---|
| 198 | SDL_Delay(500); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | delete netMan; |
|---|
| 202 | delete ss; |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | return 0; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | int startClient(int argc, char** argv) |
|---|
| 210 | { |
|---|
| 211 | if( argc < 3) |
|---|
| 212 | { |
|---|
| 213 | printf(" Wrong arguments try following notations:\n"); |
|---|
| 214 | printf(" --client [server ip] [port number]\n"); |
|---|
| 215 | printf(" --client [server name] [port number]\n"); |
|---|
| 216 | return 0; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | char* name = argv[2]; |
|---|
| 220 | int port = atoi(argv[3]); |
|---|
| 221 | printf("Connecting to %s, on port %i\n", name, port); |
|---|
| 222 | |
|---|
| 223 | IPaddress ip; |
|---|
| 224 | int error = SDLNet_ResolveHost(&ip, name, port); |
|---|
| 225 | if(error == -1) |
|---|
| 226 | printf("\n\nerror on address resolution, program inconsistancy\n\n"); |
|---|
| 227 | |
|---|
| 228 | NetworkManager* netMan = NetworkManager::getInstance(); |
|---|
| 229 | Synchronizeable* ss = new SimpleSync("Client\0"); |
|---|
| 230 | |
|---|
| 231 | netMan->establishConnection(ip, *ss); |
|---|
| 232 | |
|---|
| 233 | for(;;) |
|---|
| 234 | { |
|---|
| 235 | netMan->synchronize(); |
|---|
| 236 | SDL_Delay(500); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 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 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | delete netMan; |
|---|
| 280 | delete ss; |
|---|
| 281 | |
|---|
| 282 | return 0; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | /** |
|---|
| 288 | * |
|---|
| 289 | * main function |
|---|
| 290 | * |
|---|
| 291 | * here the journey begins |
|---|
| 292 | */ |
|---|
| 293 | int main(int argc, char** argv) |
|---|
| 294 | { |
|---|
| 295 | int i; |
|---|
| 296 | // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark. |
|---|
| 297 | for(i = 1; i < argc; ++i) |
|---|
| 298 | { |
|---|
| 299 | //else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; |
|---|
| 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); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | startHelp(argc, argv); |
|---|
| 313 | |
|---|
| 314 | return 0; |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | bool ShellBuffer::addBufferLineStatic(const char* line, ...) |
|---|
| 319 | { |
|---|
| 320 | //va_list arguments; |
|---|
| 321 | //vprintf(line, arguments); |
|---|
| 322 | printf("%s", line); |
|---|
| 323 | } |
|---|