init protocol integ

This commit is contained in:
Henrique Santiago 2011-04-16 22:06:38 -03:00
parent dc39c965cc
commit 2070b94661
8 changed files with 51 additions and 25 deletions

View File

@ -43,8 +43,11 @@ void Connection::stop()
} }
} }
bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback onConnect) bool Connection::connect(const std::string& ip, uint16 port, const Callback& callback)
{ {
logInfo("[Connection::connect]: Ip: %s - Port: %d", ip.c_str(), port);
if(m_connecting){ if(m_connecting){
logError("Already is connecting."); logError("Already is connecting.");
return false; return false;
@ -55,7 +58,7 @@ bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback
return false; return false;
} }
m_connectCallback = onConnect; m_connectCallback = callback;
m_connecting = true; m_connecting = true;
m_ip = ip; m_ip = ip;
m_port = port; m_port = port;
@ -63,14 +66,15 @@ bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback
//first resolve dns //first resolve dns
boost::asio::ip::tcp::resolver::query query(ip, convertType<std::string, uint16>(port)); 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, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
return true; return true;
} }
void Connection::onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt) void Connection::onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt)
{ {
logInfo("[Connection::onResolveDns]");
if(error){ if(error){
m_connecting = false; m_connecting = false;
logInfo("Error");
m_errorCallback(error, __FUNCTION__); m_errorCallback(error, __FUNCTION__);
return; return;
} }
@ -82,6 +86,7 @@ void Connection::onResolveDns(const boost::system::error_code& error, boost::asi
void Connection::onConnect(const boost::system::error_code& error) void Connection::onConnect(const boost::system::error_code& error)
{ {
if(error){ if(error){
logInfo("Error");
m_connecting = false; m_connecting = false;
m_errorCallback(error, __FUNCTION__); m_errorCallback(error, __FUNCTION__);
return; return;

View File

@ -47,10 +47,10 @@ public:
private: private:
Connection(boost::asio::io_service& ioService); Connection(boost::asio::io_service& ioService);
bool connect(const std::string& ip, uint16 port, ConnectionCallback onConnect); bool connect(const std::string& ip, uint16 port, const Callback& callback);
void stop(); void stop();
void setErrorCallback(ErrorCallback c) { m_errorCallback = c; } void setErrorCallback(const ErrorCallback& callback) { m_errorCallback = callback; }
void recv(RecvCallback onSend); void recv(RecvCallback onSend);
void send(NetworkMessagePtr networkMessage, ConnectionCallback onRecv); void send(NetworkMessagePtr networkMessage, ConnectionCallback onRecv);
@ -88,7 +88,7 @@ private:
std::string m_ip; std::string m_ip;
uint16_t m_port; uint16_t m_port;
ConnectionCallback m_connectCallback; Callback m_connectCallback;
ErrorCallback m_errorCallback; ErrorCallback m_errorCallback;
friend class Protocol; friend class Protocol;

View File

@ -26,12 +26,14 @@
Protocol::Protocol() Protocol::Protocol()
{ {
logInfo("Protocol()");
m_connection = g_connections.createConnection(); m_connection = g_connections.createConnection();
/*m_connection->setErrorCallback( m_connection->setErrorCallback(boost::bind(&Protocol::onError, this, boost::asio::placeholders::error, _2));
[this](const boost::system::error_code& error, const std::string& msg){ }
this->onError(error, msg);
} Protocol::~Protocol()
);*/ {
logInfo("~Protocol()");
} }
void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend) void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend)
@ -39,9 +41,9 @@ void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCall
m_connection->send(networkMessage, onSend); m_connection->send(networkMessage, onSend);
} }
bool Protocol::connect(const std::string& ip, uint16 port, Connection::ConnectionCallback onConnect) bool Protocol::connect(const std::string& ip, uint16 port, const Callback& callback)
{ {
return m_connection->connect(ip, port, onConnect); return m_connection->connect(ip, port, callback);
} }
void Protocol::recv(Connection::RecvCallback onRecv) void Protocol::recv(Connection::RecvCallback onRecv)

View File

@ -31,6 +31,7 @@ class Protocol
{ {
public: public:
Protocol(); Protocol();
~Protocol();
virtual void begin() = 0; virtual void begin() = 0;
@ -38,7 +39,7 @@ protected:
void send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend); void send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend);
void recv(Connection::RecvCallback onRecv); void recv(Connection::RecvCallback onRecv);
bool connect(const std::string& ip, uint16 port, Connection::ConnectionCallback onConnect); 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, const std::string& msg) = 0;

View File

@ -31,6 +31,7 @@
#include "core/dispatcher.h" #include "core/dispatcher.h"
#include "ui/ui.h" #include "ui/ui.h"
#include "net/connections.h" #include "net/connections.h"
#include "net/protocoltibia87.h"
#include "graphics/borderedimage.h" #include "graphics/borderedimage.h"
@ -85,9 +86,23 @@ void MenuState::render()
void MenuState::enterGameButton_clicked() void MenuState::enterGameButton_clicked()
{ {
UIElementPtr window = UIContainer::getRootContainer()->getChildById("enterGameWindow"); UIContainerPtr window = boost::static_pointer_cast<UIContainer>(UIContainer::getRootContainer()->getChildById("enterGameWindow"));
if(!window) if(!window)
window = UILoader::loadFile("ui/enterGameWindow.yml"); window = UILoader::loadFile("ui/enterGameWindow.yml")->asUIContainer();
window->getParent()->setEnabled(false);
UIButtonPtr button = boost::static_pointer_cast<UIButton>(window->getChildById("okButton"));
button->setOnClick(boost::bind(&MenuState::enterGameWindowOkButton_clicked, this));
}
void MenuState::enterGameWindowOkButton_clicked()
{
UIContainerPtr enterGameWindow = boost::static_pointer_cast<UIContainer>(UIContainer::getRootContainer()->getChildById("enterGameWindow"));
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);
} }

View File

@ -48,6 +48,8 @@ public:
private: private:
void enterGameButton_clicked(); void enterGameButton_clicked();
void enterGameWindowOkButton_clicked();
UIPanelPtr m_menuPanel; UIPanelPtr m_menuPanel;
TexturePtr m_background; TexturePtr m_background;
}; };

View File

@ -37,17 +37,16 @@ ProtocolTibia87::ProtocolTibia87()
void ProtocolTibia87::begin() void ProtocolTibia87::begin()
{ {
/* /*
connect("icechaw.otland.net", 7171,
[this](){
this->afterConnect();
}
);
*/ */
} }
void ProtocolTibia87::login(const std::string& account, const std::string& password) void ProtocolTibia87::login(const std::string& account, const std::string& password)
{ {
sendAccount(account, 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) void ProtocolTibia87::sendAccount(const std::string& account, const std::string& password)
@ -86,6 +85,7 @@ void ProtocolTibia87::sendAccount(const std::string& account, const std::string&
void ProtocolTibia87::afterConnect() void ProtocolTibia87::afterConnect()
{ {
logError("[ProtocolTibia87::afterConnect]: Connected!");
login("9418347", "lollol"); login("9418347", "lollol");
} }

View File

@ -33,9 +33,10 @@ public:
ProtocolTibia87(); ProtocolTibia87();
virtual void begin(); virtual void begin();
void login(const std::string& account, const std::string& password);
protected: protected:
void login(const std::string& account, const std::string& password);
void sendAccount(const std::string& account, const std::string& password); void sendAccount(const std::string& account, const std::string& password);
void parseCharacterList(NetworkMessagePtr networkMessage); void parseCharacterList(NetworkMessagePtr networkMessage);