CommunicationInterface.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_COMMUNICATIONINTERFACE_H
  13. #define SE_INCL_COMMUNICATIONINTERFACE_H
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. #define SERVER_CLIENTS 16
  18. #include <Engine/Network/CPacket.h>
  19. // Communication class
  20. class ENGINE_API CCommunicationInterface {
  21. public:
  22. BOOL cci_bSocketOpen; // set if socket is open and working
  23. BOOL cci_bBound; // set for udp sockets that have been explicitly or implicitly bound
  24. BOOL cci_bInitialized; // is the communication interface initialized or not
  25. BOOL cci_bWinSockOpen; // is the winsock API initialized
  26. BOOL cci_bServerInitialized;
  27. BOOL cci_bClientInitialized;
  28. CPacketBuffer cci_pbMasterOutput; // master output buffer
  29. CPacketBuffer cci_pbMasterInput; // master input buffer
  30. SOCKET cci_hSocket; // the socket handle itself
  31. public:
  32. // client
  33. void Client_OpenLocal(void);
  34. void Client_OpenNet_t(ULONG ulServerAddress);
  35. // update master UDP socket and route its messages
  36. void UpdateMasterBuffers(void);
  37. public:
  38. CCommunicationInterface(void);
  39. ~CCommunicationInterface(void){};
  40. // start/stop protocols
  41. void Init(void);
  42. void Close(void);
  43. void InitWinsock(void);
  44. void EndWinsock(void);
  45. void PrepareForUse(BOOL bUseNetwork, BOOL bClient);
  46. void Unprepare(void);
  47. BOOL IsNetworkEnabled(void);
  48. // get address of local machine
  49. void GetHostName(CTString &strName, CTString &strAddress);
  50. // create an inet-family socket
  51. void CreateSocket_t();
  52. // bind socket to the given address
  53. void Bind_t(ULONG ulLocalHost, ULONG ulLocalPort);
  54. // set socket to non-blocking mode
  55. void SetNonBlocking_t(void);
  56. // get generic socket error info string and last error
  57. CTString GetSocketError(INDEX iError);
  58. // open an UDP socket at given port
  59. void OpenSocket_t(ULONG ulLocalHost, ULONG ulLocalPort);
  60. // get address of this host
  61. void GetLocalAddress_t(ULONG &ulHost, ULONG &ulPort);
  62. // get address of the peer host connected to this socket
  63. void GetRemoteAddress_t(ULONG &ulHost, ULONG &ulPort);
  64. // broadcast communication
  65. void Broadcast_Send(const void *pvSend, SLONG slSendSize,CAddress &adrDestination);
  66. BOOL Broadcast_Receive(void *pvReceive, SLONG &slReceiveSize,CAddress &adrAddress);
  67. // here we receive connect requests
  68. void Broadcast_Update_t(void);
  69. // Server
  70. void Server_Init_t(void);
  71. void Server_Close(void);
  72. void Server_ClearClient(INDEX iClient);
  73. BOOL Server_IsClientLocal(INDEX iClient);
  74. BOOL Server_IsClientUsed(INDEX iClient);
  75. CTString Server_GetClientName(INDEX iClient);
  76. void Server_Send_Reliable(INDEX iClient, const void *pvSend, SLONG slSendSize);
  77. BOOL Server_Receive_Reliable(INDEX iClient, void *pvReceive, SLONG &slReceiveSize);
  78. void Server_Send_Unreliable(INDEX iClient, const void *pvSend, SLONG slSendSize);
  79. BOOL Server_Receive_Unreliable(INDEX iClient, void *pvReceive, SLONG &slReceiveSize);
  80. BOOL Server_Update(void);
  81. // Client
  82. void Client_Init_t(char* strServerName);
  83. void Client_Init_t(ULONG ulServerAddress);
  84. void Client_Close(void);
  85. void Client_Clear(void);
  86. BOOL Client_IsConnected(void);
  87. void Client_Send_Reliable(const void *pvSend, SLONG slSendSize);
  88. BOOL Client_Receive_Reliable(void *pvReceive, SLONG &slReceiveSize);
  89. BOOL Client_Receive_Reliable(CTStream &strmReceive);
  90. void Client_PeekSize_Reliable(SLONG &slExpectedSize,SLONG &slReceivedSize);
  91. void Client_Send_Unreliable(const void *pvSend, SLONG slSendSize);
  92. BOOL Client_Receive_Unreliable(void *pvReceive, SLONG &slReceiveSize);
  93. BOOL Client_Update(void);
  94. };
  95. extern ENGINE_API CCommunicationInterface _cmiComm;
  96. extern CPacketBufferStats _pbsSend;
  97. extern CPacketBufferStats _pbsRecv;
  98. #endif /* include-once check. */