Cuberite
A lightweight, fast and extensible game server for Minecraft
Network.h
Go to the documentation of this file.
1 
2 // Network.h
3 
4 // Declares the classes used for the Network API
5 
6 
7 
8 
9 
10 #pragma once
11 
12 
13 
14 
15 
16 // fwd:
17 class cTCPLink;
18 typedef std::shared_ptr<cTCPLink> cTCPLinkPtr;
19 typedef std::vector<cTCPLinkPtr> cTCPLinkPtrs;
21 typedef std::shared_ptr<cServerHandle> cServerHandlePtr;
22 typedef std::vector<cServerHandlePtr> cServerHandlePtrs;
23 class cCryptoKey;
24 typedef std::shared_ptr<cCryptoKey> cCryptoKeyPtr;
25 class cX509Cert;
26 typedef std::shared_ptr<cX509Cert> cX509CertPtr;
27 
28 
29 
30 
31 
33 class cTCPLink
34 {
35  friend class cNetwork;
36 
37 public:
38  class cCallbacks
39  {
40  public:
41  // Force a virtual destructor for all descendants:
42  virtual ~cCallbacks() {}
43 
46  virtual void OnLinkCreated(cTCPLinkPtr a_Link) = 0;
47 
49  virtual void OnReceivedData(const char * a_Data, size_t a_Length) = 0;
50 
54  virtual void OnRemoteClosed(void) = 0;
55 
58  virtual void OnTlsHandshakeCompleted(void) {}
59 
61  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
62  };
63  typedef std::shared_ptr<cCallbacks> cCallbacksPtr;
64 
65 
66  // Force a virtual destructor for all descendants:
67  virtual ~cTCPLink() {}
68 
71  virtual bool Send(const void * a_Data, size_t a_Length) = 0;
72 
75  bool Send(const AString & a_Data)
76  {
77  return Send(a_Data.data(), a_Data.size());
78  }
79 
81  virtual AString GetLocalIP(void) const = 0;
82 
84  virtual UInt16 GetLocalPort(void) const = 0;
85 
87  virtual AString GetRemoteIP(void) const = 0;
88 
90  virtual UInt16 GetRemotePort(void) const = 0;
91 
95  virtual void Shutdown(void) = 0;
96 
99  virtual void Close(void) = 0;
100 
106  virtual AString StartTLSClient(
107  cX509CertPtr a_OwnCert,
108  cCryptoKeyPtr a_OwnPrivKey
109  ) = 0;
110 
119  virtual AString StartTLSServer(
120  cX509CertPtr a_OwnCert,
121  cCryptoKeyPtr a_OwnPrivKey,
122  const AString & a_StartTLSData
123  ) = 0;
124 
126  cCallbacksPtr GetCallbacks(void) const { return m_Callbacks; }
127 
128 protected:
130  cCallbacksPtr m_Callbacks;
131 
132 
134  cTCPLink(cCallbacksPtr a_Callbacks):
135  m_Callbacks(a_Callbacks)
136  {
137  }
138 };
139 
140 
141 
142 
143 
146 {
147  friend class cNetwork;
148 public:
149 
150  // Force a virtual destructor for all descendants:
151  virtual ~cServerHandle() {}
152 
155  virtual void Close(void) = 0;
156 
158  virtual bool IsListening(void) const = 0;
159 };
160 
161 
162 
163 
164 
167 {
168 public:
171  {
172  public:
173  // Force a virtual destructor in all descendants:
174  virtual ~cCallbacks() {}
175 
177  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
178 
180  virtual void OnReceivedData(const char * a_Data, size_t a_Size, const AString & a_RemoteHost, UInt16 a_RemotePort) = 0;
181  };
182 
183 
184  // Force a virtual destructor for all descendants:
185  virtual ~cUDPEndpoint() {}
186 
189  virtual void Close(void) = 0;
190 
192  virtual bool IsOpen(void) const = 0;
193 
195  virtual UInt16 GetPort(void) const = 0;
196 
199  virtual bool Send(const AString & a_Payload, const AString & a_Host, UInt16 a_Port) = 0;
200 
203  virtual void EnableBroadcasts(void) = 0;
204 
205 protected:
208 
209 
211  cUDPEndpoint(cCallbacks & a_Callbacks):
212  m_Callbacks(a_Callbacks)
213  {
214  }
215 };
216 
217 typedef std::shared_ptr<cUDPEndpoint> cUDPEndpointPtr;
218 
219 
220 
221 
222 
223 class cNetwork
224 {
225 public:
228  {
229  public:
230  // Force a virtual destructor for all descendants:
231  virtual ~cConnectCallbacks() {}
232 
235  virtual void OnConnected(cTCPLink & a_Link) = 0;
236 
238  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
239  };
240  typedef std::shared_ptr<cConnectCallbacks> cConnectCallbacksPtr;
241 
242 
245  {
246  public:
247  // Force a virtual destructor for all descendants:
248  virtual ~cListenCallbacks() {}
249 
254  virtual cTCPLink::cCallbacksPtr OnIncomingConnection(const AString & a_RemoteIPAddress, UInt16 a_RemotePort) = 0;
255 
259  virtual void OnAccepted(cTCPLink & a_Link) = 0;
260 
262  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
263  };
264  typedef std::shared_ptr<cListenCallbacks> cListenCallbacksPtr;
265 
266 
269  {
270  public:
271  // Force a virtual destructor for all descendants:
273 
278  virtual void OnNameResolved(const AString & a_Name, const AString & a_IP) = 0;
279 
284  virtual bool OnNameResolvedV4(const AString & a_Name, const sockaddr_in * a_IP) { return true; }
285 
290  virtual bool OnNameResolvedV6(const AString & a_Name, const sockaddr_in6 * a_IP) { return true; }
291 
294  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
295 
298  virtual void OnFinished(void) = 0;
299  };
300  typedef std::shared_ptr<cResolveNameCallbacks> cResolveNameCallbacksPtr;
301 
302 
309  static bool Connect(
310  const AString & a_Host,
311  UInt16 a_Port,
312  cConnectCallbacksPtr a_ConnectCallbacks,
313  cTCPLink::cCallbacksPtr a_LinkCallbacks
314  );
315 
316 
322  static cServerHandlePtr Listen(
323  UInt16 a_Port,
324  cListenCallbacksPtr a_ListenCallbacks
325  );
326 
327 
333  static bool HostnameToIP(
334  const AString & a_Hostname,
335  cResolveNameCallbacksPtr a_Callbacks
336  );
337 
338 
344  static bool IPToHostName(
345  const AString & a_IP,
346  cResolveNameCallbacksPtr a_Callbacks
347  );
348 
352  static cUDPEndpointPtr CreateUDPEndpoint(UInt16 a_Port, cUDPEndpoint::cCallbacks & a_Callbacks);
353 
355  static AStringVector EnumLocalIPAddresses(void);
356 };
357 
358 
359 
360 
virtual AString GetLocalIP(void) const =0
Returns the IP address of the local endpoint of the connection.
std::vector< cTCPLinkPtr > cTCPLinkPtrs
Definition: Network.h:19
virtual ~cTCPLink()
Definition: Network.h:67
std::shared_ptr< cX509Cert > cX509CertPtr
Definition: SslConfig.h:13
virtual ~cListenCallbacks()
Definition: Network.h:248
virtual void Close(void)=0
Drops the connection without any more processing.
cUDPEndpoint(cCallbacks &a_Callbacks)
Creates a new instance of an endpoint, with the specified callbacks.
Definition: Network.h:211
virtual bool Send(const void *a_Data, size_t a_Length)=0
Queues the specified data for sending to the remote peer.
cTCPLink(cCallbacksPtr a_Callbacks)
Creates a new link, with the specified callbacks.
Definition: Network.h:134
virtual void Shutdown(void)=0
Closes the link gracefully.
Interface that provides the methods available on a listening server socket.
Definition: Network.h:145
virtual UInt16 GetLocalPort(void) const =0
Returns the port used by the local endpoint of the connection.
std::shared_ptr< cX509Cert > cX509CertPtr
Definition: Network.h:25
virtual AString StartTLSServer(cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnPrivKey, const AString &a_StartTLSData)=0
Starts a TLS handshake as a server connection.
virtual bool OnNameResolvedV6(const AString &a_Name, const sockaddr_in6 *a_IP)
Called when the hostname is successfully resolved into an IPv6 address.
Definition: Network.h:290
virtual UInt16 GetRemotePort(void) const =0
Returns the port used by the remote endpoint of the connection.
Callbacks used when resolving names to IPs.
Definition: Network.h:268
std::shared_ptr< cCryptoKey > cCryptoKeyPtr
Definition: CryptoKey.h:72
std::shared_ptr< cUDPEndpoint > cUDPEndpointPtr
Definition: Network.h:217
virtual void OnReceivedData(const char *a_Data, size_t a_Length)=0
Called when there&#39;s data incoming from the remote peer.
Interface for the callbacks for events that can happen on the endpoint.
Definition: Network.h:170
virtual ~cUDPEndpoint()
Definition: Network.h:185
virtual AString StartTLSClient(cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnPrivKey)=0
Starts a TLS handshake as a client connection.
bool Send(const AString &a_Data)
Queues the specified data for sending to the remote peer.
Definition: Network.h:75
std::vector< AString > AStringVector
Definition: StringUtils.h:14
cCallbacksPtr m_Callbacks
Callbacks to be used for the various situations.
Definition: Network.h:130
std::shared_ptr< cServerHandle > cServerHandlePtr
Definition: Network.h:20
virtual void OnTlsHandshakeCompleted(void)
Called when the TLS handshake has been completed and communication can continue regularly.
Definition: Network.h:58
std::shared_ptr< cTCPLink > cTCPLinkPtr
Definition: Network.h:17
virtual ~cCallbacks()
Definition: Network.h:42
virtual bool OnNameResolvedV4(const AString &a_Name, const sockaddr_in *a_IP)
Called when the hostname is successfully resolved into an IPv4 address.
Definition: Network.h:284
Interface that provides methods available on UDP communication endpoints.
Definition: Network.h:166
unsigned short UInt16
Definition: Globals.h:114
std::shared_ptr< cCallbacks > cCallbacksPtr
Definition: Network.h:63
Callbacks used when listening for incoming connections as a server.
Definition: Network.h:244
std::string AString
Definition: StringUtils.h:13
virtual AString GetRemoteIP(void) const =0
Returns the IP address of the remote endpoint of the connection.
std::shared_ptr< cResolveNameCallbacks > cResolveNameCallbacksPtr
Definition: Network.h:300
std::shared_ptr< cCryptoKey > cCryptoKeyPtr
Definition: Network.h:23
std::shared_ptr< cConnectCallbacks > cConnectCallbacksPtr
Definition: Network.h:240
virtual ~cConnectCallbacks()
Definition: Network.h:231
Interface that provides the methods available on a single TCP connection.
Definition: Network.h:33
virtual void OnRemoteClosed(void)=0
Called when the remote end closes the connection.
virtual void OnLinkCreated(cTCPLinkPtr a_Link)=0
Called when the cTCPLink for the connection is created.
cCallbacks & m_Callbacks
The callbacks used for various events on the endpoint.
Definition: Network.h:207
Callbacks used for connecting to other servers as a client.
Definition: Network.h:227
std::vector< cServerHandlePtr > cServerHandlePtrs
Definition: Network.h:22
cCallbacksPtr GetCallbacks(void) const
Returns the callbacks that are used.
Definition: Network.h:126
std::shared_ptr< cListenCallbacks > cListenCallbacksPtr
Definition: Network.h:264
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg)=0
Called when an error is detected on the connection.
virtual ~cServerHandle()
Definition: Network.h:151