mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-17 11:06:16 +02:00
servers
git-svn-id: svn://elaine/murooma/trunk@249 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
ae79214e2a
commit
7ab33c1bff
6 changed files with 362 additions and 169 deletions
85
server/controlServer.cpp
Normal file
85
server/controlServer.cpp
Normal file
|
@ -0,0 +1,85 @@
|
|||
#include "controlServer.h"
|
||||
|
||||
|
||||
ControlSession::ControlSession(socket_ptr sock) : active_(false), socket_(sock)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void ControlSession::sender()
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::asio::streambuf streambuf;
|
||||
std::ostream stream(&streambuf);
|
||||
for (;;)
|
||||
{
|
||||
shared_ptr<BaseMessage> message(messages.pop());
|
||||
message->serialize(stream);
|
||||
boost::asio::write(*socket_, streambuf);
|
||||
}
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception in thread: " << e.what() << "\n";
|
||||
active_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
void ControlSession::start()
|
||||
{
|
||||
active_ = true;
|
||||
senderThread = new thread(&ControlSession::sender, this);
|
||||
// readerThread.join();
|
||||
}
|
||||
|
||||
void ControlSession::send(BaseMessage* message)
|
||||
{
|
||||
boost::asio::streambuf streambuf;
|
||||
std::ostream stream(&streambuf);
|
||||
message->serialize(stream);
|
||||
boost::asio::write(*socket_, streambuf);
|
||||
}
|
||||
|
||||
|
||||
bool ControlSession::isActive() const
|
||||
{
|
||||
return active_;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ControlServer::ControlServer(unsigned short port) : port_(port), headerChunk(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void ControlServer::acceptor()
|
||||
{
|
||||
tcp::acceptor a(io_service_, tcp::endpoint(tcp::v4(), port_));
|
||||
for (;;)
|
||||
{
|
||||
socket_ptr sock(new tcp::socket(io_service_));
|
||||
a.accept(*sock);
|
||||
cout << "New connection: " << sock->remote_endpoint().address().to_string() << "\n";
|
||||
ControlSession* session = new ControlSession(sock);
|
||||
sessions.insert(shared_ptr<ControlSession>(session));
|
||||
session->start();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ControlServer::start()
|
||||
{
|
||||
acceptThread = new thread(&ControlServer::acceptor, this);
|
||||
}
|
||||
|
||||
|
||||
void ControlServer::stop()
|
||||
{
|
||||
// acceptThread->join();
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue