/// <summary> /// Connect callback! /// </summary> /// <param name="ar"></param> internal void BeginConnectCallbackAsync(object sender, SocketAsyncEventArgs e) { if (!Disposed) { BaseSocketConnection connection = null; SocketConnector connector = null; Exception exception = null; if (e.SocketError == SocketError.Success) { try { connector = (SocketConnector)e.UserToken; connection = new ClientSocketConnection(Host, connector, connector.Socket); //----- Adjust buffer size! connector.Socket.ReceiveBufferSize = Host.SocketBufferSize; connector.Socket.SendBufferSize = Host.SocketBufferSize; //----- Initialize! Host.AddSocketConnection(connection); connection.Active = true; Host.InitializeConnection(connection); } catch (Exception ex) { exception = ex; if (connection != null) { Host.DisposeConnection(connection); Host.RemoveSocketConnection(connection); connection = null; } } } else { exception = new SocketException((int)e.SocketError); } if (exception != null) { FReconnectAttempted++; ReconnectConnection(false, exception); } } e.UserToken = null; e.Dispose(); e = null; }
/// <summary> /// Reconnects the connection adjusting the reconnect timer. /// </summary> /// <param name="connection"> /// Connection. /// </param> /// <param name="sleepTimeOutValue"> /// Sleep timeout before reconnect. /// </param> internal override void BeginReconnect(ClientSocketConnection connection) { if (!Disposed) { SocketConnector connector = (SocketConnector)connection.Creator; if ((connector.ProxyInfo == null || connector.ProxyInfo.Completed)) { connector.Reconnect(true, null, null); } } }
public SocketConnector AddConnector(string name, IPEndPoint remoteEndPoint, ProxyInfo proxyData, EncryptType encryptType, CompressionType compressionType, ICryptoService cryptoService, int reconnectAttempts, int reconnectAttemptInterval, IPEndPoint localEndPoint) { SocketConnector result = null; if (!Disposed) { result = new SocketConnector(this, name, remoteEndPoint, proxyData, encryptType, compressionType, cryptoService, reconnectAttempts, reconnectAttemptInterval, localEndPoint); AddCreator(result); } return(result); }
/// <summary> /// Reconnects the connection adjusting the reconnect timer. /// </summary> /// <param name="connection"> /// Connection. /// </param> /// <param name="sleepTimeOutValue"> /// Sleep timeout before reconnect. /// </param> internal override void BeginReconnect(ClientSocketConnection connection) { if (!Disposed) { if (connection != null) { SocketConnector connector = (SocketConnector)connection.Creator; if (connector != null) { connector.ReconnectConnection(true, null); } } } }
/// <summary> /// Connect callback! /// </summary> /// <param name="ar"></param> internal void BeginConnectCallback(IAsyncResult ar) { if (!Disposed) { BaseSocketConnection connection = null; SocketConnector connector = null; try { connector = (SocketConnector)ar.AsyncState; connection = new ClientSocketConnection(Host, connector, connector.Socket); connector.Socket.EndConnect(ar); //----- Adjust buffer size! connector.Socket.ReceiveBufferSize = Host.SocketBufferSize; connector.Socket.SendBufferSize = Host.SocketBufferSize; connection.Active = true; //----- Initialize! Host.AddSocketConnection(connection); InitializeConnection(connection); } catch (Exception ex) { if (ex is SocketException) { FReconnectAttempted++; Reconnect(false, connection, ex); } else { Host.FireOnException(connection, ex); } } } }
public SocketConnector AddConnector(string name, IPEndPoint remoteEndPoint, ProxyInfo proxyData, EncryptType encryptType, CompressionType compressionType, ICryptoService cryptoService, int reconnectAttempts, int reconnectAttemptInterval, IPEndPoint localEndPoint) { SocketConnector result = null; if (!Disposed) { result = new SocketConnector(this, name, remoteEndPoint, proxyData, encryptType, compressionType, cryptoService, reconnectAttempts, reconnectAttemptInterval, localEndPoint); AddCreator(result); } return result; }
public void Connect() { if (!Disposed) { FLastException = null; if (!Connected) { FConnectEvent.Reset(); FExceptionEvent.Reset(); FDisconnectEvent.Reset(); FSocketClient = new SocketClient(CallbackThreadType.ctWorkerThread, FSocketClientEvents, FDelimiterType, FDelimiter, FSocketBufferSize, FMessageBufferSize); SocketConnector connector = FSocketClient.AddConnector("SocketClientSync", FRemoteEndPoint); connector.EncryptType = FEncryptType; connector.CompressionType = FCompressionType; connector.CryptoService = FCryptClientEvents; connector.ProxyInfo = FProxyInfo; WaitHandle[] wait = new WaitHandle[] { FConnectEvent, FExceptionEvent }; FSocketClient.Start(); int signal = WaitHandle.WaitAny(wait, FConnectTimeout, false); switch (signal) { case 0: //----- Connect! FLastException = null; Connected = true; break; case 1: //----- Exception! Connected = false; FSocketConnection = null; FSocketClient.Stop(); FSocketClient.Dispose(); FSocketClient = null; break; default: //----- TimeOut! FLastException = new TimeoutException("Connect timeout."); Connected = false; FSocketConnection = null; FSocketClient.Stop(); FSocketClient.Dispose(); FSocketClient = null; break; } } } }