From 86ed2ff287072c1b9852b8b1b464f91fc3f80ec2 Mon Sep 17 00:00:00 2001 From: Andre Antunes Date: Thu, 7 Apr 2011 05:42:57 -0300 Subject: [PATCH 1/2] adding net to framework --- CMakeLists.txt | 7 ++- src/framework/engine.cpp | 23 ++++++++- src/framework/net/connection.cpp | 86 +++++++++++++++++++++++++++++++ src/framework/net/connection.h | 60 +++++++++++++++++++++ src/framework/net/connections.cpp | 39 ++++++++++++++ src/framework/net/connections.h | 47 +++++++++++++++++ src/framework/prerequisites.h | 3 ++ 7 files changed, 262 insertions(+), 3 deletions(-) create mode 100644 src/framework/net/connection.cpp create mode 100644 src/framework/net/connection.h create mode 100644 src/framework/net/connections.cpp create mode 100644 src/framework/net/connections.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b86f4eda..92942c09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}") # find needed packages SET(Boost_USE_STATIC_LIBS ON) -FIND_PACKAGE(Boost REQUIRED) +FIND_PACKAGE(Boost COMPONENTS system REQUIRED) FIND_PACKAGE(OpenGL REQUIRED) FIND_PACKAGE(Lua51 REQUIRED) FIND_PACKAGE(YamlCpp REQUIRED) @@ -65,7 +65,10 @@ SET(SOURCES src/framework/engine.cpp src/framework/graphics.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) SET(SOURCES ${SOURCES} src/framework/win32platform.cpp) diff --git a/src/framework/engine.cpp b/src/framework/engine.cpp index 93839b43..ba9a016c 100644 --- a/src/framework/engine.cpp +++ b/src/framework/engine.cpp @@ -29,6 +29,7 @@ #include "input.h" #include "configs.h" #include "gamestate.h" +#include "net/connections.h" Engine g_engine; @@ -63,7 +64,10 @@ void Engine::run() while(!m_stopping) { // fire platform events Platform::poll(); - + + //poll network events + //debug("%d", g_connections.poll()); + // update ticks = Platform::getTicks(); update(ticks, ticks - lastFrameTicks); @@ -75,6 +79,23 @@ void Engine::run() // swap buffers 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."); + } + } + */ //} } diff --git a/src/framework/net/connection.cpp b/src/framework/net/connection.cpp new file mode 100644 index 00000000..861a5b6b --- /dev/null +++ b/src/framework/net/connection.cpp @@ -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()); +} diff --git a/src/framework/net/connection.h b/src/framework/net/connection.h new file mode 100644 index 00000000..17a5e73c --- /dev/null +++ b/src/framework/net/connection.h @@ -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 ConnectionPtr; + +#endif //CONNECTION_h diff --git a/src/framework/net/connections.cpp b/src/framework/net/connections.cpp new file mode 100644 index 00000000..59be8619 --- /dev/null +++ b/src/framework/net/connections.cpp @@ -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; +} diff --git a/src/framework/net/connections.h b/src/framework/net/connections.h new file mode 100644 index 00000000..c22b452b --- /dev/null +++ b/src/framework/net/connections.h @@ -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 ConnectionVector; + ConnectionVector m_connections; +}; + +extern Connections g_connections; + +#endif //CONNECTIONS_H diff --git a/src/framework/prerequisites.h b/src/framework/prerequisites.h index 0e4d2753..2febfeb4 100644 --- a/src/framework/prerequisites.h +++ b/src/framework/prerequisites.h @@ -63,6 +63,9 @@ typedef int8_t int8; #include #include #include +#include +#include + #define foreach BOOST_FOREACH // yaml From ee2b05dc8da14c87e03ea0c47a51e77bb63a8606 Mon Sep 17 00:00:00 2001 From: Andre Antunes Date: Thu, 7 Apr 2011 05:48:10 -0300 Subject: [PATCH 2/2] fixing merge --- src/framework/engine.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/framework/engine.cpp b/src/framework/engine.cpp index 5c95dc10..200dea5a 100644 --- a/src/framework/engine.cpp +++ b/src/framework/engine.cpp @@ -108,8 +108,7 @@ void Engine::run() // swap buffers Platform::swapBuffers(); -<<<<<<< HEAD - + /* static ConnectionPtr connection = g_connections.createConnection(); @@ -127,9 +126,7 @@ void Engine::run() } */ //} -======= } ->>>>>>> f3eaf3f7262bf6ef35cee745d40088669526125a } m_stopping = false;