-
Notifications
You must be signed in to change notification settings - Fork 5
/
Network.hpp
50 lines (42 loc) · 1.18 KB
/
Network.hpp
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
#ifndef __NETWORK__
#define __NETWORK__
#include <boost/asio.hpp>
#include <boost/bind.hpp>
class Network : public ANetwork
{
public:
~Network(){}
Network(boost::asio::io_service& io_service, short port, AProtocol* protocol)
: ANetwork(protocol),
__io_service(io_service),
__acceptor(io_service, tcp::endpoint(tcp::v4(), port))
{
NetworkSession* new_session = new NetworkSession(__io_service, this->__protocol);
__acceptor.async_accept(new_session->socket(),
boost::bind(&Network::accept, this, new_session,
boost::asio::placeholders::error));
}
void accept(NetworkSession* new_session,
const boost::system::error_code& error)
{
if (!error)
{
std::cout << "Accept" << std::endl;
new_session->start();
new_session = new NetworkSession(__io_service, this->__protocol);
__acceptor.async_accept(new_session->socket()
,boost::bind(&Network::accept
,this
,new_session
,boost::asio::placeholders::error));
}
else
delete new_session;
}
private:
Network(const Network&);
Network& operator=(const Network&);
boost::asio::io_service& __io_service;
tcp::acceptor __acceptor;
};
#endif /* __NETWORK__ */