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 | |
---|
11 | int verbose = 4; |
---|
12 | |
---|
13 | |
---|
14 | int startHelp(int argc, char** argv) |
---|
15 | { |
---|
16 | printf("Network is a network unit test\n"); |
---|
17 | printf(" --help: this output\n"); |
---|
18 | printf(" --sockettest test network_socket\n"); |
---|
19 | printf("\n"); |
---|
20 | } |
---|
21 | |
---|
22 | int testSocket(int argc, char** argv) |
---|
23 | { |
---|
24 | NetworkSocket client; |
---|
25 | NetworkSocket server; |
---|
26 | IPaddress ip; |
---|
27 | SDLNet_ResolveHost(&ip, "127.0.0.1", 9999); |
---|
28 | server.listen(9999); |
---|
29 | SDL_Delay(20); |
---|
30 | client.connectToServer(ip, 9999); |
---|
31 | char buf[1024]; |
---|
32 | |
---|
33 | printf("read from client before sending data\n"); |
---|
34 | printf("result: %d bytes\n", client.readBytes((byte*)buf, 1024)); |
---|
35 | |
---|
36 | printf("read from server before sending data\n"); |
---|
37 | printf("result: %d bytes\n", server.readBytes((byte*)buf, 1024)); |
---|
38 | |
---|
39 | int n; |
---|
40 | char * str1 = "client to server"; |
---|
41 | char * str2 = "server to client"; |
---|
42 | n = client.writeBytes((byte*)str1, strlen(str1)+1); |
---|
43 | printf("%d bytes send from client\n", n); |
---|
44 | n = server.writeBytes((byte*)str2, strlen(str2)+1); |
---|
45 | printf("%d bytes send from server\n", n); |
---|
46 | SDL_Delay(1000); |
---|
47 | |
---|
48 | printf("read from server\n"); |
---|
49 | n = server.readBytes((byte*)buf, 1024); |
---|
50 | printf("read %d bytes\n", n); |
---|
51 | if (n<0) |
---|
52 | return -1; |
---|
53 | |
---|
54 | printf("data: %s\n", buf); |
---|
55 | printf("read from client\n"); |
---|
56 | n = client.readBytes((byte*)buf, 1024); |
---|
57 | |
---|
58 | printf("read %d bytes\n", n); |
---|
59 | if (n<0) |
---|
60 | return -1; |
---|
61 | |
---|
62 | printf("data: %s\n", buf); |
---|
63 | |
---|
64 | printf("testing a bigger amount of data\n"); |
---|
65 | |
---|
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'; |
---|
71 | |
---|
72 | for (int i = 0; i<_N_ELEMENTS; i++) |
---|
73 | sendbuf[i] = i%26 + 65; |
---|
74 | |
---|
75 | printf("write\n"); |
---|
76 | printf("result = %d\n", client.writeBytes((byte*)sendbuf, _N_ELEMENTS)); |
---|
77 | |
---|
78 | SDL_Delay(50); |
---|
79 | |
---|
80 | printf("read\n"); |
---|
81 | int nbytes = server.readBytes((byte*)recvbuf, _N_ELEMENTS); |
---|
82 | int offset = nbytes; |
---|
83 | |
---|
84 | while (nbytes>0) |
---|
85 | { |
---|
86 | SDL_Delay(10); |
---|
87 | //printf("read\n"); |
---|
88 | nbytes = server.readBytes((byte*)recvbuf+offset, _N_ELEMENTS-offset); |
---|
89 | offset += nbytes; |
---|
90 | //printf("nbytes=%d, offset=%d\n", nbytes, offset); |
---|
91 | } |
---|
92 | |
---|
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 | } |
---|
105 | |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * |
---|
111 | * main function |
---|
112 | * |
---|
113 | * here the journey begins |
---|
114 | */ |
---|
115 | int main(int argc, char** argv) |
---|
116 | { |
---|
117 | // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark. |
---|
118 | int i; |
---|
119 | for(i = 1; i < argc; ++i) |
---|
120 | { |
---|
121 | if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv); |
---|
122 | //else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; |
---|
123 | else if (! strcmp( "--sockettest", argv[i]) || !strcmp("-s", argv[i])) return testSocket(argc, argv); |
---|
124 | } |
---|
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(); |
---|
134 | |
---|
135 | //ClassList::debug(3, 0); |
---|
136 | |
---|
137 | return 0; |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | bool ShellBuffer::addBufferLineStatic(const char* line, ...) |
---|
142 | { |
---|
143 | va_list arguments; |
---|
144 | vprintf(line, arguments); |
---|
145 | |
---|
146 | } |
---|