Dragonfly 4.20
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 SET_GAME_OVER, // Set game over in GM.
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 event.
71 /// Return 1 if handled, else 0 if ignored.
72 virtual int eventHandler(const Event *p_e) override;
73
74 /// Handle connect network event (base class does nothing).
75 /// Return 1 if handled, else 0 if ignored.
76 virtual int handleConnect(const EventNetwork *p_e);
77
78 /// Handle closed socket (base class does nothing).
79 /// Return 1 if handled, else 0 if ignored.
80 virtual int handleClose(const EventNetwork *p_e);
81
82 /// Handle accept network event (base class does nothing).
83 /// Return 1 if handled, else 0 if ignored.
84 virtual int handleAccept(const EventNetwork *p_e);
85
86 /// Handle data network event using data in m_p_buff.
87 /// Return 1 if handled, else 0 if ignored.
88 virtual int handleData(const EventNetwork *p_e);
89
90 /// Send message (supporting various message types).
91 /// Send message from Server to Client.
92 /// SET_GAME_OVER
93 /// Return 1 if something sent, 0 if nothing sent, -1 if error.
94 int sendMessage(MessageType msg_type, int sock_index=-1);
95
96 /// Send message from Server to Client.
97 /// SYNC_OBJECT or DELETE_OBJECT
98 /// Synchronize attr, passed to serialize() (default is sync all).
99 /// Return 1 if something sent, 0 if nothing sent, -1 if error.
100 int sendMessage(MessageType msg_type, Object *p_obj,
101 unsigned int attr=0, int sock_index=-1);
102
103 /// Send message from Client to Server.
104 /// KEYBOARD_INPUT
105 /// Return 1 if something sent, 0 if nothing sent, -1 if error.
106 int sendMessage(MessageType msg_type, EventKeyboardAction action,
107 Keyboard::Key key, int sock_index=-1);
108
109 /// Send message from Client to Server.
110 /// MOUSE_INPUT
111 /// Return 1 if something sent, 0 if nothing sent, -1 if error.
112 int sendMessage(MessageType msg_type, EventMouseAction action,
113 Mouse::Button button, Vector mouse_position,
114 int sock_index=-1);
115
116 /// Send message from Client to Server.
117 /// CUSTOM_MESSAGE
118 /// Return 1 if something sent, 0 if nothing sent, -1 if error.
119 int sendMessage(MessageType msg_type, int num_bytes, const void *bytes,
120 int sock_index=-1);
121
122 /// Create Object of given type.
123 /// Return pointer to Object.
124 virtual Object *createObject(std::string object_type);
125
126 private:
127
128 /// Increase size of network message buffer (if needed).
129 /// Return 0 if ok, else -1 if error.
130 int sizeBuffer(int msg_size);
131
132 /// Prepare message buffer and header.
133 /// Return 0 if ok, else -1 if error.
134 int prepMessageHeader(MessageType msg_type, int msg_size);
135};
136
137} // end of namespace df
138
139#endif // __NETWORK_NODE_H__
140
141#endif // NO_NETWORK
Definition: EventNetwork.h:24
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_index=-1)
Send message from Client to Server.
virtual int handleConnect(const EventNetwork *p_e)
Handle connect network event (base class does nothing).
int sendMessage(MessageType msg_type, Object *p_obj, unsigned int attr=0, int sock_index=-1)
Send message from Server to Client.
virtual int handleData(const EventNetwork *p_e)
Handle data network event using data in m_p_buff.
int sendMessage(MessageType msg_type, int num_bytes, const void *bytes, int sock_index=-1)
Send message from Client to Server.
virtual int handleAccept(const EventNetwork *p_e)
Handle accept network event (base class does nothing).
virtual int handleClose(const EventNetwork *p_e)
Handle closed socket (base class does nothing).
int sendMessage(MessageType msg_type, EventMouseAction action, Mouse::Button button, Vector mouse_position, int sock_index=-1)
Send message from Client to Server.
int sendMessage(MessageType msg_type, int sock_index=-1)
Send message (supporting various message types).
virtual Object * createObject(std::string object_type)
Create Object of given type.
int sizeBuffer(int msg_size)
Increase size of network message buffer (if needed).
virtual int eventHandler(const Event *p_e) override
Handle network event.
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