mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-02 11:46:34 +02:00
control socket on the stack
This commit is contained in:
parent
1974afc176
commit
45df7fdb42
4 changed files with 33 additions and 56 deletions
|
@ -76,59 +76,45 @@ void ControlServer::onMessageReceived(ControlSession* connection, const std::str
|
||||||
{
|
{
|
||||||
std::lock_guard<std::recursive_mutex> mlock(session_mutex_);
|
std::lock_guard<std::recursive_mutex> mlock(session_mutex_);
|
||||||
LOG(DEBUG) << "received: \"" << message << "\"\n";
|
LOG(DEBUG) << "received: \"" << message << "\"\n";
|
||||||
// if ((message == "quit") || (message == "exit") || (message == "bye"))
|
|
||||||
// {
|
|
||||||
// for (auto it = sessions_.begin(); it != sessions_.end(); ++it)
|
|
||||||
// {
|
|
||||||
// auto session = it->lock();
|
|
||||||
// if (!session)
|
|
||||||
// continue;
|
|
||||||
// if (session.get() == connection)
|
|
||||||
// {
|
|
||||||
// /// delete in a thread to avoid deadlock
|
|
||||||
// auto func = [&](std::shared_ptr<ControlSession> s) -> void { sessions_.erase(s); };
|
|
||||||
// std::thread t(func, *it);
|
|
||||||
// t.detach();
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
if (controlMessageReceiver_ != nullptr)
|
if (controlMessageReceiver_ != nullptr)
|
||||||
controlMessageReceiver_->onMessageReceived(connection, message);
|
controlMessageReceiver_->onMessageReceived(connection, message);
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void ControlServer::startAccept()
|
void ControlServer::startAccept()
|
||||||
{
|
{
|
||||||
|
auto accept_handler = [this](error_code ec, tcp::socket socket) {
|
||||||
|
if (!ec)
|
||||||
|
handleAccept(std::move(socket));
|
||||||
|
else
|
||||||
|
LOG(ERROR) << "Error while accepting socket connection: " << ec.message() << "\n";
|
||||||
|
};
|
||||||
|
|
||||||
if (acceptor_v4_)
|
if (acceptor_v4_)
|
||||||
{
|
{
|
||||||
socket_ptr socket_v4 = make_shared<tcp::socket>(*io_context_);
|
tcp::socket socket_v4(*io_context_);
|
||||||
acceptor_v4_->async_accept(*socket_v4, bind(&ControlServer::handleAccept, this, socket_v4));
|
acceptor_v4_->async_accept(accept_handler);
|
||||||
}
|
}
|
||||||
if (acceptor_v6_)
|
if (acceptor_v6_)
|
||||||
{
|
{
|
||||||
socket_ptr socket_v6 = make_shared<tcp::socket>(*io_context_);
|
tcp::socket socket_v6(*io_context_);
|
||||||
acceptor_v6_->async_accept(*socket_v6, bind(&ControlServer::handleAccept, this, socket_v6));
|
acceptor_v6_->async_accept(accept_handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ControlServer::handleAccept(socket_ptr socket)
|
void ControlServer::handleAccept(tcp::socket socket)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
tv.tv_sec = 5;
|
tv.tv_sec = 5;
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
setsockopt(socket->native_handle(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
setsockopt(socket.native_handle(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||||
setsockopt(socket->native_handle(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
setsockopt(socket.native_handle(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||||||
// socket->set_option(boost::asio::ip::tcp::no_delay(false));
|
// socket->set_option(boost::asio::ip::tcp::no_delay(false));
|
||||||
SLOG(NOTICE) << "ControlServer::NewConnection: " << socket->remote_endpoint().address().to_string() << endl;
|
SLOG(NOTICE) << "ControlServer::NewConnection: " << socket.remote_endpoint().address().to_string() << endl;
|
||||||
shared_ptr<ControlSession> session = make_shared<ControlSession>(this, socket);
|
shared_ptr<ControlSession> session = make_shared<ControlSession>(this, std::move(socket));
|
||||||
{
|
{
|
||||||
std::lock_guard<std::recursive_mutex> mlock(session_mutex_);
|
std::lock_guard<std::recursive_mutex> mlock(session_mutex_);
|
||||||
session->start();
|
session->start();
|
||||||
|
|
|
@ -34,10 +34,7 @@
|
||||||
#include "message/message.h"
|
#include "message/message.h"
|
||||||
#include "message/serverSettings.h"
|
#include "message/serverSettings.h"
|
||||||
|
|
||||||
|
|
||||||
using asio::ip::tcp;
|
using asio::ip::tcp;
|
||||||
typedef std::shared_ptr<tcp::socket> socket_ptr;
|
|
||||||
|
|
||||||
|
|
||||||
/// Telnet like remote control
|
/// Telnet like remote control
|
||||||
/**
|
/**
|
||||||
|
@ -60,7 +57,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void startAccept();
|
void startAccept();
|
||||||
void handleAccept(socket_ptr socket);
|
void handleAccept(tcp::socket socket);
|
||||||
void cleanup();
|
void cleanup();
|
||||||
|
|
||||||
mutable std::recursive_mutex session_mutex_;
|
mutable std::recursive_mutex session_mutex_;
|
||||||
|
|
|
@ -26,9 +26,8 @@ using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ControlSession::ControlSession(ControlMessageReceiver* receiver, std::shared_ptr<tcp::socket> socket) : messageReceiver_(receiver)
|
ControlSession::ControlSession(ControlMessageReceiver* receiver, tcp::socket&& socket) : messageReceiver_(receiver), socket_(std::move(socket))
|
||||||
{
|
{
|
||||||
socket_ = socket;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,7 +42,7 @@ void ControlSession::do_read()
|
||||||
{
|
{
|
||||||
const std::string delimiter = "\n";
|
const std::string delimiter = "\n";
|
||||||
auto self(shared_from_this());
|
auto self(shared_from_this());
|
||||||
asio::async_read_until(*socket_, streambuf_, delimiter, [this, self, delimiter](const std::error_code& ec, std::size_t bytes_transferred) {
|
asio::async_read_until(socket_, streambuf_, delimiter, [this, self, delimiter](const std::error_code& ec, std::size_t bytes_transferred) {
|
||||||
if (ec)
|
if (ec)
|
||||||
{
|
{
|
||||||
LOG(ERROR) << "Error while reading from control socket: " << ec.message() << "\n";
|
LOG(ERROR) << "Error while reading from control socket: " << ec.message() << "\n";
|
||||||
|
@ -75,25 +74,20 @@ void ControlSession::stop()
|
||||||
{
|
{
|
||||||
LOG(DEBUG) << "ControlSession::stop\n";
|
LOG(DEBUG) << "ControlSession::stop\n";
|
||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
if (socket_)
|
socket_.shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
||||||
{
|
|
||||||
socket_->shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
|
||||||
if (ec)
|
if (ec)
|
||||||
LOG(ERROR) << "Error in socket shutdown: " << ec.message() << "\n";
|
LOG(ERROR) << "Error in socket shutdown: " << ec.message() << "\n";
|
||||||
socket_->close(ec);
|
socket_.close(ec);
|
||||||
if (ec)
|
if (ec)
|
||||||
LOG(ERROR) << "Error in socket close: " << ec.message() << "\n";
|
LOG(ERROR) << "Error in socket close: " << ec.message() << "\n";
|
||||||
}
|
|
||||||
socket_ = nullptr;
|
|
||||||
LOG(DEBUG) << "ControlSession ControlSession stopped\n";
|
LOG(DEBUG) << "ControlSession ControlSession stopped\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void ControlSession::sendAsync(const std::string& message)
|
void ControlSession::sendAsync(const std::string& message)
|
||||||
{
|
{
|
||||||
auto self(shared_from_this());
|
auto self(shared_from_this());
|
||||||
asio::async_write(*socket_, asio::buffer(message + "\r\n"), [this, self](std::error_code ec, std::size_t length) {
|
asio::async_write(socket_, asio::buffer(message + "\r\n"), [this, self](std::error_code ec, std::size_t length) {
|
||||||
if (ec)
|
if (ec)
|
||||||
{
|
{
|
||||||
LOG(ERROR) << "Error while writing to control socket: " << ec.message() << "\n";
|
LOG(ERROR) << "Error while writing to control socket: " << ec.message() << "\n";
|
||||||
|
@ -106,9 +100,9 @@ void ControlSession::sendAsync(const std::string& message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool ControlSession::send(const std::string& message) const
|
bool ControlSession::send(const std::string& message)
|
||||||
{
|
{
|
||||||
error_code ec;
|
error_code ec;
|
||||||
asio::write(*socket_, asio::buffer(message + "\r\n"), ec);
|
asio::write(socket_, asio::buffer(message + "\r\n"), ec);
|
||||||
return !ec;
|
return !ec;
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,13 +55,13 @@ class ControlSession : public std::enable_shared_from_this<ControlSession>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// ctor. Received message from the client are passed to MessageReceiver
|
/// ctor. Received message from the client are passed to MessageReceiver
|
||||||
ControlSession(ControlMessageReceiver* receiver, std::shared_ptr<tcp::socket> socket);
|
ControlSession(ControlMessageReceiver* receiver, tcp::socket&& socket);
|
||||||
~ControlSession();
|
~ControlSession();
|
||||||
void start();
|
void start();
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
/// Sends a message to the client (synchronous)
|
/// Sends a message to the client (synchronous)
|
||||||
bool send(const std::string& message) const;
|
bool send(const std::string& message);
|
||||||
|
|
||||||
/// Sends a message to the client (asynchronous)
|
/// Sends a message to the client (asynchronous)
|
||||||
void sendAsync(const std::string& message);
|
void sendAsync(const std::string& message);
|
||||||
|
@ -69,7 +69,7 @@ public:
|
||||||
protected:
|
protected:
|
||||||
void do_read();
|
void do_read();
|
||||||
|
|
||||||
std::shared_ptr<tcp::socket> socket_;
|
tcp::socket socket_;
|
||||||
ControlMessageReceiver* messageReceiver_;
|
ControlMessageReceiver* messageReceiver_;
|
||||||
asio::streambuf streambuf_;
|
asio::streambuf streambuf_;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue