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

116 lines
3.4 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-14 04:09:11 +02:00
#include <core/eventdispatcher.h>
2011-04-20 08:40:31 +02:00
2011-07-28 01:01:33 +02:00
static asio::io_service ioService;
2011-04-20 08:40:31 +02:00
Connection::Connection() :
m_timer(ioService),
m_resolver(ioService),
m_socket(ioService)
{
}
void Connection::poll()
{
ioService.poll();
ioService.reset();
}
2011-07-28 01:01:33 +02:00
void Connection::connect(const std::string& host, uint16 port, const ConnectCallback& connectCallback)
2011-04-20 08:40:31 +02:00
{
2011-05-30 05:11:12 +02:00
m_connectCallback = connectCallback;
2011-04-20 08:40:31 +02:00
2011-08-14 04:09:11 +02:00
asio::ip::tcp::resolver::query query(host, aux::unsafe_cast<std::string>(port));
m_resolver.async_resolve(query, std::bind(&Connection::onResolve, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
2011-04-20 08:40:31 +02:00
m_timer.expires_from_now(boost::posix_time::seconds(2));
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
2011-05-30 05:11:12 +02:00
}
2011-07-28 01:01:33 +02:00
void Connection::send(uint8* buffer, uint16 size)
2011-05-30 05:11:12 +02:00
{
2011-07-28 01:01:33 +02:00
asio::async_write(m_socket,
asio::buffer(buffer, size),
std::bind(&Connection::onSend, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
2011-05-30 05:11:12 +02:00
m_timer.expires_from_now(boost::posix_time::seconds(2));
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
2011-04-20 08:40:31 +02:00
}
2011-07-27 20:10:49 +02:00
void Connection::recv(uint16 bytes, uint32 timeout, const RecvCallback& callback)
{
m_recvCallback = callback;
m_recvSize = bytes;
2011-07-28 01:01:33 +02:00
asio::async_read(m_socket,
asio::buffer(m_recvBuffer, bytes),
2011-07-27 20:10:49 +02:00
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));
}
}
2011-04-20 08:40:31 +02:00
void Connection::onTimeout(const boost::system::error_code& error)
{
2011-07-28 01:01:33 +02:00
if(error != asio::error::operation_aborted)
2011-08-14 04:09:11 +02:00
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
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
{
m_timer.cancel();
if(error) {
2011-05-30 05:11:12 +02:00
if(m_errorCallback)
2011-08-14 04:09:11 +02:00
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
2011-04-20 08:40:31 +02:00
return;
}
m_socket.async_connect(*endpointIterator, std::bind(&Connection::onConnect, shared_from_this(), std::placeholders::_1));
2011-04-20 08:40:31 +02:00
m_timer.expires_from_now(boost::posix_time::seconds(2));
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
2011-04-20 08:40:31 +02:00
}
void Connection::onConnect(const boost::system::error_code& error)
{
m_timer.cancel();
if(error) {
2011-05-30 05:11:12 +02:00
if(m_errorCallback)
2011-08-14 04:09:11 +02:00
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
2011-04-20 08:40:31 +02:00
return;
}
2011-07-27 20:10:49 +02:00
if(m_connectCallback)
2011-08-14 04:09:11 +02:00
g_dispatcher.addEvent(m_connectCallback);
2011-05-30 05:11:12 +02:00
}
void Connection::onSend(const boost::system::error_code& error, size_t)
{
m_timer.cancel();
if(error) {
if(m_errorCallback)
2011-08-14 04:09:11 +02:00
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
2011-05-30 05:11:12 +02:00
return;
}
}
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-07-27 20:10:49 +02:00
m_timer.cancel();
2011-05-30 05:11:12 +02:00
if(error) {
if(m_errorCallback)
2011-08-14 04:09:11 +02:00
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
2011-05-30 05:11:12 +02:00
return;
}
if(m_recvCallback)
2011-08-14 04:09:11 +02:00
g_dispatcher.addEvent(std::bind(m_recvCallback, m_recvBuffer, m_recvSize));
2011-04-20 08:40:31 +02:00
}