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

213 lines
5.9 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
2012-01-02 17:58:37 +01:00
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
2011-08-28 15:17:58 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
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-08-29 20:38:01 +02:00
m_connecting = false;
2011-04-20 08:40:31 +02:00
}
2012-01-13 16:41:57 +01:00
Connection::~Connection()
{
close();
}
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-08-29 20:38:01 +02:00
m_connecting = true;
2011-05-30 05:11:12 +02:00
m_connectCallback = connectCallback;
2011-04-20 08:40:31 +02:00
asio::ip::tcp::resolver::query query(host, Fw::unsafeCast<std::string>(port));
2012-01-13 16:41:57 +01:00
auto weakSelf = ConnectionWeakPtr(shared_from_this());
m_resolver.async_resolve(query, [=](const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator) {
if(!weakSelf.lock())
return;
m_readTimer.cancel();
if(!error) {
m_socket.async_connect(*endpointIterator, std::bind(&Connection::onConnect, shared_from_this(), _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);
});
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-29 20:38:01 +02:00
if(!m_connected && !m_connecting)
2011-08-16 02:30:31 +02:00
return;
2011-08-29 20:38:01 +02:00
m_connecting = false;
2011-08-16 05:27:46 +02:00
m_connected = false;
2011-08-29 20:38:01 +02:00
m_connectCallback = nullptr;
m_errorCallback = nullptr;
m_recvCallback = nullptr;
2011-08-16 05:27:46 +02:00
2012-01-13 16:41:57 +01:00
m_resolver.cancel();
2011-08-16 02:30:31 +02:00
m_readTimer.cancel();
m_writeTimer.cancel();
if(m_socket.is_open()) {
2011-08-29 20:38:01 +02:00
boost::system::error_code ec;
m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
2011-08-16 02:30:31 +02:00
m_socket.close();
}
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 05:27:46 +02:00
m_writeTimer.cancel();
2011-08-16 02:30:31 +02:00
if(!m_connected)
return;
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;
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),
2011-11-04 17:40:26 +01:00
std::bind(&Connection::onRecv, shared_from_this(), _1, bytes));
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::read_some(const RecvCallback& callback)
{
m_readTimer.cancel();
if(!m_connected)
return;
m_recvCallback = callback;
m_socket.async_read_some(asio::buffer(m_recvBuffer, RECV_BUFFER_SIZE),
std::bind(&Connection::onRecv, shared_from_this(), _1, _2));
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
}
void Connection::onConnect(const boost::system::error_code& error)
{
2011-08-16 02:30:31 +02:00
m_readTimer.cancel();
2011-08-16 05:27:46 +02:00
2011-08-16 02:30:31 +02:00
if(!error) {
2011-08-29 20:38:01 +02:00
m_connected = true;
2011-11-09 00:03:17 +01:00
// disable nagle's algorithm
boost::asio::ip::tcp::no_delay option(true);
m_socket.set_option(option);
2011-08-16 02:30:31 +02:00
if(m_connectCallback)
2012-01-09 19:45:13 +01:00
m_connectCallback();
2011-08-16 02:30:31 +02:00
} else
handleError(error);
2011-11-04 17:40:26 +01:00
m_connecting = false;
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 05:27:46 +02:00
if(!m_connected)
return;
2011-08-16 02:30:31 +02:00
if(error)
handleError(error);
2011-05-30 05:11:12 +02:00
}
2011-11-04 17:40:26 +01:00
void Connection::onRecv(const boost::system::error_code& error, size_t recvSize)
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 05:27:46 +02:00
if(!m_connected)
return;
2011-08-16 02:30:31 +02:00
if(!error) {
if(m_recvCallback)
2012-01-09 19:45:13 +01:00
m_recvCallback(m_recvBuffer, recvSize);
2011-08-16 02:30:31 +02:00
} else
handleError(error);
}
void Connection::onTimeout(const boost::system::error_code& error)
{
if(error != asio::error::operation_aborted)
2011-08-16 05:27:46 +02:00
handleError(asio::error::timed_out);
2011-08-16 02:30:31 +02:00
}
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)
{
2011-08-16 05:27:46 +02:00
if(error != asio::error::operation_aborted) {
if(m_errorCallback)
2012-01-09 19:45:13 +01:00
m_errorCallback(error);
2011-08-29 20:38:01 +02:00
if(m_connected || m_connecting)
2011-08-16 05:27:46 +02:00
close();
}
2011-04-20 08:40:31 +02:00
}
2011-08-16 02:30:31 +02:00