diff --git a/client/Makefile b/client/Makefile index a28b2ddb..f0deb6f6 100644 --- a/client/Makefile +++ b/client/Makefile @@ -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 diff --git a/client/streamClient.cpp b/client/streamClient.cpp new file mode 100644 index 00000000..f7598bb3 --- /dev/null +++ b/client/streamClient.cpp @@ -0,0 +1,16 @@ +#include "streamClient.h" + + +StreamClient::StreamClient(MessageReceiver* _receiver, const std::string& _ip, size_t _port) : ClientConnection(_receiver, _ip, _port) +{ +} + + +StreamClient::~StreamClient() +{ +} + + + + + diff --git a/client/streamClient.h b/client/streamClient.h new file mode 100644 index 00000000..e0c0af7f --- /dev/null +++ b/client/streamClient.h @@ -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 + + + + diff --git a/common/socketConnection.cpp b/common/socketConnection.cpp index eab18706..fed3c0cb 100644 --- a/common/socketConnection.cpp +++ b/common/socketConnection.cpp @@ -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 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(port)); + iterator = resolver.resolve(query); + SocketConnection::start(); } @@ -90,20 +97,20 @@ void ClientConnection::worker() active_ = true; while (active_) { + connected_ = false; try { { // std::unique_lock mlock(mutex_); - tcp::resolver resolver(io_service); - tcp::resolver::query query(tcp::v4(), ip, boost::lexical_cast(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); } diff --git a/common/socketConnection.h b/common/socketConnection.h index dd3efa49..2cefb12b 100644 --- a/common/socketConnection.h +++ b/common/socketConnection.h @@ -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 active_; + std::atomic 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();