mirror of
https://github.com/badaix/snapcast.git
synced 2025-04-29 18:27:12 +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_);
|
||||
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)
|
||||
controlMessageReceiver_->onMessageReceived(connection, message);
|
||||
// }
|
||||
if (controlMessageReceiver_ != nullptr)
|
||||
controlMessageReceiver_->onMessageReceived(connection, message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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_)
|
||||
{
|
||||
socket_ptr socket_v4 = make_shared<tcp::socket>(*io_context_);
|
||||
acceptor_v4_->async_accept(*socket_v4, bind(&ControlServer::handleAccept, this, socket_v4));
|
||||
tcp::socket socket_v4(*io_context_);
|
||||
acceptor_v4_->async_accept(accept_handler);
|
||||
}
|
||||
if (acceptor_v6_)
|
||||
{
|
||||
socket_ptr socket_v6 = make_shared<tcp::socket>(*io_context_);
|
||||
acceptor_v6_->async_accept(*socket_v6, bind(&ControlServer::handleAccept, this, socket_v6));
|
||||
tcp::socket socket_v6(*io_context_);
|
||||
acceptor_v6_->async_accept(accept_handler);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ControlServer::handleAccept(socket_ptr socket)
|
||||
void ControlServer::handleAccept(tcp::socket socket)
|
||||
{
|
||||
try
|
||||
{
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 5;
|
||||
tv.tv_usec = 0;
|
||||
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_RCVTIMEO, &tv, sizeof(tv));
|
||||
setsockopt(socket.native_handle(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||||
// socket->set_option(boost::asio::ip::tcp::no_delay(false));
|
||||
SLOG(NOTICE) << "ControlServer::NewConnection: " << socket->remote_endpoint().address().to_string() << endl;
|
||||
shared_ptr<ControlSession> session = make_shared<ControlSession>(this, socket);
|
||||
SLOG(NOTICE) << "ControlServer::NewConnection: " << socket.remote_endpoint().address().to_string() << endl;
|
||||
shared_ptr<ControlSession> session = make_shared<ControlSession>(this, std::move(socket));
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> mlock(session_mutex_);
|
||||
session->start();
|
||||
|
|
|
@ -34,10 +34,7 @@
|
|||
#include "message/message.h"
|
||||
#include "message/serverSettings.h"
|
||||
|
||||
|
||||
using asio::ip::tcp;
|
||||
typedef std::shared_ptr<tcp::socket> socket_ptr;
|
||||
|
||||
|
||||
/// Telnet like remote control
|
||||
/**
|
||||
|
@ -60,7 +57,7 @@ public:
|
|||
|
||||
private:
|
||||
void startAccept();
|
||||
void handleAccept(socket_ptr socket);
|
||||
void handleAccept(tcp::socket socket);
|
||||
void cleanup();
|
||||
|
||||
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";
|
||||
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)
|
||||
{
|
||||
LOG(ERROR) << "Error while reading from control socket: " << ec.message() << "\n";
|
||||
|
@ -75,25 +74,20 @@ void ControlSession::stop()
|
|||
{
|
||||
LOG(DEBUG) << "ControlSession::stop\n";
|
||||
std::error_code ec;
|
||||
if (socket_)
|
||||
{
|
||||
socket_->shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
||||
if (ec)
|
||||
LOG(ERROR) << "Error in socket shutdown: " << ec.message() << "\n";
|
||||
socket_->close(ec);
|
||||
if (ec)
|
||||
LOG(ERROR) << "Error in socket close: " << ec.message() << "\n";
|
||||
}
|
||||
socket_ = nullptr;
|
||||
socket_.shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
||||
if (ec)
|
||||
LOG(ERROR) << "Error in socket shutdown: " << ec.message() << "\n";
|
||||
socket_.close(ec);
|
||||
if (ec)
|
||||
LOG(ERROR) << "Error in socket close: " << ec.message() << "\n";
|
||||
LOG(DEBUG) << "ControlSession ControlSession stopped\n";
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ControlSession::sendAsync(const std::string& message)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
asio::write(*socket_, asio::buffer(message + "\r\n"), ec);
|
||||
asio::write(socket_, asio::buffer(message + "\r\n"), ec);
|
||||
return !ec;
|
||||
}
|
||||
|
|
|
@ -55,13 +55,13 @@ class ControlSession : public std::enable_shared_from_this<ControlSession>
|
|||
{
|
||||
public:
|
||||
/// 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();
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
/// 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)
|
||||
void sendAsync(const std::string& message);
|
||||
|
@ -69,7 +69,7 @@ public:
|
|||
protected:
|
||||
void do_read();
|
||||
|
||||
std::shared_ptr<tcp::socket> socket_;
|
||||
tcp::socket socket_;
|
||||
ControlMessageReceiver* messageReceiver_;
|
||||
asio::streambuf streambuf_;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue