some network refactoring
This commit is contained in:
parent
a62785911a
commit
ea2645c151
|
@ -1,30 +1,8 @@
|
|||
/* 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 "connection.h"
|
||||
|
||||
#include <core/dispatcher.h>
|
||||
#include <net/connection.h>
|
||||
|
||||
static boost::asio::io_service ioService;
|
||||
static asio::io_service ioService;
|
||||
|
||||
Connection::Connection() :
|
||||
m_timer(ioService),
|
||||
|
@ -39,21 +17,21 @@ void Connection::poll()
|
|||
ioService.reset();
|
||||
}
|
||||
|
||||
void Connection::connect(const std::string& host, uint16 port, const std::function<void()>& connectCallback)
|
||||
void Connection::connect(const std::string& host, uint16 port, const ConnectCallback& connectCallback)
|
||||
{
|
||||
m_connectCallback = connectCallback;
|
||||
|
||||
boost::asio::ip::tcp::resolver::query query(host, convert<std::string>(port));
|
||||
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_timer.expires_from_now(boost::posix_time::seconds(2));
|
||||
m_timer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||
}
|
||||
|
||||
void Connection::send(uint8 *buffer, uint16 size)
|
||||
void Connection::send(uint8* buffer, uint16 size)
|
||||
{
|
||||
boost::asio::async_write(m_socket,
|
||||
boost::asio::buffer(buffer, size),
|
||||
asio::async_write(m_socket,
|
||||
asio::buffer(buffer, size),
|
||||
std::bind(&Connection::onSend, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
|
||||
|
||||
m_timer.expires_from_now(boost::posix_time::seconds(2));
|
||||
|
@ -65,8 +43,8 @@ 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),
|
||||
asio::async_read(m_socket,
|
||||
asio::buffer(m_recvBuffer, bytes),
|
||||
std::bind(&Connection::onRecv, shared_from_this(), std::placeholders::_1));
|
||||
|
||||
if(timeout > 0) {
|
||||
|
@ -77,11 +55,11 @@ void Connection::recv(uint16 bytes, uint32 timeout, const RecvCallback& callback
|
|||
|
||||
void Connection::onTimeout(const boost::system::error_code& error)
|
||||
{
|
||||
if(error != boost::asio::error::operation_aborted)
|
||||
if(error != asio::error::operation_aborted)
|
||||
g_dispatcher.addTask(std::bind(m_errorCallback, error));
|
||||
}
|
||||
|
||||
void Connection::onResolve(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIterator)
|
||||
void Connection::onResolve(const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator)
|
||||
{
|
||||
m_timer.cancel();
|
||||
|
||||
|
|
|
@ -1,69 +1,44 @@
|
|||
/* 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.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef CONNECTION_H
|
||||
#define CONNECTION_H
|
||||
|
||||
#include <global.h>
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
typedef std::function<void(boost::system::error_code&)> ErrorCallback;
|
||||
typedef std::function<void(uint8*, uint16)> RecvCallback;
|
||||
#include "netdeclarations.h"
|
||||
|
||||
class Connection : public std::enable_shared_from_this<Connection>, boost::noncopyable
|
||||
{
|
||||
typedef std::function<void(boost::system::error_code&)> ErrorCallback;
|
||||
typedef std::function<void(uint8*, uint16)> RecvCallback;
|
||||
typedef std::function<void()> ConnectCallback;
|
||||
|
||||
public:
|
||||
Connection();
|
||||
|
||||
static void poll();
|
||||
|
||||
void connect(const std::string& host, uint16 port, const std::function<void()>& connectCallback);
|
||||
void send(uint8 *buffer, uint16 size);
|
||||
void connect(const std::string& host, uint16 port, const ConnectCallback& connectCallback);
|
||||
void send(uint8* buffer, uint16 size);
|
||||
void recv(uint16 bytes, uint32 timeout, const RecvCallback& callback);
|
||||
|
||||
void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; }
|
||||
void setRecvCallback(const RecvCallback& recvCallback) { m_recvCallback = recvCallback; }
|
||||
|
||||
private:
|
||||
void onTimeout(const boost::system::error_code& error);
|
||||
void onResolve(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIterator);
|
||||
void onResolve(const boost::system::error_code& error, 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 onRecv(const boost::system::error_code& error);
|
||||
|
||||
private:
|
||||
ErrorCallback m_errorCallback;
|
||||
std::function<void()> m_connectCallback;
|
||||
|
||||
boost::asio::deadline_timer m_timer;
|
||||
boost::asio::ip::tcp::resolver m_resolver;
|
||||
boost::asio::ip::tcp::socket m_socket;
|
||||
ErrorCallback m_errorCallback;
|
||||
ConnectCallback m_connectCallback;
|
||||
|
||||
asio::deadline_timer m_timer;
|
||||
asio::ip::tcp::resolver m_resolver;
|
||||
asio::ip::tcp::socket m_socket;
|
||||
|
||||
uint8 m_recvBuffer[65536];
|
||||
uint16 m_recvSize;
|
||||
RecvCallback m_recvCallback;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<Connection> ConnectionPtr;
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,28 +1,4 @@
|
|||
/* 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 <net/inputmessage.h>
|
||||
#include "inputmessage.h"
|
||||
|
||||
InputMessage::InputMessage()
|
||||
{
|
||||
|
@ -37,17 +13,13 @@ void InputMessage::reset()
|
|||
|
||||
uint8 InputMessage::getU8()
|
||||
{
|
||||
if(!canRead(1))
|
||||
return 0;
|
||||
|
||||
assert(canRead(1));
|
||||
return m_buffer[m_readPos++];
|
||||
}
|
||||
|
||||
uint16 InputMessage::getU16()
|
||||
{
|
||||
if(!canRead(2))
|
||||
return 0;
|
||||
|
||||
assert(canRead(2));
|
||||
uint16 v = *(uint16_t*)(m_buffer + m_readPos);
|
||||
m_readPos += 2;
|
||||
return v;
|
||||
|
@ -55,9 +27,7 @@ uint16 InputMessage::getU16()
|
|||
|
||||
uint32 InputMessage::getU32()
|
||||
{
|
||||
if(!canRead(4))
|
||||
return 0;
|
||||
|
||||
assert(canRead(4));
|
||||
uint32 v = *(uint32*)(m_buffer + m_readPos);
|
||||
m_readPos += 4;
|
||||
return v;
|
||||
|
@ -65,9 +35,7 @@ uint32 InputMessage::getU32()
|
|||
|
||||
uint64 InputMessage::getU64()
|
||||
{
|
||||
if(!canRead(8))
|
||||
return 0;
|
||||
|
||||
assert(canRead(8));
|
||||
uint64 v = *(uint64*)(m_buffer + m_readPos);
|
||||
m_readPos += 8;
|
||||
return v;
|
||||
|
@ -76,9 +44,7 @@ uint64 InputMessage::getU64()
|
|||
std::string InputMessage::getString()
|
||||
{
|
||||
uint16 stringLength = getU16();
|
||||
if(!canRead(stringLength))
|
||||
return std::string();
|
||||
|
||||
assert(canRead(stringLength));
|
||||
char* v = (char*)(m_buffer + m_readPos);
|
||||
m_readPos += stringLength;
|
||||
return std::string(v, stringLength);
|
||||
|
@ -87,6 +53,6 @@ std::string InputMessage::getString()
|
|||
bool InputMessage::canRead(int bytes)
|
||||
{
|
||||
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 false;
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -1,31 +1,7 @@
|
|||
/* 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.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef INPUTMESSAGE_H
|
||||
#define INPUTMESSAGE_H
|
||||
|
||||
#include <global.h>
|
||||
#include "netdeclarations.h"
|
||||
|
||||
class InputMessage
|
||||
{
|
||||
|
@ -50,7 +26,7 @@ public:
|
|||
uint64 getU64();
|
||||
std::string getString();
|
||||
|
||||
uint8 *getBuffer() { return m_buffer; }
|
||||
uint8* getBuffer() { return m_buffer; }
|
||||
uint16 getMessageSize() { return m_messageSize; }
|
||||
void setMessageSize(uint16 messageSize) { m_messageSize = messageSize; }
|
||||
bool end() { return m_readPos == m_messageSize; }
|
||||
|
|
|
@ -1,27 +1,3 @@
|
|||
/* 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 <net/outputmessage.h>
|
||||
|
||||
OutputMessage::OutputMessage()
|
||||
|
@ -37,9 +13,7 @@ void OutputMessage::reset()
|
|||
|
||||
void OutputMessage::addU8(uint8 value)
|
||||
{
|
||||
if(!canWrite(1))
|
||||
return;
|
||||
|
||||
assert(canWrite(1));
|
||||
m_buffer[m_writePos] = value;
|
||||
m_writePos += 1;
|
||||
m_messageSize += 1;
|
||||
|
@ -47,9 +21,7 @@ void OutputMessage::addU8(uint8 value)
|
|||
|
||||
void OutputMessage::addU16(uint16 value)
|
||||
{
|
||||
if(!canWrite(2))
|
||||
return;
|
||||
|
||||
assert(canWrite(2));
|
||||
*(uint16_t*)(m_buffer + m_writePos) = value;
|
||||
m_writePos += 2;
|
||||
m_messageSize += 2;
|
||||
|
@ -57,9 +29,7 @@ void OutputMessage::addU16(uint16 value)
|
|||
|
||||
void OutputMessage::addU32(uint32 value)
|
||||
{
|
||||
if(!canWrite(4))
|
||||
return;
|
||||
|
||||
assert(canWrite(4));
|
||||
*(uint32*)(m_buffer + m_writePos) = value;
|
||||
m_writePos += 4;
|
||||
m_messageSize += 4;
|
||||
|
@ -67,9 +37,7 @@ void OutputMessage::addU32(uint32 value)
|
|||
|
||||
void OutputMessage::addU64(uint64 value)
|
||||
{
|
||||
if(!canWrite(8))
|
||||
return;
|
||||
|
||||
assert(canWrite(8));
|
||||
*(uint64*)(m_buffer + m_writePos) = value;
|
||||
m_writePos += 8;
|
||||
m_messageSize += 8;
|
||||
|
@ -78,9 +46,7 @@ void OutputMessage::addU64(uint64 value)
|
|||
void OutputMessage::addString(const char* value)
|
||||
{
|
||||
size_t stringLength = strlen(value);
|
||||
if(stringLength > 0xFFFF || !canWrite(stringLength + 2))
|
||||
return;
|
||||
|
||||
assert(stringLength < 0xFFFF && canWrite(stringLength + 2));
|
||||
addU16(stringLength);
|
||||
strcpy((char*)(m_buffer + m_writePos), value);
|
||||
m_writePos += stringLength;
|
||||
|
@ -94,9 +60,7 @@ void OutputMessage::addString(const std::string &value)
|
|||
|
||||
void OutputMessage::addPaddingBytes(int bytes, uint8 byte)
|
||||
{
|
||||
if(!canWrite(bytes))
|
||||
return;
|
||||
|
||||
assert(canWrite(bytes) && bytes >= 0);
|
||||
memset((void*)&m_buffer[m_writePos], byte, bytes);
|
||||
m_writePos += bytes;
|
||||
m_messageSize += bytes;
|
||||
|
@ -105,6 +69,6 @@ void OutputMessage::addPaddingBytes(int bytes, uint8 byte)
|
|||
bool OutputMessage::canWrite(int bytes)
|
||||
{
|
||||
if(m_writePos + bytes > BUFFER_MAXSIZE)
|
||||
logFatal("[OutputMessage::canWrite()]: Can't write. Write position has reached buffer's maxium size.");
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -1,31 +1,7 @@
|
|||
/* 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.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef OUTPUTMESSAGE_H
|
||||
#define OUTPUTMESSAGE_H
|
||||
|
||||
#include <global.h>
|
||||
#include "netdeclarations.h"
|
||||
|
||||
class OutputMessage
|
||||
{
|
||||
|
@ -51,7 +27,7 @@ public:
|
|||
void addString(const std::string &value);
|
||||
void addPaddingBytes(int bytes, uint8 byte = 0);
|
||||
|
||||
uint8 *getBuffer() { return m_buffer; }
|
||||
uint8* getBuffer() { return m_buffer; }
|
||||
uint16 getMessageSize() { return m_messageSize; }
|
||||
void setWritePos(uint16 writePos) { m_writePos = writePos; }
|
||||
|
||||
|
|
|
@ -1,30 +1,5 @@
|
|||
/* 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 <net/protocol.h>
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
#include "protocol.h"
|
||||
#include "connection.h"
|
||||
|
||||
Protocol::Protocol() :
|
||||
m_connection(new Connection)
|
||||
|
@ -38,7 +13,7 @@ void Protocol::connect(const std::string& host, uint16 port)
|
|||
m_connection->connect(host, port, std::bind(&Protocol::onConnect, asProtocol()));
|
||||
}
|
||||
|
||||
void Protocol::send(OutputMessage *outputMessage)
|
||||
void Protocol::send(OutputMessage* outputMessage)
|
||||
{
|
||||
// Encrypt
|
||||
if(m_xteaEncryptionEnabled)
|
||||
|
@ -65,7 +40,7 @@ void Protocol::recv()
|
|||
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)
|
||||
void Protocol::internalRecvHeader(uint8* buffer, uint16 size)
|
||||
{
|
||||
memcpy(m_inputMessage.getBuffer() + InputMessage::HEADER_POS, buffer, size);
|
||||
|
||||
|
@ -75,7 +50,7 @@ void Protocol::internalRecvHeader(uint8 *buffer, uint16 size)
|
|||
m_connection->recv(dataSize, 5, std::bind(&Protocol::internalRecvData, asProtocol(), std::placeholders::_1, std::placeholders::_2));
|
||||
}
|
||||
|
||||
void Protocol::internalRecvData(uint8 *buffer, uint16 size)
|
||||
void Protocol::internalRecvData(uint8* buffer, uint16 size)
|
||||
{
|
||||
memcpy(m_inputMessage.getBuffer() + InputMessage::CHECKSUM_POS, buffer, size);
|
||||
|
||||
|
@ -107,7 +82,7 @@ void Protocol::onError(const boost::system::error_code& err)
|
|||
callField("onError", message.str());
|
||||
}
|
||||
|
||||
bool Protocol::xteaDecrypt(InputMessage *inputMessage)
|
||||
bool Protocol::xteaDecrypt(InputMessage* inputMessage)
|
||||
{
|
||||
// FIXME: this function has not been tested yet
|
||||
uint16 messageSize = inputMessage->getMessageSize() - InputMessage::CHECKSUM_LENGTH;
|
||||
|
@ -143,7 +118,7 @@ bool Protocol::xteaDecrypt(InputMessage *inputMessage)
|
|||
return true;
|
||||
}
|
||||
|
||||
void Protocol::xteaEncrypt(OutputMessage *outputMessage)
|
||||
void Protocol::xteaEncrypt(OutputMessage* outputMessage)
|
||||
{
|
||||
uint16 messageLength = outputMessage->getMessageSize();
|
||||
|
||||
|
@ -171,7 +146,7 @@ void Protocol::xteaEncrypt(OutputMessage *outputMessage)
|
|||
}
|
||||
}
|
||||
|
||||
uint32 Protocol::getAdlerChecksum(uint8 *buffer, uint16 size)
|
||||
uint32 Protocol::getAdlerChecksum(uint8* buffer, uint16 size)
|
||||
{
|
||||
uint32 a = 1, b = 0;
|
||||
while (size > 0) {
|
||||
|
|
|
@ -1,59 +1,25 @@
|
|||
/* 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.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef PROTOCOL_H
|
||||
#define PROTOCOL_H
|
||||
|
||||
#include <net/connection.h>
|
||||
#include <net/inputmessage.h>
|
||||
#include <net/outputmessage.h>
|
||||
#include "netdeclarations.h"
|
||||
#include "inputmessage.h"
|
||||
#include "outputmessage.h"
|
||||
|
||||
#include <script/luaobject.h>
|
||||
|
||||
#define CIPSOFT_PUBLIC_RSA "1321277432058722840622950990822933849527763264961655079678763618" \
|
||||
"4334395343554449668205332383339435179772895415509701210392836078" \
|
||||
"6959821132214473291575712138800495033169914814069637740318278150" \
|
||||
"2907336840325241747827401343576296990629870233111328210165697754" \
|
||||
"88792221429527047321331896351555606801473202394175817"
|
||||
|
||||
//#define RSA "109120132967399429278860960508995541528237502902798129123468757937266291492576446330739696001110603907230888610072655818825358503429057592827629436413108566029093628212635953836686562675849720620786279431090218017681061521755056710823876476444260558147179707119674283982419152118103759076030616683978566631413"
|
||||
|
||||
class Protocol;
|
||||
typedef std::shared_ptr<Protocol> ProtocolPtr;
|
||||
|
||||
class Protocol : public LuaObject
|
||||
{
|
||||
public:
|
||||
Protocol();
|
||||
|
||||
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);
|
||||
void internalRecvHeader(uint8* buffer, uint16 size);
|
||||
void internalRecvData(uint8* buffer, uint16 size);
|
||||
|
||||
virtual void onConnect() = 0;
|
||||
virtual void onRecv(InputMessage *inputMessage) = 0;
|
||||
virtual void onRecv(InputMessage* inputMessage) = 0;
|
||||
virtual void onError(const boost::system::error_code& err);
|
||||
|
||||
ProtocolPtr asProtocol() { return std::static_pointer_cast<Protocol>(shared_from_this()); }
|
||||
|
@ -66,9 +32,9 @@ protected:
|
|||
InputMessage m_inputMessage;
|
||||
|
||||
private:
|
||||
bool xteaDecrypt(InputMessage *inputMessage);
|
||||
void xteaEncrypt(OutputMessage *outputMessage);
|
||||
uint32 getAdlerChecksum(uint8 *buffer, uint16 size);
|
||||
bool xteaDecrypt(InputMessage* inputMessage);
|
||||
void xteaEncrypt(OutputMessage* outputMessage);
|
||||
uint32 getAdlerChecksum(uint8* buffer, uint16 size);
|
||||
|
||||
ConnectionPtr m_connection;
|
||||
};
|
||||
|
|
|
@ -1,27 +1,3 @@
|
|||
/* 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 <global.h>
|
||||
#include "rsa.h"
|
||||
|
||||
Rsa::Rsa()
|
||||
|
|
|
@ -1,31 +1,7 @@
|
|||
/* 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.
|
||||
*/
|
||||
|
||||
#ifndef RSA_H
|
||||
#define RSA_H
|
||||
|
||||
#include <global.h>
|
||||
|
||||
#include <gmp.h>
|
||||
|
||||
class Rsa
|
||||
|
|
|
@ -194,7 +194,7 @@ void LuaState::pushValue(const LuaValuePtr& value)
|
|||
if(value->isNil())
|
||||
pushNil();
|
||||
else
|
||||
pushRef(value->ref);
|
||||
pushRef(value->m_ref);
|
||||
}
|
||||
|
||||
void LuaState::pushValues(const LuaValueList& values)
|
||||
|
|
|
@ -4,14 +4,14 @@
|
|||
|
||||
#include <core/resources.h>
|
||||
|
||||
LuaValue::LuaValue(int ref) : ref(ref)
|
||||
LuaValue::LuaValue(int ref) : m_ref(ref)
|
||||
{
|
||||
}
|
||||
|
||||
LuaValue::~LuaValue()
|
||||
{
|
||||
// releases the reference of this value from lua
|
||||
g_lua.unref(ref);
|
||||
g_lua.unref(m_ref);
|
||||
}
|
||||
|
||||
LuaValueList LuaValue::call(const std::string& funcName, const LuaValueList& args)
|
||||
|
|
|
@ -49,7 +49,7 @@ public:
|
|||
std::string getTypeName();
|
||||
|
||||
// check types
|
||||
bool isNil() { return ref < 0; }
|
||||
bool isNil() { return m_ref < 0; }
|
||||
bool isBoolean();
|
||||
bool isString();
|
||||
bool isNumber();
|
||||
|
@ -70,7 +70,7 @@ private:
|
|||
/// Pushes this value into lua stack
|
||||
void push();
|
||||
|
||||
int ref;
|
||||
int m_ref;
|
||||
friend class LuaState;
|
||||
};
|
||||
|
||||
|
|
|
@ -92,7 +92,7 @@ void ProtocolLogin::sendPacket()
|
|||
recv();
|
||||
}
|
||||
|
||||
void ProtocolLogin::onRecv(InputMessage *inputMessage)
|
||||
void ProtocolLogin::onRecv(InputMessage* inputMessage)
|
||||
{
|
||||
while(!inputMessage->end()) {
|
||||
uint8 opt = inputMessage->getU8();
|
||||
|
@ -119,19 +119,19 @@ void ProtocolLogin::onRecv(InputMessage *inputMessage)
|
|||
}
|
||||
}
|
||||
|
||||
void ProtocolLogin::parseError(InputMessage *inputMessage)
|
||||
void ProtocolLogin::parseError(InputMessage* inputMessage)
|
||||
{
|
||||
std::string error = inputMessage->getString();
|
||||
callField("onError", error);
|
||||
}
|
||||
|
||||
void ProtocolLogin::parseMOTD(InputMessage *inputMessage)
|
||||
void ProtocolLogin::parseMOTD(InputMessage* inputMessage)
|
||||
{
|
||||
std::string motd = inputMessage->getString();
|
||||
callField("onMotd", motd);
|
||||
}
|
||||
|
||||
void ProtocolLogin::parseCharacterList(InputMessage *inputMessage)
|
||||
void ProtocolLogin::parseCharacterList(InputMessage* inputMessage)
|
||||
{
|
||||
uint8 characters = inputMessage->getU8();
|
||||
for(int i = 0; i < characters; ++i) {
|
||||
|
|
|
@ -19,7 +19,7 @@ public:
|
|||
void onConnect();
|
||||
|
||||
void sendPacket();
|
||||
void onRecv(InputMessage *inputMessage);
|
||||
void onRecv(InputMessage* inputMessage);
|
||||
|
||||
void cancel() { /* TODO: this func */ }
|
||||
|
||||
|
@ -28,9 +28,9 @@ public:
|
|||
virtual const char* getLuaTypeName() const { return "ProtocolLogin"; }
|
||||
|
||||
private:
|
||||
void parseError(InputMessage *inputMessage);
|
||||
void parseMOTD(InputMessage *inputMessage);
|
||||
void parseCharacterList(InputMessage *inputMessage);
|
||||
void parseError(InputMessage* inputMessage);
|
||||
void parseMOTD(InputMessage* inputMessage);
|
||||
void parseCharacterList(InputMessage* inputMessage);
|
||||
|
||||
std::string m_accountName, m_accountPassword;
|
||||
|
||||
|
|
Loading…
Reference in New Issue