connection fixes, motd fixed

This commit is contained in:
Henrique 2011-05-31 02:24:30 -03:00
parent 9b2d71f6d8
commit c490577ea8
6 changed files with 33 additions and 23 deletions

View File

@ -36,7 +36,11 @@ function enterGame_onOkClicked()
protocolLogin.onMotd = function(motd)
loadMessageBox.onDestroy = nil
loadMessageBox:destroy()
local msgBox = messageBox("Message of the day", motd)
local motdNumber = string.sub(motd, 0, string.find(motd, "\n"))
local motdText = string.sub(motd, string.find(motd, "\n") + 1, string.len(motd))
local msgBox = messageBox("Message of the day", motdText)
msgBox.onDestroy = function()
enterGameWindow.visible = true
end
@ -45,6 +49,8 @@ function enterGame_onOkClicked()
local account = enterGameWindow:child("accountNameTextEdit").text
local password = enterGameWindow:child("passwordTextEdit").text
account = "tibialua0"
password = "lua123456"
protocolLogin:login(account, password)
end

View File

@ -101,10 +101,9 @@ void Connection::onConnect(const boost::system::error_code& error)
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));
boost::asio::buffer(m_inputMessage.getBuffer(), InputMessage::HEADER_LENGTH),
boost::bind(&Connection::onRecvHeader, shared_from_this(), boost::asio::placeholders::error));
}
void Connection::onSend(const boost::system::error_code& error, size_t)
@ -120,7 +119,7 @@ void Connection::onSend(const boost::system::error_code& error, size_t)
}
}
void Connection::onRecvHeader(const boost::system::error_code& error, InputMessage *inputMessage)
void Connection::onRecvHeader(const boost::system::error_code& error)
{
logTrace();
@ -130,15 +129,15 @@ void Connection::onRecvHeader(const boost::system::error_code& error, InputMessa
return;
}
uint16 messageSize = inputMessage->getU16();
inputMessage->setMessageSize(messageSize);
uint16 messageSize = m_inputMessage.getU16();
m_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));
boost::asio::buffer(m_inputMessage.getBuffer() + InputMessage::CHECKSUM_POS, messageSize),
boost::bind(&Connection::onRecvData, shared_from_this(), boost::asio::placeholders::error));
}
void Connection::onRecvData(const boost::system::error_code& error, InputMessage *inputMessage)
void Connection::onRecvData(const boost::system::error_code& error)
{
logTrace();
@ -149,17 +148,17 @@ void Connection::onRecvData(const boost::system::error_code& error, InputMessage
}
// call callback
// must be called outside dispatcher cause of inputmessage.
if(m_recvCallback)
g_dispatcher.addTask(boost::bind(m_recvCallback, inputMessage));
m_recvCallback(&m_inputMessage);
//g_dispatcher.addTask(boost::bind(m_recvCallback, &m_inputMessage));
// FIXME:
// TODO declare inside class? call onRecvHeader.
// this needs a remake
/*delete inputMessage;
// keep reading
inputMessage = new InputMessage;
// TODO: lua code must be reworked.
/*m_inputMessage.reset();
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));*/
boost::asio::buffer(m_inputMessage.getBuffer(), InputMessage::HEADER_LENGTH),
boost::bind(&Connection::onRecvHeader, shared_from_this(), boost::asio::placeholders::error));*/
}

View File

@ -50,8 +50,8 @@ public:
void onResolve(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIterator);
void onConnect(const boost::system::error_code& error);
void onSend(const boost::system::error_code& error, size_t);
void onRecvHeader(const boost::system::error_code& error, InputMessage *inputMessage);
void onRecvData(const boost::system::error_code& error, InputMessage *inputMessage);
void onRecvHeader(const boost::system::error_code& error);
void onRecvData(const boost::system::error_code& error);
enum ConnectionState_t {
CONNECTION_STATE_IDLE = 0,
@ -69,6 +69,7 @@ private:
boost::asio::deadline_timer m_timer;
boost::asio::ip::tcp::resolver m_resolver;
boost::asio::ip::tcp::socket m_socket;
InputMessage m_inputMessage;
};

View File

@ -86,5 +86,7 @@ std::string InputMessage::getString()
bool InputMessage::canRead(int bytes)
{
return (m_readPos + bytes <= m_messageSize) && (m_readPos + bytes <= BUFFER_MAXSIZE);
if((m_readPos + bytes > m_messageSize) || (m_readPos + bytes > BUFFER_MAXSIZE))
logFatal("[InputMessage::canRead()]: Cant read. Message is finished or read position has reached buffer's maximum size.");
return true;
}

View File

@ -31,7 +31,7 @@ class InputMessage
{
public:
enum {
BUFFER_MAXSIZE = 256,
BUFFER_MAXSIZE = 4096,
HEADER_POS = 0,
HEADER_LENGTH = 2,
CHECKSUM_POS = 2,

View File

@ -104,5 +104,7 @@ void OutputMessage::addPaddingBytes(int bytes, uint8 byte)
bool OutputMessage::canWrite(int bytes)
{
return (m_writePos + bytes <= BUFFER_MAXSIZE);
if(m_writePos + bytes > BUFFER_MAXSIZE)
logFatal("[OutputMessage::canWrite()]: Can't write. Write position has reached buffer's maxium size.");
return true;
}