HoverRace  2.0
NetInterface.h
Go to the documentation of this file.
1 #ifdef HR_LEGACY_BUILD
2 // NetInterface.h
3 //
4 //
5 // Copyright (c) 1995-1998 - Richard Langlois and Grokksoft Inc.
6 //
7 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
8 // you may not use this file except in compliance with the License.
9 //
10 // A copy of the license should have been attached to the package from which
11 // you have taken this file. If you can not find the license you can not use
12 // this file.
13 //
14 //
15 // The author makes no representations about the suitability of
16 // this software for any purpose. It is provided "as is" "AS IS",
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
18 // implied.
19 //
20 // See the License for the specific language governing permissions
21 // and limitations under the License.
22 //
23 //
24 
25 #pragma once
26 
27 #include <WINSOCK.h>
28 
29 #include "../../engine/Util/MR_Types.h"
30 #include "../../engine/Util/Config.h"
31 
32 #define MR_ID_NOT_SET 255
33 
34 #define MR_OUT_QUEUE_LEN 2048
35 #define MR_MAX_NET_MESSAGE_LEN 255
36 
37 #define MR_NET_REQUIRED 1
38 #define MR_NET_TRY 2
39 #define MR_NOT_REQUIRED 0
40 #define MR_NET_DATAGRAM -1
41 
42 namespace HoverRace {
43 namespace Client {
44 
48 class NetMessageBuffer
49 {
50  public:
51  // MR_UInt16 mSendingTime:10; // 4ms of precision on a +-2sec range (if a datagram take more than 2 sec to travel..it will be droped (UDP only)
52  // Not used yet
53  MR_UInt16 mDatagramNumber:8;
54  MR_UInt16 mDatagramQueue:2;
55  MR_UInt16 mMessageType:6;
56  MR_UInt8 mClient;
57  MR_UInt8 mDataLen;
58  MR_UInt8 mData[MR_MAX_NET_MESSAGE_LEN];
59 
60  // int MessageLen()const { return mDataLen+5; }
61 };
62 
63 #define MR_NET_HEADER_LEN (sizeof(NetMessageBuffer) - MR_MAX_NET_MESSAGE_LEN)
64 
68 class NetworkPort
69 {
70  private:
71  // std::string mNetAddr;
72  // int mPort;
73  SOCKET mSocket;
74 
75  // UDP Information
76  SOCKET mUDPRecvSocket;
77  SOCKADDR_IN mUDPRemoteAddr;
78 
79  MR_UInt8 mLastSendedDatagramNumber[4];
80  MR_UInt8 mLastReceivedDatagramNumber[4];
81  MR_UInt8 mLastClient[4];
82 
83  int mWatchdog;
84 
85  int mAvgLag;
86  int mMinLag;
87  int mNbLagTest;
88  int mTotalLag;
89 
90  int mInputMessageBufferIndex;
91  NetMessageBuffer mInputMessageBuffer;
92 
93  // Output queue
94  // This queue is only used for messages that must absolutely be sent
95  // or those that are partially sent
96  MR_UInt8 mOutQueue[MR_OUT_QUEUE_LEN];
97  int mOutQueueLen;
98  int mOutQueueHead;
99  public:
100  NetworkPort();
101  ~NetworkPort();
102 
103  void Connect(SOCKET pSocket, SOCKET pUDPRecvSocket);
104  void SetRemoteUDPPort(unsigned int pPort);
105  void Disconnect();
106  BOOL IsConnected() const;
107 
108  SOCKET GetSocket() const;
109  SOCKET GetUDPSocket() const;
110 
111  const NetMessageBuffer *Poll(int pClientId, BOOL pCheckClientId); // parameters are hacks
112  void Send(const NetMessageBuffer *pMessage, int pReqLevel);
113  BOOL UDPSend(SOCKET pSocket, NetMessageBuffer *pMessage, unsigned pQueueId, BOOL pResendLast);
114 
115  // Time related stuff
116  BOOL AddLagSample(int pLag);
117  BOOL LagDone() const;
118 
119  int GetAvgLag() const;
120  int GetMinLag() const;
121  void SetLag(int pAvgLag, int pMinLag);
122 
123  BOOL mTriedBackupIP;
124 };
125 
135 class NetworkInterface
136 {
137  public:
138  enum {
139  eMaxClient = 9
140  };
141  private:
142  std::string mPlayer;
143  int mId;
144  BOOL mServerMode;
145  SOCKET mRegistrySocket;
146  int mServerPort;
147  int mUDPRecvPort;
148  int mTCPRecvPort;
149  std::string mServerAddr;
150  std::string mGameName;
151 
152  // UDP port
153  SOCKET mUDPOutShortPort;
154  SOCKET mUDPOutLongPort;
155  SOCKET mUDPRecvSocket;
156 
157  // Data
158  NetworkPort mClient[eMaxClient];
159  std::string mClientName[eMaxClient];
160  BOOL mAllPreLoguedRecv;
161  BOOL mCanBePreLogued[eMaxClient];
162  BOOL mPreLoguedClient[eMaxClient];
163  BOOL mConnected[eMaxClient];
164  DWORD mClientAddr[eMaxClient];
165  DWORD mClientBkAddr[eMaxClient];
166  int mClientPort[eMaxClient];
167 
168  int mReturnMessage;
169 
170  // Dialog functions
171  static NetworkInterface *mActiveInterface;
172  static BOOL CALLBACK ServerPortCallBack(HWND pWindow, UINT pMsgId, WPARAM pWParam, LPARAM pLParam);
173  static BOOL CALLBACK ServerAddrCallBack(HWND pWindow, UINT pMsgId, WPARAM pWParam, LPARAM pLParam);
174  static BOOL CALLBACK WaitGameNameCallBack(HWND pWindow, UINT pMsgId, WPARAM pWParam, LPARAM pLParam);
175  static BOOL CALLBACK ListCallBack(HWND pWindow, UINT pMsgId, WPARAM pWParam, LPARAM pLParam);
176  static BOOL CALLBACK WarnAloneCallback(HWND pWindow, UINT pMsgId, WPARAM pWParam, LPARAM pLParam);
177  static BOOL CALLBACK WaitConnCallback(HWND pWindow, UINT pMsgId, WPARAM pWParam, LPARAM pLParam);
178 
179  // Helper function
180  void SendConnectionDoneIfNeeded();
181 
182  public:
183  // Creation and destruction
184  NetworkInterface();
185  ~NetworkInterface();
186 
187  void SetPlayerName(const char *pPlayerName);
188  const char *GetPlayerName() const;
189 
190  BOOL MasterConnect(HWND pWindow, const char *pGameName,
191  BOOL pPromptForPort = TRUE,
192  unsigned pDefaultPort = HoverRace::Util::Config::GetInstance()->net.tcpServPort,
193  HWND * pModalessDlg = NULL, int pReturnMessage = 0);
194  BOOL SlavePreConnect(HWND pWindow, std::string &pGameName);
195  BOOL SlaveConnect(HWND pWindow, const char *pServerIP = NULL,
196  unsigned pPort = HoverRace::Util::Config::GetInstance()->net.tcpServPort,
197  const char *pGameName = NULL, HWND * pModalessDlg = NULL,
198  int pReturnMessage = 0);
199 
200  void Disconnect();
201 
202  int GetClientCount() const;
203  int GetId() const;
204 
205  int GetLagFromServer() const;
206  int GetAvgLag(int pClient) const;
207  int GetMinLag(int pClient) const;
208 
209  // helper function
210  BOOL CreateUDPRecvSocket(int pPort);
211 
212  // return TRUE if queue not full
213  BOOL UDPSend(int pClient, NetMessageBuffer * pMessage, BOOL pLongPort, BOOL pResendLast = FALSE);
214  BOOL BroadcastMessage(NetMessageBuffer * pMessage, int pReqLevel);
215  // BOOL BroadcastMessage( DWORD pTimeStamp, int pMessageType, int pMessageLen, const MR_UInt8* pMessage );
216  BOOL FetchMessage(DWORD & pTimeStamp, int &pMessageType, int &pMessageLen, const MR_UInt8 * &pMessage, int &pClientId);
217  // pTimeStamp must be set to current time stamp before fetch
218 
219  const char *GetPlayerName(int pIndex) const;
220  BOOL IsConnected(int pIndex) const;
221 };
222 
223 } // namespace Client
224 } // namespace HoverRace
225 #endif
uint16_t MR_UInt16
Definition: MR_Types.h:42
static Config * GetInstance()
Definition: Config.h:151
Definition: Announcement.h:24
uint8_t MR_UInt8
Definition: MR_Types.h:40