mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-25 15:06:21 +02:00
server & client connection
git-svn-id: svn://elaine/murooma/trunk@270 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
d85858ac62
commit
2cf7855261
15 changed files with 184 additions and 123 deletions
|
@ -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
|
||||||
|
|
61
client/clientConnection.cpp
Normal file
61
client/clientConnection.cpp
Normal 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
32
client/clientConnection.h
Normal 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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
43
server/serverConnection.cpp
Normal file
43
server/serverConnection.cpp
Normal 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
28
server/serverConnection.h
Normal 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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue