diff options
author | Stijn Buys <ingar@osirion.org> | 2008-02-19 17:37:01 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-02-19 17:37:01 +0000 |
commit | 41ad1e4c9e2a70d0a8811f4b035f0d3018045e61 (patch) | |
tree | abe7fa4544c22ba0cfa6375fd56f2e596f1bf626 /src/core/gameconnection.cc | |
parent | 7daaf66869b7b9f85f71b1aa5e9a1b4c40710f33 (diff) |
client-to-server connection
Diffstat (limited to 'src/core/gameconnection.cc')
-rw-r--r-- | src/core/gameconnection.cc | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/core/gameconnection.cc b/src/core/gameconnection.cc new file mode 100644 index 0000000..43df1be --- /dev/null +++ b/src/core/gameconnection.cc @@ -0,0 +1,93 @@ +/* + core/gameconnection.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include <string> +#include <sstream> + +#include "sys/sys.h" +#include "net/net.h" +#include "core/gameconnection.h" + +namespace core +{ + +GameConnection* GameConnection::connection_instance = 0; + +GameConnection::GameConnection(std::string const &connectionstr) +{ + connection_instance = this; + connection_network = 0; + connection_running = false; + + unsigned int port = net::DEFAULTPORT; + std::string host(connectionstr); + size_t found = host.find(':'); + + if (found != std::string::npos) { + std::istringstream str(host.substr(found+1)); + if (str >> port) { + host.erase(found, std::string::npos); + } else { + con_print << "Invalid hostname '" << host << "'\n"; + abort(); + return; + } + + } + + connection_network = new NetConnection(); + connection_network->connect(host, port); + + if (!connection_network->connected()) { + abort(); + return; + } + + connection_running = true; +} + +GameConnection::~GameConnection() +{ + if (connection_network) { + connection_network->disconnect(); + delete connection_network; + } + + connection_instance = 0; +} + +void GameConnection::abort() +{ + connection_running = false; +} + +void GameConnection::forward(std::string const &cmdline) +{ + + if (!connection_network->connected()) + return; + + std::string netmessage("cmd "); + netmessage.append(cmdline); + netmessage += '\n'; + connection_network->send(netmessage); +} + +void GameConnection::frame(float seconds) +{ + if (!running()) + return; + + if (!connection_network->connected()) { + abort(); + return; + } + + connection_network->frame(seconds); +} + +} + |