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 b8bab578..200dea5a 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" #define MINIMUN_UPDATE_DELAY 50 @@ -74,6 +75,9 @@ void Engine::run() while(!m_stopping) { // fire platform events Platform::poll(); + + //poll network events + //debug("%d", g_connections.poll()); // update before redering ticks = Platform::getTicks(); @@ -104,6 +108,24 @@ 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