Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # HG changeset patch
- # User Nicholas Hastings <skamonkey@gmail.com>
- # Date 1351785036 14400
- # Node ID 936eecb0a2cbdbcb6d34973ff123fa5ca1fabcb8
- # Parent 274a1e90c589ceb55d0abe3e2cb53154d3737b8c
- Steam2 socket connect timeouts.
- diff --git a/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs b/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs
- --- a/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs
- +++ b/SteamKit2/SteamKit2/Networking/Steam2/TcpSocket.cs
- @@ -1,18 +1,20 @@
- /*
- * This file is subject to the terms and conditions defined in
- * file 'license.txt', which is part of this source code package.
- */
- +using System;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- +using System.Threading;
- namespace SteamKit2
- {
- /// <summary>
- /// Represents a Tcp socket.
- /// </summary>
- sealed class TcpSocket
- {
- @@ -35,27 +37,65 @@ namespace SteamKit2
- /// <summary>
- /// Initializes a new instance of the <see cref="TcpSocket"/> class.
- /// </summary>
- public TcpSocket()
- {
- }
- + private static ManualResetEvent TimeoutObject = new ManualResetEvent(false);
- + private static Exception socketexception;
- + private static bool IsConnectionSuccessful = false;
- +
- + private static void ConnectCallback(IAsyncResult asyncresult)
- + {
- + try
- + {
- + IsConnectionSuccessful = false;
- + Socket sock = asyncresult.AsyncState as Socket;
- + sock.EndConnect(asyncresult);
- + IsConnectionSuccessful = true;
- + }
- + catch (Exception ex)
- + {
- + IsConnectionSuccessful = false;
- + socketexception = ex;
- + }
- + finally
- + {
- + TimeoutObject.Set();
- + }
- + }
- +
- /// <summary>
- /// Disconnects (if needed) and connects the specified end point.
- /// </summary>
- /// <param name="endPoint">The end point.</param>
- public void Connect( IPEndPoint endPoint )
- {
- Disconnect();
- + TimeoutObject.Reset();
- + socketexception = null;
- +
- sock = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
- - sock.Connect( endPoint );
- + sock.BeginConnect( endPoint, new AsyncCallback(ConnectCallback), sock );
- + if (TimeoutObject.WaitOne(5000, false))
- + {
- + if (!IsConnectionSuccessful)
- + throw socketexception;
- + }
- + else
- + {
- + sock.Close();
- + // timeout
- + throw new SocketException(10060);
- + }
- bConnected = true;
- sockStream = new NetworkStream( sock, true );
- Reader = new BinaryReader( sockStream );
- Writer = new BinaryWriter( sockStream );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement