PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs

https://bitbucket.org/VoiDeD/steamre/
C# | 153 lines | 75 code | 28 blank | 50 comment | 7 complexity | 1418189490bd3b006acf943d10b6fa27 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.IO;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. namespace SteamKit2
  10. {
  11. /// <summary>
  12. /// Represents a Tcp socket.
  13. /// </summary>
  14. sealed class TcpSocket
  15. {
  16. Socket sock;
  17. NetworkStream sockStream;
  18. /// <summary>
  19. /// Gets the network binary reader.
  20. /// </summary>
  21. /// <value>The binary reader.</value>
  22. public BinaryReader Reader { get; private set; }
  23. /// <summary>
  24. /// Gets the network binary writer.
  25. /// </summary>
  26. /// <value>The binary writer.</value>
  27. public BinaryWriter Writer { get; private set; }
  28. /// <summary>
  29. /// Gets whether or not the client is connected.
  30. /// </summary>
  31. /// <value>True if connected, otherwise false.</value>
  32. public bool IsConnected { get; private set; }
  33. /// <summary>
  34. /// Gets the length of time a connection will attempt to establish before timing out. The default timeout is 30 seconds.
  35. /// </summary>
  36. /// <value>The connection timeout.</value>
  37. public TimeSpan ConnectionTimeout { get; set; }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="TcpSocket"/> class.
  40. /// </summary>
  41. public TcpSocket()
  42. {
  43. ConnectionTimeout = TimeSpan.FromSeconds( 30 );
  44. }
  45. /// <summary>
  46. /// Disconnects (if needed) and connects the specified end point.
  47. /// </summary>
  48. /// <param name="endPoint">The end point.</param>
  49. public void Connect( IPEndPoint endPoint )
  50. {
  51. Disconnect();
  52. sock = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
  53. var asyncResult = sock.BeginConnect( endPoint, null, null );
  54. IsConnected = asyncResult.AsyncWaitHandle.WaitOne( ConnectionTimeout );
  55. if ( !IsConnected )
  56. {
  57. sock.Close();
  58. return;
  59. }
  60. sock.EndConnect( asyncResult );
  61. sockStream = new NetworkStream( sock, true );
  62. Reader = new BinaryReader( sockStream );
  63. Writer = new BinaryWriter( sockStream );
  64. }
  65. /// <summary>
  66. /// Disconnects this socket.
  67. /// </summary>
  68. public void Disconnect()
  69. {
  70. // mono doesn't like calling Shutdown if we're not connected
  71. if ( sock == null || !sock.Connected )
  72. return;
  73. if ( !IsConnected )
  74. return;
  75. if ( sock != null )
  76. {
  77. try
  78. {
  79. sock.Shutdown( SocketShutdown.Both );
  80. sock.Disconnect( true );
  81. sock.Close();
  82. sock = null;
  83. }
  84. catch { }
  85. }
  86. IsConnected = false;
  87. }
  88. /// <summary>
  89. /// Sends the specified data on the socket.
  90. /// </summary>
  91. /// <param name="data">The data.</param>
  92. public void Send( byte[] data )
  93. {
  94. sock.Send( data );
  95. }
  96. /// <summary>
  97. /// Sends the specified packet on the socket.
  98. /// </summary>
  99. /// <param name="packet">The packet.</param>
  100. public void Send( TcpPacket packet )
  101. {
  102. this.Send( packet.GetData() );
  103. }
  104. /// <summary>
  105. /// Attempts to receive a tcp packet from the socket.
  106. /// </summary>
  107. /// <returns>The packet.</returns>
  108. public TcpPacket ReceivePacket()
  109. {
  110. TcpPacket pack = new TcpPacket();
  111. uint size = NetHelpers.EndianSwap( this.Reader.ReadUInt32() );
  112. byte[] payload = Reader.ReadBytes( ( int )size );
  113. pack.SetPayload( payload );
  114. return pack;
  115. }
  116. /// <summary>
  117. /// Gets the local IP.
  118. /// </summary>
  119. /// <returns>The local IP.</returns>
  120. public IPAddress GetLocalIP()
  121. {
  122. return NetHelpers.GetLocalIP(sock);
  123. }
  124. }
  125. }