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;
|
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()
|
|
|
|
{
|
|
|
|
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;
|
2012-02-03 03:54:33 +01:00
|
|
|
m_error.clear();
|
2011-05-30 05:11:12 +02:00
|
|
|
m_connectCallback = connectCallback;
|
2011-04-20 08:40:31 +02:00
|
|
|
|
2011-08-28 18:02:26 +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;
|
|
|
|
|
2012-02-03 03:54:33 +01:00
|
|
|
// 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;
|
|
|
|
|
2012-02-02 00:37:40 +01:00
|
|
|
// 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 weakSelf = ConnectionWeakPtr(shared_from_this());
|
2012-03-14 19:45:15 +01:00
|
|
|
m_sendEvent = g_eventDispatcher.scheduleEvent([=] {
|
2012-02-01 23:46:31 +01:00
|
|
|
if(!weakSelf.lock())
|
|
|
|
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, shared_from_this(), _1, _2));
|
|
|
|
|
|
|
|
m_writeTimer.expires_from_now(boost::posix_time::seconds(WRITE_TIMEOUT));
|
|
|
|
m_writeTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1));
|
|
|
|
|
|
|
|
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),
|
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
|
|
|
|
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
|
|
|
|
2011-09-11 18:03:20 +02: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) {
|
2012-02-03 03:54:33 +01:00
|
|
|
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
|
|
|
|