Esempio n. 1
0
        public static Task ConnectAsync(this Socket socket, IPEndPoint endpoint)
        {
            if (socket == null)
                throw new ArgumentNullException("socket");

            var tcs = new TaskCompletionSource<bool>();

            socket.BeginConnect(endpoint, result =>
            {
                try
                {
                    var s = result.AsyncState as Socket;
                    s.EndConnect(result);

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

            }, socket);

            return tcs.Task;
        }
Esempio n. 2
0
 public static bool smethod_7(this TcpClient tcpClient_0, string string_0, int int_0, int int_1)
 {
     IAsyncResult asyncResult = tcpClient_0.BeginConnect(string_0, int_0, null, null);
     WaitHandle asyncWaitHandle = asyncResult.AsyncWaitHandle;
     bool result;
     try
     {
         if (asyncWaitHandle.WaitOne(int_1))
         {
             tcpClient_0.EndConnect(asyncResult);
             result = true;
         }
         else
         {
             tcpClient_0.Close();
             result = false;
         }
     }
     catch (SocketException)
     {
         result = false;
     }
     finally
     {
         asyncWaitHandle.Close();
     }
     return result;
 }
Esempio n. 3
0
 public static bool Connect(this TcpClient self, String host, int port, int millisecondsTimeout = Timeout.Infinite)
 {
     ManualResetEvent blocker = new ManualResetEvent(false);
     IAsyncResult connecting = self.BeginConnect(host, port, result => { blocker.Set(); }, null);
     if (blocker.WaitOne(millisecondsTimeout))
         return true;
     self.Close();
     return false;
 }
Esempio n. 4
0
 public static void ConnectAPM(this Socket socket, EndPoint remoteEndpoint, Action handler)
 {
     var callback = new AsyncCallback(asyncResult =>
     {
         ((Socket)asyncResult.AsyncState).EndConnect(asyncResult);
         handler();
     });
     socket.BeginConnect(remoteEndpoint, callback, socket);
 }
        public static void Connect(this TcpClient tcpClient, string ipAddress, int port, int connectTimeoutMS)
        {            
            IAsyncResult result = tcpClient.BeginConnect(ipAddress, port, null, null);
            result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(connectTimeoutMS));
            if (!tcpClient.Connected)
                throw new SocketException((int)SocketError.TimedOut);

            // we have connected
            tcpClient.EndConnect(result);
        }
        public static Task ConnectAsync(this Socket socket, EndPoint endPoint)
        {
            if (socket == null) throw new ArgumentNullException("socket");
            if (endPoint == null) throw new ArgumentNullException("endPoint");

            return Task.Factory.FromAsync(
                (ac, state) => socket.BeginConnect(endPoint, ac, state),
                socket.EndConnect,
                null,
                TaskCreationOptions.None);
        }
Esempio n. 7
0
 public static Task ConnectAsyncTask(this Socket socket, EndPoint endpoint, CancellationTokenSource cancellationTokenSource)
 {
     return Task.Factory.FromAsync(
         (ac, o) => socket.BeginConnect(endpoint, ac, o),
         ar =>
         {
             cancellationTokenSource.Token.ThrowIfCancellationRequested();
             socket.EndConnect(ar);
         },
         null);
 }
Esempio n. 8
0
        /// <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;
        }
        public static Task ConnectAsync(this Socket socket, EndPoint endPoint)
        {
            if (socket == null) throw new ArgumentNullException(nameof(socket));

            var tcs = new TaskCompletionSource<int>();
            socket.BeginConnect(endPoint, iar =>
            {
                try { socket.EndConnect(iar); tcs.TrySetResult(0); }
                catch (Exception e) { tcs.TrySetException(e); }
            }, state: null);
            return tcs.Task;
        }
Esempio n. 10
0
 public static Task ConnectTaskAsync(
     this Socket socket, IPAddress[] adresses, int port)
 {
     var tcs = new TaskCompletionSource<bool>(socket);
     socket.BeginConnect(adresses, port, iar =>
     {
         var t = (TaskCompletionSource<bool>)iar.AsyncState;
         var s = (Socket)t.Task.AsyncState;
         try
         {
             s.EndConnect(iar);
             t.TrySetResult(true);
         }
         catch (Exception exc) { t.TrySetException(exc); }
     }, tcs);
     return tcs.Task;
 }
		public static bool TryConnect(this TcpClient tcpClient, IPEndPoint endPoint, int timeout)
		{
			IAsyncResult ar = tcpClient.BeginConnect(endPoint.Address, endPoint.Port, null, null);
			var wh = ar.AsyncWaitHandle;
			try
			{
				if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(timeout), false))
				{
					tcpClient.Close();
					return false;
				}

				tcpClient.EndConnect(ar);
				return true;
			}
			finally
			{
				wh.Close();
			}
		}
Esempio n. 12
0
        public static Task ConnectAsync(this Socket socket, IPEndPoint endPoint)
        {
            var tcs = new TaskCompletionSource<object>(socket);

            socket.BeginConnect(endPoint, ar =>
            {
                var t = (TaskCompletionSource<object>)ar.AsyncState;
                var s = (Socket)t.Task.AsyncState;
                try
                {
                    s.EndConnect(ar);
                    t.TrySetResult(null);
                }
                catch (Exception ex)
                {
                    t.TrySetException(ex);
                }
            }, tcs);
            return tcs.Task;
        }
