Dragonfly 4.21
A text-based game engine
NetworkNode.h
1///
2/// NetworkNode.h
3///
4/// Handle incoming network messages.
5///
6
7#ifndef NO_NETWORK
8
9#ifndef __NETWORK_NODE_H__
10#define __NETWORK_NODE_H__
11
12// Engine includes.
13#include "EventKeyboard.h"
14#include "EventMouse.h"
15#include "EventNetwork.h"
16#include "Object.h"
17
18namespace df {
19
20// Network message types.
21enum class MessageType {
22 UNDEFINED_MESSAGE=-1,
23 SYNC_OBJECT, // Create or update object, as appropriate.
24 DELETE_OBJECT, // Delete object.
25 GAME_OVER, // Indicate game over.
26 KEYBOARD_INPUT, // Keyboard event.
27 MOUSE_INPUT, // Mouse event.
28 CUSTOM_MESSAGE, // Game code defined.
29};
30
31// HEADER:
32// 0) Total size is an int (total number of bytes in message).
33// 1) Message type is an int (an enum).
34
35// SYNC_OBJECT:
36// 2) Object id as int.
37// 3) String length as int.
38// 4) Object type as string.
39// 5) Serialized Object data (string of bytes).
40
41// DELETE_OBJECT:
42// 2) Object id as int.
43
44// SET_GAME_OVER:
45// (nothing)
46
47// KEYBOARD:
48// 2) Keyboard action as int (a EventKeyboardAction enum).
49// 3) Key as int (a Keyboard::Key enum).
50
51// MOUSE:
52// 2) Mouse action as int (a EventMouseAction enum).
53// 3) Mouse button as int (a Mouse::Button enum).
54// 4) Mouse-x as float.
55// 5) Mouse-y as float.
56
57// CUSTOM:
58// 2) Bytes as blob.
59
60class NetworkNode : public Object {
61
62 protected:
63 char *m_p_buff; // Data buffer for network communication.
64 int m_buff_size; // Number of bytes allocated.
65
66 public:
69
70 /// Handle network and step events.
71 /// Step events: call genDataEvents().
72 /// Network events: if type DATA, call handleData().
73 /// Return 1 if handled, else 0 if ignored.
74 virtual int eventHandler(const Event *p_e) override;
75
76 /// Handle and generate events based on type.
77 /// GAME_OVER: generate game over network event.
78 /// DELETE: markForDelete() and generate delete network event.
79 /// SYNC: create if needed, then deserialize and generate sync network event.
80 /// KEYBOARD: generate send keyboard network event.
81 /// MOUSE: generate send mouse network event.
82 /// CUSTOM: generate custom network event.
83 /// Return 1 if handled, else 0 if ignored.
84 virtual int handleData(const EventNetwork *p_e);
85
86 ////////////////////////////////////////////////////////////
87 /// Send message (supporting various message types).
88
89 /// Send message from Server to Client.
90 /// SET_GAME_OVER
91 /// Return 1 if something sent, 0 if nothing sent, -1 if error.
92 int sendMessage(MessageType msg_type, int sock=-1);
93
94 /// Send message from Server to Client.
95 /// SYNC_OBJECT or DELETE_OBJECT
96 /// Synchronize attr, passed to serialize() (default is sync all).
97 /// Return 1 if something sent, 0 if nothing sent, -1 if error.
98 int sendMessage(MessageType msg_type, Object *p_obj,
99 unsigned int attr=0, int sock=-1);
100
101 /// Send message from Client to Server.
102 /// KEYBOARD_INPUT
103 /// Return 1 if something sent, 0 if nothing sent, -1 if error.
104 int sendMessage(MessageType msg_type, EventKeyboardAction action,
105 Keyboard::Key key, int sock=-1);
106
107 /// Send message from Client to Server.
108 /// MOUSE_INPUT
109 /// Return 1 if something sent, 0 if nothing sent, -1 if error.
110 int sendMessage(MessageType msg_type, EventMouseAction action,
111 Mouse::Button button, Vector mouse_position,
112 int sock=-1);
113
114 /// Send message from either Client to Server or Server to Client.
115 /// CUSTOM_MESSAGE
116 /// Return 1 if something sent, 0 if nothing sent, -1 if error.
117 int sendMessage(MessageType msg_type, int num_bytes, const void *bytes,
118 int sock=-1);
119
120 ////////////////////////////////////////////////////////////
121
122 /// Called each step to generate data events, when appropriate. If
123 /// complete network message, (first int is message size, in bytes)
124 /// generate EventNetwork (DATA). Return 1 if generated event, 0 if
125 /// no message complete message, -1 if error.
126 int genDataEvents(int sock=0) const;
127
128 ////////////////////////////////////////////////////////////
129
130 /// Create (new) Object of given type.
131 /// Return pointer to Object (NULL if creation failed).
132 virtual Object *createObject(std::string object_type);
133
134 private:
135
136 /// Increase size of network message buffer (if needed).
137 /// Return 0 if ok, else -1 if error.
138 int sizeBuffer(int msg_size);
139
140 /// Prepare message buffer and header.
141 /// Return 0 if ok, else -1 if error.
142 int prepMessageHeader(MessageType msg_type, int msg_size);
143};
144
145} // end of namespace df
146
147#endif // __NETWORK_NODE_H__
148
149#endif // NO_NETWORK
Definition: EventNetwork.h:28
Definition: Event.h:15
Definition: NetworkNode.h:60
int prepMessageHeader(MessageType msg_type, int msg_size)
Prepare message buffer and header.
int sendMessage(MessageType msg_type, EventKeyboardAction action, Keyboard::Key key, int sock=-1)
Send message from Client to Server.
int genDataEvents(int sock=0) const
Called each step to generate data events, when appropriate.
int sendMessage(MessageType msg_type, EventMouseAction action, Mouse::Button button, Vector mouse_position, int sock=-1)
Send message from Client to Server.
virtual int handleData(const EventNetwork *p_e)
Handle and generate events based on type.
virtual Object * createObject(std::string object_type)
Create (new) Object of given type.
int sendMessage(MessageType msg_type, int num_bytes, const void *bytes, int sock=-1)
Send message from either Client to Server or Server to Client.
int sizeBuffer(int msg_size)
Increase size of network message buffer (if needed).
int sendMessage(MessageType msg_type, int sock=-1)
Send message (supporting various message types).
virtual int eventHandler(const Event *p_e) override
Handle network and step events.
int sendMessage(MessageType msg_type, Object *p_obj, unsigned int attr=0, int sock=-1)
Send message from Server to Client.
Definition: Object.h:57
Definition: Vector.h:12
Key
Keys Dragonfly recognizes.
Definition: EventKeyboard.h:25
An animation for a sprite.
Definition: Animation.h:15
EventKeyboardAction
Types of keyboard actions Dragonfly recognizes.
Definition: EventKeyboard.h:15
EventMouseAction
Set of mouse actions recognized by Dragonfly.
Definition: EventMouse.h:16