mirror of
https://github.com/edubart/otclient.git
synced 2025-11-30 23:26:51 +01:00
net improvments
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
#include <array>
|
||||
#include <iomanip>
|
||||
#include <unordered_map>
|
||||
#include <random>
|
||||
|
||||
// boost utilities
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
@@ -42,6 +42,8 @@ void Connection::close()
|
||||
if(!m_connected)
|
||||
return;
|
||||
|
||||
m_connected = false;
|
||||
|
||||
m_readTimer.cancel();
|
||||
m_writeTimer.cancel();
|
||||
|
||||
@@ -50,16 +52,18 @@ void Connection::close()
|
||||
m_socket.close();
|
||||
}
|
||||
|
||||
m_connected = false;
|
||||
m_connectCallback = nullptr;
|
||||
m_errorCallback = nullptr;
|
||||
m_recvCallback = nullptr;
|
||||
}
|
||||
|
||||
void Connection::write(uint8* buffer, uint16 size)
|
||||
{
|
||||
m_writeTimer.cancel();
|
||||
|
||||
if(!m_connected)
|
||||
return;
|
||||
|
||||
m_writeTimer.cancel();
|
||||
|
||||
asio::async_write(m_socket,
|
||||
asio::buffer(buffer, size),
|
||||
std::bind(&Connection::onWrite, shared_from_this(), _1, _2));
|
||||
@@ -102,6 +106,7 @@ void Connection::onResolve(const boost::system::error_code& error, asio::ip::tcp
|
||||
void Connection::onConnect(const boost::system::error_code& error)
|
||||
{
|
||||
m_readTimer.cancel();
|
||||
|
||||
m_connected = true;
|
||||
|
||||
if(!error) {
|
||||
@@ -115,6 +120,9 @@ void Connection::onWrite(const boost::system::error_code& error, size_t)
|
||||
{
|
||||
m_writeTimer.cancel();
|
||||
|
||||
if(!m_connected)
|
||||
return;
|
||||
|
||||
if(error)
|
||||
handleError(error);
|
||||
}
|
||||
@@ -123,6 +131,9 @@ void Connection::onRecv(const boost::system::error_code& error)
|
||||
{
|
||||
m_readTimer.cancel();
|
||||
|
||||
if(!m_connected)
|
||||
return;
|
||||
|
||||
if(!error) {
|
||||
if(m_recvCallback)
|
||||
g_dispatcher.addEvent(std::bind(m_recvCallback, m_recvBuffer, m_recvSize));
|
||||
@@ -133,14 +144,16 @@ void Connection::onRecv(const boost::system::error_code& error)
|
||||
void Connection::onTimeout(const boost::system::error_code& error)
|
||||
{
|
||||
if(error != asio::error::operation_aborted)
|
||||
handleError(error);
|
||||
handleError(asio::error::timed_out);
|
||||
}
|
||||
|
||||
void Connection::handleError(const boost::system::error_code& error)
|
||||
{
|
||||
logTraceDebug(error.message());
|
||||
close();
|
||||
if(m_errorCallback)
|
||||
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
|
||||
if(error != asio::error::operation_aborted) {
|
||||
if(m_errorCallback)
|
||||
g_dispatcher.addEvent(std::bind(m_errorCallback, error));
|
||||
if(m_connected)
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
#include "protocol.h"
|
||||
#include "connection.h"
|
||||
|
||||
Protocol::Protocol() : m_connection(new Connection)
|
||||
Protocol::Protocol()
|
||||
{
|
||||
m_connection->setErrorCallback(std::bind(&Protocol::onError, this, _1));
|
||||
m_xteaEncryptionEnabled = false;
|
||||
m_checksumEnabled = true;
|
||||
m_checksumEnabled = false;
|
||||
}
|
||||
|
||||
void Protocol::connect(const std::string& host, uint16 port)
|
||||
{
|
||||
m_connection = ConnectionPtr(new Connection);
|
||||
m_connection->setErrorCallback(std::bind(&Protocol::onError, asProtocol(), _1));
|
||||
m_connection->connect(host, port, std::bind(&Protocol::onConnect, asProtocol()));
|
||||
}
|
||||
|
||||
void Protocol::disconnect()
|
||||
{
|
||||
m_connection->close();
|
||||
m_connection.reset();
|
||||
}
|
||||
|
||||
void Protocol::send(OutputMessage& outputMessage)
|
||||
@@ -76,6 +78,16 @@ void Protocol::internalRecvData(uint8* buffer, uint16 size)
|
||||
onRecv(m_inputMessage);
|
||||
}
|
||||
|
||||
void Protocol::generateXteaKey()
|
||||
{
|
||||
std::mt19937 eng(std::time(NULL));
|
||||
std::uniform_int_distribution<uint32> unif(0, 0xFFFFFFFF);
|
||||
m_xteaKey[0] = unif(eng);
|
||||
m_xteaKey[1] = unif(eng);
|
||||
m_xteaKey[2] = unif(eng);
|
||||
m_xteaKey[3] = unif(eng);
|
||||
}
|
||||
|
||||
bool Protocol::xteaDecrypt(InputMessage& inputMessage)
|
||||
{
|
||||
// FIXME: this function has not been tested yet
|
||||
|
||||
@@ -28,8 +28,11 @@ public:
|
||||
ProtocolPtr asProtocol() { return std::static_pointer_cast<Protocol>(shared_from_this()); }
|
||||
|
||||
protected:
|
||||
void enableChecksum() { m_checksumEnabled = true; }
|
||||
void enableXteaEncryption() { m_xteaEncryptionEnabled = true; }
|
||||
void generateXteaKey();
|
||||
|
||||
uint32 m_xteaKey[4];
|
||||
bool m_checksumEnabled, m_xteaEncryptionEnabled;
|
||||
InputMessage m_inputMessage;
|
||||
|
||||
private:
|
||||
@@ -37,6 +40,8 @@ private:
|
||||
void xteaEncrypt(OutputMessage& outputMessage);
|
||||
uint32 getAdlerChecksum(uint8* buffer, uint16 size);
|
||||
|
||||
bool m_checksumEnabled;
|
||||
bool m_xteaEncryptionEnabled;
|
||||
ConnectionPtr m_connection;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user