Dragonfly 4.19
A text-based game engine
NetworkManager.h
1//
2// NetworkManager.h
3//
4// Manage network connections to/from engine.
5//
6
7#ifndef __NETWORK_MANAGER_H__
8#define __NETWORK_MANAGER_H__
9
10// System includes.
11#include <string.h>
12
13// Engine includes.
14#include "Manager.h"
15
16// Two-letter acronym for easier access to manager.
17#define NM df::NetworkManager::getInstance()
18
19namespace df {
20
21const std::string DRAGONFLY_PORT = "9876";
22
23class NetworkManager : public Manager {
24
25 private:
26 NetworkManager(); // Private since a singleton.
27 NetworkManager(NetworkManager const&); // Don't allow copy.
28 void operator=(NetworkManager const&); // Don't allow assignment.
29 int m_unconnected_sock; // Socket for accept connections.
30 std::vector<int> m_sock; // Connected network sockets.
31 int m_max_sock; // Limit of connected sockets.
32
33 public:
34
35 /// Get the one and only instance of the NetworkManager.
37
38 /// Start up NetworkManager.
39 int startUp() override;
40
41 /// Shut down NetworkManager.
42 void shutDown() override;
43
44 /// Accept only network events.
45 /// Returns false for other engine events.
46 bool isValid(std::string event_type) const override;
47
48 /// Setup NetworkManager as server (if false, reset to client).
49 /// Return 0 if ok, else -1.
50 int setServer(bool server=true, std::string port = DRAGONFLY_PORT);
51
52 /// Return true if successufully setup as server, else false.
53 bool isServer() const;
54
55 /// Accept network connection.
56 /// If successful, generate EventNetwork (accept).
57 /// Return sock index if new connection (note, 0 for first).
58 /// Return -1 if no new connection, but no error.
59 /// Return -2 if errror.
60 int accept();
61
62 /// Make network connection to host at port.
63 /// If successful, generate EventNetwork (connect).
64 /// Return return socket index if success, -1 if error.
65 int connect(std::string host, std::string port = DRAGONFLY_PORT);
66
67 /// Set maximum number of connections.
68 void setMaxConnections(int new_max_sock);
69
70 /// Get maximum number of connections.
71 int getMaxConnections() const;
72
73 /// Get number of connected sockets.
74 int getNumConnections() const;
75
76 /// Send bytes from buffer to connected network socket.
77 /// Return 0 if success, -1 if error.
78 int send(const void *buffer, int bytes, int sock_index=0);
79
80 /// Receive from connected network socket (no more than nbytes).
81 /// If leave is true, don't remove data from socket (peek).
82 /// Return number of bytes received, -1 if error.
83 int receive(void *buffer, int nbytes, bool leave, int sock_index=0);
84
85 /// Close network connection on indicated socket.
86 /// Return 0 if success, else -1.
87 int close(int sock_index=0);
88
89 /// Close all network connections.
90 /// Return 0 if success, else negative number.
91 int closeAll();
92
93 /// If complete network message on indicated socket, (first
94 /// int is message size, in bytes) generate EventNetwork (data).
95 /// Return 1 if generated event, 0 if no message, -1 if error.
96 int genDataEvents(int sock_index=0) const;
97
98 /// Return true if socket is connected, else false.
99 bool isConnected(int sock_index=0) const;
100
101 /// Check if network data on indicated socket.
102 /// Return amount of data (0 if no data), -1 if not connected or error.
103 int isData(int sock_index=0) const;
104
105 /// Check if data on any connected socket.
106 /// Return index of first socket with data.
107 /// Return -1 if no socket has data.
108 int isAnyData() const;
109
110 /// Return system socket from socket index, -1 if error.
111 int getSocket(int sock_index=0) const;
112};
113
114} // end of namespace df
115
116#endif // __NETWORK_MANAGER_H__
Definition: Manager.h:24
Definition: NetworkManager.h:23
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 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
Accept only network events.
int isAnyData() const
Check if data on any connected socket.
bool isServer() const
Return true if successufully setup as server, else false.
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 receive(void *buffer, int nbytes, bool leave, int sock_index=0)
Receive from connected network socket (no more than nbytes).
int isData(int sock_index=0) const
Check if network data on indicated socket.
An animation for a sprite.
Definition: Animation.h:15