PageRenderTime 75ms CodeModel.GetById 24ms RepoModel.GetById 7ms app.codeStats 0ms

/SteamKit2/SteamKit2/Steam2/ServerClient.cs

https://bitbucket.org/VoiDeD/steamre/
C# | 204 lines | 95 code | 18 blank | 91 comment | 4 complexity | 371b5fbdf53e6f0b464c594729751d3f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. /*
  2. * This file is subject to the terms and conditions defined in
  3. * file 'license.txt', which is part of this source code package.
  4. */
  5. using System;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. namespace SteamKit2
  10. {
  11. /// <summary>
  12. /// This is a list of known Steam2 server types.
  13. /// </summary>
  14. public enum ESteam2ServerType : uint
  15. {
  16. /// <summary>
  17. /// This is the auth server that all clients should connect to.
  18. /// Implemented as a reverse proxy for load balancing.
  19. /// </summary>
  20. ProxyASClientAuthentication = 0,
  21. /// <summary>
  22. /// Represents a server that serves game content to clients.
  23. /// </summary>
  24. ContentServer = 1,
  25. /// <summary>
  26. /// The general directory server which returns a list of other servers.
  27. /// </summary>
  28. GeneralDirectoryServer = 2,
  29. /// <summary>
  30. /// Represents a server that serves config data (such as the CDR) to clients.
  31. /// </summary>
  32. ConfigServer = 3,
  33. /// <summary>
  34. /// Content Server?/Config Server? directory server
  35. /// </summary>
  36. CSDS = 6,
  37. /// <summary>
  38. /// Unknown.
  39. /// </summary>
  40. VCDSValidateNewValveCDKey = 7,
  41. /// <summary>
  42. /// Half-Life master server.
  43. /// </summary>
  44. HLMasterServer = 15,
  45. /// <summary>
  46. /// Friends server. Most likely obsolete.
  47. /// </summary>
  48. FriendsServer = 16,
  49. /// <summary>
  50. /// Unknown.
  51. /// </summary>
  52. AllMasterASClientAuthentication = 18,
  53. /// <summary>
  54. /// Reporting server for source games.
  55. /// </summary>
  56. CSER = 20,
  57. /// <summary>
  58. /// Half-Life 2 master server.
  59. /// </summary>
  60. HL2MasterServer = 24,
  61. /// <summary>
  62. /// Unknown.
  63. /// </summary>
  64. MasterASClientAuthentication = 26,
  65. /// <summary>
  66. /// Unknown.
  67. /// </summary>
  68. SlaveASClientAuthentication = 28,
  69. /// <summary>
  70. /// Rag doll kung fu master server.
  71. /// </summary>
  72. RDKFMasterServer = 30
  73. }
  74. /// <summary>
  75. /// This is the root client class used to provide all the functionality required to connect to Steam2 servers.
  76. /// </summary>
  77. public class ServerClient
  78. {
  79. /// <summary>
  80. /// Gets the socket of the client.
  81. /// </summary>
  82. /// <value>The socket.</value>
  83. internal TcpSocket Socket { get; private set; }
  84. /// <summary>
  85. /// Gets the remote endpoint of the client.
  86. /// </summary>
  87. /// <value>The end point.</value>
  88. protected IPEndPoint EndPoint { get; private set; }
  89. /// <summary>
  90. /// Gets whether or not the client is connected.
  91. /// </summary>
  92. /// <value>True if connected, otherwise false.</value>
  93. public bool IsConnected
  94. {
  95. get { return Socket.IsConnected; }
  96. }
  97. /// <summary>
  98. /// Gets the length of time a connection will attempt to establish before timing out. The default timeout is 30 seconds.
  99. /// </summary>
  100. /// <value>The connection timeout.</value>
  101. public TimeSpan ConnectionTimeout
  102. {
  103. get { return Socket.ConnectionTimeout; }
  104. set { Socket.ConnectionTimeout = value; }
  105. }
  106. /// <summary>
  107. /// Initializes a new instance of the <see cref="ServerClient"/> class.
  108. /// </summary>
  109. public ServerClient()
  110. {
  111. Socket = new TcpSocket();
  112. }
  113. /// <summary>
  114. /// Connects to the specified end point.
  115. /// </summary>
  116. /// <param name="endPoint">The end point.</param>
  117. public void Connect( IPEndPoint endPoint )
  118. {
  119. try
  120. {
  121. Socket.Connect( endPoint );
  122. EndPoint = endPoint;
  123. }
  124. catch
  125. {
  126. throw;
  127. }
  128. }
  129. /// <summary>
  130. /// Disconnects this instance.
  131. /// </summary>
  132. public void Disconnect()
  133. {
  134. Socket.Disconnect();
  135. }
  136. /// <summary>
  137. /// Sends a command to the connected server.
  138. /// The return value of this function does not signify command success, only if the command was sent.
  139. /// </summary>
  140. /// <param name="command">The command type to send.</param>
  141. /// <param name="args">The arguments to send.</param>
  142. /// <returns>True if the command was sent; otherwise, false.</returns>
  143. protected bool SendCommand( byte command, params object[] args )
  144. {
  145. try
  146. {
  147. using ( TcpPacket packet = new TcpPacket() )
  148. {
  149. packet.Write( command );
  150. foreach ( object arg in args )
  151. {
  152. if ( arg is byte[] )
  153. packet.Write( ( byte[] )arg );
  154. else if ( arg is string )
  155. packet.Write( ( string )arg, Encoding.ASCII );
  156. else
  157. packet.Write( arg.GetType(), arg );
  158. }
  159. Socket.Send( packet );
  160. }
  161. return true;
  162. }
  163. catch
  164. {
  165. return false;
  166. }
  167. }
  168. /// <summary>
  169. /// Performs a handshake with the server.
  170. /// </summary>
  171. /// <param name="type">The expected server type the client is handshaking with.</param>
  172. /// <returns>True if the handshake succeeded; otherwise false.</returns>
  173. protected bool HandshakeServer( ESteam2ServerType type )
  174. {
  175. try
  176. {
  177. Socket.Writer.Write( NetHelpers.EndianSwap( ( uint )type ) );
  178. return Socket.Reader.ReadByte() == 1;
  179. }
  180. catch
  181. {
  182. return false;
  183. }
  184. }
  185. }
  186. }