Dragonfly  4.4
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 #define DRAGONFLY_PORT "9876" // Default server port.
17 
18 // Two-letter acronym for easier access to manager.
19 #define NM df::NetworkManager::getInstance()
20 
21 namespace df {
22 
23 class 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.
36  static NetworkManager &getInstance();
37 
38  /// Start up NetworkManager.
39  int startUp();
40 
41  /// Shut down NetworkManager.
42  void shutDown();
43 
44  /// Accept only network events.
45  /// Returns false for other engine events.
46  bool isValid(std::string event_type) const;
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 connected sockets.
68  void setMaxSockets(int new_max_sock);
69 
70  /// Get maximum number of connected sockets.
71  int getMaxSockets() const;
72 
73  /// Get number of connected sockets.
74  int getNumSockets() 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__
int startUp()
Start up NetworkManager.
int closeAll()
Close all network connections.
static NetworkManager & getInstance()
Get the one and only instance of the NetworkManager.
int connect(std::string host, std::string port=DRAGONFLY_PORT)
Make network connection to host at port.
bool isConnected(int sock_index=0) const
Return true if socket is connected, else false.
int setServer(bool server=true, std::string port=DRAGONFLY_PORT)
Setup NetworkManager as server (if false, reset to client).
void shutDown()
Shut down NetworkManager.
A 2-d bounding box.
Definition: Box.h:10
int isData(int sock_index=0) const
Check if network data on indicated socket.
int getMaxSockets() const
Get maximum number of connected sockets.
int genDataEvents(int sock_index=0) const
If complete network message on indicated socket, (first int is message size, in bytes) generate Event...
bool isValid(std::string event_type) const
Accept only network events.
bool isServer() const
Return true if successufully setup as server, else false.
int isAnyData() const
Check if data on any connected socket.
int send(const void *buffer, int bytes, int sock_index=0)
Send bytes from buffer to connected network socket.
Definition: NetworkManager.h:23
int getSocket(int sock_index=0) const
Return system socket from socket index, -1 if error.
void setMaxSockets(int new_max_sock)
Set maximum number of connected sockets.
int close(int sock_index=0)
Close network connection on indicated socket.
int accept()
Accept network connection.
Definition: Manager.h:20
int receive(void *buffer, int nbytes, bool leave, int sock_index=0)
Receive from connected network socket (no more than nbytes).
int getNumSockets() const
Get number of connected sockets.