This commit is contained in:
Eduardo Bart 2011-04-16 23:48:13 -03:00
parent 2070b94661
commit da57770f88
16 changed files with 163 additions and 335 deletions

View File

@ -59,7 +59,7 @@ SET(SOURCES
src/teststate.cpp
# game net
src/net/protocoltibia87.cpp
src/protocollogin.cpp
# framework core
src/framework/core/dispatcher.cpp
@ -108,7 +108,6 @@ SET(SOURCES
# framework net
src/framework/net/connection.cpp
src/framework/net/connections.cpp
src/framework/net/protocol.cpp
src/framework/net/networkmessage.cpp
)

View File

@ -28,9 +28,9 @@
#include "graphics/graphics.h"
#include "configs.h"
#include "dispatcher.h"
#include "net/connections.h"
#include "ui/uicontainer.h"
#include "graphics/fonts.h"
#include "net/connection.h"
Engine g_engine;
@ -70,7 +70,7 @@ void Engine::run()
Platform::poll();
// poll network events
g_connections.poll();
Connection::poll();
// poll diaptcher tasks
g_dispatcher.poll();

View File

@ -23,38 +23,52 @@
#include "connection.h"
#include <boost/bind.hpp>
static boost::asio::io_service ioService;
Connection::Connection(boost::asio::io_service& ioService)
: m_socket(ioService), m_resolver(ioService)
Connection::Connection() :
m_socket(ioService),
m_resolver(ioService),
m_connecting(false),
m_connected(false),
m_port(0)
{
m_connected = false;
m_connecting = false;
m_port = 0;
logTrace();
}
void Connection::stop()
Connection::~Connection()
{
if(m_connecting){
logTrace();
}
void Connection::poll()
{
ioService.poll();
ioService.reset();
}
void Connection::close()
{
logTrace();
if(m_connecting) {
m_resolver.cancel();
m_socket.cancel();
m_connecting = false;
m_connected = false;
closeSocket();
}
}
bool Connection::connect(const std::string& ip, uint16 port, const Callback& callback)
{
logTrace();
logInfo("[Connection::connect]: Ip: %s - Port: %d", ip.c_str(), port);
if(m_connecting){
logError("Already is connecting.");
if(m_connecting) {
logTraceError("already connecting.");
return false;
}
if(m_connected){
logError("Already is connected.");
if(m_connected) {
logTraceError("already connected.");
return false;
}
@ -63,123 +77,116 @@ bool Connection::connect(const std::string& ip, uint16 port, const Callback& cal
m_ip = ip;
m_port = port;
//first resolve dns
boost::asio::ip::tcp::resolver::query query(ip, convertType<std::string, uint16>(port));
m_resolver.async_resolve(query, boost::bind(&Connection::onResolveDns, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
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)
{
logInfo("[Connection::onResolveDns]");
if(error){
m_connecting = false;
logInfo("Error");
m_errorCallback(error, __FUNCTION__);
logTrace();
if(error) {
handleError(error);
return;
}
//lets connect
m_socket.async_connect(*endpointIt, boost::bind(&Connection::onConnect, this, boost::asio::placeholders::error));
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)
{
if(error){
logInfo("Error");
m_connecting = false;
m_errorCallback(error, __FUNCTION__);
logTrace();
if(error) {
handleError(error);
return;
}
m_connected = true;
m_connectCallback();
}
void Connection::handleError(const boost::system::error_code& error)
{
stop();
logTrace();
if(isConnected()){
closeSocket();
}
close();
m_errorCallback(error);
}
void Connection::closeSocket()
{
logTrace();
boost::system::error_code error;
m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
if(error) {
logError("Connection::closeSocket(): %s", error.message().c_str());
}
m_socket.close(error);
if(error) {
logError("Connection::closeSocket(): %s", error.message().c_str());
}
}
void Connection::send(NetworkMessagePtr networkMessage, ConnectionCallback onSend)
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::asio::buffer(networkMessage.getBuffer(), NetworkMessage::header_length),
boost::bind(&Connection::onSendHeader, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error));
}
void Connection::recv(RecvCallback onRecv)
void Connection::recv(const RecvCallback& onRecv)
{
NetworkMessagePtr networkMessage(new NetworkMessage);
logTrace();
static NetworkMessage networkMessage;
boost::asio::async_read(m_socket,
boost::asio::buffer(networkMessage->getBuffer(), NetworkMessage::header_length),
boost::asio::buffer(networkMessage.getBuffer(), NetworkMessage::header_length),
boost::bind(&Connection::onRecvHeader, shared_from_this(), networkMessage, onRecv, boost::asio::placeholders::error));
}
void Connection::onRecvHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error)
void Connection::onRecvHeader(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error)
{
if(error){
connection->handleError(error);
connection->onError(error, __FUNCTION__);
logTrace();
if(error) {
handleError(error);
return;
}
boost::asio::async_read(connection->getSocket(),
boost::asio::buffer(networkMessage->getBodyBuffer(), networkMessage->getMessageLength()),
boost::bind(&Connection::onRecvBody, connection, networkMessage, onRecv, boost::asio::placeholders::error));
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(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error)
void Connection::onRecvBody(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error)
{
logTrace();
if(error){
connection->handleError(error);
connection->onError(error, __FUNCTION__);
handleError(error);
return;
}
onRecv(networkMessage);
}
void Connection::onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error)
void Connection::onSendHeader(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error)
{
logTrace();
if(error){
connection->handleError(error);
connection->onError(error, __FUNCTION__);
handleError(error);
return;
}
boost::asio::async_write(connection->getSocket(),
boost::asio::buffer(networkMessage->getBodyBuffer(), networkMessage->getMessageLength()),
boost::bind(&Connection::onSendBody, connection, networkMessage, onSend, boost::asio::placeholders::error));
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(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error)
void Connection::onSendBody(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error)
{
if(error){
connection->handleError(error);
connection->onError(error, __FUNCTION__);
logTrace();
if(error) {
handleError(error);
return;
}

View File

@ -25,58 +25,47 @@
#define CONNECTION_H
#include "prerequisites.h"
#include "networkmessage.h"
#include <boost/asio.hpp>
#include "networkmessage.h"
class TestState;
class Protocol;
class Connections;
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:
typedef boost::function<void()> ConnectionCallback;
typedef boost::function<void(NetworkMessagePtr)> RecvCallback;
typedef boost::function<void(const boost::system::error_code&, const std::string&)> ErrorCallback;
typedef boost::shared_ptr<Connection> ConnectionPtr;
private:
Connection(boost::asio::io_service& ioService);
Connection();
~Connection();
bool connect(const std::string& ip, uint16 port, const Callback& callback);
void stop();
void close();
void setErrorCallback(const ErrorCallback& callback) { m_errorCallback = callback; }
void setOnError(const ErrorCallback& callback) { m_errorCallback = callback; }
void recv(RecvCallback onSend);
void send(NetworkMessagePtr networkMessage, ConnectionCallback onRecv);
void recv(const RecvCallback& onSend);
void send(const NetworkMessage& networkMessage, const ConnectionCallback& onRecv);
bool isConnecting() const { return m_connecting; }
bool isConnected() const { return m_connected; }
boost::asio::ip::tcp::socket& getSocket() { return m_socket; }
void onError(const boost::system::error_code& error, const std::string& msg) { m_errorCallback(error, msg); }
static void poll();
private:
static void onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error);
static void onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, 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);
static void onRecvHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error);
static void onRecvBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error);
void onRecvHeader(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error);
void onRecvBody(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error);
private:
void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt);
void onConnect(const boost::system::error_code& error);
private:
void closeSocket();
private:
void handleError(const boost::system::error_code& error);
boost::asio::ip::tcp::socket m_socket;
@ -90,11 +79,6 @@ private:
Callback m_connectCallback;
ErrorCallback m_errorCallback;
friend class Protocol;
friend class Connections;
};
typedef boost::shared_ptr<Connection> ConnectionPtr;
#endif //CONNECTION_h

View File

@ -168,11 +168,10 @@ uint64 NetworkMessage::getU64()
return v;
}
char* NetworkMessage::getBuffer() {
char* NetworkMessage::getBuffer() const {
return (char*)&m_msgBuf[0];
}
char* NetworkMessage::getBodyBuffer() {
m_readPos = 2;
char* NetworkMessage::getBodyBuffer() const {
return (char*)&m_msgBuf[header_length];
}

View File

@ -78,8 +78,8 @@ public:
void setMessageLength(int32 newSize);
int32 getReadPos() const;
int32 getHeaderSize();
char* getBuffer();
char* getBodyBuffer();
char* getBuffer() const;
char* getBodyBuffer() const;
void updateHeaderLength();
@ -92,6 +92,4 @@ protected:
uint8 m_msgBuf[NETWORKMESSAGE_MAXSIZE];
};
typedef boost::shared_ptr<NetworkMessage> NetworkMessagePtr;
#endif //NETWORKMESSAGE_H

View File

@ -22,21 +22,20 @@
*/
#include "protocol.h"
#include "connections.h"
Protocol::Protocol()
Protocol::Protocol() :
m_connection(new Connection)
{
logInfo("Protocol()");
m_connection = g_connections.createConnection();
m_connection->setErrorCallback(boost::bind(&Protocol::onError, this, boost::asio::placeholders::error, _2));
logTrace();
m_connection->setOnError(boost::bind(&Protocol::onError, this, boost::asio::placeholders::error));
}
Protocol::~Protocol()
{
logInfo("~Protocol()");
logTrace();
}
void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend)
void Protocol::send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend)
{
m_connection->send(networkMessage, onSend);
}
@ -46,7 +45,7 @@ bool Protocol::connect(const std::string& ip, uint16 port, const Callback& callb
return m_connection->connect(ip, port, callback);
}
void Protocol::recv(Connection::RecvCallback onRecv)
void Protocol::recv(const RecvCallback& onRecv)
{
m_connection->recv(onRecv);
}

View File

@ -31,17 +31,13 @@ class Protocol
{
public:
Protocol();
~Protocol();
virtual void begin() = 0;
virtual ~Protocol();
protected:
void send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend);
void recv(Connection::RecvCallback onRecv);
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, const std::string& msg) = 0;
virtual void onError(const boost::system::error_code& error) = 0;
ConnectionPtr m_connection;
};

