tibia-client/src/framework/net/connection.cpp

147 lines
3.7 KiB
C++
Raw Normal View History

2011-07-28 01:01:33 +02:00
#include "connection.h"
2011-04-20 08:40:31 +02:00
2011-08-15 16:06:15 +02:00
#include <framework/core/eventdispatcher.h>
2011-08-16 02:30:31 +02:00
#include <boost/asio.hpp>
2011-04-20 08:40:31 +02:00
2011-08-16 02:30:31 +02:00
asio::io_service g_ioService;
2011-04-20 08:40:31 +02:00
Connection::Connection() :
2011-08-16 02:30:31 +02:00
m_readTimer(g_ioService),
m_writeTimer(g_ioService),
m_resolver(g_ioService),
m_socket(g_ioService)
2011-04-20 08:40:31 +02:00
{
2011-08-16 02:30:31 +02:00
m_connected = false;
2011-04-20 08:40:31 +02:00
}
2011-08-16 02:30:31 +02:00
void Connection::poll()
2011-08-15 07:09:27 +02:00
{
2011-08-16 02:30:31 +02:00
g_ioService.poll();
g_ioService.reset();
2011-08-15 07:09:27 +02:00
}
2011-08-16 02:30:31 +02:00
void Connection::terminate()
2011-04-20 08:40:31 +02:00
{
2011-08-16 02:30:31 +02:00
g_ioService.stop();
2011-04-20 08:40:31 +02:00
}
2011-08-16 02:30:31 +02:00
void Connection::connect(const std::string& host, uint16 port, const SimpleCallback& connectCallback)
2011-04-20 08:40:31 +02:00
{
2011-08-16 02:30:31 +02:00
m_connected = false;
2011-05-30 05:11:12 +02:00
m_connectCallback = connectCallback;
2011-04-20 08:40:31 +02:00
2011-08-15 16:06:15 +02:00
asio::ip::tcp::resolver::query query(host, fw::unsafe_cast<std::string>(port));
2011-08-16 02:30:31 +02:00
m_resolver.async_resolve(query, std::bind(&Connection::onResolve, shared_from_this(), _1, _2));
2011-04-20 08:40:31 +02:00
2011-08-16 02:30:31 +02:00
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1));
2011-05-30 05:11:12 +02:00
}
2011-08-16 02:30:31 +02:00
void Connection::close()
2011-08-15 07:09:27 +02:00
{
2011-08-16 02:30:31 +02:00
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;
2011-08-15 07:09:27 +02:00
}
2011-08-16 02:30:31 +02:00
void Connection::write(uint8* buffer, uint16 size)
2011-05-30 05:11:12 +02:00
{
2011-08-16 02:30:31 +02:00
if(!m_connected)
return;
m_writeTimer.cancel();
2011-07-28 01:01:33 +02:00
asio::async_write(m_socket,
2011-08-16 02:30:31 +02:00
asio::buffer(buffer, size),
std::bind(&Connection::onWrite, shared_from_this(), _1, _2));
2011-05-30 05:11:12 +02:00
2011-08-16 02:30:31 +02:00
m_writeTimer.expires_from_now(boost::posix_time::seconds(WRITE_TIMEOUT));
m_writeTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1));
2011-04-20 08:40:31 +02:00
}
2011-08-16 02:30:31 +02:00
void Connection::read(uint16 bytes, const RecvCallback& callback)
2011-07-27 20:10:49 +02:00
{
2011-08-16 02:30:31 +02:00
m_readTimer.cancel();
if(!m_connected)
return;
2011-07-27 20:10:49 +02:00
m_recvCallback = callback;
m_recvSize = bytes;
2011-07-28 01:01:33 +02:00
asio::async_read(m_socket,
2011-08-16 02:30:31 +02:00
asio::buffer(m_recvBuffer, bytes),
std::bind(&Connection::onRecv, shared_from_this(), _1));
2011-07-27 20:10:49 +02:00
2011-08-16 02:30:31 +02:00
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1));
2011-04-20 08:40:31 +02:00
}
2011-07-28 01:01:33 +02:00
void Connection::onResolve(const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator)
2011-04-20 08:40:31 +02:00
{
2011-08-16 02:30:31 +02:00
m_readTimer.cancel();
2011-04-20 08:40:31 +02:00
2011-08-16 02:30:31 +02:00
if(!error) {
m_socket.async_connect(*endpointIterator, std::bind(&Connection::onConnect, shared_from_this(), _1));
2011-04-20 08:40:31 +02:00
2011-08-16 02:30:31 +02:00
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);
2011-04-20 08:40:31 +02:00
}
void Connection::onConnect(const boost::system::error_code& error)
{
2011-08-16 02:30:31 +02:00
m_readTimer.cancel();
m_connected = true;
if(!error) {
if(m_connectCallback)
g_dispatcher.addEvent(m_connectCallback);
} else
handleError(error);
2011-05-30 05:11:12 +02:00
}
2011-08-16 02:30:31 +02:00
void Connection::onWrite(const boost::system::error_code& error, size_t)
2011-05-30 05:11:12 +02:00
{
2011-08-16 02:30:31 +02:00
m_writeTimer.cancel();
2011-05-30 05:11:12 +02:00
2011-08-16 02:30:31 +02:00
if(error)
handleError(error);
2011-05-30 05:11:12 +02:00
}
2011-07-27 20:10:49 +02:00
void Connection::onRecv(const boost::system::error_code& error)
2011-05-30 05:11:12 +02:00
{
2011-08-16 02:30:31 +02:00
m_readTimer.cancel();
2011-05-30 05:11:12 +02:00
2011-08-16 02:30:31 +02:00
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);
}
2011-05-30 05:11:12 +02:00
2011-08-16 02:30:31 +02:00
void Connection::handleError(const boost::system::error_code& error)
{
logTraceDebug(error.message());
close();
if(m_errorCallback)
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
2011-04-20 08:40:31 +02:00
}
2011-08-16 02:30:31 +02:00