Exemplo n.º 1
0
        /// <summary>
        /// 断开连接
        /// </summary>
        public override void Disconnect()
        {
            if (!this.Connected)
            {
                return;
            }

            this.Connected = false;

            this.OnDisconnected?.Invoke(this);
            ConnectSender.SendFIN(this.SendParser.Packet, this.NetSocket, this.RemoteEndPoint, this.Id);

            //服务端连接断开把缓冲区丢进池
            if (this.NetService.ServiceType == NetServiceType.Server)
            {
                ParserStorage.Push(this.SendParser);
                ParserStorage.Push(this.RecvParser);
            }
            else
            {
                this.SendParser.Clear();
                this.RecvParser.Clear();
            }

            KCP.KcpRelease(this.Kcp);
            this.Kcp = IntPtr.Zero;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 处理连接请求ACK应答
        /// </summary>
        /// <param name="packet"></param>
        /// <param name="socket"></param>
        /// <param name="remoteEP"></param>
        private void HandleACK(Packet packet, Socket socket, EndPoint remoteEP)
        {
            var        conv = packet.RpcId;
            KcpChannel channel;

            if (this.ServiceType == NetServiceType.Client)
            {
                if (this.ClientChannel == null)
                {
                    return;
                }

                channel = this.ClientChannel as KcpChannel;
                channel.RemoteEndPoint = remoteEP as IPEndPoint;
                channel.Id             = packet.RpcId;
                channel.OnConnected    = OnConnect;
                ConnectSender.SendACK(this.ConnectParser.Packet, this.Acceptor, remoteEP, conv);
            }
            else
            {
                channel = new KcpChannel(socket, remoteEP as IPEndPoint, this, conv)
                {
                    OnConnected = OnAccept
                };
            }

            channel.InitKcp();
            channel.OnConnected?.Invoke(channel);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 处理客户端SYN连接请求
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="remoteEP"></param>
        private void HandleSYN(Socket socket, EndPoint remoteEP)
        {
            var conv = KcpConvIdCreator.CreateId();

            while (this.Channels.ContainsKey(conv))
            {
                conv = KcpConvIdCreator.CreateId();
            }
            ConnectSender.SendACK(this.ConnectParser.Packet, this.Acceptor, remoteEP, conv);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 模拟TCP三次握手连接服务端
        /// </summary>
        /// <returns></returns>
        public override void StartConnecting()
        {
            try
            {
                var now = TimeUitls.Now();
                if (now - this.LastConnectTime < ANetChannel.ReConnectInterval)
                {
                    return;
                }

                this.LastConnectTime = now;

                if (this.Connected)
                {
                    return;
                }

                ConnectSender.SendSYN(this.SendParser.Packet, this.NetSocket, this.RemoteEndPoint);
            }
            catch (Exception e)
            {
                Log.Warn(e, LoggerBllType.Network);
            }
        }