1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 18:34:25 +01:00
socket might be closed after session-thread is spawned.
socket might throw errors very early.
This commit is contained in:
Ralf Ramsauer 2015-09-24 23:10:23 +02:00
parent 93fccda5d8
commit b07c0e8f54

View File

@ -47,14 +47,21 @@ static void signal_handler(int signum)
static void session(tcp::socket &&sock) static void session(tcp::socket &&sock)
{ {
boost::asio::ip::address remoteAddress;
unsigned short remotePort;
try {
boost::system::error_code error; boost::system::error_code error;
const auto remoteIP = sock.remote_endpoint().address();
std::vector<char> data; std::vector<char> data;
data.resize(SOCKET_BUFFERLENGTH); data.resize(SOCKET_BUFFERLENGTH);
l("Incoming TCP connection from " + remoteIP.to_string(), LogLevel::notice); remoteAddress = sock.remote_endpoint().address();
remotePort = sock.remote_endpoint().port();
l("Incoming TCP connection from " + remoteAddress.to_string() + "("
+ std::to_string(remotePort) + ")",
LogLevel::notice);
try {
size_t length = sock.read_some(boost::asio::buffer(data), size_t length = sock.read_some(boost::asio::buffer(data),
error); error);
if (error == boost::asio::error::eof) if (error == boost::asio::error::eof)
@ -92,7 +99,7 @@ static void session(tcp::socket &&sock)
if (command == "lock" || command == "unlock") { if (command == "lock" || command == "unlock") {
response = logic->parseRequest(root); response = logic->parseRequest(root);
} else if (command == "subscribe") { } else if (command == "subscribe") {
if (remoteIP.is_loopback() == false) { if (remoteAddress.is_loopback() == false) {
response.code = Response::Code::AccessDenied; response.code = Response::Code::AccessDenied;
response.message = "Subscriptions are only allowed from localhost"; response.message = "Subscriptions are only allowed from localhost";
l(response.message, LogLevel::warning); l(response.message, LogLevel::warning);
@ -129,11 +136,11 @@ static void session(tcp::socket &&sock)
} }
} }
catch (const std::exception &e) { catch (const std::exception &e) {
std::string message = "Exception in session " + remoteIP.to_string() std::string message = "Exception in session " + remoteAddress.to_string()
+ ": " + e.what(); + ":" + std::to_string(remotePort) + " : " + e.what();
l(message, LogLevel::error); l(message, LogLevel::error);
} }
l("Closing TCP connection from " + remoteIP.to_string(), LogLevel::notice); l("Closing TCP connection from " + remoteAddress.to_string(), LogLevel::notice);
} }
static void server(unsigned short port) static void server(unsigned short port)