restructure network code a bit
authorterom
Wed, 03 Dec 2008 19:40:37 +0000
changeset 187 f41f894213ca
parent 186 0738f2949a2b
child 188 156df474ad00
restructure network code a bit
src/Network/Address.cc
src/Network/Client.cc
src/Network/Client.hh
src/Network/Network.cc
src/Network/Network.hh
src/Network/Protocol.hh
src/Network/Server.cc
src/Network/Server.hh
src/Network/Socket.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Network/Address.cc	Wed Dec 03 19:40:37 2008 +0000
@@ -0,0 +1,12 @@
+
+#include "Address.hh"
+
+#include <sstream>
+
+std::ostream& operator<< (std::ostream &s, const NetworkAddress &addr) {
+    s << "[" << addr.get_address() << ":" << addr.get_port() << "]";
+
+    return s;
+
+}
+ 
--- a/src/Network/Client.cc	Wed Dec 03 19:30:54 2008 +0000
+++ b/src/Network/Client.cc	Wed Dec 03 19:40:37 2008 +0000
@@ -1,12 +1,14 @@
 
 #include "Client.hh"
+#include "Protocol.hh"
+#include "Config.hh"
 #include "../Engine.hh"
 #include "../Logger.hh"
 
 #include <cassert>
 
 NetworkClient::NetworkClient (GameState &state, const NetworkAddress &connect_to) : 
