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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
#include "uscxml/config.h"
#include "uscxml/Convenience.h"
#include "uscxml/server/Socket.h"
#include <iostream>
#include <stdexcept>
#include <event2/event.h>
#include "event2/thread.h"
#ifdef HAS_SIGNAL_H
#include <signal.h>
#endif
#include "uscxml/concurrency/tinythread.h"
using namespace uscxml;
class TestServer : public ServerSocket {
public:
TestServer(int domain, int type, int protocol) : ServerSocket(domain, type, protocol) {}
virtual void readCallback(const char* data, size_t size, Connection& conn) {
std::string content(data, size);
// std::cout << "Server got: " << content << std::endl;
std::string urghs("hi!");
conn.reply(urghs.data(), urghs.size());
};
};
int packetSeq = 0;
class LogServer : public ServerSocket {
public:
LogServer(int domain, int type, int protocol) : ServerSocket(domain, type, protocol) {}
virtual void readCallback(const char* data, size_t size, Connection& conn) {
std::string content(data, size);
std::cout << "Server got: " << content << std::endl;
};
};
class CountingPacketServer : public PacketServerSocket {
public:
CountingPacketServer(int domain, int type, int protocol, const std::string& sep) : PacketServerSocket(domain, type, protocol, sep) {}
virtual void readCallback(const std::string& packet, Connection& conn) {
// std::cout << "-- " << packet << std::endl;
size_t seq = strTo<size_t>(packet);
assert(seq == packetSeq);
packetSeq++;
};
};
class TestClient : public ClientSocket {
public:
TestClient(int domain, int type, int protocol) : ClientSocket(domain, type, protocol) {}
virtual void readCallback(const char* data, size_t size) {
std::string content(data, size);
};
};
int main(int argc, char** argv) {
#if defined(HAS_SIGNAL_H) && !defined(WIN32)
signal(SIGPIPE, SIG_IGN);
#endif
#ifndef _WIN32
evthread_use_pthreads();
#else
evthread_use_windows_threads();
#endif
if (1) {
packetSeq = 0;
CountingPacketServer server(PF_INET, SOCK_STREAM, 0, std::string("tadaa!"));
// LogServer server(PF_INET, SOCK_STREAM, 0);
server.listen("*", 1235);
server.setBlockSizeRead(1);
TestClient client(PF_INET, SOCK_STREAM, 0);
client.connect("127.0.0.1", 1235);
int iterations = 1000;
std::stringstream contentSS;
for (int i = 0; i < iterations; i++) {
contentSS << toStr(i);
contentSS << "tadaa!";
}
client.write(contentSS.str());
while(packetSeq != iterations)
tthread::this_thread::sleep_for(tthread::chrono::milliseconds(20));
}
if (1) {
packetSeq = 0;
CountingPacketServer server(PF_INET, SOCK_STREAM, 0, std::string("\0", 1));
server.listen("*", 1235);
TestClient client(PF_INET, SOCK_STREAM, 0);
client.connect("127.0.0.1", 1235);
int iterations = 1000;
for (int i = 0; i < iterations; i++) {
client.write(toStr(i));
client.write("\0", 1);
}
while(packetSeq != iterations)
tthread::this_thread::sleep_for(tthread::chrono::milliseconds(20));
}
exit(0);
if (1) {
// start server socket and connect
int iterations = 100;
TestServer server(PF_INET, SOCK_STREAM, 0);
try {
server.listen("*", 1234);
while(iterations--) {
std::cout << iterations << std::endl;
TestClient client(PF_INET, SOCK_STREAM, 0);
client.connect("127.0.0.1", 1234);
std::string hello("hello");
client.write(hello.data(), hello.size());
tthread::this_thread::sleep_for(tthread::chrono::milliseconds(20));
}
} catch (std::runtime_error e) {
std::cout << e.what() << std::endl;
}
}
{
// connect client to server and kill server
int iterations = 100;
try {
while(iterations--) {
std::cout << iterations << std::endl;
TestServer* server = new TestServer(PF_INET, SOCK_STREAM, 0);
server->listen("*", 1236 + iterations);
TestClient client(PF_INET, SOCK_STREAM, 0);
client.connect("127.0.0.1", 1236 + iterations);
std::string hello("hello");
client.write(hello.data(), hello.size());
delete server;
tthread::this_thread::sleep_for(tthread::chrono::milliseconds(20));
}
} catch (std::runtime_error e) {
std::cout << e.what() << std::endl;
}
}
}
|