Renamed ServerSession => ClientSession

This commit is contained in:
badaix 2015-09-05 11:16:24 +02:00
parent e279b12f31
commit 8991347947
6 changed files with 38 additions and 38 deletions

View file

@ -6,7 +6,7 @@ CC = /usr/bin/g++
CFLAGS = -std=c++0x -Wall -Wno-unused-function -O3 -pthread -DVERSION=\"$(VERSION)\" -I..
LDFLAGS = -lrt -lboost_system -lboost_program_options -lvorbis -lvorbisenc -logg -lFLAC -lavahi-client -lavahi-common
OBJ = snapServer.o config.o controlServer.o controlSession.o streamServer.o jsonrpc.o encoder/encoderFactory.o encoder/flacEncoder.o encoder/pcmEncoder.o encoder/oggEncoder.o serverSession.o publishAvahi.o pipeReader.o ../common/log.o ../message/pcmChunk.o ../message/sampleFormat.o
OBJ = snapServer.o config.o controlServer.o controlSession.o streamServer.o jsonrpc.o encoder/encoderFactory.o encoder/flacEncoder.o encoder/pcmEncoder.o encoder/oggEncoder.o clientSession.o publishAvahi.o pipeReader.o ../common/log.o ../message/pcmChunk.o ../message/sampleFormat.o
BIN = snapserver
all: $(TARGET)

View file

