diff --git a/src/framework/net/connection.cpp b/src/framework/net/connection.cpp index 9654c399..4dea9d14 100644 --- a/src/framework/net/connection.cpp +++ b/src/framework/net/connection.cpp @@ -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 -#include -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& 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(port)); + asio::ip::tcp::resolver::query query(host, convert(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(); diff --git a/src/framework/net/connection.h b/src/framework/net/connection.h index 0a267bda..fbeb131d 100644 --- a/src/framework/net/connection.h +++ b/src/framework/net/connection.h @@ -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 -#include - -typedef std::function ErrorCallback; -typedef std::function RecvCallback; +#include "netdeclarations.h" class Connection : public std::enable_shared_from_this, boost::noncopyable { + typedef std::function ErrorCallback; + typedef std::function RecvCallback; + typedef std::function ConnectCallback; + public: Connection(); static void poll(); - void connect(const std::string& host, uint16 port, const std::function& 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 m_connectCallback; + ConnectCallback m_connectCallback; - boost::asio::deadline_timer m_timer; - boost::asio::ip::tcp::resolver m_resolver; - boost::asio::ip::tcp::socket m_socket; + 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 ConnectionPtr; - #endif diff --git a/src/framework/net/inputmessage.cpp b/src/framework/net/inputmessage.cpp index c2da5b5d..0e7cfc60 100644 --- a/src/framework/net/inputmessage.cpp +++ b/src/framework/net/inputmessage.cpp @@ -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 +#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; } diff --git a/src/framework/net/inputmessage.h b/src/framework/net/inputmessage.h index a762b638..d2d0d9e3 100644 --- a/src/framework/net/inputmessage.h +++ b/src/framework/net/inputmessage.h @@ -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 +#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; } diff --git a/src/framework/net/outputmessage.cpp b/src/framework/net/outputmessage.cpp index 8ab995b7..9c2c8df5 100644 --- a/src/framework/net/outputmessage.cpp +++ b/src/framework/net/outputmessage.cpp @@ -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 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; } diff --git a/src/framework/net/outputmessage.h b/src/framework/net/outputmessage.h index 36ac8399..2274e947 100644 --- a/src/framework/net/outputmessage.h +++ b/src/framework/net/outputmessage.h @@ -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 +#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; } diff --git a/src/framework/net/protocol.cpp b/src/framework/net/protocol.cpp index 2284fb9b..6e07866d 100644 --- a/src/framework/net/protocol.cpp +++ b/src/framework/net/protocol.cpp @@ -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 - -#include +#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) { diff --git a/src/framework/net/protocol.h b/src/framework/net/protocol.h index 36978452..9f9c1051 100644 --- a/src/framework/net/protocol.h +++ b/src/framework/net/protocol.h @@ -1,45 +1,11 @@ -/* 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 -#include -#include -#include