From 296d613f3b95b7d947401b4306af5dc0048f79dd Mon Sep 17 00:00:00 2001 From: Andre Antunes Date: Sat, 9 Apr 2011 22:26:57 -0300 Subject: [PATCH] adding net files --- src/framework/net/networkmessage.cpp | 178 +++++++++++++++++++++++++++ src/framework/net/networkmessage.h | 97 +++++++++++++++ src/framework/net/protocol.cpp | 50 ++++++++ src/framework/net/protocol.h | 48 ++++++++ src/main.cpp | 4 +- 5 files changed, 375 insertions(+), 2 deletions(-) create mode 100644 src/framework/net/networkmessage.cpp create mode 100644 src/framework/net/networkmessage.h create mode 100644 src/framework/net/protocol.cpp create mode 100644 src/framework/net/protocol.h diff --git a/src/framework/net/networkmessage.cpp b/src/framework/net/networkmessage.cpp new file mode 100644 index 00000000..38789abf --- /dev/null +++ b/src/framework/net/networkmessage.cpp @@ -0,0 +1,178 @@ +/* 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 "networkmessage.h" + +void NetworkMessage::updateHeaderLength() +{ + uint16 size = m_msgSize; + memcpy(m_msgBuf, &size, 2); +} + +bool NetworkMessage::canAdd(int size) { + return (size + m_readPos < NETWORKMESSAGE_MAXSIZE - 16); +} + +std::string NetworkMessage::getString() +{ + uint16 stringlen = getU16(); + if(stringlen >= (16384 - m_readPos)) + return std::string(); + + char* v = (char*)(m_msgBuf + m_readPos); + m_readPos += stringlen; + return std::string(v, stringlen); +} + +std::string NetworkMessage::getRaw() +{ + uint16 stringlen = m_msgSize - m_readPos; + if(stringlen >= (16384 - m_readPos)) + return std::string(); + + char* v = (char*)(m_msgBuf + m_readPos); + m_readPos += stringlen; + return std::string(v, stringlen); +} + +void NetworkMessage::addString(const char* value) +{ + uint32 stringlen = (uint32)strlen(value); + if(!canAdd(stringlen + 2) || stringlen > 8192) + return; + + addU16(stringlen); + strcpy((char*)(m_msgBuf + m_readPos), value); + m_readPos += stringlen; + m_msgSize += stringlen; +} + +void NetworkMessage::addBytes(const char* bytes, uint32 size) +{ + if(!canAdd(size) || size > 8192) + return; + + memcpy(m_msgBuf + m_readPos, bytes, size); + m_readPos += size; + m_msgSize += size; +} + +void NetworkMessage::addPaddingBytes(uint32 n) +{ + if(!canAdd(n)) + return; + + memset((void*)&m_msgBuf[m_readPos], 0x33, n); + m_msgSize = m_msgSize + n; +} + +void NetworkMessage::skipBytes(int count) { + m_readPos += count; +} + +// simply write functions for outgoing message +void NetworkMessage::addByte(uint8 value) { + if(!canAdd(1)) + return; + + m_msgBuf[m_readPos++] = value; + m_msgSize++; +} + +void NetworkMessage::addU16(uint16 value) { + if(!canAdd(2)) + return; + + *(uint16*)(m_msgBuf + m_readPos) = value; + m_readPos += 2; + m_msgSize += 2; +} + +void NetworkMessage::addU32(uint32 value) { + if(!canAdd(4)) + return; + + *(uint32*)(m_msgBuf + m_readPos) = value; + m_readPos += 4; + m_msgSize += 4; +} + +void NetworkMessage::addU64(uint64 value) { + if(!canAdd(8)) + return; + + *(uint64*)(m_msgBuf + m_readPos) = value; + m_readPos += 8; + m_msgSize += 8; +} + +void NetworkMessage::addString(const std::string &value) { + addString(value.c_str()); +} + +int32 NetworkMessage::getMessageLength() const { + return m_msgSize; +} + +void NetworkMessage::setMessageLength(int32 newSize) { + m_msgSize = newSize; +} + +int32 NetworkMessage::getReadPos() const { + return m_readPos; +} + +uint8 NetworkMessage::getByte() +{ + return m_msgBuf[m_readPos++]; +} + +uint16 NetworkMessage::getU16() +{ + uint16 v = *(uint16*)(m_msgBuf + m_readPos); + m_readPos += 2; + return v; +} + +uint32 NetworkMessage::getU32() +{ + uint32 v = *(uint32*)(m_msgBuf + m_readPos); + m_readPos += 4; + return v; +} + +uint64 NetworkMessage::getU64() +{ + uint64 v = *(uint64*)(m_msgBuf + m_readPos); + m_readPos += 8; + return v; +} + +char* NetworkMessage::getBuffer() { + return (char*)&m_msgBuf[0]; +} + +char* NetworkMessage::getBodyBuffer() { + m_readPos = 2; + return (char*)&m_msgBuf[header_length]; +} diff --git a/src/framework/net/networkmessage.h b/src/framework/net/networkmessage.h new file mode 100644 index 00000000..29d75e08 --- /dev/null +++ b/src/framework/net/networkmessage.h @@ -0,0 +1,97 @@ +/* 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 NETWORKMESSAGE_H +#define NETWORKMESSAGE_H + +#include "../prerequisites.h" + +class Rsa; + +class NetworkMessage +{ +public: + enum { + header_length = 2, + NETWORKMESSAGE_MAXSIZE = 1500 + }; + + enum { + max_body_length = NETWORKMESSAGE_MAXSIZE - header_length + }; + + // constructor/destructor + NetworkMessage() { + reset(); + } + + // resets the internal buffer to an empty message +protected: + void reset() { + m_msgSize = 0; + m_readPos = 2; + } +public: + // simply read functions for incoming message + uint8 getByte(); + uint16 getU16(); + uint32 getU32(); + uint64 getU64(); + + std::string getString(); + std::string getRaw(); + + // skips count unknown/unused bytes in an incoming message + void skipBytes(int count); + + // simply write functions for outgoing message + void addByte(uint8 value); + void addU16(uint16 value); + void addU32(uint32 value); + void addU64(uint64 value); + void addBytes(const char* bytes, uint32_t size); + void addPaddingBytes(uint32 n); + void addString(const std::string &value); + void addString(const char* value); + int32 getMessageLength() const; + + void setMessageLength(int32 newSize); + int32 getReadPos() const; + int32 getHeaderSize(); + char* getBuffer(); + char* getBodyBuffer(); + + void updateHeaderLength(); + +protected: + inline bool canAdd(int size); + + int32 m_msgSize; + int32 m_readPos; + + uint8 m_msgBuf[NETWORKMESSAGE_MAXSIZE]; +}; + +typedef std::shared_ptr NetworkMessagePtr; + +#endif //NETWORKMESSAGE_H \ No newline at end of file diff --git a/src/framework/net/protocol.cpp b/src/framework/net/protocol.cpp new file mode 100644 index 00000000..dbf5aefe --- /dev/null +++ b/src/framework/net/protocol.cpp @@ -0,0 +1,50 @@ +/* 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 "protocol.h" +#include "connections.h" + +Protocol::Protocol() +{ + m_connection = g_connections.createConnection(); + m_connection->setErrorCallback( + [this](const boost::system::error_code& error, const std::string& msg){ + this->onError(error, msg); + } + ); +} + +void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend) +{ + m_connection->send(networkMessage, onSend); +} + +bool Protocol::connect(const std::string& ip, uint16 port, Connection::ConnectionCallback onConnect) +{ + return m_connection->connect(ip, port, onConnect); +} + +void Protocol::recv(Connection::RecvCallback onRecv) +{ + m_connection->recv(onRecv); +} diff --git a/src/framework/net/protocol.h b/src/framework/net/protocol.h new file mode 100644 index 00000000..38539a79 --- /dev/null +++ b/src/framework/net/protocol.h @@ -0,0 +1,48 @@ +/* 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 "../prerequisites.h" +#include "connection.h" + +class Protocol +{ +public: + Protocol(); + + virtual void begin() = 0; + +protected: + void send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend); + void recv(Connection::RecvCallback onRecv); + + bool connect(const std::string& ip, uint16 port, Connection::ConnectionCallback onConnect); + + virtual void onError(const boost::system::error_code& error, const std::string& msg) = 0; + + ConnectionPtr m_connection; +}; + +#endif //PROTOCOL_H diff --git a/src/main.cpp b/src/main.cpp index ba41376d..e94ece8a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -109,8 +109,8 @@ int main(int argc, const char *argv[]) // state scope { - //std::shared_ptr initialState(new MenuState); - std::shared_ptr initialState(new TestState); + std::shared_ptr initialState(new MenuState); + //std::shared_ptr initialState(new TestState); g_engine.changeState(initialState.get()); Platform::showWindow();