-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_server.cpp
57 lines (49 loc) · 1.34 KB
/
my_server.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
#include <iostream>
#include <asio.hpp>
#include <memory>
#include <functional>
#include "my_session.h"
using namespace std::placeholders;
class Server {
public:
Server(asio::io_context& io_context, short port)
: acceptor_(io_context, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port)),
socket_(io_context) {}
void start() {
do_accept();
}
private:
void do_accept() {
acceptor_.async_accept(socket_, std::bind(&Server::handle_accept, this, _1));
}
/*
void handle_accept(const asio::error_code& error) {
if (!error) {
std::make_shared<Session>(std::move(socket_))->start();
}
do_accept();
}
*/
// when a new client connects to server
// use thread to run the new session and detach it, so that it will not block the main thread
void handle_accept(const asio::error_code& error) {
if (!error) {
std::thread t([this]() {
std::make_shared<Session>(std::move(socket_))->start();
});
t.detach();
}
do_accept();
}
private:
asio::ip::tcp::acceptor acceptor_;
asio::ip::tcp::socket socket_;
};
int main() {
asio::io_service io_service;
short port = 8000;
Server server(io_service, port);
server.start();
io_service.run();
return 0;
}