@ -19,7 +19,7 @@
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <mutex>
#include "serverSession.h"
#include "clientSession.h"
#include "common/log.h"
#include "message/pcmChunk.h"
@ -27,28 +27,28 @@ using namespace std;
ServerSession::ServerSession(MessageReceiver* receiver, std::shared_ptr<tcp::socket> socket) : messageReceiver_(receiver)
ClientSession::ClientSession(MessageReceiver* receiver, std::shared_ptr<tcp::socket> socket) : messageReceiver_(receiver)
{
socket_ = socket;
}
ServerSession::~ServerSession()
ClientSession::~ClientSession()
{
stop();
}
void ServerSession::start()
void ClientSession::start()
{
active_ = true;
streamActive_ = false;
readerThread_ = new thread(&ServerSession::reader, this);
writerThread_ = new thread(&ServerSession::writer, this);
readerThread_ = new thread(&ClientSession::reader, this);
writerThread_ = new thread(&ClientSession::writer, this);
}
void ServerSession::stop()
void ClientSession::stop()
{
std::unique_lock<std::mutex> mlock(mutex_);
setActive(false);
@ -81,11 +81,11 @@ void ServerSession::stop()
readerThread_ = NULL;
writerThread_ = NULL;
socket_ = NULL;
logD << "ServerSession stopped\n";
logD << "ClientSession stopped\n";
}
void ServerSession::socketRead(void* _to, size_t _bytes)
void ClientSession::socketRead(void* _to, size_t _bytes)
{
size_t read = 0;
do
@ -97,7 +97,7 @@ void ServerSession::socketRead(void* _to, size_t _bytes)
}
void ServerSession::add(const shared_ptr<const msg::BaseMessage>& message)
void ClientSession::add(const shared_ptr<const msg::BaseMessage>& message)
{
if (!message || !streamActive_)
return;
@ -108,7 +108,7 @@ void ServerSession::add(const shared_ptr<const msg::BaseMessage>& message)
}
bool ServerSession::send(const msg::BaseMessage* message) const
bool ClientSession::send(const msg::BaseMessage* message) const
{
// logO << "send: " << message->type << ", size: " << message->size << ", id: " << message->id << ", refers: " << message->refersTo << "\n";
std::unique_lock<std::mutex> mlock(mutex_);
@ -125,7 +125,7 @@ bool ServerSession::send(const msg::BaseMessage* message) const
}
void ServerSession::getNextMessage()
void ClientSession::getNextMessage()
{
msg::BaseMessage baseMessage;
size_t baseMsgSize = baseMessage.getSize();
@ -144,7 +144,7 @@ void ServerSession::getNextMessage()
}
void ServerSession::reader()
void ClientSession::reader()
{
try
{
@ -155,13 +155,13 @@ void ServerSession::reader()
}
catch (const std::exception& e)
{
logS(kLogErr) << "Exception in ServerSession::reader(): " << e.what() << endl;
logS(kLogErr) << "Exception in ClientSession::reader(): " << e.what() << endl;
}
setActive(false);
}
void ServerSession::writer()
void ClientSession::writer()
{
try
{
@ -192,13 +192,13 @@ void ServerSession::writer()
}
catch (const std::exception& e)
{
logS(kLogErr) << "Exception in ServerSession::writer(): " << e.what() << endl;
logS(kLogErr) << "Exception in ClientSession::writer(): " << e.what() << endl;
}
setActive(false);
}
void ServerSession::setActive(bool active)
void ClientSession::setActive(bool active)
{
if (active_ && !active && (messageReceiver_ != NULL))
messageReceiver_->onDisconnect(this);

View file

@ -34,15 +34,15 @@
using boost::asio::ip::tcp;
class ServerSession;
class ClientSession;
/// Interface: callback for a received message.
class MessageReceiver
{
public:
virtual void onMessageReceived(ServerSession* connection, const msg::BaseMessage& baseMessage, char* buffer) = 0;
virtual void onDisconnect(ServerSession* connection) = 0;
virtual void onMessageReceived(ClientSession* connection, const msg::BaseMessage& baseMessage, char* buffer) = 0;
virtual void onDisconnect(ClientSession* connection) = 0;
};
@ -52,12 +52,12 @@ public:
* Messages are sent to the client with the "send" method.
* Received messages from the client are passed to the MessageReceiver callback
*/
class ServerSession
class ClientSession
{
public:
/// ctor. Received message from the client are passed to MessageReceiver
ServerSession(MessageReceiver* receiver, std::shared_ptr<tcp::socket> socket);
~ServerSession();
ClientSession(MessageReceiver* receiver, std::shared_ptr<tcp::socket> socket);
~ClientSession();
void start();
void stop();

View file

@ -53,7 +53,7 @@ void ControlServer::send(const std::string& message)
if (!(*it)->active())
{
logS(kLogErr) << "Session inactive. Removing\n";
// don't block: remove ServerSession in a thread
// don't block: remove ClientSession in a thread
auto func = [](shared_ptr<ControlSession> s)->void{s->stop();};
std::thread t(func, *it);
t.detach();

View file

@ -51,8 +51,8 @@ void StreamServer::send(const msg::BaseMessage* message)
if (!(*it)->active())
{
logS(kLogErr) << "Session inactive. Removing\n";
// don't block: remove ServerSession in a thread
auto func = [](shared_ptr<ServerSession> s)->void{s->stop();};
// don't block: remove ClientSession in a thread
auto func = [](shared_ptr<ClientSession> s)->void{s->stop();};
std::thread t(func, *it);
t.detach();
controlServer->send("Client gone: " + (*it)->macAddress);
@ -81,7 +81,7 @@ void StreamServer::onResync(const PipeReader* pipeReader, double ms)
}
void StreamServer::onDisconnect(ServerSession* connection)
void StreamServer::onDisconnect(ClientSession* connection)
{
ClientInfoPtr client = Config::instance().getClientInfo(connection->macAddress);
client->connected = false;
@ -141,7 +141,7 @@ void StreamServer::onMessageReceived(ControlSession* connection, const std::stri
{
int volume = request.getParam("volume").get<int>();
logO << "client: " << request.getParam("client").get<string>() << ", volume: " << volume << "\n";
ServerSession* session = getClientSession(request.getParam("client").get<string>());
ClientSession* session = getClientSession(request.getParam("client").get<string>());
// if (session != NULL)
response = volume;
@ -176,7 +176,7 @@ void StreamServer::onMessageReceived(ControlSession* connection, const std::stri
}
void StreamServer::onMessageReceived(ServerSession* connection, const msg::BaseMessage& baseMessage, char* buffer)
void StreamServer::onMessageReceived(ClientSession* connection, const msg::BaseMessage& baseMessage, char* buffer)
{
// logO << "getNextMessage: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " << baseMessage.id << ", refers: " << baseMessage.refersTo << ", sent: " << baseMessage.sent.sec << "," << baseMessage.sent.usec << ", recv: " << baseMessage.received.sec << "," << baseMessage.received.usec << "\n";
if (baseMessage.type == message_type::kRequest)
@ -245,7 +245,7 @@ void StreamServer::onMessageReceived(ServerSession* connection, const msg::BaseM
}
ServerSession* StreamServer::getClientSession(const std::string& mac)
ClientSession* StreamServer::getClientSession(const std::string& mac)
{
for (auto session: sessions_)
{
@ -271,7 +271,7 @@ void StreamServer::handleAccept(socket_ptr socket)
setsockopt(socket->native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(socket->native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
logS(kLogNotice) << "StreamServer::NewConnection: " << socket->remote_endpoint().address().to_string() << endl;
shared_ptr<ServerSession> session = make_shared<ServerSession>(this, socket);
shared_ptr<ClientSession> session = make_shared<ClientSession>(this, socket);
{
std::unique_lock<std::mutex> mlock(mutex_);
session->setBufferMs(settings_.bufferMs);

View file

@ -27,7 +27,7 @@
#include <sstream>
#include <mutex>
#include "serverSession.h"
#include "clientSession.h"
#include "pipeReader.h"
#include "common/queue.h"
#include "message/message.h"
@ -64,7 +64,7 @@ struct StreamServerSettings
/// Forwars PCM data to the connected clients
/**
* Reads PCM data using PipeReader, implements PipeListener to get the (encoded) PCM stream.
* Accepts and holds client connections (ServerSession)
* Accepts and holds client connections (ClientSession)
* Receives (via the MessageReceiver interface) and answers messages from the clients
* Forwards PCM data to the clients
*/
@ -81,8 +81,8 @@ public:
void send(const msg::BaseMessage* message);
/// Clients call this when they receive a message. Implementation of MessageReceiver::onMessageReceived
virtual void onMessageReceived(ServerSession* connection, const msg::BaseMessage& baseMessage, char* buffer);
virtual void onDisconnect(ServerSession* connection);
virtual void onMessageReceived(ClientSession* connection, const msg::BaseMessage& baseMessage, char* buffer);
virtual void onDisconnect(ClientSession* connection);
virtual void onMessageReceived(ControlSession* connection, const std::string& message);
@ -94,10 +94,10 @@ private:
void startAccept();
void handleAccept(socket_ptr socket);
void acceptor();
ServerSession* getClientSession(const std::string& mac);
ClientSession* getClientSession(const std::string& mac);
mutable std::mutex mutex_;
PipeReader* pipeReader_;
std::set<std::shared_ptr<ServerSession>> sessions_;
std::set<std::shared_ptr<ClientSession>> sessions_;
boost::asio::io_service io_service_;
std::shared_ptr<tcp::acceptor> acceptor_;