adding net to framework

This commit is contained in:
Andre Antunes 2011-04-07 05:42:57 -03:00
parent 9bd9f4ba53
commit 86ed2ff287
7 changed files with 262 additions and 3 deletions

View File

@ -5,7 +5,7 @@ SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
# find needed packages # find needed packages
SET(Boost_USE_STATIC_LIBS ON) SET(Boost_USE_STATIC_LIBS ON)
FIND_PACKAGE(Boost REQUIRED) FIND_PACKAGE(Boost COMPONENTS system REQUIRED)
FIND_PACKAGE(OpenGL REQUIRED) FIND_PACKAGE(OpenGL REQUIRED)
FIND_PACKAGE(Lua51 REQUIRED) FIND_PACKAGE(Lua51 REQUIRED)
FIND_PACKAGE(YamlCpp REQUIRED) FIND_PACKAGE(YamlCpp REQUIRED)
@ -65,7 +65,10 @@ SET(SOURCES
src/framework/engine.cpp src/framework/engine.cpp
src/framework/graphics.cpp src/framework/graphics.cpp
src/framework/logger.cpp src/framework/logger.cpp
src/framework/util.cpp) src/framework/util.cpp
src/framework/net/connection.cpp
src/framework/net/connections.cpp)
IF(WIN32) IF(WIN32)
SET(SOURCES ${SOURCES} src/framework/win32platform.cpp) SET(SOURCES ${SOURCES} src/framework/win32platform.cpp)

View File

@ -29,6 +29,7 @@
#include "input.h" #include "input.h"
#include "configs.h" #include "configs.h"
#include "gamestate.h" #include "gamestate.h"
#include "net/connections.h"
Engine g_engine; Engine g_engine;
@ -64,6 +65,9 @@ void Engine::run()
// fire platform events // fire platform events
Platform::poll(); Platform::poll();
//poll network events
//debug("%d", g_connections.poll());
// update // update
ticks = Platform::getTicks(); ticks = Platform::getTicks();
update(ticks, ticks - lastFrameTicks); update(ticks, ticks - lastFrameTicks);
@ -75,6 +79,23 @@ void Engine::run()
// swap buffers // swap buffers
Platform::swapBuffers(); Platform::swapBuffers();
/*
static ConnectionPtr connection = g_connections.createConnection();
if(connection->getLastError()){
error("%s", connection->getLastError().message().c_str());
}
else{
if(!connection->isConnecting() && !connection->isConnected()){
connection->connect("www.google.com.br", 80);
}
if(!connection->isConnected()){
debug("still not connected.");
}
}
*/
//} //}
} }

View File

@ -0,0 +1,86 @@
/* 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 "connection.h"
Connection::Connection(boost::asio::io_service& ioService)
: m_socket(ioService), m_resolver(ioService)
{
m_connected = false;
m_connecting = false;
m_port = 0;
}
void Connection::stop()
{
if(m_connecting){
m_resolver.cancel();
m_socket.cancel();
m_connecting = false;
}
}
void Connection::connect(const std::string& ip, uint16 port)
{
if(m_connecting){
error("Already is connecting.");
return;
}
m_connecting = true;
m_ip = ip;
m_port = port;
debug("connecting...");
//first resolve dns
boost::asio::ip::tcp::resolver::query query(ip, 80);
m_resolver.async_resolve(query, boost::bind(&Connection::onResolveDns, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
}
void Connection::onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{
debug("resolving dns..");
if(error){
m_connecting = false;
m_lastError = error;
return;
}
//lets connect
m_socket.async_connect(*endpoint_iterator, boost::bind(&Connection::onConnect, this, boost::asio::placeholders::error));
}
void Connection::onConnect(const boost::system::error_code& error)
{
if(error){
m_connecting = false;
m_lastError = error;
return;
}
m_connected = true;
notice("Connected on %s.", m_ip.c_str());
}

View File

@ -0,0 +1,60 @@
/* 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"
class Connection
{
public:
Connection(boost::asio::io_service& ioService);
void connect(const std::string& ip, uint16 port);
void stop();
bool isConnecting() const { return m_connecting; }
bool isConnected() const { return m_connected; }
const boost::system::error_code& getLastError() const { return m_lastError; }
private:
void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
void onConnect(const boost::system::error_code& error);
private:
boost::asio::ip::tcp::socket m_socket;
boost::asio::ip::tcp::resolver m_resolver;
boost::system::error_code m_lastError;
bool m_connecting;
bool m_connected;
std::string m_ip;
uint16_t m_port;
};
typedef std::shared_ptr<Connection> ConnectionPtr;
#endif //CONNECTION_h

View File

@ -0,0 +1,39 @@
/* 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 "connections.h"
Connections g_connections;
size_t Connections::poll()
{
return m_ioService.poll();
}
ConnectionPtr Connections::createConnection()
{
ConnectionPtr connection(new Connection(m_ioService));
m_connections.push_back(connection);
return connection;
}

View File

@ -0,0 +1,47 @@
/* 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 CONNECTIONS_H
#define CONNECTIONS_H
#include "../prerequisites.h"
#include "connection.h"
class Connections
{
public:
size_t poll();
ConnectionPtr createConnection();
private:
boost::asio::io_service m_ioService;
typedef std::vector<ConnectionPtr> ConnectionVector;
ConnectionVector m_connections;
};
extern Connections g_connections;
#endif //CONNECTIONS_H

View File

@ -63,6 +63,9 @@ typedef int8_t int8;
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#define foreach BOOST_FOREACH #define foreach BOOST_FOREACH
// yaml // yaml