handle NetworkClient server-disconnect by logging an error and stopping the engine
authorterom
Mon, 08 Dec 2008 23:47:48 +0000
changeset 332 78657bf06302
parent 331 65e104a2db8f
child 333 50ec1ab32d2d
handle NetworkClient server-disconnect by logging an error and stopping the engine
src/Engine.cc
src/Network/Client.cc
src/Network/Client.hh
--- a/src/Engine.cc	Mon Dec 08 23:44:45 2008 +0000
+++ b/src/Engine.cc	Mon Dec 08 23:47:48 2008 +0000
@@ -25,7 +25,7 @@
     CL_IPAddress connect_addr(connect_host, connect_port);
 
     // create the client
-    net_client = new NetworkClient(game_state, connect_addr);
+    net_client = new NetworkClient(*this, game_state, connect_addr);
 }
 
 void Engine::setupSinglePlayer (void) {
--- a/src/Network/Client.cc	Mon Dec 08 23:44:45 2008 +0000
+++ b/src/Network/Client.cc	Mon Dec 08 23:47:48 2008 +0000
@@ -7,15 +7,20 @@
 
 #include <cassert>
 
-NetworkClient::NetworkClient (GameState &state, const NetworkAddress &connect_to) : 
-    state(state), netsession(NETWORK_MAGIC_ID), server(netsession.connect(connect_to)), controller(*this)
+NetworkClient::NetworkClient (Engine &engine, GameState &state, const NetworkAddress &connect_to) : 
+    engine(engine), state(state), netsession(NETWORK_MAGIC_ID), server(netsession.connect(connect_to)),
+    controller(*this)
 {
     // connect slots
     slots.connect(netsession.sig_chan_message(NETCHAN_TERRAIN_ARRAY), this, &NetworkClient::on_terrain_array);
-
-    // XXX: sig_disconnected
+    slots.connect(server->sig_disconnected(), this, &NetworkClient::on_disconnected);
 }
         
+void NetworkClient::on_disconnected (void) {
+    Engine::log(ERROR, "client.on_disconnect") << "Disconnected from server";
+    engine.stop();
+}
+
 void NetworkClient::on_terrain_array (NetworkPacketInput &pkt, NetworkNode *node) {
     // ignore if not from server
     if (node != server)
--- a/src/Network/Client.hh	Mon Dec 08 23:44:45 2008 +0000
+++ b/src/Network/Client.hh	Mon Dec 08 23:47:48 2008 +0000
@@ -1,15 +1,16 @@
 #ifndef NETWORKCLIENT_HH
 #define NETWORKCLIENT_HH
 
-#include "../GameState.hh"
-#include "Session.hh"
-#include "Object.hh"
-
 // forward-declare
 class NetworkClient;
 class NetworkClientLocalPlayer;
 class NetworkClientRemotePlayer;
 
+#include "../GameState.hh"
+#include "../Engine.hh"
+#include "Session.hh"
+#include "Object.hh"
+
 /**
  * Our specialized NetworkObject_ClientController that overrides handle_create to create the right kind of
  * object (a subclass of NetowrkClientObject).
@@ -70,6 +71,11 @@
 
     protected:
         /**
+         * The Engine
+         */
+        Engine &engine;
+
+        /**
          * The GameState
          */
         GameState &state;
@@ -95,13 +101,19 @@
         /**
          * Create a NetworkClient with the given GameState, connecting a server on the given NetworkAddress
          *
+         * @param engine the Engine we're running as
          * @param state the GameState to use
          * @param connect_to the address to connect to
          */
-        NetworkClient (GameState &state, const NetworkAddress &connect_to);
+        NetworkClient (Engine &engine, GameState &state, const NetworkAddress &connect_to);
     
     protected:
         /**
+         * We have disconnected from the server
+         */
+        void on_disconnected (void);
+
+        /**
          * Receive the NETCHAN_TERRAIN_ARRAY message from the server and apply it to our GameState::world terrain
          */
         void on_terrain_array (NetworkPacketInput &pkt, NetworkNode *node);