-    NetworkCore(state), netsession(NETWORK_MAGIC_ID), server(netsession.connect(connect_to)), netobjs(netsession, NETCHAN_CORE, server) {
+    state(state), netsession(NETWORK_MAGIC_ID), server(netsession.connect(connect_to)), netobjs(netsession, NETCHAN_CORE, server) {
     
     // connect slots
     slots.connect(netobjs.sig_create(), this, &NetworkClient::on_create);
--- a/src/Network/Client.hh	Wed Dec 03 19:30:54 2008 +0000
+++ b/src/Network/Client.hh	Wed Dec 03 19:40:37 2008 +0000
@@ -2,7 +2,6 @@
 #define NETWORKCLIENT_HH
 
 #include "../GameState.hh"
-#include "Network.hh"
 #include "Session.hh"
 #include "Object.hh"
 
@@ -10,11 +9,14 @@
 class NetworkClientLocalPlayer;
 class NetworkClientRemotePlayer;
 
-class NetworkClient : public NetworkCore {
+class NetworkClient {
     friend class NetworkClientLocalPlayer;
     friend class NetworkClientRemotePlayer;
 
-    private:
+    protected:
+        GameState &state;
+        CL_SlotContainer slots;
+
         NetworkSession netsession;
         NetworkNode *server;
 
--- a/src/Network/Network.cc	Wed Dec 03 19:30:54 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,29 +0,0 @@
-#include "Network.hh"
-#include "Address.hh"
-#include "Socket.hh"
-#include "../Engine.hh"
-
-#include <sstream>
-#include <cstring>
-
-std::ostream& operator<< (std::ostream &s, const NetworkAddress &addr) {
-    s << "[" << addr.get_address() << ":" << addr.get_port() << "]";
-
-    return s;
-
-}
-        
-std::string NetworkSocketError::build_str (const NetworkSocket &socket, const char *op, const char *err) {
-    std::stringstream ss;
-
-    ss << "socket #" << socket.get_socket() << " " << op << ": " << err;
-
-    return ss.str();
-}
-
-NetworkSocketError::NetworkSocketError (const NetworkSocket &socket, const char *op, const char *err) :
-    Error(build_str(socket, op, err)) {
-    
-    // nothing
-}
-
--- a/src/Network/Network.hh	Wed Dec 03 19:30:54 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,83 +0,0 @@
-#ifndef NETWORK_HH
-#define NETWORK_HH
-
-#include "Config.hh"
-#include "../GameState.hh"
-
-#include <ClanLib/network.h>
-
-const int32_t COORDINATE_MAX = 1 << 30;
-
-class NetworkCore {
-    protected:
-        GameState &state;
-
-        CL_SlotContainer slots;
-
-        // constructor
-        NetworkCore (GameState &state) : state(state) { }
-
-
-
-
-};
-
-enum NetworkChannel {
-    /*
-     * Core channel used for NetworkSession
-     */
-    NETCHAN_CORE            = 0x01,
-};
-
-enum NetworkPhysicsFlags {
-    NETWORK_PHYSICS_INAIR      = 0x01,
-};
-
-enum NetworkMessage {
-    NETMSG_PACKET_INVALID   = 0x00,
-
-    /*
-     * You have joined the game:
-     *
-     *  Vector      initial_position
-     */
-    NETMSG_SERVER_HELLO = 0x0100,
-
-    /*
-     * New client has connected to server:
-     *  
-     *  Vector      initial_position
-     */
-    NETMSG_PLAYER_JOIN  = 0x0101,
-
-    /*
-     * Client has left server:
-     *
-     */
-    NETMSG_PLAYER_QUIT  = 0x0102,
-
-    /*
-     * Client has moved
-     *
-     *  uint16_t    PlayerInput_Move
-     */
-    NETMSG_CLIENT_MOVE  = 0x0201,
-    
-    /*
-     * Initial player info
-     *
-     *  Vector      initial_position
-     */
-    NETMSG_PLAYER_INFO  = 0x0300,
-
-    /*
-     * Player position update
-     *
-     * Vector   position
-     * Vector   velocity
-     * uint8_t  NetworkPhysicsFlags
-     */
-    NETMSG_PLAYER_POSITION  = 0x0301,
-};
-
-#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Network/Protocol.hh	Wed Dec 03 19:40:37 2008 +0000
@@ -0,0 +1,66 @@
+#ifndef NETWORK_PROTOCOL_HH
+#define NETWORK_PROTOCOL_HH
+
+/*
+ * The network protocol is defined here :o
+ */
+
+enum NetworkChannel {
+    /*
+     * Core channel used for NetworkSession
+     */
+    NETCHAN_CORE            = 0x01,
+};
+
+enum NetworkPhysicsFlags {
+    NETWORK_PHYSICS_INAIR      = 0x01,
+};
+
+enum NetworkMessage {
+    NETMSG_PACKET_INVALID   = 0x00,
+
+    /*
+     * You have joined the game:
+     *
+     *  Vector      initial_position
+     */
+    NETMSG_SERVER_HELLO = 0x0100,
+
+    /*
+     * New client has connected to server:
+     *  
+     *  Vector      initial_position
+     */
+    NETMSG_PLAYER_JOIN  = 0x0101,
+
+    /*
+     * Client has left server:
+     *
+     */
+    NETMSG_PLAYER_QUIT  = 0x0102,
+
+    /*
+     * Client has moved
+     *
+     *  uint16_t    PlayerInput_Move
+     */
+    NETMSG_CLIENT_MOVE  = 0x0201,
+    
+    /*
+     * Initial player info
+     *
+     *  Vector      initial_position
+     */
+    NETMSG_PLAYER_INFO  = 0x0300,
+
+    /*
+     * Player position update
+     *
+     * Vector   position
+     * Vector   velocity
+     * uint8_t  NetworkPhysicsFlags
+     */
+    NETMSG_PLAYER_POSITION  = 0x0301,
+};
+
+#endif
--- a/src/Network/Server.cc	Wed Dec 03 19:30:54 2008 +0000
+++ b/src/Network/Server.cc	Wed Dec 03 19:40:37 2008 +0000
@@ -1,11 +1,14 @@
+
 #include "Server.hh"
+#include "Protocol.hh"
+#include "Config.hh"
 #include "../Engine.hh"
 #include "../Logger.hh"
 
 #include <cassert>
 
 NetworkServer::NetworkServer (GameState &state, const NetworkAddress &listen_addr) : 
-    NetworkCore(state), netsession(NETWORK_MAGIC_ID), netobjs(netsession, NETCHAN_CORE) {
+    state(state), netsession(NETWORK_MAGIC_ID), netobjs(netsession, NETCHAN_CORE) {
     
     // connect slots
     slots.connect(netsession.sig_node_connected(), this, &NetworkServer::on_node_connected);
--- a/src/Network/Server.hh	Wed Dec 03 19:30:54 2008 +0000
+++ b/src/Network/Server.hh	Wed Dec 03 19:40:37 2008 +0000
@@ -1,7 +1,6 @@
 #ifndef NETWORKSERVER_HH
 #define NETWORKSERVER_HH
 
-#include "Network.hh"
 #include "../GameState.hh"
 #include "Session.hh"
 #include "Object.hh"
@@ -13,10 +12,13 @@
 // forward-declare
 class NetworkServerPlayer;
 
-class NetworkServer : public NetworkCore {
+class NetworkServer {
     friend class NetworkServerPlayer;
 
     protected:
+        GameState &state;
+        CL_SlotContainer slots;
+
         NetworkSession netsession;
         NetworkObject_ServerController netobjs;
         std::list<NetworkServerPlayer *> players;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Network/Socket.cc	Wed Dec 03 19:40:37 2008 +0000
@@ -0,0 +1,19 @@
+
+#include "Socket.hh"
+
+#include <sstream>
+
+std::string NetworkSocketError::build_str (const NetworkSocket &socket, const char *op, const char *err) {
+    std::stringstream ss;
+
+    ss << "socket #" << socket.get_socket() << " " << op << ": " << err;
+
+    return ss.str();
+}
+
+NetworkSocketError::NetworkSocketError (const NetworkSocket &socket, const char *op, const char *err) :
+    Error(build_str(socket, op, err)) {
+    
+    // nothing
+}
+