Dragonfly 4.21
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// For delayed message sending.
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; // int for accept connections.
38 int m_max_sock; // Limit of connected sockets.
39 std::vector<int> m_sock; // Connected network sockets.
40 std::vector<int> m_delay; // Delay (in ticks) for each socket.
41 std::vector<delayQueue> m_delay_q; // Queue of delayed messages.
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 socket if new connection.
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 return socket 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 (-1 for all).
87 /// Return 0 if success, -1 if error.
88 int send(const void *buffer, int bytes, int sock = -1);
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 = 0);
94
95 /// Close network connection on indicated socket (-1 for all).
96 /// If successful, generate EventNetwork (close).
97 /// Return 0 if success, else -1.
98 int close(int sock = -1);
99
100 /// Return socket at indicated index. -1 if out of range.
101 int getSocket(int index = 0) const;
102
103 /// Check if socket is connected.
104 /// Default (-1) checks if any socket is connected.
105 /// Return true if (any) socket is connected, else false.
106 bool isConnected(int sock = -1) const;
107
108 /// Check if network data on indicated socket.
109 /// Return amount of data (0 if no data), -1 if not connected or error.
110 int isData(int sock = 0) const;
111
112 /// Set delay amount added to outgoing messages (in ticks).
113 /// Return 0 if ok, else -1.
114 int setDelay(int new_delay, int sock = 0);
115
116 ////////////////////////////////////////////////////
117 // Helper methods for adding delay to outgoing messages.
118
119 // Send any delayed messages that have expired.
120 // Return 0 if success, -1 if error.
121 int sendDelayed();
122
123 // Send bytes from buffer to connected network socket
124 // immediately, without delay added via setDelay().
125 // Return 0 if success, -1 if error.
126 int sendNow(const void *buffer, int bytes, int sock = 0);
127 ////////////////////////////////////////////////////
128
129};
130
131} // end of namespace df
132
133#endif // __NETWORK_MANAGER_H__
134
135#endif // NO_NETWORK
Definition: Manager.h:24
Definition: NetworkManager.h:31
int getMaxConnections() const
Get maximum number of connections.
int send(const void *buffer, int bytes, int sock=-1)
Send bytes from buffer to connected network socket (-1 for all).
int getNumConnections() const
Get number of connected sockets.
int accept()
Accept network connection.
int close(int sock=-1)
Close network connection on indicated socket (-1 for all).
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 setDelay(int new_delay, int sock=0)
Set delay amount added to outgoing messages (in ticks).
bool isServer() const
Return true if successufully setup as server, else false.
void setMaxConnections(int new_max_sock)
Set maximum number of connections.
bool isConnected(int sock=-1) const
Check if socket is connected.
int isData(int sock=0) const
Check if network data on indicated socket.
int startUp() override
Start up NetworkManager.
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 receive(void *buffer, int bytes, bool leave, int sock=0)
Receive from connected network socket (no more than bytes).
int getSocket(int index=0) const
Return socket at indicated index. -1 if out of range.
An animation for a sprite.
Definition: Animation.h:15