-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.h
More file actions
63 lines (51 loc) · 1.18 KB
/
server.h
File metadata and controls
63 lines (51 loc) · 1.18 KB
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
#pragma once
#include <iostream>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
class session : public std::enable_shared_from_this<session>
{
public:
session(tcp::socket socket, const unsigned int& id):
socket_(std::move(socket)),
session_id(id)
{
}
void start()
{
do_read();
}
~session() noexcept
{
boost::system::error_code ec;
if (socket_.is_open())
{
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
socket_.close(ec);
if (ec) {
std::cerr << "\tClose socket error: " << ec.message() << std::endl;
}
}
}
private:
void do_read();
tcp::socket socket_;
enum { max_length = 1024 };
char data_[max_length] = {0};
unsigned int session_id = 0 ;
};
class server
{
public:
server(boost::asio::io_service&& io_service, const unsigned int& port) :
ep(tcp::v4(), port),
acceptor_(io_service, ep),
session_id(0)
{
do_accept();
}
private:
void do_accept();
const tcp::endpoint ep;
tcp::acceptor acceptor_;
unsigned int session_id = 0;
};