diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 48b15654..55715803 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -1,7 +1,8 @@ set(SERVER_SOURCES config.cpp controlServer.cpp - controlSession.cpp + control_session_tcp.cpp + control_session_ws.cpp snapServer.cpp streamServer.cpp streamSession.cpp diff --git a/server/controlServer.cpp b/server/controlServer.cpp index 2e3e33c7..e0a5e0ee 100644 --- a/server/controlServer.cpp +++ b/server/controlServer.cpp @@ -114,7 +114,7 @@ void ControlServer::handleAccept(tcp::socket socket) 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 session = make_shared(this, std::move(socket)); + shared_ptr session = make_shared(this, std::move(socket)); { std::lock_guard mlock(session_mutex_); session->start(); diff --git a/server/controlServer.h b/server/controlServer.h index cfd09beb..8cf267d0 100644 --- a/server/controlServer.h +++ b/server/controlServer.h @@ -29,7 +29,7 @@ #include "common/queue.h" #include "common/sampleFormat.h" -#include "controlSession.h" +#include "control_session_tcp.hpp" #include "message/codecHeader.h" #include "message/message.h" #include "message/serverSettings.h" diff --git a/server/controlSession.h b/server/control_session.hpp similarity index 84% rename from server/controlSession.h rename to server/control_session.hpp index 8606123c..24a16d7d 100644 --- a/server/controlSession.h +++ b/server/control_session.hpp @@ -55,23 +55,22 @@ class ControlSession : public std::enable_shared_from_this { public: /// ctor. Received message from the client are passed to MessageReceiver - ControlSession(ControlMessageReceiver* receiver, tcp::socket&& socket); - ~ControlSession(); - void start(); - void stop(); + ControlSession(ControlMessageReceiver* receiver, tcp::socket&& socket) : message_receiver_(receiver), socket_(std::move(socket)) + { + } + virtual ~ControlSession() = default; + virtual void start() = 0; + virtual void stop() = 0; /// 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) - void sendAsync(const std::string& message); + virtual void sendAsync(const std::string& message) = 0; protected: - void do_read(); - tcp::socket socket_; - ControlMessageReceiver* messageReceiver_; - asio::streambuf streambuf_; + ControlMessageReceiver* message_receiver_; }; diff --git a/server/controlSession.cpp b/server/control_session_tcp.cpp similarity index 80% rename from server/controlSession.cpp rename to server/control_session_tcp.cpp index 1f774cc6..8b7ed87e 100644 --- a/server/controlSession.cpp +++ b/server/control_session_tcp.cpp @@ -16,7 +16,7 @@ along with this program. If not, see . ***/ -#include "controlSession.h" +#include "control_session_tcp.hpp" #include "aixlog.hpp" #include "message/pcmChunk.h" #include @@ -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(); } -void ControlSession::do_read() +void ControlSessionTcp::do_read() { const std::string delimiter = "\n"; auto self(shared_from_this()); @@ -56,21 +56,21 @@ void ControlSession::do_read() if (line.back() == '\r') line.resize(line.size() - 1); LOG(INFO) << "received: " << line << "\n"; - if ((messageReceiver_ != nullptr) && !line.empty()) - messageReceiver_->onMessageReceived(this, line); + if ((message_receiver_ != nullptr) && !line.empty()) + message_receiver_->onMessageReceived(this, line); } streambuf_.consume(bytes_transferred); do_read(); }); } -void ControlSession::start() +void ControlSessionTcp::start() { do_read(); } -void ControlSession::stop() +void ControlSessionTcp::stop() { LOG(DEBUG) << "ControlSession::stop\n"; 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()); 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; asio::write(socket_, asio::buffer(message + "\r\n"), ec); diff --git a/server/control_session_tcp.hpp b/server/control_session_tcp.hpp new file mode 100644 index 00000000..cc783bc2 --- /dev/null +++ b/server/control_session_tcp.hpp @@ -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 . +***/ + +#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 diff --git a/server/control_session_ws.cpp b/server/control_session_ws.cpp new file mode 100644 index 00000000..05a83bf8 --- /dev/null +++ b/server/control_session_ws.cpp @@ -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 . +***/ + +#include "control_session_ws.hpp" +#include "aixlog.hpp" +#include "message/pcmChunk.h" +#include +#include + +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; +} diff --git a/server/control_session_ws.hpp b/server/control_session_ws.hpp new file mode 100644 index 00000000..84a7d1b8 --- /dev/null +++ b/server/control_session_ws.hpp @@ -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 . +***/ + +#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