View File

@ -30,10 +30,9 @@
#include "graphics/fonts.h"
#include "core/dispatcher.h"
#include "ui/ui.h"
#include "net/connections.h"
#include "net/protocoltibia87.h"
#include "net/connection.h"
#include "graphics/borderedimage.h"
#include "protocollogin.h"
void MenuState::onEnter()
{
@ -101,8 +100,7 @@ void MenuState::enterGameWindowOkButton_clicked()
std::string accountName = boost::static_pointer_cast<UITextEdit>(enterGameWindow->getChildById("accountNameTextEdit"))->getText();
std::string password = boost::static_pointer_cast<UITextEdit>(enterGameWindow->getChildById("passwordTextEdit"))->getText();
//ProtocolTibia87Ptr protocol = ProtocolTibia87Ptr(new ProtocolTibia87);
ProtocolTibia87 *protocol = new ProtocolTibia87;
protocol->login(accountName, password);
m_protocolLogin = ProtocolLoginPtr(new ProtocolLogin);
m_protocolLogin->login(accountName, password);
}

View File

@ -29,6 +29,7 @@
#include "graphics/texture.h"
#include "net/connection.h"
#include "ui/uipanel.h"
#include "protocollogin.h"
class MenuState : public GameState
{
@ -52,6 +53,7 @@ private:
UIPanelPtr m_menuPanel;
TexturePtr m_background;
ProtocolLoginPtr m_protocolLogin;
};
#endif // MENUSTATE_H

