-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
149 lines (116 loc) · 3.71 KB
/
test.cpp
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
#include <stdlib.h>
#include <iostream>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/random_device.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/chrono.hpp>
#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <MemoryStreamService.h>
namespace ba = boost::asio;
#define BUF_SIZE 20
class RandomDataGenerator
{
public:
RandomDataGenerator(MemoryStream &svc) : m_svc(svc), m_packet_cnt(0) {};
void run()
{
boost::random::mt19937 gen;
std::string chars(
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890"
"!@#$%^&*()"
"`~-_=+[{]{\\|;:'\",<.>/? ");
boost::random::uniform_int_distribution<> dist(1, 200);
boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
while(true)
{
boost::this_thread::sleep_for(boost::chrono::milliseconds(dist(gen)));
std::stringstream ss;
ss << m_packet_cnt++ << " " << ">| ";
for(int i = 0; i < 8; ++i)
{
ss << chars[index_dist(gen)];
}
ss << " |<" << std::endl;
m_svc.pushReadBuffer(ss.str());
}
}
private:
std::size_t m_packet_cnt;
MemoryStream &m_svc;
};
class AsyncReader
{
public:
AsyncReader(MemoryStream &svc) : m_svc(svc)
{
buf = new char[BUF_SIZE + 1];
memset(buf, 0, BUF_SIZE + 1);
}
virtual ~AsyncReader()
{
delete[] buf;
}
void do_read()
{
ba::async_read(m_svc, ba::buffer(buf, BUF_SIZE),
boost::bind(&AsyncReader::read_done_handler, this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void read_done_handler(const boost::system::error_code& ec,
std::size_t bytes_transferred)
{
std::cout << buf;
do_read();
}
private:
char *buf;
MemoryStream &m_svc;
};
class AsyncWriter
{
public:
AsyncWriter(MemoryStream &svc) : m_svc(svc)
{
buf = "0123456789 ";
}
virtual ~AsyncWriter()
{
}
void do_write()
{
ba::async_write(m_svc, ba::buffer((void *)buf.data(), buf.size()),
boost::bind(&AsyncWriter::write_done_handler, this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void write_done_handler(const boost::system::error_code& ec,
std::size_t bytes_transferred)
{
do_write();
}
private:
std::string buf;
MemoryStream &m_svc;
};
int main()
{
ba::io_service io_service;
MemoryStream port(io_service);
// spin up worker thread for the io_service
boost::thread worker_thread(boost::bind(&boost::asio::io_service::run, &io_service));
// spin up the dummy data generator simulating packets from server
RandomDataGenerator data_gen(port);
boost::thread worker_thread2(boost::bind(&RandomDataGenerator::run, &data_gen));
// async reader & writer
AsyncReader reader(port);
AsyncWriter writer(port);
reader.do_read();
writer.do_write();
while(true)
{
std::cerr << port.popWriteBuffer() << std::endl;
//boost::this_thread::sleep_for(boost::chrono::seconds(1));
}
}