mirror of
https://github.com/badaix/snapcast.git
synced 2025-07-15 23:55:39 +02:00
streamClient
git-svn-id: svn://elaine/murooma/trunk@254 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
25f7f3060d
commit
f7cf7a2537
5 changed files with 66 additions and 11 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 oggDecoder.o pcmDecoder.o controller.o ../common/pcmChunk.o ../common/log.o ../common/sampleFormat.o
|
OBJ = snapClient.o stream.o player.o ../common/socketConnection.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
|
||||||
|
|
16
client/streamClient.cpp
Normal file
16
client/streamClient.cpp
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#include "streamClient.h"
|
||||||
|
|
||||||
|
|
||||||
|
StreamClient::StreamClient(MessageReceiver* _receiver, const std::string& _ip, size_t _port) : ClientConnection(_receiver, _ip, _port)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
StreamClient::~StreamClient()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
23
client/streamClient.h
Normal file
23
client/streamClient.h
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#ifndef STREAM_CLIENT_H
|
||||||
|
#define STREAM_CLIENT_H
|
||||||
|
|
||||||
|
#include "common/socketConnection.h"
|
||||||
|
|
||||||
|
|
||||||
|
using boost::asio::ip::tcp;
|
||||||
|
|
||||||
|
|
||||||
|
class StreamClient : public ClientConnection
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
StreamClient(MessageReceiver* _receiver, const std::string& _ip, size_t _port);
|
||||||
|
virtual ~StreamClient();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
SocketConnection::SocketConnection(MessageReceiver* _receiver) : active_(false), messageReceiver(_receiver)
|
SocketConnection::SocketConnection(MessageReceiver* _receiver) : active_(false), connected_(false), messageReceiver(_receiver)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,6 +50,8 @@ void SocketConnection::stop()
|
||||||
void SocketConnection::send(BaseMessage* message)
|
void SocketConnection::send(BaseMessage* message)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> mlock(mutex_);
|
std::unique_lock<std::mutex> mlock(mutex_);
|
||||||
|
if (!connected())
|
||||||
|
return;
|
||||||
boost::asio::streambuf streambuf;
|
boost::asio::streambuf streambuf;
|
||||||
std::ostream stream(&streambuf);
|
std::ostream stream(&streambuf);
|
||||||
message->serialize(stream);
|
message->serialize(stream);
|
||||||
|
@ -78,10 +80,15 @@ void SocketConnection::getNextMessage()
|
||||||
|
|
||||||
ClientConnection::ClientConnection(MessageReceiver* _receiver, const std::string& _ip, size_t _port) : SocketConnection(_receiver), ip(_ip), port(_port)
|
ClientConnection::ClientConnection(MessageReceiver* _receiver, const std::string& _ip, size_t _port) : SocketConnection(_receiver), ip(_ip), port(_port)
|
||||||
{
|
{
|
||||||
// endpt.address(boost::asio::ip::address::from_string(ip));
|
}
|
||||||
// endpt.port((port));
|
|
||||||
// std::cout << "Endpoint IP: " << endpt.address().to_string() << std::endl;
|
|
||||||
// std::cout << "Endpoint Port: " << endpt.port() << std::endl;
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -90,20 +97,20 @@ void ClientConnection::worker()
|
||||||
active_ = true;
|
active_ = true;
|
||||||
while (active_)
|
while (active_)
|
||||||
{
|
{
|
||||||
|
connected_ = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
// std::unique_lock<std::mutex> mlock(mutex_);
|
// std::unique_lock<std::mutex> mlock(mutex_);
|
||||||
tcp::resolver resolver(io_service);
|
cout << "connecting\n";
|
||||||
tcp::resolver::query query(tcp::v4(), ip, boost::lexical_cast<string>(port));
|
|
||||||
iterator = resolver.resolve(query);
|
|
||||||
|
|
||||||
socket.reset(new tcp::socket(io_service));
|
socket.reset(new tcp::socket(io_service));
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
tv.tv_sec = 5;
|
tv.tv_sec = 5;
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
setsockopt(socket->native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
setsockopt(socket->native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||||
socket->connect(*iterator);
|
socket->connect(*iterator);
|
||||||
|
connected_ = true;
|
||||||
|
cout << "connected\n";
|
||||||
std::clog << kLogNotice << "connected\n";// to " << ip << ":" << port << std::endl;
|
std::clog << kLogNotice << "connected\n";// to " << ip << ":" << port << std::endl;
|
||||||
}
|
}
|
||||||
while(active_)
|
while(active_)
|
||||||
|
@ -113,6 +120,7 @@ void ClientConnection::worker()
|
||||||
}
|
}
|
||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
|
connected_ = false;
|
||||||
cout << kLogNotice << "Exception: " << e.what() << ", trying to reconnect" << std::endl;
|
cout << kLogNotice << "Exception: " << e.what() << ", trying to reconnect" << std::endl;
|
||||||
usleep(500*1000);
|
usleep(500*1000);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,11 +30,17 @@ public:
|
||||||
virtual void start();
|
virtual void start();
|
||||||
virtual void stop();
|
virtual void stop();
|
||||||
virtual void send(BaseMessage* _message);
|
virtual void send(BaseMessage* _message);
|
||||||
virtual bool isActive()
|
|
||||||
|
virtual bool active()
|
||||||
{
|
{
|
||||||
return active_;
|
return active_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual bool connected()
|
||||||
|
{
|
||||||
|
return (connected_ && socket);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void worker() = 0;
|
virtual void worker() = 0;
|
||||||
|
|
||||||
|
@ -43,6 +49,7 @@ protected:
|
||||||
|
|
||||||
// boost::asio::ip::tcp::endpoint endpt;
|
// boost::asio::ip::tcp::endpoint endpt;
|
||||||
std::atomic<bool> active_;
|
std::atomic<bool> active_;
|
||||||
|
std::atomic<bool> connected_;
|
||||||
MessageReceiver* messageReceiver;
|
MessageReceiver* messageReceiver;
|
||||||
void getNextMessage();
|
void getNextMessage();
|
||||||
boost::asio::io_service io_service;
|
boost::asio::io_service io_service;
|
||||||
|
@ -56,6 +63,7 @@ class ClientConnection : public SocketConnection
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ClientConnection(MessageReceiver* _receiver, const std::string& _ip, size_t _port);
|
ClientConnection(MessageReceiver* _receiver, const std::string& _ip, size_t _port);
|
||||||
|
virtual void start();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void worker();
|
virtual void worker();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue