From a9a7b0456defbad149b0048b76e09cfdc670b2ba Mon Sep 17 00:00:00 2001 From: Ryan Stecker Date: Thu, 1 Nov 2012 18:53:24 -0500 Subject: [PATCH] Expose steam2 connection timeouts. --- .../SteamKit2/Networking/Steam2/TcpSocket.cs | 21 ++++++++++++++++--- SteamKit2/SteamKit2/Steam2/ServerClient.cs | 14 ++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs b/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs index 48592c3fc..f5591efb3 100644 --- a/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs +++ b/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs @@ -4,7 +4,7 @@ */ - +using System; using System.IO; using System.Net; using System.Net.Sockets; @@ -32,12 +32,19 @@ sealed class TcpSocket /// The binary writer. public BinaryWriter Writer { get; private set; } + /// + /// Gets the length of time a connection will attempt to establish before timing out. The default timeout is 30 seconds. + /// + /// The connection timeout. + public TimeSpan ConnectionTimeout { get; set; } + /// /// Initializes a new instance of the class. /// public TcpSocket() { + ConnectionTimeout = TimeSpan.FromSeconds( 30 ); } @@ -50,9 +57,17 @@ public void Connect( IPEndPoint endPoint ) Disconnect(); sock = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); - sock.Connect( endPoint ); + var asyncResult = sock.BeginConnect( endPoint, null, null ); + + bConnected = asyncResult.AsyncWaitHandle.WaitOne( ConnectionTimeout ); + + if ( !bConnected ) + { + sock.Close(); + return; + } - bConnected = true; + sock.EndConnect( asyncResult ); sockStream = new NetworkStream( sock, true ); diff --git a/SteamKit2/SteamKit2/Steam2/ServerClient.cs b/SteamKit2/SteamKit2/Steam2/ServerClient.cs index 565295b12..dd83cd4fe 100644 --- a/SteamKit2/SteamKit2/Steam2/ServerClient.cs +++ b/SteamKit2/SteamKit2/Steam2/ServerClient.cs @@ -4,7 +4,7 @@ */ - +using System; using System.Net; using System.Net.Sockets; using System.Text; @@ -91,6 +91,17 @@ public class ServerClient /// The end point. protected IPEndPoint EndPoint { get; private set; } + /// + /// Gets the length of time a connection will attempt to establish before timing out. The default timeout is 30 seconds. + /// + /// The connection timeout. + public TimeSpan ConnectionTimeout + { + get { return Socket.ConnectionTimeout; } + set { Socket.ConnectionTimeout = value; } + } + + /// /// Initializes a new instance of the class. /// @@ -99,6 +110,7 @@ public ServerClient() Socket = new TcpSocket(); } + /// /// Connects to the specified end point. ///