server & client connection

git-svn-id: svn://elaine/murooma/trunk@270 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
(no author) 2014-09-17 04:57:59 +00:00
parent d85858ac62
commit 2cf7855261
15 changed files with 184 additions and 123 deletions

View file

@ -3,7 +3,7 @@ CC = /usr/bin/g++
CFLAGS = -std=gnu++0x -Wall -Wno-unused-function -g -D_REENTRANT -DVERSION=\"$(VERSION)\" -I.. CFLAGS = -std=gnu++0x -Wall -Wno-unused-function -g -D_REENTRANT -DVERSION=\"$(VERSION)\" -I..
LDFLAGS = -lrt -lpthread -lboost_system -lboost_program_options -lasound -logg -lvorbis -lvorbisenc LDFLAGS = -lrt -lpthread -lboost_system -lboost_program_options -lasound -logg -lvorbis -lvorbisenc
OBJ = snapClient.o stream.o player.o ../common/socketConnection.o timeProvider.o streamClient.o oggDecoder.o pcmDecoder.o controller.o ../common/pcmChunk.o ../common/log.o ../common/sampleFormat.o OBJ = snapClient.o stream.o player.o clientConnection.o ../common/socketConnection.o timeProvider.o streamClient.o oggDecoder.o pcmDecoder.o controller.o ../common/pcmChunk.o ../common/log.o ../common/sampleFormat.o
BIN = snapclient BIN = snapclient
all: client all: client

View file

@ -0,0 +1,61 @@
#include "clientConnection.h"
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <mutex>
#include "common/log.h"
using namespace std;
ClientConnection::ClientConnection(MessageReceiver* _receiver, const std::string& _ip, size_t _port) : SocketConnection(_receiver), ip(_ip), port(_port)
{
}
void ClientConnection::start()
{
tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), ip, boost::lexical_cast<string>(port));
iterator = resolver.resolve(query);
SocketConnection::start();
}
void ClientConnection::worker()
{
active_ = true;
while (active_)
{
connected_ = false;
try
{
{
// std::unique_lock<std::mutex> mlock(mutex_);
cout << "connecting\n";
socket.reset(new tcp::socket(io_service));
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(socket->native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
socket->connect(*iterator);
connected_ = true;
cout << "connected\n";
std::clog << kLogNotice << "connected\n";// to " << ip << ":" << port << std::endl;
}
while(active_)
{
getNextMessage();
}
}
catch (const std::exception& e)
{
connected_ = false;
cout << kLogNotice << "Exception: " << e.what() << ", trying to reconnect" << std::endl;
usleep(1000*1000);
}
}
}

32
client/clientConnection.h Normal file
View file

@ -0,0 +1,32 @@
#ifndef CLIENT_CONNECTION_H
#define CLIENT_CONNECTION_H
#include "common/socketConnection.h"
using boost::asio::ip::tcp;
class ClientConnection : public SocketConnection
{
public:
ClientConnection(MessageReceiver* _receiver, const std::string& _ip, size_t _port);
virtual void start();
protected:
virtual void worker();
private:
std::string ip;
size_t port;
};
#endif

View file

@ -27,11 +27,11 @@ void Controller::onMessageReceived(SocketConnection* connection, const BaseMessa
{ {
PcmChunk* pcmChunk = new PcmChunk(*sampleFormat, 0); PcmChunk* pcmChunk = new PcmChunk(*sampleFormat, 0);
pcmChunk->deserialize(baseMessage, buffer); pcmChunk->deserialize(baseMessage, buffer);
cout << "chunk: " << pcmChunk->payloadSize; //cout << "chunk: " << pcmChunk->payloadSize;
if (decoder->decode(pcmChunk)) if (decoder->decode(pcmChunk))
{ {
stream->addChunk(pcmChunk); stream->addChunk(pcmChunk);
cout << ", decoded: " << pcmChunk->payloadSize << ", Duration: " << pcmChunk->getDuration() << ", sec: " << pcmChunk->timestamp.sec << ", usec: " << pcmChunk->timestamp.usec/1000 << ", type: " << pcmChunk->type << "\n"; //cout << ", decoded: " << pcmChunk->payloadSize << ", Duration: " << pcmChunk->getDuration() << ", sec: " << pcmChunk->timestamp.sec << ", usec: " << pcmChunk->timestamp.usec/1000 << ", type: " << pcmChunk->type << "\n";
} }
else else
delete pcmChunk; delete pcmChunk;

View file

@ -24,11 +24,11 @@ private:
std::thread* controllerThread; std::thread* controllerThread;
StreamClient* streamClient; StreamClient* streamClient;
ClientConnection* controlConnection; ClientConnection* controlConnection;
Decoder* decoder;
Stream* stream; Stream* stream;
int bufferMs; int bufferMs;
std::string ip; std::string ip;
std::shared_ptr<SampleFormat> sampleFormat; std::shared_ptr<SampleFormat> sampleFormat;
Decoder* decoder;
}; };

View file

@ -1,7 +1,7 @@
#ifndef STREAM_CLIENT_H #ifndef STREAM_CLIENT_H
#define STREAM_CLIENT_H #define STREAM_CLIENT_H
#include "common/socketConnection.h" #include "clientConnection.h"
using boost::asio::ip::tcp; using boost::asio::ip::tcp;

View file

@ -5,7 +5,6 @@
#include "log.h" #include "log.h"
#define PCM_DEVICE "default"
using namespace std; using namespace std;
@ -138,88 +137,4 @@ void SocketConnection::getNextMessage()
ClientConnection::ClientConnection(MessageReceiver* _receiver, const std::string& _ip, size_t _port) : SocketConnection(_receiver), ip(_ip), port(_port)
{
}
void ClientConnection::start()
{
tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), ip, boost::lexical_cast<string>(port));
iterator = resolver.resolve(query);
SocketConnection::start();
}
void ClientConnection::worker()
{
active_ = true;
while (active_)
{
connected_ = false;
try
{
{
// std::unique_lock<std::mutex> mlock(mutex_);
cout << "connecting\n";
socket.reset(new tcp::socket(io_service));
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(socket->native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
socket->connect(*iterator);
connected_ = true;
cout << "connected\n";
std::clog << kLogNotice << "connected\n";// to " << ip << ":" << port << std::endl;
}
while(active_)
{
getNextMessage();
}
}
catch (const std::exception& e)
{
connected_ = false;
cout << kLogNotice << "Exception: " << e.what() << ", trying to reconnect" << std::endl;
usleep(1000*1000);
}
}
}
ServerConnection::ServerConnection(MessageReceiver* _receiver, std::shared_ptr<tcp::socket> _socket) : SocketConnection(_receiver)
{
socket = _socket;
}
void ServerConnection::worker()
{
active_ = true;
try
{
while (active_)
{
getNextMessage();
}
}
catch (const std::exception& e)
{
cout << kLogNotice << "Exception: " << e.what() << ", trying to reconnect" << std::endl;
}
active_ = false;
}

View file

@ -88,33 +88,6 @@ protected:
}; };
class ClientConnection : public SocketConnection
{
public:
ClientConnection(MessageReceiver* _receiver, const std::string& _ip, size_t _port);
virtual void start();
protected:
virtual void worker();
private:
std::string ip;
size_t port;
};
class ServerConnection : public SocketConnection
{
public:
ServerConnection(MessageReceiver* _receiver, std::shared_ptr<tcp::socket> _socket);
protected:
virtual void worker();
};
#endif #endif

View file

@ -3,7 +3,7 @@ CC = /usr/bin/g++
CFLAGS = -std=gnu++0x -Wall -Wno-unused-function -D_REENTRANT -DVERSION=\"$(VERSION)\" -I.. CFLAGS = -std=gnu++0x -Wall -Wno-unused-function -D_REENTRANT -DVERSION=\"$(VERSION)\" -I..
LDFLAGS = -lrt -lpthread -lboost_system -lboost_program_options -lvorbis -lvorbisenc -logg LDFLAGS = -lrt -lpthread -lboost_system -lboost_program_options -lvorbis -lvorbisenc -logg
OBJ = snapServer.o streamServer.o controlServer.o pcmEncoder.o oggEncoder.o ../common/log.o ../common/socketConnection.o ../common/pcmChunk.o ../common/sampleFormat.o OBJ = snapServer.o streamServer.o controlServer.o pcmEncoder.o oggEncoder.o serverConnection.o ../common/log.o ../common/socketConnection.o ../common/pcmChunk.o ../common/sampleFormat.o
BIN = snapserver BIN = snapserver
all: server all: server

