connection rework, entergame.lua fix
This commit is contained in:
parent
81ff15fe6b
commit
f34b6b1bab
|
@ -41,7 +41,7 @@ function EnterGame_connectToLoginServer()
|
||||||
local motdBox = displayInfoBox("Message of the day", motdText)
|
local motdBox = displayInfoBox("Message of the day", motdText)
|
||||||
|
|
||||||
-- cancel protocol and reacreate enter game window
|
-- cancel protocol and reacreate enter game window
|
||||||
protocol.cancel()
|
protocol:cancel()
|
||||||
motdBox.onOk = recreateEnterGame
|
motdBox.onOk = recreateEnterGame
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,6 @@
|
||||||
static boost::asio::io_service ioService;
|
static boost::asio::io_service ioService;
|
||||||
|
|
||||||
Connection::Connection() :
|
Connection::Connection() :
|
||||||
m_connectionState(CONNECTION_STATE_IDLE),
|
|
||||||
m_timer(ioService),
|
m_timer(ioService),
|
||||||
m_resolver(ioService),
|
m_resolver(ioService),
|
||||||
m_socket(ioService)
|
m_socket(ioService)
|
||||||
|
@ -43,7 +42,6 @@ void Connection::poll()
|
||||||
void Connection::connect(const std::string& host, uint16 port, const std::function<void()>& connectCallback)
|
void Connection::connect(const std::string& host, uint16 port, const std::function<void()>& connectCallback)
|
||||||
{
|
{
|
||||||
m_connectCallback = connectCallback;
|
m_connectCallback = connectCallback;
|
||||||
m_connectionState = CONNECTION_STATE_RESOLVING;
|
|
||||||
|
|
||||||
boost::asio::ip::tcp::resolver::query query(host, convert<std::string>(port));
|
boost::asio::ip::tcp::resolver::query query(host, convert<std::string>(port));
|
||||||
m_resolver.async_resolve(query, std::bind(&Connection::onResolve, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
|
m_resolver.async_resolve(query, std::bind(&Connection::onResolve, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
|
||||||
|
@ -52,16 +50,31 @@ void Connection::connect(const std::string& host, uint16 port, const std::functi
|
||||||
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::send(OutputMessage *outputMessage)
|
void Connection::send(uint8 *buffer, uint16 size)
|
||||||
{
|
{
|
||||||
boost::asio::async_write(m_socket,
|
boost::asio::async_write(m_socket,
|
||||||
boost::asio::buffer(outputMessage->getBuffer(), outputMessage->getMessageSize()),
|
boost::asio::buffer(buffer, size),
|
||||||
std::bind(&Connection::onSend, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
|
std::bind(&Connection::onSend, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
|
||||||
|
|
||||||
m_timer.expires_from_now(boost::posix_time::seconds(2));
|
m_timer.expires_from_now(boost::posix_time::seconds(2));
|
||||||
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Connection::recv(uint16 bytes, uint32 timeout, const RecvCallback& callback)
|
||||||
|
{
|
||||||
|
m_recvCallback = callback;
|
||||||
|
m_recvSize = bytes;
|
||||||
|
|
||||||
|
boost::asio::async_read(m_socket,
|
||||||
|
boost::asio::buffer(m_recvBuffer, bytes),
|
||||||
|
std::bind(&Connection::onRecv, shared_from_this(), std::placeholders::_1));
|
||||||
|
|
||||||
|
if(timeout > 0) {
|
||||||
|
m_timer.expires_from_now(boost::posix_time::seconds(timeout));
|
||||||
|
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Connection::onTimeout(const boost::system::error_code& error)
|
void Connection::onTimeout(const boost::system::error_code& error)
|
||||||
{
|
{
|
||||||
if(error != boost::asio::error::operation_aborted)
|
if(error != boost::asio::error::operation_aborted)
|
||||||
|
@ -94,12 +107,8 @@ void Connection::onConnect(const boost::system::error_code& error)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(m_connectCallback)
|
||||||
g_dispatcher.addTask(m_connectCallback);
|
g_dispatcher.addTask(m_connectCallback);
|
||||||
|
|
||||||
// Start listening.
|
|
||||||
boost::asio::async_read(m_socket,
|
|
||||||
boost::asio::buffer(m_inputMessage.getBuffer(), InputMessage::HEADER_LENGTH),
|
|
||||||
std::bind(&Connection::onRecvHeader, shared_from_this(), std::placeholders::_1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::onSend(const boost::system::error_code& error, size_t)
|
void Connection::onSend(const boost::system::error_code& error, size_t)
|
||||||
|
@ -113,42 +122,16 @@ void Connection::onSend(const boost::system::error_code& error, size_t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::onRecvHeader(const boost::system::error_code& error)
|
void Connection::onRecv(const boost::system::error_code& error)
|
||||||
{
|
{
|
||||||
|
m_timer.cancel();
|
||||||
|
|
||||||
if(error) {
|
if(error) {
|
||||||
if(m_errorCallback)
|
if(m_errorCallback)
|
||||||
g_dispatcher.addTask(std::bind(m_errorCallback, error));
|
g_dispatcher.addTask(std::bind(m_errorCallback, error));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16 messageSize = m_inputMessage.getU16();
|
|
||||||
m_inputMessage.setMessageSize(messageSize);
|
|
||||||
|
|
||||||
boost::asio::async_read(m_socket,
|
|
||||||
boost::asio::buffer(m_inputMessage.getBuffer() + InputMessage::CHECKSUM_POS, messageSize),
|
|
||||||
std::bind(&Connection::onRecvData, shared_from_this(), std::placeholders::_1));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::onRecvData(const boost::system::error_code& error)
|
|
||||||
{
|
|
||||||
if(error) {
|
|
||||||
if(m_errorCallback)
|
|
||||||
g_dispatcher.addTask(std::bind(m_errorCallback, error));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// call callback
|
|
||||||
// must be called outside dispatcher cause of inputmessage.
|
|
||||||
if(m_recvCallback)
|
if(m_recvCallback)
|
||||||
m_recvCallback(&m_inputMessage);
|
g_dispatcher.addTask(std::bind(m_recvCallback, m_recvBuffer, m_recvSize));
|
||||||
//g_dispatcher.addTask(std::bind(m_recvCallback, &m_inputMessage));
|
|
||||||
|
|
||||||
// keep reading
|
|
||||||
|
|
||||||
// TODO: lua code must be reworked.
|
|
||||||
/*m_inputMessage.reset();
|
|
||||||
boost::asio::async_read(m_socket,
|
|
||||||
boost::asio::buffer(m_inputMessage.getBuffer(), InputMessage::HEADER_LENGTH),
|
|
||||||
std::bind(&Connection::onRecvHeader, shared_from_this(), std::placeholders::_1));*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,14 +26,10 @@
|
||||||
#define CONNECTION_H
|
#define CONNECTION_H
|
||||||
|
|
||||||
#include <global.h>
|
#include <global.h>
|
||||||
|
|
||||||
#include <net/inputmessage.h>
|
|
||||||
#include <net/outputmessage.h>
|
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
|
|
||||||
typedef std::function<void(boost::system::error_code&)> ErrorCallback;
|
typedef std::function<void(boost::system::error_code&)> ErrorCallback;
|
||||||
typedef std::function<void(InputMessage*)> RecvCallback;
|
typedef std::function<void(uint8*, uint16)> RecvCallback;
|
||||||
|
|
||||||
class Connection : public std::enable_shared_from_this<Connection>, boost::noncopyable
|
class Connection : public std::enable_shared_from_this<Connection>, boost::noncopyable
|
||||||
{
|
{
|
||||||
|
@ -43,7 +39,9 @@ public:
|
||||||
static void poll();
|
static void poll();
|
||||||
|
|
||||||
void connect(const std::string& host, uint16 port, const std::function<void()>& connectCallback);
|
void connect(const std::string& host, uint16 port, const std::function<void()>& connectCallback);
|
||||||
void send(OutputMessage *outputMessage);
|
void send(uint8 *buffer, uint16 size);
|
||||||
|
void recv(uint16 bytes, uint32 timeout, const RecvCallback& callback);
|
||||||
|
void recv(const std::string& data, uint32 timeout, const RecvCallback& callback);
|
||||||
|
|
||||||
void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; }
|
void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; }
|
||||||
void setRecvCallback(const RecvCallback& recvCallback) { m_recvCallback = recvCallback; }
|
void setRecvCallback(const RecvCallback& recvCallback) { m_recvCallback = recvCallback; }
|
||||||
|
@ -52,27 +50,19 @@ public:
|
||||||
void onResolve(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIterator);
|
void onResolve(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIterator);
|
||||||
void onConnect(const boost::system::error_code& error);
|
void onConnect(const boost::system::error_code& error);
|
||||||
void onSend(const boost::system::error_code& error, size_t);
|
void onSend(const boost::system::error_code& error, size_t);
|
||||||
void onRecvHeader(const boost::system::error_code& error);
|
void onRecv(const boost::system::error_code& error);
|
||||||
void onRecvData(const boost::system::error_code& error);
|
|
||||||
|
|
||||||
enum ConnectionState_t {
|
|
||||||
CONNECTION_STATE_IDLE = 0,
|
|
||||||
CONNECTION_STATE_RESOLVING,
|
|
||||||
CONNECTION_STATE_CONNECTING,
|
|
||||||
CONNECTION_STATE_CONNECTED
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ErrorCallback m_errorCallback;
|
ErrorCallback m_errorCallback;
|
||||||
RecvCallback m_recvCallback;
|
|
||||||
std::function<void()> m_connectCallback;
|
std::function<void()> m_connectCallback;
|
||||||
ConnectionState_t m_connectionState;
|
|
||||||
|
|
||||||
boost::asio::deadline_timer m_timer;
|
boost::asio::deadline_timer m_timer;
|
||||||
boost::asio::ip::tcp::resolver m_resolver;
|
boost::asio::ip::tcp::resolver m_resolver;
|
||||||
boost::asio::ip::tcp::socket m_socket;
|
boost::asio::ip::tcp::socket m_socket;
|
||||||
InputMessage m_inputMessage;
|
|
||||||
|
|
||||||
|
uint8 m_recvBuffer[65536];
|
||||||
|
uint16 m_recvSize;
|
||||||
|
RecvCallback m_recvCallback;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::shared_ptr<Connection> ConnectionPtr;
|
typedef std::shared_ptr<Connection> ConnectionPtr;
|
||||||
|
|
|
@ -30,13 +30,12 @@ Protocol::Protocol() :
|
||||||
m_connection(new Connection)
|
m_connection(new Connection)
|
||||||
{
|
{
|
||||||
m_connection->setErrorCallback(std::bind(&Protocol::onError, this, std::placeholders::_1));
|
m_connection->setErrorCallback(std::bind(&Protocol::onError, this, std::placeholders::_1));
|
||||||
m_connection->setRecvCallback(std::bind(&Protocol::onRecv, this, std::placeholders::_1));
|
|
||||||
m_xteaEncryptionEnabled = false;
|
m_xteaEncryptionEnabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Protocol::connect(const std::string& host, uint16 port, const std::function<void()>& callback)
|
void Protocol::connect(const std::string& host, uint16 port)
|
||||||
{
|
{
|
||||||
m_connection->connect(host, port, callback);
|
m_connection->connect(host, port, std::bind(&Protocol::onConnect, asProtocol()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Protocol::send(OutputMessage *outputMessage)
|
void Protocol::send(OutputMessage *outputMessage)
|
||||||
|
@ -56,30 +55,56 @@ void Protocol::send(OutputMessage *outputMessage)
|
||||||
outputMessage->addU16(messageSize);
|
outputMessage->addU16(messageSize);
|
||||||
|
|
||||||
// Send
|
// Send
|
||||||
m_connection->send(outputMessage);
|
m_connection->send(outputMessage->getBuffer(), outputMessage->getMessageSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Protocol::onRecv(InputMessage *inputMessage)
|
void Protocol::recv()
|
||||||
{
|
{
|
||||||
uint32 checksum = getAdlerChecksum(inputMessage->getBuffer() + InputMessage::DATA_POS, inputMessage->getMessageSize() - InputMessage::CHECKSUM_LENGTH);
|
m_inputMessage.reset();
|
||||||
if(inputMessage->getU32() != checksum) {
|
|
||||||
|
m_connection->recv(InputMessage::HEADER_LENGTH, 2, std::bind(&Protocol::internalRecvHeader, asProtocol(), std::placeholders::_1, std::placeholders::_2));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Protocol::internalRecvHeader(uint8 *buffer, uint16 size)
|
||||||
|
{
|
||||||
|
memcpy(m_inputMessage.getBuffer() + InputMessage::HEADER_POS, buffer, size);
|
||||||
|
|
||||||
|
uint16 dataSize = m_inputMessage.getU16();
|
||||||
|
m_inputMessage.setMessageSize(dataSize);
|
||||||
|
|
||||||
|
m_connection->recv(dataSize, 5, std::bind(&Protocol::internalRecvData, asProtocol(), std::placeholders::_1, std::placeholders::_2));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Protocol::internalRecvData(uint8 *buffer, uint16 size)
|
||||||
|
{
|
||||||
|
memcpy(m_inputMessage.getBuffer() + InputMessage::CHECKSUM_POS, buffer, size);
|
||||||
|
|
||||||
|
uint32 checksum = getAdlerChecksum(m_inputMessage.getBuffer() + InputMessage::DATA_POS, m_inputMessage.getMessageSize() - InputMessage::CHECKSUM_LENGTH);
|
||||||
|
if(m_inputMessage.getU32() != checksum) {
|
||||||
// error
|
// error
|
||||||
logError("Checksum is invalid.");
|
logError("Checksum is invalid.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(m_xteaEncryptionEnabled)
|
if(m_xteaEncryptionEnabled)
|
||||||
xteaDecrypt(inputMessage);
|
xteaDecrypt(&m_inputMessage);
|
||||||
|
|
||||||
|
onRecv(&m_inputMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Protocol::onError(const boost::system::error_code& err)
|
void Protocol::onError(const boost::system::error_code& err)
|
||||||
{
|
{
|
||||||
|
std::stringstream message;
|
||||||
|
|
||||||
logError("PROTOCOL ERROR: ", err.message());
|
logError("PROTOCOL ERROR: ", err.message());
|
||||||
|
message << "Boost error: " << err.message();
|
||||||
|
|
||||||
// invalid hostname
|
// invalid hostname
|
||||||
// connection timeouted
|
// connection timeouted
|
||||||
|
|
||||||
// displays a dialog, finish protocol
|
// displays a dialog, finish protocol
|
||||||
|
|
||||||
|
callField("onError", message.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Protocol::xteaDecrypt(InputMessage *inputMessage)
|
bool Protocol::xteaDecrypt(InputMessage *inputMessage)
|
||||||
|
@ -163,4 +188,3 @@ uint32 Protocol::getAdlerChecksum(uint8 *buffer, uint16 size)
|
||||||
|
|
||||||
return (b << 16) | a;
|
return (b << 16) | a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,22 +38,32 @@
|
||||||
|
|
||||||
//#define RSA "109120132967399429278860960508995541528237502902798129123468757937266291492576446330739696001110603907230888610072655818825358503429057592827629436413108566029093628212635953836686562675849720620786279431090218017681061521755056710823876476444260558147179707119674283982419152118103759076030616683978566631413"
|
//#define RSA "109120132967399429278860960508995541528237502902798129123468757937266291492576446330739696001110603907230888610072655818825358503429057592827629436413108566029093628212635953836686562675849720620786279431090218017681061521755056710823876476444260558147179707119674283982419152118103759076030616683978566631413"
|
||||||
|
|
||||||
|
class Protocol;
|
||||||
|
typedef std::shared_ptr<Protocol> ProtocolPtr;
|
||||||
|
|
||||||
class Protocol : public LuaObject
|
class Protocol : public LuaObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Protocol();
|
Protocol();
|
||||||
|
|
||||||
void connect(const std::string& host, uint16 port, const std::function<void()>& callback);
|
void connect(const std::string& host, uint16 port);
|
||||||
void send(OutputMessage *outputMessage);
|
void send(OutputMessage *outputMessage);
|
||||||
|
void recv();
|
||||||
|
void internalRecvHeader(uint8 *buffer, uint16 size);
|
||||||
|
void internalRecvData(uint8 *buffer, uint16 size);
|
||||||
|
|
||||||
virtual void onRecv(InputMessage *inputMessage);
|
virtual void onConnect() = 0;
|
||||||
|
virtual void onRecv(InputMessage *inputMessage) = 0;
|
||||||
virtual void onError(const boost::system::error_code& err);
|
virtual void onError(const boost::system::error_code& err);
|
||||||
|
|
||||||
|
ProtocolPtr asProtocol() { return std::static_pointer_cast<Protocol>(shared_from_this()); }
|
||||||
|
|
||||||
virtual const char* getLuaTypeName() const { return "Protocol"; }
|
virtual const char* getLuaTypeName() const { return "Protocol"; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint32 m_xteaKey[4];
|
uint32 m_xteaKey[4];
|
||||||
bool m_xteaEncryptionEnabled;
|
bool m_xteaEncryptionEnabled;
|
||||||
|
InputMessage m_inputMessage;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool xteaDecrypt(InputMessage *inputMessage);
|
bool xteaDecrypt(InputMessage *inputMessage);
|
||||||
|
@ -63,6 +73,4 @@ private:
|
||||||
ConnectionPtr m_connection;
|
ConnectionPtr m_connection;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::shared_ptr<Protocol> ProtocolPtr;
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -41,7 +41,7 @@ void ProtocolLogin::login(const std::string& accountName, const std::string& acc
|
||||||
//std::string host = "tecserver.zapto.org";
|
//std::string host = "tecserver.zapto.org";
|
||||||
uint16 port = 7171;
|
uint16 port = 7171;
|
||||||
|
|
||||||
connect(host, port, std::bind(&ProtocolLogin::onConnect, asProtocolLogin()));
|
connect(host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProtocolLogin::onConnect()
|
void ProtocolLogin::onConnect()
|
||||||
|
@ -55,11 +55,11 @@ void ProtocolLogin::sendPacket()
|
||||||
|
|
||||||
oMsg.addU8(0x01); // Protocol id
|
oMsg.addU8(0x01); // Protocol id
|
||||||
oMsg.addU16(0x02); // OS
|
oMsg.addU16(0x02); // OS
|
||||||
oMsg.addU16(874); // Client version
|
oMsg.addU16(910); // Client version
|
||||||
|
|
||||||
oMsg.addU32(0x4DBAA20B); // Data Signature
|
oMsg.addU32(0x4E12DAFF); // Data Signature
|
||||||
oMsg.addU32(0x4DAD1A32); // Sprite Signature
|
oMsg.addU32(0x4E12DB27); // Sprite Signature
|
||||||
oMsg.addU32(0x4DA2D2B5); // Picture Signature
|
oMsg.addU32(0x4E119CBF); // Picture Signature
|
||||||
|
|
||||||
oMsg.addU8(0); // First RSA byte must be 0x00 // 1
|
oMsg.addU8(0); // First RSA byte must be 0x00 // 1
|
||||||
|
|
||||||
|
@ -88,12 +88,12 @@ void ProtocolLogin::sendPacket()
|
||||||
send(&oMsg);
|
send(&oMsg);
|
||||||
|
|
||||||
m_xteaEncryptionEnabled = true;
|
m_xteaEncryptionEnabled = true;
|
||||||
|
|
||||||
|
recv();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProtocolLogin::onRecv(InputMessage *inputMessage)
|
void ProtocolLogin::onRecv(InputMessage *inputMessage)
|
||||||
{
|
{
|
||||||
Protocol::onRecv(inputMessage);
|
|
||||||
|
|
||||||
while(!inputMessage->end()) {
|
while(!inputMessage->end()) {
|
||||||
uint8 opt = inputMessage->getU8();
|
uint8 opt = inputMessage->getU8();
|
||||||
logDebug("opt:",(uint)opt);
|
logDebug("opt:",(uint)opt);
|
||||||
|
|
Loading…
Reference in New Issue