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 | client.connectToServer(ip, 9999); |
---|
30 | char buf[1024]; |
---|
31 | |
---|
32 | printf("read from client before sending data\n"); |
---|
33 | printf("result: %d bytes\n", client.readBytes((byte*)buf, 1024)); |
---|
34 | |
---|
35 | printf("read from server before sending data\n"); |
---|
36 | printf("result: %d bytes\n", server.readBytes((byte*)buf, 1024)); |
---|
37 | |
---|
38 | int n; |
---|
39 | char * str1 = "client to server"; |
---|
40 | char * str2 = "server to client"; |
---|
41 | n = client.writeBytes((byte*)str1, strlen(str1)+1); |
---|
42 | printf("%d bytes send from client\n", n); |
---|
43 | n = server.writeBytes((byte*)str2, strlen(str2)+1); |
---|
44 | printf("%d bytes send from server\n", n); |
---|
45 | SDL_Delay(1000); |
---|
46 | |
---|
47 | printf("read from server\n"); |
---|
48 | n = server.readBytes((byte*)buf, 1024); |
---|
49 | printf("read %d bytes\n", n); |
---|
50 | if (n<0) |
---|
51 | return -1; |
---|
52 | |
---|
53 | printf("data: %s\n", buf); |
---|
54 | printf("read from client\n"); |
---|
55 | n = client.readBytes((byte*)buf, 1024); |
---|
56 | |
---|
57 | printf("read %d bytes\n", n); |
---|
58 | if (n<0) |
---|
59 | return -1; |
---|
60 | |
---|
61 | printf("data: %s\n", buf); |
---|
62 | |
---|
63 | printf("testing a bigger amount of data\n"); |
---|
64 | |
---|
65 | #define _N_ELEMENTS 212992 |
---|
66 | char sendbuf[_N_ELEMENTS]; |
---|
67 | char recvbuf[_N_ELEMENTS]; |
---|
68 | |
---|
69 | for (int i = 0; i<_N_ELEMENTS; i++) |
---|
70 | sendbuf[i] = i%30 + 30; |
---|
71 | |
---|
72 | printf("write\n"); |
---|
73 | client.writeBytes((byte*)sendbuf, _N_ELEMENTS); |
---|
74 | |
---|
75 | SDL_Delay(500); |
---|
76 | |
---|
77 | printf("read\n"); |
---|
78 | int nbytes = server.readBytes((byte*)recvbuf, _N_ELEMENTS); |
---|
79 | int offset = nbytes; |
---|
80 | |
---|
81 | while (nbytes>0) |
---|
82 | { |
---|
83 | SDL_Delay(10); |
---|
84 | printf("read\n"); |
---|
85 | nbytes = server.readBytes((byte*)recvbuf+offset, _N_ELEMENTS-offset); |
---|
86 | offset += nbytes; |
---|
87 | printf("nbytes=%d, offset=%d\n", nbytes, offset); |
---|
88 | } |
---|
89 | |
---|
90 | printf("strcmp = %d (0 is good :D not 0 is evil)\noffset = %d\n", strncmp(sendbuf, recvbuf, _N_ELEMENTS), offset); |
---|
91 | |
---|
92 | return 0; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * |
---|
97 | * main function |
---|
98 | * |
---|
99 | * here the journey begins |
---|
100 | */ |
---|
101 | int main(int argc, char** argv) |
---|
102 | { |
---|
103 | // here the pre-arguments are loaded, these are needed to go either to orxonx itself, Help, or Benchmark. |
---|
104 | int i; |
---|
105 | for(i = 1; i < argc; ++i) |
---|
106 | { |
---|
107 | if(! strcmp( "--help", argv[i]) || !strcmp("-h", argv[i])) return startHelp(argc, argv); |
---|
108 | //else if(!strcmp( "--gui", argv[i]) || !strcmp("-g", argv[i])) showGui = true; |
---|
109 | else if (! strcmp( "--sockettest", argv[i]) || !strcmp("-s", argv[i])) return testSocket(argc, argv); |
---|
110 | } |
---|
111 | |
---|
112 | /* create the network manager */ |
---|
113 | NetworkManager* nm = new NetworkManager(); |
---|
114 | |
---|
115 | /* initialize the network manager */ |
---|
116 | nm->initialize(); |
---|
117 | |
---|
118 | /* esatblish a connection */ |
---|
119 | nm->establishConnection(); |
---|
120 | |
---|
121 | //ClassList::debug(3, 0); |
---|
122 | |
---|
123 | return 0; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | bool ShellBuffer::addBufferLineStatic(const char* line, ...) |
---|
128 | { |
---|
129 | va_list arguments; |
---|
130 | vprintf(line, arguments); |
---|
131 | |
---|
132 | } |
---|