diff options
author | Stijn Buys <ingar@osirion.org> | 2008-02-16 14:31:02 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-02-16 14:31:02 +0000 |
commit | d198b7b8d9ff713d891f35ab173d1f428f610e7d (patch) | |
tree | 6a1f76ee5788ee3dfac858e2c8233207637c01bc /src | |
parent | d6ee7ec642cc6b3097c8d321a1a00630e24027d1 (diff) |
code cleanup
Diffstat (limited to 'src')
-rw-r--r-- | src/core/application.cc | 14 | ||||
-rw-r--r-- | src/core/application.h | 2 | ||||
-rw-r--r-- | src/core/core.cc | 48 | ||||
-rw-r--r-- | src/core/core.h | 24 | ||||
-rw-r--r-- | src/core/gameinterface.cc | 55 | ||||
-rw-r--r-- | src/core/gameinterface.h | 21 | ||||
-rw-r--r-- | src/core/netserver.cc | 40 | ||||
-rw-r--r-- | src/core/netserver.h | 7 | ||||
-rw-r--r-- | src/game/game.cc | 17 | ||||
-rw-r--r-- | src/game/game.h | 1 | ||||
-rw-r--r-- | src/server/server.cc | 5 |
11 files changed, 105 insertions, 129 deletions
diff --git a/src/core/application.cc b/src/core/application.cc index 55f8fc2..ce7460f 100644 --- a/src/core/application.cc +++ b/src/core/application.cc @@ -30,6 +30,12 @@ Cvar sv_private; Cvar net_host; Cvar net_port; +// return the global application object +Application *application() +{ + return Application::instance(); +} + // --------------- engine functions ------------------------------ void func_print(std::stringstream &args) { @@ -92,7 +98,7 @@ void func_say(std::stringstream &args) if (application()->netserver) { std::ostringstream osstream; osstream << "msg public " << localplayer.name << " " << args.str() << "\n"; - application()->netserver->broadcast(osstream); + application()->netserver->broadcast(osstream.str()); con_print << localplayer.name << ": " << args.str() << std::endl; } else if (application()->netconnection.connected()) { std::ostringstream osstream; @@ -122,8 +128,8 @@ void func_name(std::stringstream &args) { if (application()->netserver) { std::ostringstream osstream; osstream << "msg info " << localplayer.name << " renamed to " << name << "\n"; - application()->netserver->broadcast(osstream); - con_print << "name " << name << std::endl; + application()->netserver->broadcast(osstream.str()); + con_print << "msg info " << localplayer.name << " renamed to " << name << std::endl; } else if (application()->netconnection.connected()) { std::ostringstream osstream; osstream << args.str() << std::endl; @@ -211,7 +217,7 @@ void Application::init() gameinterface_preload = core::GameInterface::gameinterface_instance; core::GameInterface::gameinterface_instance = 0; if (gameinterface_preload) { - con_print << " preloaded game found: " << gameinterface_preload->name << std::endl; + con_print << " preloaded game found: " << gameinterface_preload->name() << std::endl; } game_time = 0; diff --git a/src/core/application.h b/src/core/application.h index 5bdc6ef..81e6a64 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -79,6 +79,8 @@ private: }; +/// pointer to the current ApplicationInterface +Application *application(); } // namespace core diff --git a/src/core/core.cc b/src/core/core.cc index b1083a8..b2327ee 100644 --- a/src/core/core.cc +++ b/src/core/core.cc @@ -4,55 +4,7 @@ the terms of the GNU General Public License version 2. */ -#include <iostream> -#include <sstream> - -#include "core/core.h" -#include "core/netclient.h" - - namespace core { -GameInterface *game() -{ - return GameInterface::instance(); -} - -Application *application() -{ - return Application::instance(); -} - - -void net_broadcast(std::ostringstream &osstream, int ignoreplayer) -{ - if (!application()->netserver) - return; - - application()->netserver->broadcast(osstream, ignoreplayer); -} - -void net_send(Player &player, std::ostringstream &osstream) -{ - if (!application()->netserver) - return; - - NetClient *client = application()->netserver->find_client(player); - if (client) - application()->netserver->send(client, osstream); -} - - -void net_send(Player &player, std::string message) -{ - if (!application()->netserver) - return; - - NetClient *client = application()->netserver->find_client(player); - if (client) - application()->netserver->send(client, message); -} - - } diff --git a/src/core/core.h b/src/core/core.h index 0fc7e8d..4e2af22 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -7,33 +7,21 @@ #ifndef __INCLUDED_CORE_H__ #define __INCLUDED_CORE_H__ -#include "core/entity.h" -#include "core/player.h" -#include "core/gameinterface.h" #include "core/application.h" #include "core/commandbuffer.h" #include "core/cvar.h" +#include "core/entity.h" #include "core/func.h" +#include "core/gameinterface.h" +#include "core/netserver.h" +#include "core/netclient.h" +#include "core/netconnection.h" +#include "core/player.h" /// core contains the basic functionality of the engine namespace core { -/// pointer to the current GameInterface -GameInterface *game(); - -/// pointer to the current ApplicationInterface -Application *application(); - -/// broadcast a network message to all connected clients -void net_broadcast(std::ostringstream &osstream, int ignoreplayer=-1); - -/// send a network message to a player -void net_send(Player &player, std::ostringstream &osstream); - -/// send a network message to a player -void net_send(Player &player, std::string message); - } #endif // __INCLUDED_CORE_H__ diff --git a/src/core/gameinterface.cc b/src/core/gameinterface.cc index a4a402d..d07f804 100644 --- a/src/core/gameinterface.cc +++ b/src/core/gameinterface.cc @@ -9,22 +9,29 @@ class GameInterface; +#include "core/application.h" #include "core/gameinterface.h" #include "core/player.h" +#include "sys/sys.h" namespace core { +GameInterface *game() +{ + return GameInterface::instance(); +} + GameInterface *GameInterface::gameinterface_instance = 0; -GameInterface::GameInterface(const char *game_name) +GameInterface::GameInterface(const char *gamename) { gameinterface_instance = this; connected = false; - if (game_name) - name.assign(game_name); + if (gamename) + game_name.assign(gamename); else - name.clear(); + game_name.clear(); } GameInterface::~GameInterface() @@ -37,4 +44,44 @@ GameInterface *GameInterface::instance() return gameinterface_instance; } +std::string const &GameInterface::name() +{ + return game_name; +} + +void message_broadcast(std::string const & message, int ignoreplayer) +{ + // send to console + con_print << message << std::endl; + + // broadcast to remote clients + if (application()->netserver) { + std::string netmessage("msg info "); + netmessage.append(message); + netmessage += '\n'; + + application()->netserver->broadcast(netmessage, ignoreplayer); + } +} + +void message_send(Player const &player, const char *protohdr, std::string message) +{ + // send to console + if (&player == &localplayer) { + con_print << message << std::endl; + } + + // send to remote clients + if (application()->netserver) { + NetClient *client = application()->netserver->find_client(player); + if (client) { + std::string netmessage("msg info "); + netmessage.append(message); + netmessage += '\n'; + + application()->netserver->send(client, message); + } + } +} + } // namespace core diff --git a/src/core/gameinterface.h b/src/core/gameinterface.h index 4b18a9e..457df86 100644 --- a/src/core/gameinterface.h +++ b/src/core/gameinterface.h @@ -12,14 +12,18 @@ namespace core { +/// broadcast a network message to all players +void message_broadcast(std::string const & message, int ignoreplayer=-1); + +/// send a message to a player +void message_send(Player &player, std::string const & message); + /// abstract interface from the core to the game-specific code -/** The real game class has to derive from this class - */ class GameInterface { public: /// create a new game - GameInterface(const char *game_name = 0); + GameInterface(const char *gamename = 0); /// destroy the game virtual ~GameInterface(); @@ -29,9 +33,6 @@ public: /// true if the game is ready and running bool connected; - /// time the game has been running, in seconds - float current_time; - /// run one frame of the game /// @param sec time since the previous frame, in seconds virtual void frame(float seconds) = 0; @@ -51,9 +52,15 @@ public: static GameInterface *gameinterface_instance; /// the name of the game - std::string name; + std::string const & name(); + +private: + std::string game_name; }; +/// pointer to the current GameInterface +GameInterface *game(); + } #endif // __INCLUDED_CORE_GAMEINTERFACE_H__ diff --git a/src/core/netserver.cc b/src/core/netserver.cc index 072e790..08863df 100644 --- a/src/core/netserver.cc +++ b/src/core/netserver.cc @@ -58,8 +58,12 @@ void NetServer::client_connect(int const clientfd, std::string const host, int c FD_SET(client->fd(), &serverset); // TODO send infos + con_print << client->host() << ":" << client->port() << " " << client->player().name << " connected."<< std::endl; - con_print << client->host() << ":" << client->port() << " connected."<< std::endl; + // BROADCAST connect message + std::ostringstream osstream; + osstream << "msg info " << client->player().name << " connected."<< std::endl; + broadcast(osstream.str(), clientfd); // notify the game if (game() && game()->connected) { @@ -67,11 +71,6 @@ void NetServer::client_connect(int const clientfd, std::string const host, int c } else { // TODO send disconnect message and remove client } - - // BROADCAST connect message - std::ostringstream osstream; - osstream << "msg info " << client->player().name << " connected."<< std::endl; - broadcast(osstream); } // remove disconnected clients @@ -86,7 +85,7 @@ void NetServer::reap() // BROADCAST disconnect message std::ostringstream osstream; osstream << "msg info " << client->player().name << " disconnected."<< std::endl; - broadcast(osstream); + broadcast(osstream.str()); // notify the game if (core::game()) @@ -147,29 +146,20 @@ void NetServer::frame(float seconds) { reap(); } -// send a message to one client -void NetServer::send(NetClient * client, std::ostringstream &osstream) { - // FIXME large messages - //std::cout << "NetServer: " << osstream.str(); - client->send(osstream.str()); - osstream.str(""); -} - -void NetServer::send(NetClient * client, std::string message) { - //std::cout << "NetServer: " << message; +void NetServer::send(NetClient * client, std::string const & message) +{ client->send(message); } // send a message to all clients -void NetServer::broadcast(std::ostringstream &osstream, int ignorefd) +void NetServer::broadcast(std::string const & message, int ignorefd) { //std::cout << "NetServer: " << osstream.str(); for (std::list<NetClient *>::iterator it = clients.begin(); it != clients.end(); it++) { NetClient *client = *it; if (!client->error() && client->fd() != ignorefd) - client->send(osstream.str()); + client->send(message); } - osstream.str(""); } // find the client corresponding to a player id @@ -212,7 +202,7 @@ void NetServer::parse_incoming_message(NetClient *client, const std::string & me if (message.size() > command.size()+1) { std::ostringstream osstream; osstream << "msg public " << client->player().name << " " << message.substr(command.size()+1) << "\n"; - broadcast(osstream); + broadcast(osstream.str()); con_print << client->player().name << " " << message.substr(command.size()+1) << std::endl; } return; @@ -227,8 +217,8 @@ void NetServer::parse_incoming_message(NetClient *client, const std::string & me if (name != client->player().name) { std::ostringstream osstream; osstream << "msg info " << client->player().name << " renamed to " << name << "\n"; - broadcast(osstream); - con_print << "name " << name << std::endl; + broadcast(osstream.str()); + con_print << client->player().name << " renamed to " << name << std::endl; client->player().name = name; } } @@ -241,7 +231,7 @@ void NetServer::parse_incoming_message(NetClient *client, const std::string & me osstream << "msg info " << (*it)->player().name << " " << (*it)->host() << ":" << (*it)->port() << "\n"; } osstream << "msg info " << clients.size() << " connected players\n" << std::endl; - send(client, osstream); + send(client, osstream.str()); } if (command == "help") { @@ -279,7 +269,7 @@ void NetServer::parse_client_variable(NetClient * client, const std::string varn name = name.substr(0,16); if (name != client->player().name) { osstream << "msg info " << client->player().name << " renamed to " << name << "\n"; - broadcast(osstream); + broadcast(osstream.str()); client->player().name = name; } } diff --git a/src/core/netserver.h b/src/core/netserver.h index ab4976d..8ebd83d 100644 --- a/src/core/netserver.h +++ b/src/core/netserver.h @@ -29,13 +29,10 @@ public: void frame(float seconds); /// broadcast a message to all clients - void broadcast(std::ostringstream &osstream, int ignorefd=-1); + void broadcast(std::string const & message, int ignorefd=-1); /// send a message to a client - void send(NetClient * client, std::ostringstream &osstream); - - /// send a message to a client - void send(NetClient * client, std::string message); + void send(NetClient * client, std::string const & message); /// find the client corresponding to a player NetClient *find_client(Player const &player); diff --git a/src/game/game.cc b/src/game/game.cc index 7dff676..a9d5a6e 100644 --- a/src/game/game.cc +++ b/src/game/game.cc @@ -30,13 +30,9 @@ void func_join(core::Player &player, std::stringstream &args) ship->owner = &player; player.control = ship; - // net message std::ostringstream osstream; - osstream << "msg info " << player.name << " joins the game\n"; - core::net_broadcast(osstream, player.id); - - // console message - con_print << player.name << " joins the game" << std::endl; + osstream << player.name << " joins the game"; + core::message_broadcast(osstream.str(), player.id); } /// a player joins the spectators @@ -45,13 +41,9 @@ void func_spectate(core::Player &player, std::stringstream &args) if (!player.control) return; - // net message std::ostringstream osstream; - osstream << "msg info " << player.name << " spectates\n"; - core::net_broadcast(osstream, player.id); - - // console message - con_print << player.name << " spectates" << std::endl; + osstream << player.name << " spectates"; + core::message_broadcast(osstream.str(), player.id); if (player.control) { // player has only ship for now @@ -75,6 +67,7 @@ bool Game::init() //using filesystem::IniFile; con_print << "Initializing game..." << std::endl; + con_print << " " << name() << std::endl; /* // read game.ini diff --git a/src/game/game.h b/src/game/game.h index 3811a06..7a6348e 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -46,7 +46,6 @@ public: std::vector<Sector*> sectors; private: - std::string name; std::string label; std::string author; }; diff --git a/src/server/server.cc b/src/server/server.cc index dde1346..98efdab 100644 --- a/src/server/server.cc +++ b/src/server/server.cc @@ -8,7 +8,6 @@ #include "server/console.h" #include "server/server.h" #include "server/timer.h" -#include "game/game.h" namespace server { @@ -31,7 +30,6 @@ public: }; Server app; -game::Game *game; //--- public ------------------------------------------------------ @@ -94,9 +92,6 @@ void Server::shutdown() void Server::quit(int status) { - // FIXME core should be able to unload game.so - delete server::game; - core::Application::quit(status); } |