[5579] | 1 | |
---|
[5580] | 2 | #include "stdlibincl.h" |
---|
| 3 | #include <stdarg.h> |
---|
| 4 | #include <stdio.h> |
---|
| 5 | #include "shell_buffer.h" |
---|
[5609] | 6 | #include "class_list.h" |
---|
[5579] | 7 | |
---|
[5580] | 8 | #include "network_manager.h" |
---|
[5624] | 9 | #include "network_socket.h" |
---|
[5647] | 10 | #include "synchronizeable.h" |
---|
[5579] | 11 | |
---|
[5580] | 12 | int verbose = 4; |
---|
[5579] | 13 | |
---|
[5580] | 14 | |
---|
[5647] | 15 | /* outputs the help */ |
---|
[5580] | 16 | int startHelp(int argc, char** argv) |
---|
[5609] | 17 | { |
---|
| 18 | printf("Network is a network unit test\n"); |
---|
| 19 | printf(" --help: this output\n"); |
---|
[5624] | 20 | printf(" --sockettest test network_socket\n"); |
---|
[5647] | 21 | printf(" --frameworktest test the network module\n"); |
---|
[5609] | 22 | printf("\n"); |
---|
| 23 | } |
---|
[5579] | 24 | |
---|
[5647] | 25 | |
---|
| 26 | /* test SDL network socket */ |
---|
[5624] | 27 | int testSocket(int argc, char** argv) |
---|
| 28 | { |
---|
| 29 | NetworkSocket client; |
---|
| 30 | NetworkSocket server; |
---|
| 31 | IPaddress ip; |
---|
| 32 | SDLNet_ResolveHost(&ip, "127.0.0.1", 9999); |
---|
| 33 | server.listen(9999); |
---|
[5630] | 34 | SDL_Delay(20); |
---|
[5624] | 35 | client.connectToServer(ip, 9999); |
---|
| 36 | char buf[1024]; |
---|
| 37 | |
---|
| 38 | printf("read from client before sending data\n"); |
---|
[5627] | 39 | printf("result: %d bytes\n", client.readBytes((byte*)buf, 1024)); |
---|
[5624] | 40 | |
---|
| 41 | printf("read from server before sending data\n"); |
---|
[5627] | 42 | printf("result: %d bytes\n", server.readBytes((byte*)buf, 1024)); |
---|
[5624] | 43 | |
---|
[5627] | 44 | int n; |
---|
[5630] | 45 | char * str1 = "client to server"; |
---|
[5624] | 46 | char * str2 = "server to client"; |
---|
[5627] | 47 | n = client.writeBytes((byte*)str1, strlen(str1)+1); |
---|
[5630] | 48 | printf("%d bytes send from client\n", n); |
---|
[5627] | 49 | n = server.writeBytes((byte*)str2, strlen(str2)+1); |
---|
[5630] | 50 | printf("%d bytes send from server\n", n); |
---|
[5624] | 51 | SDL_Delay(1000); |
---|
[5630] | 52 | |
---|
[5624] | 53 | printf("read from server\n"); |
---|
| 54 | n = server.readBytes((byte*)buf, 1024); |
---|
| 55 | printf("read %d bytes\n", n); |
---|
| 56 | if (n<0) |
---|
| 57 | return -1; |
---|
| 58 | |
---|
| 59 | printf("data: %s\n", buf); |
---|
| 60 | printf("read from client\n"); |
---|
| 61 | n = client.readBytes((byte*)buf, 1024); |
---|
| 62 | |
---|
| 63 | printf("read %d bytes\n", n); |
---|
| 64 | if (n<0) |
---|
| 65 | return -1; |
---|
| 66 | |
---|
| 67 | printf("data: %s\n", buf); |
---|
| 68 | |
---|
[5628] | 69 | printf("testing a bigger amount of data\n"); |
---|
| 70 | |
---|
[5630] | 71 | #define _N_ELEMENTS 212994 |
---|
| 72 | char sendbuf[_N_ELEMENTS+1]; |
---|
| 73 | char recvbuf[_N_ELEMENTS+1]; |
---|
| 74 | sendbuf[_N_ELEMENTS] = '\0'; |
---|
| 75 | recvbuf[_N_ELEMENTS] = '\0'; |
---|
[5628] | 76 | |
---|
| 77 | for (int i = 0; i<_N_ELEMENTS; i++) |
---|
[5630] | 78 | sendbuf[i] = i%26 + 65; |
---|
[5628] | 79 | |
---|
| 80 | printf("write\n"); |
---|
[5630] | 81 | printf("result = %d\n", client.writeBytes((byte*)sendbuf, _N_ELEMENTS)); |
---|
[5628] | 82 | |
---|
[5630] | 83 | SDL_Delay(50); |
---|
[5628] | 84 | |
---|
| 85 | printf("read\n"); |
---|
| 86 | int nbytes = server.readBytes((byte*)recvbuf, _N_ELEMENTS); |
---|
| 87 | int offset = nbytes; |
---|
| 88 | |
---|
| 89 | while (nbytes>0) |
---|
| 90 | { |
---|
| 91 | SDL_Delay(10); |
---|
[5630] | 92 | //printf("read\n"); |
---|
[5628] | 93 | nbytes = server.readBytes((byte*)recvbuf+offset, _N_ELEMENTS-offset); |
---|
| 94 | offset += nbytes; |
---|
[5630] | 95 | //printf("nbytes=%d, offset=%d\n", nbytes, offset); |
---|
[5628] | 96 | } |
---|
| 97 | |
---|
| 98 | printf("strcmp = %d (0 is good :D not 0 is evil)\noffset = %d\n", strncmp(sendbuf, recvbuf, _N_ELEMENTS), offset); |
---|
| 99 | |
---|
[5630] | 100 | //printf("%s\n%s\n", sendbuf, recvbuf); |
---|
| 101 | |
---|
| 102 | for (int i = 0; i<_N_ELEMENTS; i++) |
---|
| 103 | { |
---|
| 104 | if (sendbuf[i]!=recvbuf[i]) |
---|
| 105 | { |
---|
| 106 | printf("byte %d is the first difference\n", i+1); |
---|
| 107 | break; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
[5624] | 111 | return 0; |
---|
| 112 | } |
---|
| 113 | |
---|
[5647] | 114 | |
---|
| 115 | int testFramework(int argc, char** argv) |
---|
| 116 | { |
---|
| 117 | printf("=================\n"); |
---|
| 118 | printf("TestFramework\n"); |
---|
| 119 | printf("=================\n"); |
---|
| 120 | |
---|
| 121 | IPaddress ip; |
---|
| 122 | //SDLNet_ResolveHost(&ip, "127.0.0.1", 9999); |
---|
| 123 | SDLNet_ResolveHost(&ip, "localhost", 9999); |
---|
| 124 | Synchronizeable sync; |
---|
| 125 | |
---|
| 126 | /* create the network manager */ |
---|
| 127 | NetworkManager* nm = new NetworkManager(); |
---|
| 128 | |
---|
| 129 | /* initialize the network manager */ |
---|
| 130 | nm->initialize(); |
---|
| 131 | |
---|
| 132 | /* esatblish a connection */ |
---|
| 133 | nm->establishConnection(ip, sync); |
---|
| 134 | |
---|
| 135 | /* delete the network manager again */ |
---|
| 136 | delete nm; |
---|
| 137 | |
---|
| 138 | return 0; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
[5579] | 142 | /** |
---|
| 143 | * |
---|
| 144 | * main function |
---|
| 145 | * |
---|
| 146 | * here the journey begins |
---|
| 147 | */ |
---|
| 148 | int main(int argc, char** argv) |
---|
| 149 | { |
---|
| 150 | // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark. |
---|
| 151 | int i; |
---|
| 152 | for(i = 1; i < argc; ++i) |
---|
| 153 | { |
---|
| 154 | //else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; |
---|
[5647] | 155 | if (! strcmp( "--sockettest", argv[i]) || !strcmp("-s", argv[i])) return testSocket(argc, argv); |
---|
| 156 | else if (! strcmp( "--frameworktest", argv[i]) || !strcmp("-s", argv[i])) return testFramework(argc, argv); |
---|
[5579] | 157 | } |
---|
| 158 | |
---|
[5647] | 159 | startHelp(argc, argv); |
---|
| 160 | |
---|
[5580] | 161 | return 0; |
---|
[5579] | 162 | } |
---|
| 163 | |
---|
| 164 | |
---|
[5580] | 165 | bool ShellBuffer::addBufferLineStatic(const char* line, ...) |
---|
| 166 | { |
---|
| 167 | va_list arguments; |
---|
| 168 | vprintf(line, arguments); |
---|
| 169 | |
---|
[5616] | 170 | } |
---|