示例#1
0
        private void AcknowledgeConnect(IPEndPoint endPoint, IReadStream readStream,
                                        PacketHead packetHead)
        {
            if (_connections.ContainsKey(endPoint))
            {
                Logger.Warn($"{endPoint}已经连接,无需重复连接!");
                return;
            }

            var service = new SocketService();

            service.Connection.RemoteAddress = endPoint;
            service.Connection.LocalAddress  = _writer.Client.LocalEndPoint as IPEndPoint;

            if (_connections.TryAdd(endPoint, service))
            {
                _autoResetEvent.Set();
                UdpKeepAlive.AddConnection(service,
                                           () => HandleReconnect?.Invoke(service, true),
                                           () => {
                    HandleReconnect?.Invoke(service, false);
                    OnDisconnect(service);
                });

                HandleConnect?.Invoke(service, readStream);

                Logger.Info($"{endPoint}连接成功!");
            }
            else
            {
                Logger.Error($"添加连接{endPoint}失败!");
            }
        }
示例#2
0
        private void ResponseAck(IPEndPoint endPoint, IReadStream readStream,
                                 PacketHead packetHead)
        {
            if (!_connections.TryGetValue(endPoint, out var socketService))
            {
                Logger.Error($"{endPoint}连接没有添加!");
                return;
            }

            Logger.Info($"接收到{endPoint}心跳包!");

            Acknowledge(socketService, SendOption.Acknowledge | SendOption.Ack, packetHead);

            if (socketService.ReconnectCount > 0)
            {
                Logger.Debug("关闭重连:" + socketService.ReconnectCount);
                HandleReconnect?.Invoke(socketService, false);
            }

            UdpKeepAlive.Reset(socketService);
        }