PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/SteamKit2/SteamKit2/Steam2/DSClient.cs

https://bitbucket.org/VoiDeD/steamre/
C# | 90 lines | 54 code | 24 blank | 12 comment | 5 complexity | a3e2dff1eeea0ff266a7704dd75339eb 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. namespace SteamKit2
  8. {
  9. /// <summary>
  10. /// Represents a client capable of connecting to a generic Directory Server.
  11. /// </summary>
  12. public class DSClient : ServerClient
  13. {
  14. /// <summary>
  15. /// Gets the server list for a specific server type.
  16. /// </summary>
  17. /// <param name="type">The server type.</param>
  18. /// <returns>A list of servers on success; otherwise, <c>null</c>.</returns>
  19. public IPEndPoint[] GetServerList( ESteam2ServerType type )
  20. {
  21. TcpPacket packet = this.GetRawServerList( ( byte )type );
  22. if ( packet == null )
  23. return null;
  24. DataStream ds = new DataStream( packet.GetPayload(), true );
  25. ushort numAddrs = ds.ReadUInt16();
  26. IPEndPoint[] serverList = new IPEndPoint[ numAddrs ];
  27. for ( int x = 0 ; x < numAddrs ; ++x )
  28. {
  29. IPAddrPort ipAddr = IPAddrPort.Deserialize( ds.ReadBytes( 6 ) );
  30. serverList[ x ] = ipAddr;
  31. }
  32. return serverList;
  33. }
  34. internal TcpPacket GetRawServerList( ESteam2ServerType type, params object[] args )
  35. {
  36. return this.GetRawServerList( ( byte )type, args );
  37. }
  38. internal TcpPacket GetRawServerList( byte commandOrType, params object[] args )
  39. {
  40. try
  41. {
  42. if ( !this.HandshakeServer( ESteam2ServerType.GeneralDirectoryServer ) )
  43. {
  44. DebugLog.WriteLine( "DSClient", "GetServerList failed handshake." );
  45. Socket.Disconnect();
  46. return null;
  47. }
  48. bool bRet = this.SendCommand( commandOrType, args );
  49. if ( !bRet )
  50. {
  51. DebugLog.WriteLine( "DSClient", "GetServerList failed sending EServerType command." );
  52. Socket.Disconnect();
  53. return null;
  54. }
  55. TcpPacket packet = Socket.ReceivePacket();
  56. return packet;
  57. }
  58. catch ( Exception ex )
  59. {
  60. DebugLog.WriteLine( "DSClient", "GetServerList threw an exception.\n{0}", ex.ToString() );
  61. Socket.Disconnect();
  62. return null;
  63. }
  64. }
  65. }
  66. }