View File

@ -1,112 +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 "protocoltibia87.h"
#include "util/rsa.h"
const char* ProtocolTibia87::rsa = "4673033022358411862216018001503683214873298680851934467521055526294025873980576"
"6860224610646919605860206328024326703361630109888417839241959507572247284807035"
"2355696191737922927869078457919049551036016528225191219083671878855092700253886"
"41700821735345222087940578381210879116823013776808975766851829020659073";
ProtocolTibia87::ProtocolTibia87()
{
}
void ProtocolTibia87::begin()
{
/*
*/
}
void ProtocolTibia87::login(const std::string& account, const std::string& password)
{
logInfo("Account: %s - Password: %s", account.c_str(), password.c_str());
connect("google.com", 80, boost::bind(&ProtocolTibia87::afterConnect, this));
//sendAccount(account, password);
}
void ProtocolTibia87::sendAccount(const std::string& account, const std::string& password)
{
NetworkMessagePtr networkMessage(new NetworkMessage);
networkMessage->addByte(0x01);//login Server
networkMessage->addU16(0x00);//OS
networkMessage->addU16(0x00);//VERSION
networkMessage->addPaddingBytes(12);//
int32 m_notEncriptedLen = networkMessage->getMessageLength();
//begin RSA encrypt
networkMessage->addU32(1); //xtea
networkMessage->addU32(2); //xtea
networkMessage->addU32(3); //xtea
networkMessage->addU32(4); //xtea
networkMessage->addString(account);
networkMessage->addString(password);
Rsa::encrypt(networkMessage->getBodyBuffer() + m_notEncriptedLen, networkMessage->getMessageLength() - m_notEncriptedLen, ProtocolTibia87::rsa);
networkMessage->setMessageLength(m_notEncriptedLen + 128);
networkMessage->updateHeaderLength();
/*
send(networkMessage,
[this](){
this->afterSendAccount();
}
);
*/
}
void ProtocolTibia87::afterConnect()
{
logError("[ProtocolTibia87::afterConnect]: Connected!");
login("9418347", "lollol");
}
void ProtocolTibia87::afterSendAccount()
{
/*
recv(
[this](NetworkMessagePtr networkMessage){
this->parseCharacterList(networkMessage);
}
);
*/
}
void ProtocolTibia87::onError(const boost::system::error_code& error, const std::string& msg)
{
logError("%s; %s", error.message().c_str(), msg.c_str());
}
void ProtocolTibia87::parseCharacterList(NetworkMessagePtr networkMessage)
{
logInfo("Parsing characters. msglen: %d", networkMessage->getMessageLength());
}

