/// <summary> /// Connect to a remote server /// </summary> /// <param name="remoteEndPoint">Address information of a remote server</param> /// <returns></returns> public bool Connect(IPEndPoint remoteEndPoint) { if (_udpSocket != null && _udpSocket.State != SocketState.Disconnected) { _udpSocket.Dispose(); } _udpSocket = new UdpSocket(_connectionConfig.MtuSize, DisconnectTimeout); if (_connectionConfig.BindPortRange != null) { _udpSocket.SetBindPort(_connectionConfig.BindPortRange.StartPort, _connectionConfig.BindPortRange.EndPort); } _connectionBroker = new ConnectingBroker(_udpSocket, this); _udpSocket.SetBroker(_connectionBroker, SocketState.Connecting); _peerId = 0; _roundTripTime = new RoundTripTime(); ResetQueues(); RemoteEndPoint = remoteEndPoint; AvailableSpace = (uint)(MTU - (uint)Lengths.FragmentHeader - (uint)Lengths.MtuHeader); _udpBuffer = new byte[MTU]; _cipher.GenerateLocalKeys(); if (_udpSocket.Prepare(RemoteEndPoint)) { _isAckThreadRunning = true; if (_sendAckBackgroundThread != null) { _sendAckBackgroundThread.Abort(); } #if PERF TryToConnectAsync(); #else _sendAckBackgroundThread = new Thread(SendAckBackground); _sendAckBackgroundThread.Name = "SendAckThread"; _sendAckBackgroundThread.IsBackground = true; _sendAckBackgroundThread.Start(); #endif _udpSocket.Send(_connectionBroker.CreateConnectCommand( ChannelCount, MTU, DataSerializer.Version, DisconnectTimeout, IsCrcEnabled, _cipher.PublicKey, EnvironmentTimer.GetTickCount())); } else { if (_listener != null) { string message = string.Format("Fail to connect to [{0}]{1}:{2} - SocketState: {3}", remoteEndPoint.AddressFamily, remoteEndPoint.Address, remoteEndPoint.Port, _udpSocket.State); _listener.OnStatusChanged(StatusCode.FailedToConnect, message); } return(false); } return(true); }