streamClient

git-svn-id: svn://elaine/murooma/trunk@254 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
(no author) 2014-09-12 13:43:28 +00:00
parent 25f7f3060d
commit f7cf7a2537
5 changed files with 66 additions and 11 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..
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
all: client

16
client/streamClient.cpp Normal file
View 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
View 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

View file

@ -10,7 +10,7 @@
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)
{
std::unique_lock<std::mutex> mlock(mutex_);
if (!connected())
return;
boost::asio::streambuf streambuf;
std::ostream stream(&streambuf);
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)
{
// 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;
while (active_)
{
connected_ = false;
try
{
{
// std::unique_lock<std::mutex> mlock(mutex_);
tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), ip, boost::lexical_cast<string>(port));
iterator = resolver.resolve(query);
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_)
@ -113,6 +120,7 @@ void ClientConnection::worker()
}
catch (const std::exception& e)
{
connected_ = false;
cout << kLogNotice << "Exception: " << e.what() << ", trying to reconnect" << std::endl;
usleep(500*1000);
}

View file

@ -30,11 +30,17 @@ public:
virtual void start();
virtual void stop();
virtual void send(BaseMessage* _message);
virtual bool isActive()
virtual bool active()
{
return active_;
}
virtual bool connected()
{
return (connected_ && socket);
}
protected:
virtual void worker() = 0;
@ -43,6 +49,7 @@ protected:
// boost::asio::ip::tcp::endpoint endpt;
std::atomic<bool> active_;
std::atomic<bool> connected_;
MessageReceiver* messageReceiver;
void getNextMessage();
boost::asio::io_service io_service;
@ -56,6 +63,7 @@ class ClientConnection : public SocketConnection
{
public:
ClientConnection(MessageReceiver* _receiver, const std::string& _ip, size_t _port);
virtual void start();
protected:
virtual void worker();