init protocol integ

master
Henrique Santiago 13 years ago
parent dc39c965cc
commit 2070b94661

@ -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){
logError("Already is connecting.");
return false;
@ -55,7 +58,7 @@ bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback
return false;
}
m_connectCallback = onConnect;
m_connectCallback = callback;
m_connecting = true;
m_ip = ip;
m_port = port;
@ -63,14 +66,15 @@ bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback
//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));
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__);
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)
{
if(error){
logInfo("Error");
m_connecting = false;
m_errorCallback(error, __FUNCTION__);
return;

@ -47,10 +47,10 @@ public:
private:
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 setErrorCallback(ErrorCallback c) { m_errorCallback = c; }
void setErrorCallback(const ErrorCallback& callback) { m_errorCallback = callback; }
void recv(RecvCallback onSend);
void send(NetworkMessagePtr networkMessage, ConnectionCallback onRecv);
@ -88,7 +88,7 @@ private:
std::string m_ip;
uint16_t m_port;
ConnectionCallback m_connectCallback;
Callback m_connectCallback;
ErrorCallback m_errorCallback;
friend class Protocol;

@ -26,12 +26,14 @@
Protocol::Protocol()
{
logInfo("Protocol()");
m_connection = g_connections.createConnection();
/*m_connection->setErrorCallback(
[this](const boost::system::error_code& error, const std::string& msg){
this->onError(error, msg);
}
);*/
m_connection->setErrorCallback(boost::bind(&Protocol::onError, this, boost::asio::placeholders::error, _2));
}
Protocol::~Protocol()
{
logInfo("~Protocol()");
}
void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend)
@ -39,9 +41,9 @@ void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCall
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)

@ -31,6 +31,7 @@ class Protocol
{
public:
Protocol();
~Protocol();
virtual void begin() = 0;
@ -38,7 +39,7 @@ protected:
void send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend);
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;

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

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

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

@ -33,9 +33,10 @@ public:
ProtocolTibia87();
virtual void begin();
void login(const std::string& account, const std::string& password);
protected:
void login(const std::string& account, const std::string& password);
void sendAccount(const std::string& account, const std::string& password);
void parseCharacterList(NetworkMessagePtr networkMessage);
@ -52,4 +53,4 @@ private:
typedef boost::shared_ptr<ProtocolTibia87> ProtocolTibia87Ptr;
#endif //PROTOCOLTIBIA87_H
#endif //PROTOCOLTIBIA87_H

Loading…
Cancel
Save