-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpServer.cpp
More file actions
65 lines (54 loc) · 1.73 KB
/
Copy pathTcpServer.cpp
File metadata and controls
65 lines (54 loc) · 1.73 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
64
65
#include "TcpServer.h"
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
TcpConnection::TcpConnection(TcpServer* tcpServer, boost::asio::ip::tcp::socket socket)
: tcpServer(tcpServer)
, socket(std::move(socket))
, buffer(tcpServer->bufferSize)
{
startRead();
}
void TcpConnection::startRead()
{
boost::asio::async_read(socket, boost::asio::buffer(buffer), [this](const boost::system::error_code& error, std::size_t bytesReceived) {
if (error) {
if (error == boost::asio::error::eof) {
std::cerr << "Connection closed" << std::endl;
} else {
std::cerr << "Error on receive: " << error.message() << std::endl;
}
tcpServer->closeConnection(this);
return;
}
if (bytesReceived > 0) {
std::cout << bytesReceived << " bytes received" << std::endl;
tcpServer->dataReceived("source", buffer.data(), bytesReceived);
}
startRead();
});
}
TcpServer::TcpServer(boost::asio::io_service& ioService, uint16_t port, uint32_t bufferSize)
: bufferSize(bufferSize)
, acceptor(ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))
, newSocket(ioService)
{
std::cout << "Listening on TCP port " << port << std::endl;
startAccept();
}
void TcpServer::startAccept()
{
acceptor.async_accept(newSocket, [this](const boost::system::error_code& error) {
if (!error) {
std::cout << "New client connected from " << newSocket.remote_endpoint().address() << std::endl;
connections.emplace_back(this, std::move(newSocket));
}
startAccept();
});
}
void TcpServer::closeConnection(TcpConnection* connection)
{
connections.erase(std::remove_if(connections.begin(), connections.end(), [connection](TcpConnection& testConn) {
return connection == &testConn;
}), connections.end());
}