/// <summary>
        /// Attempts connecting to the given host and port with a timeout. If the timeout limit is reached,
        /// the connection is dropped.
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static bool CanConnect(this Socket socket, string host, int port, TimeSpan timeout)
        {
            var result = socket.BeginConnect(host, port, null, null);
            result.AsyncWaitHandle.WaitOne(timeout, true);

            var connected = socket.Connected;

            if (connected)
                socket.BeginDisconnect(true, ar => {}, null);

            return connected;
        }
Exemple #2
0
 public static Task DisconnectTaskAsync(
     this Socket socket, bool reuseSocket)
 {
     var tcs = new TaskCompletionSource<bool>(socket);
     socket.BeginDisconnect(reuseSocket, iar =>
     {
         var t = (TaskCompletionSource<bool>)iar.AsyncState;
         var s = (Socket)t.Task.AsyncState;
         try
         {
             s.EndDisconnect(iar);
             t.TrySetResult(true);
         }
         catch (Exception exc) { t.TrySetException(exc); }
     }, tcs);
     return tcs.Task;
 }
        public static Task DisconnectAsync(this Socket socket, bool reuseSocket)
        {
            var tcs = new TaskCompletionSource<object>(socket);

            socket.BeginDisconnect(reuseSocket, ar =>
            {
                var t = (TaskCompletionSource<object>)ar.AsyncState;
                var s = (Socket)t.Task.AsyncState;
                try
                {
                    s.EndDisconnect(ar);
                    t.TrySetResult(null);
                }
                catch (Exception ex)
                {
                    t.TrySetException(ex);
                }
            }, tcs);
            return tcs.Task;
        }
        /// <summary>
        /// Extends BeginDisconnect so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// socket.BeginDisconnect(reuseSocket, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginDisconnect(this Socket socket, Boolean reuseSocket, AsyncCallback callback)
        {
            if(socket == null) throw new ArgumentNullException("socket");

            return socket.BeginDisconnect(reuseSocket, callback, null);
        }
Exemple #5
0
        public static Task DisconnectAsync(this Socket socket, bool reuseSocket = false)
        {
            if (socket == null)
                throw new ArgumentNullException("socket");

            var tcs = new TaskCompletionSource<bool>();

            socket.BeginDisconnect(reuseSocket, result =>
            {
                try
                {
                    var s = result.AsyncState as Socket;
                    s.EndDisconnect(result);

                    tcs.SetResult(!s.Connected);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }

            }, socket);

            return tcs.Task;
        }