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

241 lines
7.1 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
#include <framework/core/application.h>
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;
2012-02-01 23:46:31 +01:00
m_sendBufferSize = 0;
2011-04-20 08:40:31 +02:00
}
2012-01-13 16:41:57 +01:00
Connection::~Connection()
{
assert(!g_app.isTerminated());
2012-01-13 16:41:57 +01:00
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
}
void Connection::connect(const std::string& host, uint16 port, const std::function<void()>& 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;
m_error.clear();
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, stdext::unsafe_cast<std::string>(port));
2012-01-13 16:41:57 +01:00
auto self = asConnection();
2012-01-13 16:41:57 +01:00
m_resolver.async_resolve(query, [=](const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator) {
if(self->is_unique_ref())
2012-01-13 16:41:57 +01:00
return;
m_readTimer.cancel();
if(!error) {
m_socket.async_connect(*endpointIterator, std::bind(&Connection::onConnect, asConnection(), std::placeholders::_1));
2012-01-13 16:41:57 +01:00
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
m_readTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1));
2012-01-13 16:41:57 +01:00
} 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, asConnection(), std::placeholders::_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;
// flush send data before disconnecting on clean connections
if(m_connected && !m_error && m_sendBufferSize > 0 && m_sendEvent)
m_sendEvent->execute();
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 02:30:31 +02:00
if(!m_connected)
return;
// send old buffer if we can't add more data
if(m_sendBufferSize + size >= SEND_BUFFER_SIZE && m_sendEvent)
m_sendEvent->execute();
2012-02-03 05:18:54 +01:00
// we can't send the data right away, otherwise we could create tcp congestion
2012-02-01 23:46:31 +01:00
memcpy(m_sendBuffer + m_sendBufferSize, buffer, size);
m_sendBufferSize += size;
if(!m_sendEvent || m_sendEvent->isExecuted() || m_sendEvent->isCanceled()) {
auto self = asConnection();
2012-04-03 16:15:11 +02:00
// wait 1 ms to do the real send
2012-06-26 00:13:30 +02:00
m_sendEvent = g_dispatcher.scheduleEvent([=] {
if(self->is_unique_ref())
2012-02-01 23:46:31 +01:00
return;
//m_writeTimer.cancel();
2011-05-30 05:11:12 +02:00
2012-02-01 23:46:31 +01:00
asio::async_write(m_socket,
asio::buffer(m_sendBuffer, m_sendBufferSize),
std::bind(&Connection::onWrite, asConnection(), std::placeholders::_1, std::placeholders::_2));
2012-02-01 23:46:31 +01:00
m_writeTimer.expires_from_now(boost::posix_time::seconds(WRITE_TIMEOUT));
m_writeTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1));
2012-02-01 23:46:31 +01:00
m_sendBufferSize = 0;
}, SEND_INTERVAL);
}
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),
std::bind(&Connection::onRecv, asConnection(), std::placeholders::_1, bytes));
2011-11-04 17:40:26 +01:00
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
m_readTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1));
2011-11-04 17:40:26 +01:00
}
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, asConnection(), std::placeholders::_1, std::placeholders::_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, asConnection(), std::placeholders::_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
2012-02-03 05:18:54 +01:00
// disable nagle's algorithm, this make the game play smoother
2011-11-09 00:03:17 +01:00
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) {
m_error = error;
2011-08-16 05:27:46 +02:00
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