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