-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
134 lines (116 loc) · 3.34 KB
/
main.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
#include <iostream>
#include "async.h"
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include "config.h"
//https://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/example/echo/async_tcp_echo_server.cpp
using boost::asio::ip::tcp;
class BulkSession
{
public:
BulkSession(boost::asio::io_service& io_service, std::size_t bulkSize)
: m_socket(io_service)
{
#ifdef _DEBUG
std::cout << "BulkSession " << this << std::endl;
#endif
m_asyncHandler = async::connect(bulkSize);
}
~BulkSession()
{
#ifdef _DEBUG
std::cout << "~BulkSession " << this << std::endl;
#endif
async::disconnect(m_asyncHandler);
}
tcp::socket& socket()
{
return m_socket;
}
void start()
{
m_socket.async_read_some(boost::asio::buffer(m_data, max_length),
boost::bind(&BulkSession::handleRead, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handleRead(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (!error)
{
async::receive(m_asyncHandler, m_data, bytes_transferred);
//close();
}
else
{
close();
}
}
void close()
{
delete this;
}
private:
tcp::socket m_socket;
enum { max_length = (1024 * 5) };
char m_data[max_length];
async::handle_t m_asyncHandler;
};
class BulkServer
{
public:
BulkServer(boost::asio::io_service& io_service, short port, std::size_t bulkSize)
: m_io_service(io_service)
, m_acceptor(io_service, tcp::endpoint(tcp::v4(), port))
, m_bulkSize(bulkSize)
{
BulkSession* newSession = new BulkSession(m_io_service, m_bulkSize);
m_acceptor.async_accept(newSession->socket(),
boost::bind(&BulkServer::handleAccept, this, newSession,
boost::asio::placeholders::error));
}
void handleAccept(BulkSession* newSession,
const boost::system::error_code& error)
{
if (!error)
{
newSession->start();
newSession = new BulkSession(m_io_service, m_bulkSize);
m_acceptor.async_accept(newSession->socket(),
boost::bind(&BulkServer::handleAccept, this, newSession,
boost::asio::placeholders::error));
}
else
{
delete newSession;
}
}
private:
boost::asio::io_service& m_io_service;
tcp::acceptor m_acceptor;
std::size_t m_bulkSize;
};
//sudo apt-get install netcat-traditional
//bulk_server 9000 3
//seq 0 999 | nc -q 0 localhost 9000
int main(int argc, char * argv[]) {
try
{
if (argc != 3)
{
std::cerr << "Usage: bulk_server <port> <bulkSize>\n";
return 1;
}
boost::asio::io_service io_service;
using namespace std;
BulkServer s(io_service, std::atoi(argv[1]), std::atoi(argv[2]));
io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}