summaryrefslogtreecommitdiff
path: root/Sockets/tests/crlf.cpp
blob: 34500c48cbb0f11ccf79ebb297a0c2d88adf8a5c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <TcpSocket.h>
#include <SocketHandler.h>
#include <ListenSocket.h>


static	bool quit = false;


class IOSocket : public TcpSocket
{
public:
	IOSocket(ISocketHandler& h) : TcpSocket(h) {
		SetLineProtocol();
	}
	~IOSocket() {}

	void OnConnect() {
		Utility::GetTime(&m_start);
		while (GetOutputLength() < 500000)
		{
			Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
		}
//		Send("\r\n");
	}

	void OnAccept() {
		SetLineProtocol();
	}

	void OnDelete() {
		printf("OnDelete\n");
	}

	void OnWriteComplete() {
printf("OnWriteComplete\n");
		struct timeval tv;
		Utility::GetTime(&tv);

		tv.tv_sec -= m_start.tv_sec;
		tv.tv_usec -= m_start.tv_usec;
		Utility::GetTime(&m_start);
		if (tv.tv_usec < 0)
		{
			tv.tv_usec += 1000000;
			tv.tv_sec -= 1;
		}
		double t = tv.tv_usec;
		t /= 1000000;
		t += tv.tv_sec;
		printf("  Time: %ld.%06ld (%f)\n", tv.tv_sec, tv.tv_usec, t);
		double r = GetBytesReceived();
		printf("  bytes in: %lld (%f Mbytes/sec)\n", GetBytesReceived(true), r / t / 1000000);
		double s = GetBytesSent();
		printf("  bytes out: %lld (%f Mbytes/sec)\n", GetBytesSent(true), s / t / 1000000);
		printf("\n");

		while (GetOutputLength() < 500000)
		{
			Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk54\r\n");
			Send("zzzzzzzzzmmmmmmmmm1234lkkk543\r\n");
		}
//		Send("\r\n");
	}

	void OnLine(const std::string& line) {
		if (line == "")
		{
			struct timeval tv;
			Utility::GetTime(&tv);

			tv.tv_sec -= m_start.tv_sec;
			tv.tv_usec -= m_start.tv_usec;
			Utility::GetTime(&m_start);
			if (tv.tv_usec < 0)
			{
				tv.tv_usec += 1000000;
				tv.tv_sec -= 1;
			}
			double t = tv.tv_usec;
			t /= 1000000;
			t += tv.tv_sec;
			printf("  Time: %ld.%06ld (%f)\n", tv.tv_sec, tv.tv_usec, t);
			double r = GetBytesReceived();
			printf("  bytes in: %lld (%f Mbytes/sec)\n", GetBytesReceived(true), r / t / 1000000);
			double s = GetBytesSent();
			printf("  bytes out: %lld (%f Mbytes/sec)\n", GetBytesSent(true), s / t / 1000000);
			printf("\n");

		}
		else
		if (line != "zzzzzzzzzmmmmmmmmm1234lkkk54" && 
			line != "zzzzzzzzzmmmmmmmmm1234lkkk543")
		{
printf("\n-------------------------------------------------------\n");
			for (size_t i = 0; i < line.size(); i++)
				if (!isprint(line[i]))
					printf("<%d>", line[i]);
				else
					printf("%c", line[i]);
			printf("\n");
fflush(stdout);
exit(-1);
			quit = true;
		}
	}

	struct timeval m_start;
};


int main(int argc, char *argv[])
{
	SocketHandler h;
	ListenSocket<IOSocket> l(h);
	l.Bind(12344);
	h.Add(&l);
	IOSocket sock(h);
	sock.Open("192.168.7.4", 12344);
	h.Add(&sock);
	while (!quit)
	{
		h.Select(1, 0);
	}
}