Advertisement
Guest User

Untitled

a guest
Mar 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 3.02 KB | None | 0 0
  1. # HG changeset patch
  2. # User Nicholas Hastings <skamonkey@gmail.com>
  3. # Date 1351785036 14400
  4. # Node ID 936eecb0a2cbdbcb6d34973ff123fa5ca1fabcb8
  5. # Parent  274a1e90c589ceb55d0abe3e2cb53154d3737b8c
  6. Steam2 socket connect timeouts.
  7.  
  8. diff --git a/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs b/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs
  9. --- a/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs
  10. +++ b/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs
  11. @@ -1,18 +1,20 @@
  12.  /*
  13.   * This file is subject to the terms and conditions defined in
  14.   * file 'license.txt', which is part of this source code package.
  15.   */
  16.  
  17.  
  18.  
  19. +using System;
  20.  using System.IO;
  21.  using System.Net;
  22.  using System.Net.Sockets;
  23. +using System.Threading;
  24.  
  25.  namespace SteamKit2
  26.  {
  27.      /// <summary>
  28.      /// Represents a Tcp socket.
  29.      /// </summary>
  30.      sealed class TcpSocket
  31.      {
  32. @@ -35,27 +37,65 @@ namespace SteamKit2
  33.  
  34.          /// <summary>
  35.          /// Initializes a new instance of the <see cref="TcpSocket"/> class.
  36.          /// </summary>
  37.          public TcpSocket()
  38.          {
  39.          }
  40.  
  41. +        private static ManualResetEvent TimeoutObject = new ManualResetEvent(false);
  42. +        private static Exception socketexception;
  43. +        private static bool IsConnectionSuccessful = false;
  44. +
  45. +        private static void ConnectCallback(IAsyncResult asyncresult)
  46. +        {
  47. +            try
  48. +            {
  49. +                IsConnectionSuccessful = false;
  50. +                Socket sock = asyncresult.AsyncState as Socket;
  51. +                sock.EndConnect(asyncresult);
  52. +                IsConnectionSuccessful = true;
  53. +            }
  54. +            catch (Exception ex)
  55. +            {
  56. +                IsConnectionSuccessful = false;
  57. +                socketexception = ex;
  58. +            }
  59. +            finally
  60. +            {
  61. +                TimeoutObject.Set();
  62. +            }
  63. +        }
  64. +
  65.  
  66.          /// <summary>
  67.          /// Disconnects (if needed) and connects the specified end point.
  68.          /// </summary>
  69.          /// <param name="endPoint">The end point.</param>
  70.          public void Connect( IPEndPoint endPoint )
  71.          {
  72.              Disconnect();
  73.  
  74. +            TimeoutObject.Reset();
  75. +            socketexception = null;
  76. +
  77.              sock = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
  78. -            sock.Connect( endPoint );
  79. +            sock.BeginConnect( endPoint, new AsyncCallback(ConnectCallback), sock );
  80. +            if (TimeoutObject.WaitOne(5000, false))
  81. +            {
  82. +                if (!IsConnectionSuccessful)
  83. +                    throw socketexception;
  84. +            }
  85. +            else
  86. +            {
  87. +                sock.Close();
  88. +                // timeout
  89. +                throw new SocketException(10060);
  90. +            }
  91.  
  92.              bConnected = true;
  93.  
  94.              sockStream = new NetworkStream( sock, true );
  95.  
  96.              Reader = new BinaryReader( sockStream );
  97.              Writer = new BinaryWriter( sockStream );
  98.          }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement