Dragonfly 4.20
A text-based game engine
NetworkManager.h
1//
2// NetworkManager.h
3//
4// Manage network connections to/from engine.
5//
6
7#ifndef NO_NETWORK
8
9#ifndef __NETWORK_MANAGER_H__
10#define __NETWORK_MANAGER_H__
11
12// System includes.
13#include <queue>
14#include <string>
15#include <vector>
16
17// Engine includes.
18#include "Manager.h"
19
20// Two-letter acronym for easier access to manager.
21#define NM df::NetworkManager::getInstance()
22
23namespace df {
24
25const std::string DRAGONFLY_PORT = "9876";
26
27// Delayed messages: time (ticks), message (char *), length (bytes).
28using delayedMessage = std::tuple<int, char *, size_t>;
29using delayQueue = std::queue<delayedMessage>;
30
31class NetworkManager : public Manager {
32
33 private:
34 NetworkManager(); // Private since a singleton.
35 NetworkManager(NetworkManager const&); // Don't allow copy.
36 void operator=(NetworkManager const&); // Don't allow assignment.
37 int m_accept_sock; // Socket for accept connections.
38 std::vector<int> m_sock; // Connected network sockets.
39 int m_max_sock; // Limit of connected sockets.
40 std::vector<int> m_delay; // Delay for outgoing mgs per socket.
41 std::vector<delayQueue> m_delay_q; // Queue for outgoing delayed msgs per socket.
42
43public:
44
45 /// Get the one and only instance of the NetworkManager.
47
48 /// Start up NetworkManager.
49 int startUp() override;
50
51 /// Shut down NetworkManager.
52 void shutDown() override;
53
54 /// Return true only for network events.
55 /// Return false for other engine events.
56 bool isValid(std::string event_type) const override;
57
58 /// Setup NetworkManager as server (if false, reset to client).
59 /// Return 0 if ok, else -1.
60 int setServer(bool server=true, std::string port = DRAGONFLY_PORT);
61
62 /// Return true if successufully setup as server, else false.
63 bool isServer() const;
64
65 /// Accept network connection.
66 /// If successful, generate EventNetwork (accept).
67 /// Return sock index if new connection (note, 0 for first).
68 /// Return -1 if no new connection, but no error.
69 /// Return -2 if error.
70 int accept();
71
72 /// Make network connection to host at port.
73 /// If successful, generate EventNetwork (connect).
74 /// Return socket index if success, -1 if error.
75 int connect(std::string host, std::string port = DRAGONFLY_PORT);
76
77 /// Set maximum number of connections.
78 void setMaxConnections(int new_max_sock);
79
80 /// Get maximum number of connections.
81 int getMaxConnections() const;
82
83 /// Get number of connected sockets.
84 int getNumConnections() const;
85
86 /// Send bytes from buffer to connected network socket.
87 /// Return 0 if success, -1 if error.
88 int send(const void *buffer, int bytes, int sock_index=0);
89
90 /// Receive from connected network socket (no more than bytes).
91 /// If leave is true, don't remove data from socket (peek).
92 /// Return number of bytes received, -1 if error.
93 int receive(void *buffer, int bytes, bool leave, int sock_index=0);
94
95 /// Close network connection on indicated socket.
96 /// If successful, generate EventNetwork (close).
97 /// Return 0 if success, else -1.
98 int close(int sock_index=0);
99
100 /// Close all network connections.
101 /// If successful, generate EventNetwork (close), for each.
102 /// Return 0 if success, else negative number.
103 int closeAll();
104
105 /// If complete network message on indicated socket, (first
106 /// int is message size, in bytes) generate EventNetwork (data).
107 /// Return 1 if generated event, 0 if no message, -1 if error.
108 int genDataEvents(int sock_index=0) const;
109
110 /// Return true if socket is connected, else false.
111 bool isConnected(int sock_index=0) const;
112
113 /// Check if network data on indicated socket.
114 /// Return amount of data (0 if no data), -1 if not connected or error.
115 int isData(int sock_index=0) const;
116
117 /// Check if data on any connected socket.
118 /// Return index of first socket with data.
119 /// Return -1 if no socket has data.
120 int isAnyData() const;
121
122 /// Return system socket from socket index, -1 if error.
123 int getSocket(int sock_index=0) const;
124
125 /// Set delay amount added to outgoing messages (in ticks).
126 /// Return 0 if ok, else -1.
127 int setDelay(int new_delay, int sock_index);
128
129 ////////////////////////////////////////////////////
130 // Helper methods for adding delay to outgoing messages.
131
132 // Send any delayed messages that have expired.
133 // Return 0 if success, -1 if error.
134 int sendDelayed();
135
136 // Send bytes from buffer to connected network socket
137 // immediately, without delay added via setDelay().
138 // Return 0 if success, -1 if error.
139 int sendNow(const void *buffer, int bytes, int sock_index=0);
140 ////////////////////////////////////////////////////
141
142};
143
144} // end of namespace df
145
146#endif // __NETWORK_MANAGER_H__
147
148#endif // NO_NETWORK
Definition: Manager.h:24
Definition: NetworkManager.h:31
bool isConnected(int sock_index=0) const
Return true if socket is connected, else false.
int close(int sock_index=0)
Close network connection on indicated socket.
int receive(void *buffer, int bytes, bool leave, int sock_index=0)
Receive from connected network socket (no more than bytes).
int genDataEvents(int sock_index=0) const
If complete network message on indicated socket, (first int is message size, in bytes) generate Event...
int closeAll()
Close all network connections.
int getMaxConnections() const
Get maximum number of connections.
int getNumConnections() const
Get number of connected sockets.
int accept()
Accept network connection.
void shutDown() override
Shut down NetworkManager.
int connect(std::string host, std::string port=DRAGONFLY_PORT)
Make network connection to host at port.
bool isValid(std::string event_type) const override
Return true only for network events.
int isAnyData() const
Check if data on any connected socket.
bool isServer() const
Return true if successufully setup as server, else false.
int setDelay(int new_delay, int sock_index)
Set delay amount added to outgoing messages (in ticks).
void setMaxConnections(int new_max_sock)
Set maximum number of connections.
int send(const void *buffer, int bytes, int sock_index=0)
Send bytes from buffer to connected network socket.
int startUp() override
Start up NetworkManager.
int getSocket(int sock_index=0) const
Return system socket from socket index, -1 if error.
int setServer(bool server=true, std::string port=DRAGONFLY_PORT)
Setup NetworkManager as server (if false, reset to client).
static NetworkManager & getInstance()
Get the one and only instance of the NetworkManager.
int isData(int sock_index=0) const
Check if network data on indicated socket.
An animation for a sprite.
Definition: Animation.h:15