init network

master
Henrique Santiago 13 years ago
parent 3de455fe7e
commit 8952bddb7a

@ -59,7 +59,7 @@ SET(SOURCES
src/teststate.cpp
# game net
#src/protocollogin.cpp
src/protocollogin.cpp
# framework core
src/framework/core/dispatcher.cpp
@ -104,8 +104,8 @@ SET(SOURCES
src/framework/ui/uicheckbox.cpp
# framework net
#src/framework/net/connection.cpp
#src/framework/net/protocol.cpp
src/framework/net/connection.cpp
src/framework/net/protocol.cpp
#src/framework/net/networkmessage.cpp
)

@ -29,7 +29,7 @@
#include <graphics/graphics.h>
#include <graphics/fonts.h>
#include <ui/uicontainer.h>
//#include <net/connection.h>
#include <net/connection.h>
Engine g_engine;
@ -69,7 +69,7 @@ void Engine::run()
Platform::poll();
// poll network events
//Connection::poll();
Connection::poll();
// poll diaptcher tasks
g_dispatcher.poll();

@ -0,0 +1,90 @@
/* 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 <core/dispatcher.h>
#include <net/connection.h>
static boost::asio::io_service ioService;
Connection::Connection() :
m_connectionState(CONNECTION_STATE_IDLE),
m_timer(ioService),
m_resolver(ioService),
m_socket(ioService)
{
}
void Connection::poll()
{
ioService.poll();
ioService.reset();
}
void Connection::connect(const std::string& host, uint16 port, const Callback& callback)
{
m_connectCallback = callback;
m_connectionState = CONNECTION_STATE_RESOLVING;
boost::asio::ip::tcp::resolver::query query(host, convertType<std::string, uint16>(port));
m_resolver.async_resolve(query, boost::bind(&Connection::onResolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
m_timer.expires_from_now(boost::posix_time::seconds(2));
m_timer.async_wait(boost::bind(&Connection::onTimeout, this, boost::asio::placeholders::error));
}
void Connection::onTimeout(const boost::system::error_code& error)
{
if(error != boost::asio::error::operation_aborted)
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
}
void Connection::onResolve(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIterator)
{
logTrace();
m_timer.cancel();
if(error) {
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
return;
}
m_socket.async_connect(*endpointIterator, boost::bind(&Connection::onConnect, this, boost::asio::placeholders::error));
m_timer.expires_from_now(boost::posix_time::seconds(2));
m_timer.async_wait(boost::bind(&Connection::onTimeout, this, boost::asio::placeholders::error));
}
void Connection::onConnect(const boost::system::error_code& error)
{
logTrace();
m_timer.cancel();
if(error) {
g_dispatcher.addTask(boost::bind(m_errorCallback, error));
return;
}
g_dispatcher.addTask(m_connectCallback);
}

@ -0,0 +1,67 @@
/* 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 <boost/asio.hpp>
typedef boost::function<void(boost::system::error_code&)> ErrorCallback;
class Connection
{
public:
Connection();
static void poll();
void connect(const std::string& host, uint16 port, const Callback& callback);
void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; }
void onTimeout(const boost::system::error_code& error);
void onResolve(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIterator);
void onConnect(const boost::system::error_code& error);
enum ConnectionState_t {
CONNECTION_STATE_IDLE = 0,
CONNECTION_STATE_RESOLVING,
CONNECTION_STATE_CONNECTING,
CONNECTION_STATE_CONNECTED
};
private:
ErrorCallback m_errorCallback;
Callback m_connectCallback;
ConnectionState_t m_connectionState;
boost::asio::deadline_timer m_timer;
boost::asio::ip::tcp::resolver m_resolver;
boost::asio::ip::tcp::socket m_socket;
};
typedef boost::shared_ptr<Connection> ConnectionPtr;
#endif

@ -0,0 +1,46 @@
/* 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 <net/protocol.h>
Protocol::Protocol() :
m_connection(new Connection)
{
m_connection->setErrorCallback(boost::bind(&Protocol::onError, this, _1));
}
void Protocol::connect(const std::string& host, uint16 port, const Callback& callback)
{
m_connection->connect(host, port, callback);
}
void Protocol::onError(const boost::system::error_code& error)
{
logError(error.message().c_str());
// invalid hostname
// connection timeouted
// displays a dialog, finish protocol
}

@ -0,0 +1,43 @@
/* 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 <net/connection.h>
class Protocol
{
public:
Protocol();
void connect(const std::string& host, uint16 port, const Callback& callback);
virtual void onError(const boost::system::error_code& error);
private:
ConnectionPtr m_connection;
};
#endif

@ -84,6 +84,9 @@ int main(int argc, const char *argv[])
// init platform stuff
Platform::init("OTClient");
// init random numbers
srand(time(NULL));
// init resources
g_resources.init(argv[0]);
if(g_resources.setWriteDir(Platform::getAppUserDir()))

@ -123,8 +123,8 @@ 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();
//m_protocolLogin = ProtocolLoginPtr(new ProtocolLogin);
//m_protocolLogin->login(accountName, password);
m_protocolLogin = ProtocolLoginPtr(new ProtocolLogin);
m_protocolLogin->login(accountName, password);
}
void MenuState::optionsButton_clicked()

@ -28,7 +28,7 @@
#include <prerequisites.h>
#include <core/gamestate.h>
#include <graphics/texture.h>
//#include "protocollogin.h"
#include "protocollogin.h"
class MenuState : public GameState
{
@ -53,7 +53,7 @@ private:
void enterGameWindowOkButton_clicked();
TexturePtr m_background;
//ProtocolLoginPtr m_protocolLogin;
ProtocolLoginPtr m_protocolLogin;
};
#endif // MENUSTATE_H

@ -0,0 +1,67 @@
/* 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 "protocollogin.h"
ProtocolLogin::ProtocolLogin()
{
}
void ProtocolLogin::login(const std::string& accountName, const std::string& password)
{
// return error if acc or password is empty or any other condition
m_accountName = accountName;
m_password = password;
static const char hosts[][32] = {
"login01.tibia.com",
"login02.tibia.com",
"login03.tibia.com",
"login04.tibia.com",
"login05.tibia.com",
"tibia01.cipsoft.com",
"tibia02.cipsoft.com",
"tibia03.cipsoft.com",
"tibia04.cipsoft.com",
"tibia05.cipsoft.com"
};
const std::string host = hosts[rand() % 10];
const uint16 port = 7171;
connect(host, port, boost::bind(&ProtocolLogin::onConnect, this));
}
void ProtocolLogin::onConnect()
{
logTrace();
sendPacket();
}
void ProtocolLogin::sendPacket()
{
}

@ -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 PROTOCOLLOGIN_H
#define PROTOCOLLOGIN_H
#include <net/protocol.h>
class ProtocolLogin : public Protocol
{
public:
ProtocolLogin();
void login(const std::string& accountName, const std::string& password);
void onConnect();
void sendPacket();
private:
std::string m_accountName, m_password;
};
typedef boost::shared_ptr<ProtocolLogin> ProtocolLoginPtr;
#endif
Loading…
Cancel
Save