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

165 lines
5.6 KiB
C++
Raw Normal View History

2011-04-20 08:40:31 +02:00
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* 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.
*/
#include <core/dispatcher.h>
#include <net/connection.h>
static boost::asio::io_service ioService;
Connection::Connection() :
m_connectionState(CONNECTION_STATE_IDLE),
m_timer(ioService),
m_resolver(ioService),
m_socket(ioService)
{
}
void Connection::poll()
{
ioService.poll();
ioService.reset();
}
2011-05-30 05:11:12 +02:00
void Connection::connect(const std::string& host, uint16 port, const SimpleCallback& 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
m_connectionState = CONNECTION_STATE_RESOLVING;
2011-05-12 00:16:11 +02:00
boost::asio::ip::tcp::resolver::query query(host, convert_cast<std::string>(port));
2011-05-30 05:11:12 +02:00
m_resolver.async_resolve(query, boost::bind(&Connection::onResolve, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::iterator));
2011-04-20 08:40:31 +02:00
m_timer.expires_from_now(boost::posix_time::seconds(2));
2011-05-30 05:11:12 +02:00
m_timer.async_wait(boost::bind(&Connection::onTimeout, shared_from_this(), boost::asio::placeholders::error));
}
void Connection::send(OutputMessage *outputMessage)
{
boost::asio::async_write(m_socket,
boost::asio::buffer(outputMessage->getBuffer(), outputMessage->getMessageSize()),
boost::bind(&Connection::onSend, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
m_timer.expires_from_now(boost::posix_time::seconds(2));
m_timer.async_wait(boost::bind(&Connection::onTimeout, shared_from_this(), boost::asio::placeholders::error));
2011-04-20 08:40:31 +02:00
}
void Connection::onTimeout(const boost::system::error_code& error)
{
if(error != boost::asio::error::operation_aborted)
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
}
void Connection::onResolve(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIterator)
{
logTrace();
m_timer.cancel();
if(error) {
2011-05-30 05:11:12 +02:00
if(m_errorCallback)
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
2011-04-20 08:40:31 +02:00
return;
}
2011-05-30 05:11:12 +02:00
m_socket.async_connect(*endpointIterator, boost::bind(&Connection::onConnect, shared_from_this(), boost::asio::placeholders::error));
2011-04-20 08:40:31 +02:00
m_timer.expires_from_now(boost::posix_time::seconds(2));
2011-05-30 05:11:12 +02:00
m_timer.async_wait(boost::bind(&Connection::onTimeout, shared_from_this(), boost::asio::placeholders::error));
2011-04-20 08:40:31 +02:00
}
void Connection::onConnect(const boost::system::error_code& error)
{
logTrace();
m_timer.cancel();
if(error) {
2011-05-30 05:11:12 +02:00
if(m_errorCallback)
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
2011-04-20 08:40:31 +02:00
return;
}
g_dispatcher.addTask(m_connectCallback);
2011-05-30 05:11:12 +02:00
// Start listening.
boost::asio::async_read(m_socket,
2011-05-31 07:24:30 +02:00
boost::asio::buffer(m_inputMessage.getBuffer(), InputMessage::HEADER_LENGTH),
boost::bind(&Connection::onRecvHeader, shared_from_this(), boost::asio::placeholders::error));
2011-05-30 05:11:12 +02:00
}
void Connection::onSend(const boost::system::error_code& error, size_t)
{
logTrace();
m_timer.cancel();
if(error) {
if(m_errorCallback)
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
return;
}
}
2011-05-31 07:24:30 +02:00
void Connection::onRecvHeader(const boost::system::error_code& error)
2011-05-30 05:11:12 +02:00
{
logTrace();
if(error) {
if(m_errorCallback)
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
return;
}
2011-05-31 07:24:30 +02:00
uint16 messageSize = m_inputMessage.getU16();
m_inputMessage.setMessageSize(messageSize);
2011-05-30 05:11:12 +02:00
boost::asio::async_read(m_socket,
2011-05-31 07:24:30 +02:00
boost::asio::buffer(m_inputMessage.getBuffer() + InputMessage::CHECKSUM_POS, messageSize),
boost::bind(&Connection::onRecvData, shared_from_this(), boost::asio::placeholders::error));
2011-05-30 05:11:12 +02:00
}
2011-05-31 07:24:30 +02:00
void Connection::onRecvData(const boost::system::error_code& error)
2011-05-30 05:11:12 +02:00
{
logTrace();
if(error) {
if(m_errorCallback)
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
return;
}
// call callback
2011-05-31 07:24:30 +02:00
// must be called outside dispatcher cause of inputmessage.
2011-05-30 05:11:12 +02:00
if(m_recvCallback)
2011-05-31 07:24:30 +02:00
m_recvCallback(&m_inputMessage);
//g_dispatcher.addTask(boost::bind(m_recvCallback, &m_inputMessage));
2011-05-30 05:11:12 +02:00
2011-05-31 07:24:30 +02:00
// keep reading
2011-05-30 05:11:12 +02:00
2011-05-31 07:24:30 +02:00
// TODO: lua code must be reworked.
/*m_inputMessage.reset();
2011-05-30 05:11:12 +02:00
boost::asio::async_read(m_socket,
2011-05-31 07:24:30 +02:00
boost::asio::buffer(m_inputMessage.getBuffer(), InputMessage::HEADER_LENGTH),
boost::bind(&Connection::onRecvHeader, shared_from_this(), boost::asio::placeholders::error));*/
2011-05-30 05:11:12 +02:00
2011-04-20 08:40:31 +02:00
}