remove bugged net files
This commit is contained in:
parent
5be199e566
commit
3de455fe7e
|
@ -1,272 +0,0 @@
|
||||||
/* 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 <prerequisites.h>
|
|
||||||
#include <core/dispatcher.h>
|
|
||||||
#include <net/connection.h>
|
|
||||||
|
|
||||||
static boost::asio::io_service ioService;
|
|
||||||
|
|
||||||
Connection::Connection() :
|
|
||||||
m_socket(ioService),
|
|
||||||
m_resolver(ioService),
|
|
||||||
m_writeError(false),
|
|
||||||
m_readError(false),
|
|
||||||
m_readTimer(ioService),
|
|
||||||
m_writeTimer(ioService),
|
|
||||||
m_state(STATE_CLOSED)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
Connection::~Connection()
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::poll()
|
|
||||||
{
|
|
||||||
ioService.poll();
|
|
||||||
ioService.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::close()
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
ioService.post(boost::bind(&Connection::internalCloseConnection, shared_from_this()));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::internalCloseConnection()
|
|
||||||
{
|
|
||||||
if(m_state != STATE_CLOSED) {
|
|
||||||
m_pendingRead = 0;
|
|
||||||
m_pendingWrite = 0;
|
|
||||||
|
|
||||||
m_resolver.cancel();
|
|
||||||
m_readTimer.cancel();
|
|
||||||
m_writeTimer.cancel();
|
|
||||||
|
|
||||||
g_dispatcher.addTask(m_closeCallback);
|
|
||||||
|
|
||||||
if(m_socket.is_open()) {
|
|
||||||
boost::system::error_code error;
|
|
||||||
m_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
|
|
||||||
|
|
||||||
if(error) {
|
|
||||||
if(error == boost::asio::error::not_connected) {
|
|
||||||
//Transport endpoint is not connected.
|
|
||||||
} else {
|
|
||||||
logError("shutdown socket error = %s", error.message());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_socket->close(error);
|
|
||||||
|
|
||||||
if(error) {
|
|
||||||
logError("close socket error = %s", error.message());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_state = STATE_CLOSED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Connection::connect(const std::string& host, uint16 port, const Callback& callback)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
|
|
||||||
if(m_state != STATE_CLOSED) {
|
|
||||||
logTraceError("connection not closed");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_connectCallback = callback;
|
|
||||||
boost::asio::ip::tcp::resolver::query query(ip, convertType<std::string>(port));
|
|
||||||
m_resolver.async_resolve(query, boost::bind(&Connection::onResolveDns, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::iterator));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
if(error) {
|
|
||||||
handleError(error);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
|
|
||||||
m_socket.async_connect(*endpointIt, boost::bind(&Connection::onConnect, shared_from_this(), boost::asio::placeholders::error));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::onConnect(const boost::system::error_code& error)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
if(error) {
|
|
||||||
handleError(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_state = STATE_OPEN;
|
|
||||||
|
|
||||||
if(m_connectCallback)
|
|
||||||
g_dispatcher.addTask(m_connectCallback);
|
|
||||||
|
|
||||||
recvNext();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::recvNext()
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
++m_pendingRead;
|
|
||||||
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
|
|
||||||
m_readTimer.async_wait(boost::bind(&Connection::handleReadTimeout,
|
|
||||||
boost::weak_ptr<Connection>(shared_from_this()),
|
|
||||||
boost::asio::placeholders::error));
|
|
||||||
|
|
||||||
static InputMessage inputMessage;
|
|
||||||
boost::asio::async_read(*m_socket,
|
|
||||||
boost::asio::buffer(inputMessage->getBuffer(), InputMessage::HEADER_LENGTH),
|
|
||||||
boost::bind(&Connection::parseHeader, shared_from_this(), inputMessage, boost::asio::placeholders::error));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::parseHeader(const InputMessage& inputMessage, const boost::system::error_code& error)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
|
|
||||||
--m_pendingRead;
|
|
||||||
m_readTimer.cancel();
|
|
||||||
|
|
||||||
if(error && !handleReadError(error))
|
|
||||||
return;
|
|
||||||
|
|
||||||
uint16_t size = inputMessage->decodeHeader();
|
|
||||||
if(size <= 0 || size + 2 > InputMessage::INPUTMESSAGE_MAXSIZE) {
|
|
||||||
internalCloseConnection();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
++m_pendingRead;
|
|
||||||
m_readTimer.expires_from_now(boost::posix_time::seconds(Connection::read_timeout));
|
|
||||||
m_readTimer.async_wait(boost::bind(&Connection::handleReadTimeout, boost::weak_ptr<Connection>(shared_from_this()),
|
|
||||||
boost::asio::placeholders::error));
|
|
||||||
|
|
||||||
inputMessage->setMessageLength(size + InputMessage::HEADER_LENGTH);
|
|
||||||
boost::asio::async_read(*m_socket, boost::asio::buffer(inputMessage->getBuffer() + InputMessage::HEADER_LENGTH, size),
|
|
||||||
boost::bind(&Connection::parsePacket, shared_from_this(), inputMessage, boost::asio::placeholders::error));
|
|
||||||
} catch(boost::system::system_error& e) {
|
|
||||||
logError("async read error = " << e.what());
|
|
||||||
internalCloseConnection();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::parsePacket(const InputMessage& inputMessage, const boost::system::error_code& error)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
|
|
||||||
--m_pendingRead;
|
|
||||||
m_readTimer.cancel();
|
|
||||||
|
|
||||||
if(error && !handleReadError(error))
|
|
||||||
return;
|
|
||||||
|
|
||||||
//g_dispatcher.addTask(boost);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::handleError(const boost::system::error_code& error)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
|
|
||||||
internalCloseConnection();
|
|
||||||
m_errorCallback(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
|
|
||||||
boost::asio::async_write(m_socket,
|
|
||||||
boost::asio::buffer(networkMessage.getBuffer(), NetworkMessage::header_length),
|
|
||||||
boost::bind(&Connection::onSendHeader, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::recv(const RecvCallback& onRecv)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
|
|
||||||
static NetworkMessage networkMessage;
|
|
||||||
boost::asio::async_read(m_socket,
|
|
||||||
boost::asio::buffer(networkMessage.getBuffer(), NetworkMessage::header_length),
|
|
||||||
boost::bind(&Connection::onRecvHeader, shared_from_this(), networkMessage, onRecv, boost::asio::placeholders::error));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::onRecvHeader(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
|
|
||||||
if(error) {
|
|
||||||
handleError(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
boost::asio::async_read(m_socket,
|
|
||||||
boost::asio::buffer(networkMessage.getBodyBuffer(), networkMessage.getMessageLength()),
|
|
||||||
boost::bind(&Connection::onRecvBody, shared_from_this(), networkMessage, onRecv, boost::asio::placeholders::error));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::onRecvBody(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
|
|
||||||
if(error){
|
|
||||||
handleError(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
onRecv(networkMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::onSendHeader(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
|
|
||||||
if(error){
|
|
||||||
handleError(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
boost::asio::async_write(m_socket,
|
|
||||||
boost::asio::buffer(networkMessage.getBodyBuffer(), networkMessage.getMessageLength()),
|
|
||||||
boost::bind(&Connection::onSendBody, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Connection::onSendBody(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
|
|
||||||
if(error) {
|
|
||||||
handleError(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
onSend();
|
|
||||||
}
|
|
|
@ -1,105 +0,0 @@
|
||||||
/* 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 <prerequisites.h>
|
|
||||||
#include <net/networkmessage.h>
|
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
|
||||||
|
|
||||||
class Protocol;
|
|
||||||
class Connection;
|
|
||||||
typedef boost::shared_ptr<Connection> ConnectionPtr;
|
|
||||||
|
|
||||||
typedef boost::function<void()> ConnectionCallback;
|
|
||||||
typedef boost::function<void(const NetworkMessage&)> RecvCallback;
|
|
||||||
typedef boost::function<void(const boost::system::error_code&)> ErrorCallback;
|
|
||||||
|
|
||||||
class Connection : public boost::enable_shared_from_this<Connection>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
enum {
|
|
||||||
WRITE_TIMEOUT = 10,
|
|
||||||
READ_TIMEOUT = 10
|
|
||||||
};
|
|
||||||
|
|
||||||
enum EConnectionState {
|
|
||||||
STATE_CONNECTING,
|
|
||||||
STATE_OPEN,
|
|
||||||
STATE_CLOSED
|
|
||||||
}
|
|
||||||
|
|
||||||
Connection();
|
|
||||||
~Connection();
|
|
||||||
|
|
||||||
bool connect(const std::string& host, uint16 port, const Callback& callback);
|
|
||||||
void close();
|
|
||||||
|
|
||||||
void setOnError(const ErrorCallback& callback) { m_errorCallback = callback; }
|
|
||||||
void setOnRecv(const RecvCallback& callback) { m_recvCallback = callback; }
|
|
||||||
|
|
||||||
void send(const OutputMessage& networkMessage);
|
|
||||||
|
|
||||||
bool isConnecting() const { return m_state == STATE_CONNECTING; }
|
|
||||||
bool isConnected() const { return m_state == STATE_OPEN; }
|
|
||||||
|
|
||||||
static void poll();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt);
|
|
||||||
void onConnect(const boost::system::error_code& error);
|
|
||||||
|
|
||||||
void recvNext();
|
|
||||||
|
|
||||||
void onRecvBody(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error);
|
|
||||||
|
|
||||||
void onSendHeader(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error);
|
|
||||||
void onSendBody(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error);
|
|
||||||
|
|
||||||
void onRecvHeader(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error);
|
|
||||||
|
|
||||||
void handleError(const boost::system::error_code& error);
|
|
||||||
void internalCloseConnection();
|
|
||||||
|
|
||||||
boost::asio::ip::tcp::resolver m_resolver;
|
|
||||||
boost::asio::ip::tcp::socket m_socket;
|
|
||||||
|
|
||||||
int32_t m_pendingWrite;
|
|
||||||
int32_t m_pendingRead;
|
|
||||||
bool m_writeError;
|
|
||||||
bool m_readError;
|
|
||||||
boost::asio::deadline_timer m_readTimer;
|
|
||||||
boost::asio::deadline_timer m_writeTimer;
|
|
||||||
|
|
||||||
EConnectionState m_state;
|
|
||||||
|
|
||||||
Callback m_connectCallback;
|
|
||||||
Callback m_closeCallback;
|
|
||||||
ErrorCallback m_errorCallback;
|
|
||||||
RecvCallback m_recvCallback;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif //CONNECTION_h
|
|
|
@ -1,26 +0,0 @@
|
||||||
/* 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 <prerequisites.h>
|
|
||||||
#include <net/networkmessage.h>
|
|
|
@ -1,210 +0,0 @@
|
||||||
/* 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 InputMessage
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
enum {
|
|
||||||
INPUTMESSAGE_MAXSIZE = 16834,
|
|
||||||
HEADER_LENGTH = 2
|
|
||||||
};
|
|
||||||
|
|
||||||
InputMessage() : m_messageSize(0), m_readPos(HEADER_LENGTH) { }
|
|
||||||
~InputMessage() { }
|
|
||||||
|
|
||||||
inline void reset() {
|
|
||||||
m_messageSize = 0;
|
|
||||||
m_readPos = HEADER_LENGTH;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t decodeHeader() {
|
|
||||||
return (int32_t)(m_buffer[0] | m_buffer[1] << 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t getByte() {
|
|
||||||
return m_buffer[m_readPos++];
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t getU16() {
|
|
||||||
uint16_t v = *(uint16_t*)(m_buffer + m_readPos);
|
|
||||||
m_readPos += 2;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t getU32() {
|
|
||||||
uint32_t v = *(uint32_t*)(m_buffer + m_readPos);
|
|
||||||
m_readPos += 4;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t getU64() {
|
|
||||||
uint64_t v = *(uint64_t*)(m_buffer + m_readPos);
|
|
||||||
m_readPos += 8;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string getString() {
|
|
||||||
uint16_t stringlen = getU16();
|
|
||||||
if(stringlen >= (INPUTMESSAGE_MAXSIZE - m_readPos))
|
|
||||||
return std::string();
|
|
||||||
|
|
||||||
char* v = (char*)(m_buffer + m_readPos);
|
|
||||||
m_readPos += stringlen;
|
|
||||||
return std::string(v, stringlen);
|
|
||||||
}
|
|
||||||
|
|
||||||
void skipBytes(int count) { m_readPos += count; }
|
|
||||||
|
|
||||||
int32_t getMessageLength() const {return m_messageSize; }
|
|
||||||
void setMessageLength(int32_t newSize) { m_messageSize = newSize; }
|
|
||||||
|
|
||||||
int32_t getReadPos() const { return m_readPos; }
|
|
||||||
const char *getBuffer() const { return (char*)&m_buffer[0]; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
uint16_t m_messageSize;
|
|
||||||
uint16_t m_readPos;
|
|
||||||
uint8_t m_buffer[INPUTMESSAGE_MAXSIZE];
|
|
||||||
};
|
|
||||||
|
|
||||||
class OutputMessage
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
enum {
|
|
||||||
OUTPUTMESSAGE_MAXSIZE = 1460
|
|
||||||
};
|
|
||||||
|
|
||||||
OutputMessage() : m_outputBufferStart(4), m_messageSize(0), m_writePos(4) { }
|
|
||||||
~OutputMessage() { }
|
|
||||||
|
|
||||||
void reset() {
|
|
||||||
m_messageSize = 0;
|
|
||||||
m_writePos = 4;
|
|
||||||
m_outputBufferStart = 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addByte(uint8_t value)
|
|
||||||
{
|
|
||||||
if(!canAdd(1))
|
|
||||||
return;
|
|
||||||
m_buffer[m_writePos++] = value;
|
|
||||||
m_messageSize++;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addU16(uint16_t value)
|
|
||||||
{
|
|
||||||
if(!canAdd(2))
|
|
||||||
return;
|
|
||||||
*(uint16_t*)(m_buffer + m_writePos) = value;
|
|
||||||
m_writePos += 2;
|
|
||||||
m_messageSize += 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addU32(uint32_t value)
|
|
||||||
{
|
|
||||||
if(!canAdd(4))
|
|
||||||
return;
|
|
||||||
*(uint32_t*)(m_buffer + m_writePos) = value;
|
|
||||||
m_writePos += 4;
|
|
||||||
m_messageSize += 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addU64(uint64_t value)
|
|
||||||
{
|
|
||||||
if(!canAdd(8))
|
|
||||||
return;
|
|
||||||
*(uint64_t*)(m_buffer + m_writePos) = value;
|
|
||||||
m_writePos += 8;
|
|
||||||
m_messageSize += 8;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addBytes(const char* bytes, uint32_t size)
|
|
||||||
{
|
|
||||||
if(!canAdd(size) || size > 8192)
|
|
||||||
return;
|
|
||||||
|
|
||||||
memcpy(m_buffer + m_writePos, bytes, size);
|
|
||||||
m_writePos += size;
|
|
||||||
m_messageSize += size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addPaddingBytes(uint32_t n) {
|
|
||||||
if(!canAdd(n))
|
|
||||||
return;
|
|
||||||
|
|
||||||
memset((void*)&m_buffer[m_writePos], 0x33, n);
|
|
||||||
m_messageSize = m_messageSize + n;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addString(const char* value)
|
|
||||||
{
|
|
||||||
uint32_t stringlen = (uint32_t)strlen(value);
|
|
||||||
if(!canAdd(stringlen + 2) || stringlen > 8192)
|
|
||||||
return;
|
|
||||||
|
|
||||||
addU16(stringlen);
|
|
||||||
strcpy((char*)(m_buffer + m_writePos), value);
|
|
||||||
m_writePos += stringlen;
|
|
||||||
m_messageSize += stringlen;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addString(const std::string &value) {
|
|
||||||
addString(value.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
void writeMessageLength() {
|
|
||||||
*(uint16_t*)(m_buffer + 2) = m_messageSize;
|
|
||||||
m_messageSize += 2;
|
|
||||||
m_outputBufferStart = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
void writeCryptoHeader() {
|
|
||||||
*(uint16_t*)(m_buffer) = m_messageSize;
|
|
||||||
m_messageSize += 2;
|
|
||||||
m_outputBufferStart = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t getMessageLength() const { return m_messageSize; }
|
|
||||||
void setMessageLength(int32_t newSize) { m_messageSize = newSize; }
|
|
||||||
|
|
||||||
const char *getBuffer() const { return (char*)&m_buffer[0]; }
|
|
||||||
const char *getOutputBuffer() const { return (char*)&m_buffer[m_outputBufferStart]; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
inline bool canAdd(int size) {
|
|
||||||
return (size + m_writePos < OUTPUTMESSAGE_MAXSIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t m_outputBufferStart;
|
|
||||||
uint16_t m_messageSize;
|
|
||||||
uint16_t m_writePos;
|
|
||||||
uint8_t m_buffer[OUTPUTMESSAGE_MAXSIZE];
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif //NETWORKMESSAGE_H
|
|
|
@ -1,53 +0,0 @@
|
||||||
/* 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 <prerequisites.h>
|
|
||||||
#include <net/protocol.h>
|
|
||||||
|
|
||||||
Protocol::Protocol() :
|
|
||||||
m_connection(new Connection)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
m_connection->setOnError(boost::bind(&Protocol::onError, this, boost::asio::placeholders::error));
|
|
||||||
}
|
|
||||||
|
|
||||||
Protocol::~Protocol()
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Protocol::send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend)
|
|
||||||
{
|
|
||||||
m_connection->send(networkMessage, onSend);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Protocol::connect(const std::string& ip, uint16 port, const Callback& callback)
|
|
||||||
{
|
|
||||||
return m_connection->connect(ip, port, callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Protocol::recv(const RecvCallback& onRecv)
|
|
||||||
{
|
|
||||||
m_connection->recv(onRecv);
|
|
||||||
}
|
|
|
@ -1,46 +0,0 @@
|
||||||
/* 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 <net/connection.h>
|
|
||||||
|
|
||||||
class Protocol
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Protocol();
|
|
||||||
virtual ~Protocol();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend);
|
|
||||||
void recv(const RecvCallback& onRecv);
|
|
||||||
bool connect(const std::string& ip, uint16 port, const Callback& callback);
|
|
||||||
virtual void onError(const boost::system::error_code& error) = 0;
|
|
||||||
|
|
||||||
ConnectionPtr m_connection;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif //PROTOCOL_H
|
|
|
@ -1,59 +0,0 @@
|
||||||
/* 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 <prerequisites.h>
|
|
||||||
#include <util/rsa.h>
|
|
||||||
#include "protocollogin.h"
|
|
||||||
|
|
||||||
ProtocolLogin::ProtocolLogin()
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
ProtocolLogin::~ProtocolLogin()
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ProtocolLogin::login(const std::string& account, const std::string& password)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
m_connection = ConnectionPtr(new Connection);
|
|
||||||
m_connection->connect("www.google.com", 80, boost::bind(&ProtocolLogin::afterConnect, this));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ProtocolLogin::afterConnect()
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void ProtocolLogin::onError(const boost::system::error_code& error, const std::string& msg)
|
|
||||||
{
|
|
||||||
logTrace();
|
|
||||||
logError("Connection error: %s", error.message().c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,48 +0,0 @@
|
||||||
/* 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 PROTOCOLLOGIN_H
|
|
||||||
#define PROTOCOLLOGIN_H
|
|
||||||
|
|
||||||
#include <prerequisites.h>
|
|
||||||
#include <net/connection.h>
|
|
||||||
|
|
||||||
class ProtocolLogin
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
ProtocolLogin();
|
|
||||||
~ProtocolLogin();
|
|
||||||
|
|
||||||
void login(const std::string& account, const std::string& password);
|
|
||||||
void afterConnect();
|
|
||||||
void sendAccount();
|
|
||||||
void onError(const boost::system::error_code& error, const std::string& msg);
|
|
||||||
|
|
||||||
private:
|
|
||||||
ConnectionPtr m_connection;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef boost::shared_ptr<ProtocolLogin> ProtocolLoginPtr;
|
|
||||||
|
|
||||||
#endif // PROTOCOLLOGIN_H
|
|
Loading…
Reference in New Issue