Ejemplo n.º 1
0
        internal async Task <bool> AttemptConnectAsync(bool isDedicatedContext)
        {
            try {
                if (_clientCancellationToken != null && _clientCancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                await socket.ConnectAsync(RemoteIpEndpoint);

                NetworkClient = new NetworkClient(socket, _clientCancellationToken, CommandManager, _symmetricEncryptionAlgorithm);
                NetworkClient.OnDisconnected += NetworkClient_OnDisconnected;

#pragma warning disable CS4014
                Task.Run(() => OnNetworkClientConnected?.Invoke(this, new ConnectedEventArgs(RemoteIpEndpoint)));
#pragma warning restore CS4014

                if (!isDedicatedContext)
                {
                    _clientWorkerTask = Task.Run(async() => await NetworkClient.ReceiveAsync());
                }
                else
                {
                    await NetworkClient.ReceiveAsync();
                }

                return(IsConnected = true);
            } catch (SocketException) {
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Accept new clients loop
        /// </summary>
        private void AcceptCallback(IAsyncResult iAR)
        {
            if (Stopping)
            {
                return;
            }

            var ConnectingClient = BaseSocket.EndAccept(iAR);

            OnNetworkClientConnected?.Start(this, new OnNetworkClientConnectedEventArgs(new NLCSocket(ConnectingClient, SerializationProdiver, UseSSL, AllowInsecureCerts, ServerCertificate)));

            BaseSocket.BeginAccept(AcceptCallback, null);
        }