Beispiel #1
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 (Connected)
                {
                    return;
                }

                ConnectSender.SendSYN(this.NetSocket, this.RemoteEndPoint);
            }
            catch (Exception e)
            {
#if DEBUG
                LogRecord.Log(LogLevel.Warn, "StartConnecting", e);
#endif
            }
        }
Beispiel #2
0
 /// <summary>
 /// 断开连接
 /// </summary>
 public override void DisConnect()
 {
     try
     {
         Connected = false;
         ConnectSender.SendFIN(this.NetSocket, this.RemoteEndPoint, this.Id);
         OnDisConnect?.Invoke(this);
     }
     catch { }
 }
Beispiel #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();
            }
            var channel = new KcpChannel(socket, remoteEP as IPEndPoint, this, conv);

            channel.InitKcp();
            channel.OnConnect = HandleAccept;
            channel.OnConnect?.Invoke(channel);
            ConnectSender.SendACK(this.acceptor, channel.RemoteEndPoint, conv);
        }