Example #1
0
        private async Task CheckDeadConnectionsLoopAsync()
        {
            while (_working)
            {
                var now         = DateTime.UtcNow;
                var connections =
                    _connections.GetAllConnections();

                foreach (var connection in connections.Where(itm => itm.Connected))
                {
                    try
                    {
                        connection.SocketStatistic.EachSecondTimer();

                        if (!connection.IsServerSocketDead(now, ReceiveDataTimeoutToKill, InitTimeoutToKill))
                        {
                            continue;
                        }

                        _log.InvokeInfoLog(connection, $"Found dead connection {connection.ContextName} with ID {connection.Id}. Disconnecting...");
                        connection.Disconnect();
                    }
                    catch (Exception e)
                    {
                        _log.InvokeExceptionLog(connection, e, false);
                    }
                }

                await Task.Delay(1000);
            }
        }
        private async Task <ClientTcpContext <TSocketData> > ConnectAsync(IPEndPoint ipEndPoint, long socketId)
        {
            _log.InvokeInfoLog(null, "Attempt To Connect:" + ipEndPoint.Address + ":" + ipEndPoint.Port);

            var tcpClient = new TcpClient();
            await tcpClient.ConnectAsync(ipEndPoint.Address, ipEndPoint.Port);

            var clientTcpContext = _socketContextFactory();

            clientTcpContext.Id = socketId;

            await clientTcpContext.StartAsync(tcpClient,
                                              _socketSerializerFactory(),
                                              _lockObject,
                                              _log,
                                              null,
                                              _deliveryBuffer);

            _log.InvokeInfoLog(clientTcpContext, "Connected. Id=" + clientTcpContext.Id);

            return(clientTcpContext);
        }