net.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // net.h
  19. // Project: Nostril (aka Postal)
  20. //
  21. // History:
  22. // 05/26/97 MJR Started.
  23. //
  24. // 05/28/97 JMI Upped NetMaxPlayers to 5.
  25. //
  26. // 06/16/97 MJR Upped NetMaxPlayers to 16.
  27. //
  28. ////////////////////////////////////////////////////////////////////////////////
  29. #ifndef NET_H
  30. #define NET_H
  31. ////////////////////////////////////////////////////////////////////////////////
  32. //
  33. // This is the main respository of hardwired network-related values
  34. //
  35. ////////////////////////////////////////////////////////////////////////////////
  36. // The ping code was only 3/4 implimented. The client sent the pings every so often
  37. // and the server echoed them back, but nobody ever made use of the ping times, and
  38. // the server didn't check to see if a client had "timed-out" based on a lack of pings.
  39. // So, in order to preserve bandwidth, I disabled this until (or if?) we ever need it.
  40. #define NET_PING 0
  41. // These macros make it easier to do comparisons between two Net::SEQ values.
  42. // The tricky part is, of course, that they are unsigned values that may wrap
  43. // around past 0. These means that 65535 is considered less than 0! In fact,
  44. // it is exactly 1 less than 0, because if you decrement 0, you go back to 65535.
  45. //
  46. // The macro names are encoded as follows:
  47. //
  48. // SEQ_EQ(a, b) Returns true if "a" is equal-to "b", false otherwise
  49. //
  50. // SEQ_NE(a, b) Returns true if "a" is not-equal-to "b", false otherwise
  51. //
  52. // SEQ_GT(a, b) Returns true if "a" is greater-than "b", false otherwise
  53. //
  54. // SEQ_GTE(a, b) Returns true if "a" is greater-than-or-equal-to "b", false otherwise
  55. //
  56. // SEQ_LT(a, b) Returns true if "a" is less-than "b"
  57. //
  58. // SEQ_LTE(a, b) Returns true if "a" is less-than-or-equal-to "b"
  59. //
  60. #define SEQ_EQ(a, b) ((Net::SEQ)(a) == (Net::SEQ)(b))
  61. #define SEQ_NE(a, b) ((Net::SEQ)(a) != (Net::SEQ)(b))
  62. #define SEQ_GTE(a, b) ( (Net::SEQ)( (Net::SEQ)(a) - (Net::SEQ)(b) ) < (Net::SEQ)(Net::MaxAheadSeq * 3) )
  63. #define SEQ_GT(a, b) (SEQ_GTE(a, b) && SEQ_NE(a, b))
  64. #define SEQ_LT(a, b) SEQ_GT(b, a)
  65. #define SEQ_LTE(a, b) SEQ_GTE(b, a)
  66. #ifdef WIN32
  67. namespace Net
  68. {
  69. #else
  70. class Net
  71. {
  72. public:
  73. #endif
  74. //------------------------------------------------------------------------------
  75. // These set some overall limits on various network stuff.
  76. // The primary reason for moving these into their own file was to get around
  77. // some inter-dependancies that came up when they were part of other files.
  78. //------------------------------------------------------------------------------
  79. typedef enum
  80. {
  81. // Maximum number of ID's. Can't be more than 16 right now because
  82. // some bit masks are hardwired to 16-bits, which is 1 bit per player.
  83. MaxNumIDs = 16,
  84. // Indicates an invalid/unused net ID
  85. InvalidID = 255,
  86. // Maximum length of chat text
  87. MaxChatSize = 80,
  88. // Maximum length of name text
  89. MaxPlayerNameSize = 32,
  90. // Maximum length of realm text
  91. MaxRealmNameSize = 32,
  92. // Maximum length of host text
  93. MaxHostNameSize = 32,
  94. #if NET_PING
  95. // These values relate the a ping message that is periodically sent by the client
  96. // to the server. The server echos these pings back as it receives them. This
  97. // serves two purposes: (1) it let's us measure the actual ping times, which may
  98. // be usefull for something, and (2) if the server doesn't get a ping from a client
  99. // for a long time, it can decide that the client has timed-out.
  100. MaxTimeBetweenSends = 1000, // Maximum time between sends to the server
  101. MaxTimeBeforeTimeout = 5000, // Maximum time before client is considered dead
  102. #endif
  103. // Maximum amount of time to wait before giving up on an attempt to connect to the server
  104. MaxConnectWaitTime = 10000,
  105. // This is the maximum amount of time the input sequence is allowed to get ahead of the
  106. // frame sequence and the minimum amount of time per frame (which can also be thought of
  107. // as maximum frames per second). These two values are CRITICALLY INTER-TWINED!!!
  108. //
  109. // It is EXTREMELY IMPORTANT to consider the effect that changing these values will have
  110. // on MaxAheadSeq, which is the maximum amount by which the input sequence is allowed to
  111. // get ahead of the frame sequence.
  112. //
  113. // Consider that one player is having problems, and for whatever reason is stuck at frame 0.
  114. // The maximum input sequence this player will transmit to other players is 0 + MaxAheadSeq.
  115. // Therefore, assuming the other players receive this data, they wil AT BEST be able to
  116. // go up to frame 0 + MaxAheadSeq. At that point, they will be stuck because they haven't
  117. // gotten any further input from the stuck player. They will, however, send out more of
  118. // their own inputs, and the furthest they will get is to 0 + MaxAheadSeq + MaxAheadSeq.
  119. // Why? Because they are stuck at frame 0 + MaxAheadSeq, and their inputs can get MaxAheadSeq
  120. // ahead of that. So, it would seem that each player must buffer up to 2 * MaxAheadSeq worth
  121. // of each other player's input data. But that's not the whole story, yet.
  122. //
  123. // Consider what happens if one of the players that was way ahead dropped from the game,
  124. // and if the reason the stuck player was stuck was because he never got any input from the
  125. // player that dropped (their communication link was only workingin in one direction.) Now,
  126. // the stuck player needs to get all the inputs that he never got from one of the other
  127. // players, since they've already used those inputs. At most, he will need MaxAheadSeq worth
  128. // of inputs, because even if other players have more than that from the dropped player, all
  129. // that matters is what frame they got up to -- anything beyond that was never used. In order
  130. // to allow for the fact that any particular player might be the only one that got some set
  131. // of inputs from another player, we need each player to remember MaxAheadSeq worth of inputs
  132. // PRIOR to the current frame number.
  133. //
  134. // So, the final result is that each player must be able to store up to 3 * MaxAheadSeq worth
  135. // of inputs for each other player.
  136. //
  137. // One might be tempted to conclude separately that 3 * MaxAheadSeq could also be used to
  138. // determine the minimum number of bits required to store a sequence value. In other words,
  139. // if 3 * MaxAheadSeq came to, say, 180, you might be tempted to say you could use an 8-bit
  140. // value to store the sequence numbers, and simply have them go from 0 to 255 and then wrap
  141. // around to 0 again -- or even have them go to 180 and then wrap-around to 0. However,
  142. // one would be wrong to do this.
  143. //
  144. // The problem is that we are using datagram messages to transmit inputs between players,
  145. // and datagrams are unreliable. They may not get through at all, or they may get through
  146. // but in a different order from the order they were sent in. The problem as it applies to
  147. // the sequence numbers is that if we used a range of 0 to 180, or even 0 to 255, it is
  148. // quite possible that long after we went past a particular frame, we might get a very old
  149. // packet that contained very old data. If we can't recognize this packet as being obsolete,
  150. // we would mistakingly use it's data, which would most likely throw the entire game permanently
  151. // out of sync.
  152. //
  153. // Therefore, while we can limit the size of the our BUFFER to 180 (using the above example),
  154. // we can't limit the sequence numbers to that range. Instead, we want a much larger number
  155. // so we can recognize older packets and ignore them. (Note that the opposite is not a problem --
  156. // we can never receive a "too new" packet, since other player's can never get too far ahead.)
  157. //
  158. // The range of sequence numbers should be determined based on how long an "old" message
  159. // might hang around before it finally gets delivered. I have no idea what this time might
  160. // be -- 5 seconds? 10 seconds? 5 minutes????? Consider a reasonable maximimum frame rate
  161. // of 30 fps (for network mode). If we used a 16-bit sequence number, we'd get up to 65536
  162. // frames before wrap around. However, in order to DETECT wrap-around, we need to use half
  163. // that, or 32768. At 30fps, 32768 / 30 = 1092 seconds / 60 = 18 minutes. This seems
  164. // like a very reasonable time period. We could probably get away with less, but 16-bits
  165. // is convenient, and saving just a few more bits is probably not worth the hassle.
  166. // (By the way, the goal of using as few bits as possible is reducing the size of the
  167. // messages being sent back and forth).
  168. //
  169. MaxAheadOfFrameTime = 1000,
  170. MinFrameTime = 33, // 1000/33 = 30 frames per second
  171. MaxAheadSeq = MaxAheadOfFrameTime / MinFrameTime,
  172. TotalRequiredSeqs = MaxAheadSeq * 3,
  173. // Maximum amount of time to spend receiving peer data. If this is too low,
  174. // we might end up with a growing backlog of peer data. If it is too high, we
  175. // could end up spending all our time getting peer data and hardly any on the game.
  176. // It's probably better to err on the high side because in theory the peers will be
  177. // sending less data than our bandwidth, so we shouldn't have to spend too much
  178. // time receiving it.
  179. MaxPeerReceiveTime = 200,
  180. // Minimum time between sends to a peer. The actual time is calculated according to the
  181. // available bandwidth, but even when huge bandwidths are available, we don't want to
  182. // go nuts saturating it. Setting it to the minimum frame time seems like a good idea.
  183. MinPeerSendInterval = MinFrameTime,
  184. // Datagram packet overhead that isn't under our control. For TCP/IP, datagram
  185. // packets (UDP) have a 36 byte header, so that's what I went with.
  186. DatagramPacketHeader = 36,
  187. // Maximum datagram size. It seems that all protocols support a minimum datagram
  188. // size of just over 500 bytes. Anything larger might be broken into multiple
  189. // datagrams, which we don't want because datagrams are not necessarily received in
  190. // the same order they are sent in, which is something we don't want to deal with.
  191. MaxDatagramSize = 500,
  192. // Interval between broadcasts (clients browsing for hosts will generate a
  193. // "looking for a host" message every this often)
  194. BroadcastInterval = 1500,
  195. // If we don't hear from a host again for this amount of time, then
  196. // we assume he isn't going to respond. I
  197. BroadcastDropTime = BroadcastInterval * 2,
  198. // Add this offset to the base port to calculate the listen port
  199. ListenPortOffset = 0,
  200. // Add this offset to the base port to calculate the broadcast port
  201. // (the port from which broadcasts are sent)
  202. BroadcastPortOffset = 1,
  203. // Add this offset to the base port to calculate the antenna port
  204. // (the port on which broadcasts are received)
  205. AntennaPortOffset = 2,
  206. // In order to allow for multiple games running on the same system, which
  207. // is only usefull for debugging, but then becomes VERY usefull, we need
  208. // to use a different port for each "peer". This offset determines the
  209. // first peer port (add this offset onto the base port). Each successive
  210. // peer uses the the next successive port.
  211. FirstPeerPortOffset = 3,
  212. // Magic number embedded at start of broadcasts so we can validate them.
  213. // To avoid having to deal with endian crap, it's simply defined as four
  214. // bytes in a row, that are always in this order.
  215. BroadcastMagic0 = 0x06,
  216. BroadcastMagic1 = 0x16,
  217. BroadcastMagic2 = 0x63,
  218. BroadcastMagic3 = 0x89
  219. };
  220. // This should probably be unsigned, and may as well be as small as we can get
  221. // it since we're inherently limited to 16 players anyway.
  222. typedef unsigned char ID;
  223. // This MUST be unsigned to work properly!!!
  224. // See above for an explanation of why 16-bits is a good choice.
  225. typedef U16 SEQ;
  226. //------------------------------------------------------------------------------
  227. // These are the various bandwidths we support
  228. //------------------------------------------------------------------------------
  229. typedef enum
  230. {
  231. FirstBandwidth = 0,
  232. Analog14_4 = 0,
  233. Analog28_8,
  234. Analog33_6,
  235. Analog57_6,
  236. ISDN1Channel,
  237. ISDN2Channel,
  238. LAN10Mb,
  239. LAN100Mb,
  240. // Number of entries
  241. NumBandwidths
  242. } Bandwidth;
  243. // In a namespace this data is extern, but in a class it's static
  244. #ifdef WIN32
  245. #define NETCRAPTHING extern
  246. #else
  247. #define NETCRAPTHING static
  248. #endif
  249. // Lookup tables associated with the NetBandwidth enums.
  250. NETCRAPTHING long lBandwidthValues[Net::NumBandwidths];
  251. NETCRAPTHING char* BandwidthText[Net::NumBandwidths];
  252. };
  253. #endif //NET_H
  254. ////////////////////////////////////////////////////////////////////////////////
  255. // EOF
  256. ////////////////////////////////////////////////////////////////////////////////