-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cpp
66 lines (52 loc) · 1.49 KB
/
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
58
59
60
61
62
63
64
65
#include "Network.h"
#include <iostream>
class Server {
public:
Server(asio::io_service& service);
~Server();
void accept();
void onNewClient(const std::error_code& e);
void readHeader();
void onHeaderRead(const std::error_code& e, const std::size_t bytes);
private:
asio::ip::tcp::acceptor acceptor_;
asio::ip::tcp::socket socket_;
char buffer_[BUFFER_SIZE];
};
Server::Server(asio::io_service& service)
: acceptor_(service, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), 8096)),
socket_(service)
{
}
void Server::accept() {
acceptor_.async_accept(socket_, std::bind(&Server::onNewClient, this, std::placeholders::_1));
}
void Server::onNewClient(const std::error_code& e) {
if (!e) {
std::cout << "New client connected, reading header" << std::endl;
readHeader();
} else {
std::cout << "Connection accept error: " << e.message() << std::endl;
}
}
void Server::readHeader() {
asio::async_read(socket_, asio::buffer(buffer_, HEADER_SIZE),
std::bind(&Server::onHeaderRead, this, std::placeholders::_1, std::placeholders::_2));
}
void Server::onHeaderRead(const std::error_code& e, const std::size_t bytes) {
if (!e) {
std::cout << "Got header, size: " << bytes << std::endl;
std::cout << buffer_ << std::endl;
} else {
std::cout << "Header read error: " << e.message() << std::endl;
}
}
Server::~Server() {
}
int main() {
asio::io_service service;
Server s(service);
s.accept();
service.run();
return 0;
}