mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-28 00:16:17 +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
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue