# HG changeset patch # User terom # Date 1228333237 0 # Node ID f41f894213cada0c3dc2e3f43013baa551eab06f # Parent 0738f2949a2bdc0e56b5b95985f6075849baa8f8 restructure network code a bit diff -r 0738f2949a2b -r f41f894213ca src/Network/Address.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 + +std::ostream& operator<< (std::ostream &s, const NetworkAddress &addr) { + s << "[" << addr.get_address() << ":" << addr.get_port() << "]"; + + return s; + +} + diff -r 0738f2949a2b -r f41f894213ca src/Network/Client.cc --- 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 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); diff -r 0738f2949a2b -r f41f894213ca src/Network/Client.hh --- 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; diff -r 0738f2949a2b -r f41f894213ca src/Network/Network.cc --- 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 -#include - -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 -} - diff -r 0738f2949a2b -r f41f894213ca src/Network/Network.hh --- 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 - -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 diff -r 0738f2949a2b -r f41f894213ca src/Network/Protocol.hh --- /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 diff -r 0738f2949a2b -r f41f894213ca src/Network/Server.cc --- 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 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); diff -r 0738f2949a2b -r f41f894213ca src/Network/Server.hh --- 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 players; diff -r 0738f2949a2b -r f41f894213ca src/Network/Socket.cc --- /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 + +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 +} +