private void ConnectAsync(Action callback, Action <TcpTransportResult> faultCallback) { WRITE_LOG(string.Format("Socket.ConnectAsync[#3] {0} ({1}:{2})", Id, Host, Port)); TLSocks5Proxy socks5Proxy = ProxyConfig != null && ProxyConfig.IsEnabled.Value && !ProxyConfig.IsEmpty ? ProxyConfig.GetProxy() as TLSocks5Proxy : null; if (socks5Proxy != null) { try { ActualHost = StaticHost; ActualPort = StaticPort; RaiseConnectingAsync(); SocksProxy.ConnectToSocks5Proxy(_socket, socks5Proxy.Server.ToString(), (ushort)socks5Proxy.Port.Value, StaticHost, (ushort)StaticPort, socks5Proxy.Username.ToString(), socks5Proxy.Password.ToString()); OnConnected(new SocketAsyncEventArgs { SocketError = SocketError.Success }, callback, faultCallback); } catch (Exception ex) { faultCallback.SafeInvoke(new TcpTransportResult(SocketAsyncOperation.Connect, ex)); WRITE_LOG("Socket.ConnectAsync[#3]", ex); } } else { System.Diagnostics.Debug.WriteLine(" Connecting mtproto=[server={0} port={1}]", Host, Port); var args = new SocketAsyncEventArgs { RemoteEndPoint = new IPEndPoint(_address, Port) }; args.Completed += (o, e) => OnConnected(e, callback, faultCallback); try { ActualHost = Host; ActualPort = Port; RaiseConnectingAsync(); _socket.ConnectAsync(args); } catch (Exception ex) { faultCallback.SafeInvoke(new TcpTransportResult(SocketAsyncOperation.Connect, ex)); WRITE_LOG("Socket.ConnectAsync[#3]", ex); } } }
private async Task <bool> ConnectAsync(double timeout, Action <TcpTransportResult> faultCallback) { TLSocks5Proxy socks5Proxy = ProxyConfig != null && ProxyConfig.IsEnabled.Value && !ProxyConfig.IsEmpty ? ProxyConfig.GetProxy() as TLSocks5Proxy : null; if (socks5Proxy != null) { try { ActualHost = StaticHost; ActualPort = StaticPort; RaiseConnectingAsync(); await SocksProxy.ConnectToSocks5Proxy(timeout, _socket, _dataWriter, _dataReader, socks5Proxy.Server.ToString(), (ushort)socks5Proxy.Port.Value, StaticHost, (ushort)StaticPort, socks5Proxy.Username.ToString(), socks5Proxy.Password.ToString()); lock (_dataWriterSyncRoot) { var buffer = GetInitBufferInternal(); _dataWriter.WriteBytes(buffer); var result = _dataWriter.StoreAsync().AsTask().Result; } lock (_isConnectedSyncRoot) { _isConnecting = false; _isConnected = true; } } catch (Exception ex) { var status = SocketError.GetStatus(ex.HResult); WRITE_LOG("TCPTransportWinRT.ConnectAsync " + status, ex); var error = SocketError.GetStatus(ex.HResult); faultCallback.SafeInvoke(new TcpTransportResult(ex)); return(false); } } else { try { ActualHost = Host; ActualPort = Port; RaiseConnectingAsync(); //var address = IPAddress.IsValidIPv6(Host); await _socket.ConnectAsync(new HostName(Host), Port.ToString(CultureInfo.InvariantCulture)).WithTimeout(timeout); lock (_dataWriterSyncRoot) { var buffer = GetInitBufferInternal(); _dataWriter.WriteBytes(buffer); } lock (_isConnectedSyncRoot) { _isConnecting = false; _isConnected = true; } } catch (Exception ex) { var status = SocketError.GetStatus(ex.HResult); WRITE_LOG("TCPTransportWinRT.ConnectAsync " + status, ex); var error = SocketError.GetStatus(ex.HResult); faultCallback.SafeInvoke(new TcpTransportResult(ex)); return(false); } } return(true); }