add dummy control session for websockets

This commit is contained in:
badaix 2019-09-28 16:57:58 +02:00
parent 45df7fdb42
commit 08d0ce58f2
8 changed files with 182 additions and 24 deletions

View file

@ -1,7 +1,8 @@
set(SERVER_SOURCES set(SERVER_SOURCES
config.cpp config.cpp
controlServer.cpp controlServer.cpp
controlSession.cpp control_session_tcp.cpp
control_session_ws.cpp
snapServer.cpp snapServer.cpp
streamServer.cpp streamServer.cpp
streamSession.cpp streamSession.cpp

View file

@ -114,7 +114,7 @@ void ControlServer::handleAccept(tcp::socket socket)
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, std::move(socket)); shared_ptr<ControlSessionTcp> session = make_shared<ControlSessionTcp>(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();

View file

@ -29,7 +29,7 @@
#include "common/queue.h" #include "common/queue.h"
#include "common/sampleFormat.h" #include "common/sampleFormat.h"
#include "controlSession.h" #include "control_session_tcp.hpp"
#include "message/codecHeader.h" #include "message/codecHeader.h"
#include "message/message.h" #include "message/message.h"
#include "message/serverSettings.h" #include "message/serverSettings.h"

View file

@ -55,23 +55,22 @@ 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, tcp::socket&& socket); ControlSession(ControlMessageReceiver* receiver, tcp::socket&& socket) : message_receiver_(receiver), socket_(std::move(socket))
~ControlSession(); {
void start(); }
void stop(); virtual ~ControlSession() = default;
virtual void start() = 0;
virtual void stop() = 0;
/// Sends a message to the client (synchronous) /// Sends a message to the client (synchronous)
bool send(const std::string& message); virtual bool send(const std::string& message) = 0;
/// Sends a message to the client (asynchronous) /// Sends a message to the client (asynchronous)
void sendAsync(const std::string& message); virtual void sendAsync(const std::string& message) = 0;
protected: protected:
void do_read();
tcp::socket socket_; tcp::socket socket_;
ControlMessageReceiver* messageReceiver_; ControlMessageReceiver* message_receiver_;
asio::streambuf streambuf_;
}; };

View file

@ -16,7 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
***/ ***/
#include "controlSession.h" #include "control_session_tcp.hpp"
#include "aixlog.hpp" #include "aixlog.hpp"
#include "message/pcmChunk.h" #include "message/pcmChunk.h"
#include <iostream> #include <iostream>
@ -26,19 +26,19 @@ using namespace std;
ControlSession::ControlSession(ControlMessageReceiver* receiver, tcp::socket&& socket) : messageReceiver_(receiver), socket_(std::move(socket)) ControlSessionTcp::ControlSessionTcp(ControlMessageReceiver* receiver, tcp::socket&& socket) : ControlSession(receiver, std::move(socket))
{ {
} }
ControlSession::~ControlSession() ControlSessionTcp::~ControlSessionTcp()
{ {
LOG(DEBUG) << "ControlSession::~ControlSession()\n"; LOG(DEBUG) << "ControlSessionTcp::~ControlSessionTcp()\n";
stop(); stop();
} }
void ControlSession::do_read() void ControlSessionTcp::do_read()
{ {
const std::string delimiter = "\n"; const std::string delimiter = "\n";
auto self(shared_from_this()); auto self(shared_from_this());
@ -56,21 +56,21 @@ void ControlSession::do_read()
if (line.back() == '\r') if (line.back() == '\r')
line.resize(line.size() - 1); line.resize(line.size() - 1);
LOG(INFO) << "received: " << line << "\n"; LOG(INFO) << "received: " << line << "\n";
if ((messageReceiver_ != nullptr) && !line.empty()) if ((message_receiver_ != nullptr) && !line.empty())
messageReceiver_->onMessageReceived(this, line); message_receiver_->onMessageReceived(this, line);
} }
streambuf_.consume(bytes_transferred); streambuf_.consume(bytes_transferred);
do_read(); do_read();
}); });
} }
void ControlSession::start() void ControlSessionTcp::start()
{ {
do_read(); do_read();
} }
void ControlSession::stop() void ControlSessionTcp::stop()
{ {
LOG(DEBUG) << "ControlSession::stop\n"; LOG(DEBUG) << "ControlSession::stop\n";
std::error_code ec; std::error_code ec;
@ -84,7 +84,7 @@ void ControlSession::stop()
} }
void ControlSession::sendAsync(const std::string& message) void ControlSessionTcp::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) {
@ -100,7 +100,7 @@ void ControlSession::sendAsync(const std::string& message)
} }
bool ControlSession::send(const std::string& message) bool ControlSessionTcp::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);

View file

@ -0,0 +1,52 @@
/***
This file is part of snapcast
Copyright (C) 2014-2019 Johannes Pohl
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef CONTROL_SESSION_TCP_HPP
#define CONTROL_SESSION_TCP_HPP
#include "control_session.hpp"
/// Endpoint for a connected control client.
/**
* Endpoint for a connected control client.
* Messages are sent to the client with the "send" method.
* Received messages from the client are passed to the ControlMessageReceiver callback
*/
class ControlSessionTcp : public ControlSession
{
public:
/// ctor. Received message from the client are passed to MessageReceiver
ControlSessionTcp(ControlMessageReceiver* receiver, tcp::socket&& socket);
~ControlSessionTcp() override;
void start() override;
void stop() override;
/// Sends a message to the client (synchronous)
bool send(const std::string& message) override;
/// Sends a message to the client (asynchronous)
void sendAsync(const std::string& message) override;
protected:
void do_read();
asio::streambuf streambuf_;
};
#endif

View file

@ -0,0 +1,58 @@
/***
This file is part of snapcast
Copyright (C) 2014-2019 Johannes Pohl
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "control_session_ws.hpp"
#include "aixlog.hpp"
#include "message/pcmChunk.h"
#include <iostream>
#include <mutex>
using namespace std;
ControlSessionWs::ControlSessionWs(ControlMessageReceiver* receiver, tcp::socket&& socket) : ControlSession(receiver, std::move(socket))
{
}
ControlSessionWs::~ControlSessionWs()
{
LOG(DEBUG) << "ControlSessionWs::~ControlSessionWs()\n";
stop();
}
void ControlSessionWs::start()
{
}
void ControlSessionWs::stop()
{
}
void ControlSessionWs::sendAsync(const std::string& message)
{
}
bool ControlSessionWs::send(const std::string& message)
{
return true;
}

View file

@ -0,0 +1,48 @@
/***
This file is part of snapcast
Copyright (C) 2014-2019 Johannes Pohl
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef CONTROL_SESSION_WS_HPP
#define CONTROL_SESSION_WS_HPP
#include "control_session.hpp"
/// Endpoint for a connected control client.
/**
* Endpoint for a connected control client.
* Messages are sent to the client with the "send" method.
* Received messages from the client are passed to the ControlMessageReceiver callback
*/
class ControlSessionWs : public ControlSession
{
public:
/// ctor. Received message from the client are passed to MessageReceiver
ControlSessionWs(ControlMessageReceiver* receiver, tcp::socket&& socket);
~ControlSessionWs() override;
void start() override;
void stop() override;
/// Sends a message to the client (synchronous)
bool send(const std::string& message) override;
/// Sends a message to the client (asynchronous)
void sendAsync(const std::string& message) override;
};
#endif