View file

@ -52,14 +52,16 @@ void ControlServer::acceptor()
for (;;) for (;;)
{ {
socket_ptr sock(new tcp::socket(io_service_)); socket_ptr sock(new tcp::socket(io_service_));
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(sock->native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(sock->native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
a.accept(*sock); a.accept(*sock);
cout << "ControlServer::NewConnection: " << sock->remote_endpoint().address().to_string() << "\n"; cout << "ControlServer::NewConnection: " << sock->remote_endpoint().address().to_string() << "\n";
ServerConnection* session = new ServerConnection(this, sock); ServerConnection* session = new ServerConnection(this, sock);
sessions.insert(shared_ptr<ServerConnection>(session)); sessions.insert(shared_ptr<ServerConnection>(session));
session->start(); session->start();
// session->send(serverSettings);
// session->send(sampleFormat);
// session->send(headerChunk);
} }
} }

View file

@ -7,13 +7,14 @@
#include <memory> #include <memory>
#include <set> #include <set>
#include <sstream> #include <sstream>
#include "serverConnection.h"
#include "common/timeUtils.h" #include "common/timeUtils.h"
#include "common/queue.h" #include "common/queue.h"
#include "common/message.h" #include "common/message.h"
#include "common/headerMessage.h" #include "common/headerMessage.h"
#include "common/sampleFormat.h" #include "common/sampleFormat.h"
#include "common/serverSettings.h" #include "common/serverSettings.h"
#include "common/socketConnection.h"
using boost::asio::ip::tcp; using boost::asio::ip::tcp;

View file

@ -0,0 +1,43 @@
#include "serverConnection.h"
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <mutex>
#include "common/log.h"
using namespace std;
ServerConnection::ServerConnection(MessageReceiver* _receiver, std::shared_ptr<tcp::socket> _socket) : SocketConnection(_receiver)
{
socket = _socket;
}
void ServerConnection::worker()
{
active_ = true;
try
{
while (active_)
{
getNextMessage();
}
}
catch (const std::exception& e)
{
cout << kLogNotice << "Exception: " << e.what() << ", trying to reconnect" << std::endl;
}
active_ = false;
}

28
server/serverConnection.h Normal file
View file

@ -0,0 +1,28 @@
#ifndef SERVER_CONNECTION_H
#define SERVER_CONNECTION_H
#include "common/socketConnection.h"
using boost::asio::ip::tcp;
class ServerConnection : public SocketConnection
{
public:
ServerConnection(MessageReceiver* _receiver, std::shared_ptr<tcp::socket> _socket);
protected:
virtual void worker();
};
#endif

View file

@ -54,6 +54,11 @@ void StreamServer::acceptor()
for (;;) for (;;)
{ {
socket_ptr sock(new tcp::socket(io_service_)); socket_ptr sock(new tcp::socket(io_service_));
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(sock->native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(sock->native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
a.accept(*sock); a.accept(*sock);
cout << "StreamServer::New connection: " << sock->remote_endpoint().address().to_string() << "\n"; cout << "StreamServer::New connection: " << sock->remote_endpoint().address().to_string() << "\n";
StreamSession* session = new StreamSession(sock); StreamSession* session = new StreamSession(sock);

View file

@ -7,12 +7,13 @@
#include <memory> #include <memory>
#include <set> #include <set>
#include <sstream> #include <sstream>
#include "serverConnection.h"
#include "common/timeUtils.h" #include "common/timeUtils.h"
#include "common/queue.h" #include "common/queue.h"
#include "common/message.h" #include "common/message.h"
#include "common/headerMessage.h" #include "common/headerMessage.h"
#include "common/sampleFormat.h" #include "common/sampleFormat.h"
#include "common/socketConnection.h"
using boost::asio::ip::tcp; using boost::asio::ip::tcp;