init protocol login

This commit is contained in:
Henrique
2011-05-30 00:11:12 -03:00
parent a54f5dd3f9
commit ad10754779
11 changed files with 379 additions and 29 deletions

View File

@@ -40,16 +40,26 @@ void Connection::poll()
ioService.reset();
}
void Connection::connect(const std::string& host, uint16 port, const SimpleCallback& callback)
void Connection::connect(const std::string& host, uint16 port, const SimpleCallback& connectCallback)
{
m_connectCallback = callback;
m_connectCallback = connectCallback;
m_connectionState = CONNECTION_STATE_RESOLVING;
boost::asio::ip::tcp::resolver::query query(host, convert_cast<std::string>(port));
m_resolver.async_resolve(query, boost::bind(&Connection::onResolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
m_resolver.async_resolve(query, boost::bind(&Connection::onResolve, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::iterator));
m_timer.expires_from_now(boost::posix_time::seconds(2));
m_timer.async_wait(boost::bind(&Connection::onTimeout, this, boost::asio::placeholders::error));
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));
}
void Connection::onTimeout(const boost::system::error_code& error)
@@ -65,14 +75,15 @@ void Connection::onResolve(const boost::system::error_code& error, boost::asio::
m_timer.cancel();
if(error) {
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
if(m_errorCallback)
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
return;
}
m_socket.async_connect(*endpointIterator, boost::bind(&Connection::onConnect, this, boost::asio::placeholders::error));
m_socket.async_connect(*endpointIterator, boost::bind(&Connection::onConnect, shared_from_this(), boost::asio::placeholders::error));
m_timer.expires_from_now(boost::posix_time::seconds(2));
m_timer.async_wait(boost::bind(&Connection::onTimeout, this, boost::asio::placeholders::error));
m_timer.async_wait(boost::bind(&Connection::onTimeout, shared_from_this(), boost::asio::placeholders::error));
}
void Connection::onConnect(const boost::system::error_code& error)
@@ -82,9 +93,73 @@ void Connection::onConnect(const boost::system::error_code& error)
m_timer.cancel();
if(error) {
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
if(m_errorCallback)
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
return;
}
g_dispatcher.addTask(m_connectCallback);
// Start listening.
InputMessage *inputMessage = new InputMessage;
boost::asio::async_read(m_socket,
boost::asio::buffer(inputMessage->getBuffer(), InputMessage::HEADER_LENGTH),
boost::bind(&Connection::onRecvHeader, shared_from_this(), boost::asio::placeholders::error, inputMessage));
}
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;
}
}
void Connection::onRecvHeader(const boost::system::error_code& error, InputMessage *inputMessage)
{
logTrace();
if(error) {
if(m_errorCallback)
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
return;
}
uint16 messageSize = inputMessage->getU16();
inputMessage->setMessageSize(messageSize);
boost::asio::async_read(m_socket,
boost::asio::buffer(inputMessage->getBuffer() + InputMessage::CHECKSUM_POS, messageSize),
boost::bind(&Connection::onRecvData, shared_from_this(), boost::asio::placeholders::error, inputMessage));
}
void Connection::onRecvData(const boost::system::error_code& error, InputMessage *inputMessage)
{
logTrace();
if(error) {
if(m_errorCallback)
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
return;
}
// call callback
if(m_recvCallback)
g_dispatcher.addTask(boost::bind(m_recvCallback, inputMessage));
// FIXME:
// TODO declare inside class? call onRecvHeader.
// this needs a remake
/*delete inputMessage;
inputMessage = new InputMessage;
boost::asio::async_read(m_socket,
boost::asio::buffer(inputMessage->getBuffer(), InputMessage::HEADER_LENGTH),
boost::bind(&Connection::onRecvHeader, shared_from_this(), boost::asio::placeholders::error, inputMessage));*/
}