34
|
1 |
#ifndef NETWORK_OBJECT_HH
|
|
2 |
#define NETWORK_OBJECT_HH
|
|
3 |
|
36
|
4 |
#include "NetworkSession.hh"
|
|
5 |
#include "NetworkNode.hh"
|
|
6 |
#include "Logger.hh"
|
|
7 |
|
34
|
8 |
#include <map>
|
|
9 |
|
|
10 |
typedef uint32_t NetworkObjectID;
|
|
11 |
typedef uint16_t NetworkMessageID;
|
|
12 |
|
36
|
13 |
// forward-declare
|
|
14 |
class NetworkObject;
|
|
15 |
class NetworkObject_Client;
|
|
16 |
class NetworkObject_Server;
|
|
17 |
|
34
|
18 |
class NetworkObjectController {
|
|
19 |
friend class NetworkObject;
|
37
|
20 |
friend class NetworkObject_Server;
|
|
21 |
friend class NetworkObject_Client;
|
34
|
22 |
|
|
23 |
private:
|
|
24 |
NetworkSession &session;
|
|
25 |
NetworkChannelID channel_id;
|
|
26 |
|
|
27 |
std::map<NetworkObjectID, NetworkObject*> objects;
|
|
28 |
|
|
29 |
CL_Slot slot_message;
|
|
30 |
|
|
31 |
protected:
|
|
32 |
NetworkObjectController (NetworkSession &session, NetworkChannelID channel_id);
|
|
33 |
|
|
34 |
private:
|
36
|
35 |
void on_message (NetworkPacket &pkt, NetworkNode *node);
|
34
|
36 |
|
|
37 |
protected:
|
36
|
38 |
virtual void handle_create (NetworkObjectID obj_id, NetworkMessageID msg_id, NetworkPacket &pkt, NetworkNode *node) = 0;
|
34
|
39 |
};
|
|
40 |
|
36
|
41 |
class NetworkObject_ServerController : public NetworkObjectController {
|
34
|
42 |
friend class NetworkObject_Server;
|
|
43 |
|
|
44 |
private:
|
|
45 |
NetworkObjectID id_pool;
|
|
46 |
|
|
47 |
public:
|
|
48 |
NetworkObject_ServerController (NetworkSession &session, NetworkChannelID channel_id);
|
|
49 |
|
|
50 |
protected:
|
37
|
51 |
NetworkObjectID getObjectID (void);
|
34
|
52 |
|
36
|
53 |
virtual void handle_create (NetworkObjectID obj_id, NetworkMessageID msg_id, NetworkPacket &pkt, NetworkNode *node);
|
34
|
54 |
};
|
|
55 |
|
36
|
56 |
class NetworkObject_ClientController : public NetworkObjectController {
|
34
|
57 |
friend class NetworkObject_Client;
|
|
58 |
|
|
59 |
private:
|
|
60 |
NetworkNode *server;
|
|
61 |
|
36
|
62 |
CL_Signal_v3<NetworkObject_Client*, NetworkMessageID, NetworkPacket&> _sig_create;
|
34
|
63 |
|
|
64 |
public:
|
|
65 |
NetworkObject_ClientController (NetworkSession &session, NetworkChannelID channel_id, NetworkNode *server);
|
36
|
66 |
|
|
67 |
protected:
|
|
68 |
virtual void handle_create (NetworkObjectID obj_id, NetworkMessageID msg_id, NetworkPacket &pkt, NetworkNode *node);
|
|
69 |
|
|
70 |
public:
|
|
71 |
CL_Signal_v3<NetworkObject_Client*, NetworkMessageID, NetworkPacket&>& sig_create (void) { return _sig_create; }
|
34
|
72 |
};
|
|
73 |
|
|
74 |
class NetworkObject {
|
|
75 |
friend class NetworkObjectController;
|
38
|
76 |
friend std::ostream& operator<< (std::ostream &s, const NetworkObject &obj);
|
34
|
77 |
|
37
|
78 |
protected:
|
34
|
79 |
NetworkObjectID obj_id;
|
|
80 |
|
37
|
81 |
protected:
|
|
82 |
NetworkObject (NetworkObjectController &controller, NetworkObjectID obj_id);
|
34
|
83 |
|
37
|
84 |
virtual void handle_packet (NetworkNode *node, NetworkMessageID msg_id, NetworkPacket &pkt) = 0;
|
34
|
85 |
|
|
86 |
void buildPacket (NetworkPacket &pkt, NetworkMessageID msg_id, const NetworkPacket &payload);
|
|
87 |
};
|
|
88 |
|
36
|
89 |
std::ostream& operator<< (std::ostream &s, const NetworkObject &obj);
|
|
90 |
|
34
|
91 |
class NetworkObject_Server : public NetworkObject {
|
|
92 |
friend class NetworkObject_ServerController;
|
|
93 |
|
|
94 |
private:
|
|
95 |
NetworkObject_ServerController &controller;
|
|
96 |
|
37
|
97 |
std::map<NetworkMessageID, CL_Signal_v2<NetworkNode*, NetworkPacket&> > _map_sig_message;
|
|
98 |
|
34
|
99 |
public:
|
36
|
100 |
NetworkObject_Server (NetworkObject_ServerController &controller);
|
37
|
101 |
|
|
102 |
protected:
|
|
103 |
virtual void handle_packet (NetworkNode *node, NetworkMessageID msg_id, NetworkPacket &pkt);
|
34
|
104 |
|
37
|
105 |
public:
|
34
|
106 |
void send_to (NetworkNode *dst, NetworkMessageID msg_id, const NetworkPacket &pkt, bool reliable = true);
|
|
107 |
void send_all (NetworkMessageID msg_id, const NetworkPacket &pkt, bool reliable = true);
|
|
108 |
void send_all_except (NetworkMessageID msg_id, const NetworkPacket &pkt, NetworkNode *black_sheep, bool reliable = true);
|
37
|
109 |
|
|
110 |
CL_Signal_v2<NetworkNode*, NetworkPacket&>& sig_message (NetworkMessageID msg_id) { return _map_sig_message[msg_id]; }
|
34
|
111 |
};
|
|
112 |
|
|
113 |
class NetworkObject_Client : public NetworkObject {
|
|
114 |
friend class NetworkObject_ClientController;
|
|
115 |
|
|
116 |
private:
|
|
117 |
NetworkObject_ClientController &controller;
|
|
118 |
|
37
|
119 |
std::map<NetworkMessageID, CL_Signal_v1<NetworkPacket&> > _map_sig_message;
|
|
120 |
|
34
|
121 |
protected:
|
37
|
122 |
NetworkObject_Client (NetworkObject_ClientController &controller, NetworkObjectID id);
|
|
123 |
|
|
124 |
virtual void handle_packet (NetworkNode *node, NetworkMessageID msg_id, NetworkPacket &pkt);
|
34
|
125 |
|
|
126 |
public:
|
|
127 |
void send (NetworkMessageID msg_id, const NetworkPacket &pkt, bool reliable = true);
|
37
|
128 |
|
|
129 |
CL_Signal_v1<NetworkPacket&>& sig_message (NetworkMessageID msg_id) { return _map_sig_message[msg_id]; }
|
36
|
130 |
};
|
34
|
131 |
|
|
132 |
#endif /* NETWORK_OBJECT_HH */
|