1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 10:24:26 +01:00

Implemented multi-threaded TCP session handling

This commit is contained in:
Ralf Ramsauer 2015-09-22 17:41:15 +02:00
parent cd1fcdc96b
commit 25c07e28a1

View File

@ -17,90 +17,75 @@ using namespace std;
namespace po = boost::program_options; namespace po = boost::program_options;
using boost::asio::ip::tcp; using boost::asio::ip::tcp;
// The receive buffer length of the TCP socket
const int constexpr SOCKET_BUFFERLENGTH = 2048;
const static Logger &l = Logger::get(); const static Logger &l = Logger::get();
static unique_ptr<Logic> logic = nullptr; static unique_ptr<Logic> logic = nullptr;
static boost::asio::io_service io_service;
boost::asio::io_service io_service; static void signal_handler(int signum)
void signal_handler(int signum)
{ {
(void)signum; l((std::string)"Received Signal " + std::to_string(signum),
LogLevel::warning);
io_service.stop(); io_service.stop();
logic.reset();
} }
/* static void session(tcp::socket &&sock)
* Session class handles asynchronosly handles one incoming TCP session
*/
class session
: public std::enable_shared_from_this<session>
{ {
boost::system::error_code error;
public: std::vector<char> data;
data.resize(SOCKET_BUFFERLENGTH);
session(tcp::socket socket) try {
: _socket(std::move(socket)) size_t length = sock.read_some(boost::asio::buffer(data), error);
{ if (error == boost::asio::error::eof)
return;
else if (error)
throw boost::system::system_error(error);
string request(data.begin(), data.begin()+length);
const auto rc = logic->parseRequest(request);
sock.write_some(boost::asio::buffer(to_string(rc) + "\n"), error);
if (error == boost::asio::error::eof)
return;
else if (error)
throw boost::system::system_error(error);
} }
catch (std::exception& e) {
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
void start() static void server(unsigned short port)
{ {
auto self(shared_from_this()); l(LogLevel::info, "Starting TCP Server");
_socket.async_read_some(boost::asio::buffer(_data, _maxLen),
[this, self](boost::system::error_code ec, std::size_t length) const auto endpoint = tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), port);
tcp::acceptor a(io_service, endpoint);
tcp::socket sock(io_service);
std::function<void(void)> accept_connection = [&] () {
a.async_accept(sock,
[&] (boost::system::error_code ec) {
if (ec)
{ {
if (!ec) return;
{ }
const string payload(_data, length); std::thread(session, std::move(sock)).detach();
const auto rc = logic->parseRequest(payload); accept_connection();
boost::asio::write(_socket, boost::asio::buffer(to_string(rc) + "\n")); });
} };
});
}
private: accept_connection();
tcp::socket _socket; io_service.run();
static constexpr int _maxLen = { 2048 }; l(LogLevel::info, "Stopped TCP Server");
char _data[_maxLen]; }
};
/*
* The TCP server
*/
class server
{
public:
server(boost::asio::io_service& io_service, short port)
: _acceptor(io_service, tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), port)),
_socket(io_service)
{
do_accept();
}
private:
void do_accept()
{
_acceptor.async_accept(_socket,
[this](boost::system::error_code ec)
{
if (!ec)
{
std::make_shared<session>(std::move(_socket))->start();
}
do_accept();
});
}
tcp::acceptor _acceptor;
tcp::socket _socket;
};
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
@ -159,6 +144,7 @@ int main(int argc, char** argv)
signal(SIGUSR1, signal_handler); signal(SIGUSR1, signal_handler);
signal(SIGUSR2, signal_handler); signal(SIGUSR2, signal_handler);
l(LogLevel::info, "Starting Doorlock Logic");
logic = unique_ptr<Logic>(new Logic(tokenTimeout, logic = unique_ptr<Logic>(new Logic(tokenTimeout,
ldapUri, ldapUri,
bindDN, bindDN,
@ -166,8 +152,7 @@ int main(int argc, char** argv)
serDev)); serDev));
try { try {
server s(io_service, port); server(port);
io_service.run();
} }
catch (const char* const &ex) { catch (const char* const &ex) {
ostringstream str; ostringstream str;
@ -180,6 +165,8 @@ int main(int argc, char** argv)
retval = 0; retval = 0;
out: out:
l(LogLevel::info, "Stopping Doorlock Logic");
logic.reset();
l(LogLevel::notice, "Doorlockd stopped"); l(LogLevel::notice, "Doorlockd stopped");
return retval; return retval;
} }