View File

@ -1,56 +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 PROTOCOLTIBIA87_H
#define PROTOCOLTIBIA87_H
#include "prerequisites.h"
#include "net/protocol.h"
class ProtocolTibia87 : public Protocol
{
public:
ProtocolTibia87();
virtual void begin();
void login(const std::string& account, const std::string& password);
protected:
void sendAccount(const std::string& account, const std::string& password);
void parseCharacterList(NetworkMessagePtr networkMessage);
void readCharacterList();
void afterConnect();
void afterSendAccount();
virtual void onError(const boost::system::error_code& error, const std::string& msg);
private:
static const char* rsa;
};
typedef boost::shared_ptr<ProtocolTibia87> ProtocolTibia87Ptr;
#endif //PROTOCOLTIBIA87_H

View File

@ -21,19 +21,38 @@
* THE SOFTWARE.
*/
#include "connections.h"
Connections g_connections;
#include "protocollogin.h"
#include "util/rsa.h"
size_t Connections::poll()
ProtocolLogin::ProtocolLogin()
{
return m_ioService.poll();
logTrace();
}
ConnectionPtr Connections::createConnection()
ProtocolLogin::~ProtocolLogin()
{
ConnectionPtr connection(new Connection(m_ioService));
m_connections.push_back(connection);
return connection;
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());
}

View File

@ -21,27 +21,28 @@
* THE SOFTWARE.
*/
#ifndef CONNECTIONS_H
#define CONNECTIONS_H
#ifndef PROTOCOLLOGIN_H
#define PROTOCOLLOGIN_H
#include "prerequisites.h"
#include "net/connection.h"
#include "connection.h"
class Connections
class ProtocolLogin
{
public:
size_t poll();
ProtocolLogin();
~ProtocolLogin();
ConnectionPtr createConnection();
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:
boost::asio::io_service m_ioService;
typedef std::vector<ConnectionPtr> ConnectionVector;
ConnectionVector m_connections;
ConnectionPtr m_connection;
};
extern Connections g_connections;
typedef boost::shared_ptr<ProtocolLogin> ProtocolLoginPtr;
#endif //CONNECTIONS_H
#endif // PROTOCOLLOGIN_H

View File

@ -25,13 +25,10 @@
#include "teststate.h"
#include "graphics/graphics.h"
#include "core/engine.h"
#include "net/connections.h"
#include "net/protocoltibia87.h"
void TestState::onEnter()
{
m_protocol = ProtocolTibia87Ptr(new ProtocolTibia87);
m_protocol->begin();
}
void TestState::onLeave()

View File

@ -25,8 +25,8 @@
#ifndef TESTSTATE_H
#define TESTSTATE_H
#include "prerequisites.h"
#include "core/gamestate.h"
#include "net/protocoltibia87.h"
class TestState : public GameState
{
@ -42,9 +42,6 @@ public:
virtual void render();
virtual void update(int ticks, int elapsedTicks);
private:
ProtocolTibia87Ptr m_protocol;
};
#endif // TESTSTATE_H