Esempio n. 13
0
        public static void ConnectWithTimeout(this TcpClient client, Uri remoteUri, TimeSpan timeout)
        {
            var connectResult = client.BeginConnect(remoteUri.Host, remoteUri.Port, ar => { }, null);
            if (!connectResult.AsyncWaitHandle.WaitOne(HalibutLimits.TcpClientConnectTimeout))
            {
                try
                {
                    client.Close();
                }
                catch (SocketException)
                {
                }
                catch (ObjectDisposedException)
                {
                }

                throw new Exception("The client was unable to establish the initial connection within " + HalibutLimits.TcpClientConnectTimeout);
            }

            client.EndConnect(connectResult);
        }
Esempio n. 14
0
 /// <summary>
 /// 开始一个对远程主机连接的异步请求。
 /// </summary>
 /// <param name="server">服务端。</param>
 /// <param name="endPoint">打算连接到的 <see cref="IPEndPoint"/>。</param>
 /// <param name="timeout">表示等待的毫秒数的 <see cref="TimeSpan"/>,或表示 -1 毫秒(无限期等待)的 <see cref="TimeSpan"/>。</param>
 public static void Connect(this TcpClient server, IPEndPoint endPoint, TimeSpan timeout)
 {
     var asyncResult = server.BeginConnect(endPoint.Address, endPoint.Port, null, null);
     if(!asyncResult.AsyncWaitHandle.WaitOne(timeout)) throw new SocketException((int)SocketError.TimedOut);
     server.EndConnect(asyncResult);
 }
        /// <summary>
        /// Extends BeginConnect so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// tcpclient.BeginConnect(addresses, port, requestCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginConnect(this TcpClient tcpclient, System.Net.IPAddress[] addresses, Int32 port, AsyncCallback requestCallback)
        {
            if(tcpclient == null) throw new ArgumentNullException("tcpclient");

            return tcpclient.BeginConnect(addresses, port, requestCallback, null);
        }
        /// <summary>
        /// Extends BeginConnect so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// tcpclient.BeginConnect(host, port, requestCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginConnect(this TcpClient tcpclient, String host, Int32 port, AsyncCallback requestCallback)
        {
            if(tcpclient == null) throw new ArgumentNullException("tcpclient");

            return tcpclient.BeginConnect(host, port, requestCallback, null);
        }
 public static async Task ConnectAsync(this Socket socket, IPEndPoint endpoint)
 {
     await Task.Factory.FromAsync((callback, state) => socket.BeginConnect(endpoint, callback, state), ias => socket.EndConnect(ias), null);
 }
Esempio n. 18
0
        public static Task<Maybe<SocketError>> ConnectNonBlocking(this Socket s, EndPoint ep)
        {
            var tcs = new TaskCompletionSource<Maybe<SocketError>>();

            try
            {
                IAsyncResult r = s.BeginConnect(ep, iar =>
                {
                    try
                    {
                        s.EndConnect(iar);
                        tcs.SetResult(Maybe<SocketError>.Nothing);
                    }
                    catch (SocketException skex)
                    {
                        tcs.SetResult(skex.SocketErrorCode);
                    }
                    catch (Exception ex)
                    {
                        tcs.SetException(ex);
                    }
                }, (object)null);

                Debug.Assert(r != null);
            }
            catch (SocketException skex)
            {
                tcs.SetResult(skex.SocketErrorCode);
            }
            catch (Exception ex)
            {
                tcs.SetException(ex);
            }

            return tcs.Task;
        }
        /// <summary>
        /// Extends BeginConnect so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// socket.BeginConnect(remoteEP, callback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginConnect(this Socket socket, System.Net.EndPoint remoteEP, AsyncCallback callback)
        {
            if(socket == null) throw new ArgumentNullException("socket");

            return socket.BeginConnect(remoteEP, callback, null);
        }
 public static IAsyncResult BeginConnect(this TcpClient tcpClient, IPEndPoint endPoint, AsyncCallback asyncCallback, object state)
 {
     if (tcpClient == null) throw new ArgumentNullException("tcpClient");
     return tcpClient.BeginConnect(endPoint.Address, endPoint.Port, asyncCallback, state);
 }
        /// <summary>
        /// Extends BeginConnect so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// socket.BeginConnect(host, port, requestCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginConnect(this Socket socket, String host, Int32 port, AsyncCallback requestCallback)
        {
            if(socket == null) throw new ArgumentNullException("socket");

            return socket.BeginConnect(host, port, requestCallback, null);
        }
        /// <summary>
        /// Extends BeginConnect so that when a state object is not needed, null does not need to be passed.
        /// <example>
        /// socket.BeginConnect(address, port, requestCallback);
        /// </example>
        /// </summary>
        public static IAsyncResult BeginConnect(this Socket socket, System.Net.IPAddress address, Int32 port, AsyncCallback requestCallback)
        {
            if(socket == null) throw new ArgumentNullException("socket");

            return socket.BeginConnect(address, port, requestCallback, null);
        }
Esempio n. 23
0
 internal static Task ConnectAsync(this Socket socket, string host, int port)
 {
     return Task.Factory.FromAsync(socket.BeginConnect(host, port, null, null), socket.EndConnect);
 }
Esempio n. 24
0
 /// <summary>
 /// 开始一个对远程主机连接的异步请求。
 /// </summary>
 /// <param name="server">服务端。</param>
 /// <param name="host">远程主机的名称。</param>
 /// <param name="port">远程主机的端口号。</param>
 /// <param name="timeout">表示等待的毫秒数的 <see cref="TimeSpan"/>,或表示 -1 毫秒(无限期等待)的 <see cref="TimeSpan"/>。</param>
 public static void Connect(this TcpClient server, string host, int port, TimeSpan timeout)
 {
     var asyncResult = server.BeginConnect(host, port, null, null);
     if(!asyncResult.AsyncWaitHandle.WaitOne(timeout)) throw new SocketException((int)SocketError.TimedOut);
     server.EndConnect(asyncResult);
 }