parent
be15b1820d
commit
f87b475f49
@ -1,125 +1,147 @@
|
||||
#include "connection.h"
|
||||
#include "networkmanager.h"
|
||||
|
||||
#include <framework/core/eventdispatcher.h>
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
static asio::io_service ioService;
|
||||
asio::io_service g_ioService;
|
||||
|
||||
Connection::Connection() :
|
||||
m_timer(ioService),
|
||||
m_resolver(ioService),
|
||||
m_socket(ioService)
|
||||
m_readTimer(g_ioService),
|
||||
m_writeTimer(g_ioService),
|
||||
m_resolver(g_ioService),
|
||||
m_socket(g_ioService)
|
||||
{
|
||||
m_connected = false;
|
||||
}
|
||||
|
||||
Connection::~Connection()
|
||||
void Connection::poll()
|
||||
{
|
||||
disconnect();
|
||||
g_ioService.poll();
|
||||
g_ioService.reset();
|
||||
}
|
||||
|
||||
void Connection::poll()
|
||||
void Connection::terminate()
|
||||
{
|
||||
ioService.poll();
|
||||
ioService.reset();
|
||||
g_ioService.stop();
|
||||
}
|
||||
|
||||
void Connection::connect(const std::string& host, uint16 port, const ConnectCallback& connectCallback)
|
||||
void Connection::connect(const std::string& host, uint16 port, const SimpleCallback& connectCallback)
|
||||
{
|
||||
m_connected = false;
|
||||
m_connectCallback = connectCallback;
|
||||
|
||||
asio::ip::tcp::resolver::query query(host, fw::unsafe_cast<std::string>(port));
|
||||
m_resolver.async_resolve(query, std::bind(&Connection::onResolve, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
|
||||
m_resolver.async_resolve(query, std::bind(&Connection::onResolve, shared_from_this(), _1, _2));
|
||||
|
||||
m_timer.expires_from_now(boost::posix_time::seconds(2));
|
||||
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
|
||||
m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1));
|
||||
}
|
||||
|
||||
void Connection::disconnect()
|
||||
void Connection::close()
|
||||
{
|
||||
m_socket.close();
|
||||
if(!m_connected)
|
||||
return;
|
||||
|
||||
m_readTimer.cancel();
|
||||
m_writeTimer.cancel();
|
||||
|
||||
if(m_socket.is_open()) {
|
||||
m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
|
||||
m_socket.close();
|
||||
}
|
||||
|
||||
m_connected = false;
|
||||
}
|
||||
|
||||
void Connection::send(uint8* buffer, uint16 size)
|
||||
void Connection::write(uint8* buffer, uint16 size)
|
||||
{
|
||||
if(!m_connected)
|
||||
return;
|
||||
|
||||
m_writeTimer.cancel();
|
||||
|
||||
asio::async_write(m_socket,
|
||||
asio::buffer(buffer, size),
|
||||
std::bind(&Connection::onSend, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
|
||||
asio::buffer(buffer, size),
|
||||
std::bind(&Connection::onWrite, shared_from_this(), _1, _2));
|
||||
|
||||
m_timer.expires_from_now(boost::posix_time::seconds(2));
|
||||
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||
m_writeTimer.expires_from_now(boost::posix_time::seconds(WRITE_TIMEOUT));
|
||||
m_writeTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1));
|
||||
}
|
||||
|
||||
void Connection::recv(uint16 bytes, uint32 timeout, const RecvCallback& callback)
|
||||
void Connection::read(uint16 bytes, const RecvCallback& callback)
|
||||
{
|
||||
m_readTimer.cancel();
|
||||
|
||||
if(!m_connected)
|
||||
return;
|
||||
|
||||
m_recvCallback = callback;
|
||||
m_recvSize = bytes;
|
||||
|
||||
asio::async_read(m_socket,
|
||||
asio::buffer(m_recvBuffer, bytes),
|
||||
std::bind(&Connection::onRecv, shared_from_this(), std::placeholders::_1));
|
||||
|
||||
if(timeout > 0) {
|
||||
m_timer.expires_from_now(boost::posix_time::seconds(timeout));
|
||||
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||
}
|
||||
}
|
||||
asio::buffer(m_recvBuffer, bytes),
|
||||
std::bind(&Connection::onRecv, shared_from_this(), _1));
|
||||
|
||||
void Connection::onTimeout(const boost::system::error_code& error)
|
||||
{
|
||||
if(error != asio::error::operation_aborted)
|
||||
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
|
||||
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
|
||||
m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1));
|
||||
}
|
||||
|
||||
void Connection::onResolve(const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator)
|
||||
{
|
||||
m_timer.cancel();
|
||||
m_readTimer.cancel();
|
||||
|
||||
if(error) {
|
||||
if(m_errorCallback)
|
||||
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
|
||||
return;
|
||||
}
|
||||
|
||||
m_socket.async_connect(*endpointIterator, std::bind(&Connection::onConnect, shared_from_this(), std::placeholders::_1));
|
||||
if(!error) {
|
||||
m_socket.async_connect(*endpointIterator, std::bind(&Connection::onConnect, shared_from_this(), _1));
|
||||
|
||||
m_timer.expires_from_now(boost::posix_time::seconds(2));
|
||||
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
|
||||
m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1));
|
||||
} else
|
||||
handleError(error);
|
||||
}
|
||||
|
||||
void Connection::onConnect(const boost::system::error_code& error)
|
||||
{
|
||||
m_timer.cancel();
|
||||
|
||||
if(error) {
|
||||
if(m_errorCallback)
|
||||
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
|
||||
return;
|
||||
}
|
||||
|
||||
if(m_connectCallback)
|
||||
g_dispatcher.addEvent(m_connectCallback);
|
||||
m_readTimer.cancel();
|
||||
m_connected = true;
|
||||
|
||||
if(!error) {
|
||||
if(m_connectCallback)
|
||||
g_dispatcher.addEvent(m_connectCallback);
|
||||
} else
|
||||
handleError(error);
|
||||
}
|
||||
|
||||
void Connection::onSend(const boost::system::error_code& error, size_t)
|
||||
void Connection::onWrite(const boost::system::error_code& error, size_t)
|
||||
{
|
||||
m_timer.cancel();
|
||||
m_writeTimer.cancel();
|
||||
|
||||
if(error) {
|
||||
if(m_errorCallback)
|
||||
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
|
||||
return;
|
||||
}
|
||||
if(error)
|
||||
handleError(error);
|
||||
}
|
||||
|
||||
void Connection::onRecv(const boost::system::error_code& error)
|
||||
{
|
||||
m_timer.cancel();
|
||||
m_readTimer.cancel();
|
||||
|
||||
if(error) {
|
||||
if(m_errorCallback)
|
||||
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
|
||||
return;
|
||||
}
|
||||
if(!error) {
|
||||
if(m_recvCallback)
|
||||
g_dispatcher.addEvent(std::bind(m_recvCallback, m_recvBuffer, m_recvSize));
|
||||
} else
|
||||
handleError(error);
|
||||
}
|
||||
|
||||
void Connection::onTimeout(const boost::system::error_code& error)
|
||||
{
|
||||
if(error != asio::error::operation_aborted)
|
||||
handleError(error);
|
||||
}
|
||||
|
||||
if(m_recvCallback)
|
||||
g_dispatcher.addEvent(std::bind(m_recvCallback, m_recvBuffer, m_recvSize));
|
||||
void Connection::handleError(const boost::system::error_code& error)
|
||||
{
|
||||
logTraceDebug(error.message());
|
||||
close();
|
||||
if(m_errorCallback)
|
||||
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in new issue