switched from PracticalSocket to libasio
[anytun.git] / src / Sockets / tests / crlf.cpp
blob34500c48cbb0f11ccf79ebb297a0c2d88adf8a5c
1 #include <TcpSocket.h>
2 #include <SocketHandler.h>
3 #include <ListenSocket.h>
6 static bool quit = false;
9 class IOSocket : public TcpSocket
11 public:
12 IOSocket(ISocketHandler& h) : TcpSocket(h) {
13 SetLineProtocol();
15 ~IOSocket() {}
17 void OnConnect() {
18 Utility::GetTime(&m_start);
19 while (GetOutputLength() < 500000)
21 Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
22 Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
23 Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
24 Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
25 Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
26 Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
27 Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
28 Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
30 // Send("\r\n");
33 void OnAccept() {
34 SetLineProtocol();
37 void OnDelete() {
38 printf("OnDelete\n");
41 void OnWriteComplete() {
42 printf("OnWriteComplete\n");
43 struct timeval tv;
44 Utility::GetTime(&tv);
46 tv.tv_sec -= m_start.tv_sec;
47 tv.tv_usec -= m_start.tv_usec;
48 Utility::GetTime(&m_start);
49 if (tv.tv_usec < 0)
51 tv.tv_usec += 1000000;
52 tv.tv_sec -= 1;
54 double t = tv.tv_usec;
55 t /= 1000000;
56 t += tv.tv_sec;
57 printf(" Time: %ld.%06ld (%f)\n", tv.tv_sec, tv.tv_usec, t);
58 double r = GetBytesReceived();
59 printf(" bytes in: %lld (%f Mbytes/sec)\n", GetBytesReceived(true), r / t / 1000000);
60 double s = GetBytesSent();
61 printf(" bytes out: %lld (%f Mbytes/sec)\n", GetBytesSent(true), s / t / 1000000);
62 printf("\n");
64 while (GetOutputLength() < 500000)
66 Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
67 Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
68 Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
69 Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
70 Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
71 Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
72 Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
73 Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
75 // Send("\r\n");
78 void OnLine(const std::string& line) {
79 if (line == "")
81 struct timeval tv;
82 Utility::GetTime(&tv);
84 tv.tv_sec -= m_start.tv_sec;
85 tv.tv_usec -= m_start.tv_usec;
86 Utility::GetTime(&m_start);
87 if (tv.tv_usec < 0)
89 tv.tv_usec += 1000000;
90 tv.tv_sec -= 1;
92 double t = tv.tv_usec;
93 t /= 1000000;
94 t += tv.tv_sec;
95 printf(" Time: %ld.%06ld (%f)\n", tv.tv_sec, tv.tv_usec, t);
96 double r = GetBytesReceived();
97 printf(" bytes in: %lld (%f Mbytes/sec)\n", GetBytesReceived(true), r / t / 1000000);
98 double s = GetBytesSent();
99 printf(" bytes out: %lld (%f Mbytes/sec)\n", GetBytesSent(true), s / t / 1000000);
100 printf("\n");
103 else
104 if (line != "zzzzzzzzzmmmmmmmmm1234lkkk54" &&
105 line != "zzzzzzzzzmmmmmmmmm1234lkkk543")
107 printf("\n-------------------------------------------------------\n");
108 for (size_t i = 0; i < line.size(); i++)
109 if (!isprint(line[i]))
110 printf("<%d>", line[i]);
111 else
112 printf("%c", line[i]);
113 printf("\n");
114 fflush(stdout);
115 exit(-1);
116 quit = true;
120 struct timeval m_start;
124 int main(int argc, char *argv[])
126 SocketHandler h;
127 ListenSocket<IOSocket> l(h);
128 l.Bind(12344);
129 h.Add(&l);
130 IOSocket sock(h);
131 sock.Open("192.168.7.4", 12344);
132 h.Add(&sock);
133 while (!quit)
135 h.Select(1, 0);