Esempio n. 1
0
        // connect
        public KChannel(long id, uint localConn, Socket socket, IPEndPoint remoteEndPoint, KService kService)
        {
            this.LocalConn = localConn;
            if (kChannels.ContainsKey(this.LocalConn))
            {
                throw new Exception($"channel create error: {this.LocalConn} {remoteEndPoint} {this.ChannelType}");
            }

            this.Id          = id;
            this.ChannelType = ChannelType.Connect;

            Log.Info($"channel create: {this.Id} {this.LocalConn} {remoteEndPoint} {this.ChannelType}");

            this.Service       = kService;
            this.RemoteAddress = remoteEndPoint;
            this.socket        = socket;
            this.kcp           = Kcp.KcpCreate(this.RemoteConn, (IntPtr)this.LocalConn);

            kChannels.Add(this.LocalConn, this);

            this.lastRecvTime = kService.TimeNow;
            this.CreateTime   = kService.TimeNow;

            this.Connect();
        }
Esempio n. 2
0
        public void HandleConnnect()
        {
            // 如果连接上了就不用处理了
            if (this.IsConnected)
            {
                return;
            }

            this.kcp = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.LocalConn));
            this.InitKcp();

            ulong localRmoteConn = ((ulong)this.RemoteConn << 32) | this.LocalConn;

            idLocalRemoteConn.TryAdd(this.Id, localRmoteConn);

            Log.Info($"channel connected: {this.Id} {this.LocalConn} {this.RemoteConn} {this.RemoteAddress}");
            this.IsConnected  = true;
            this.lastRecvTime = this.Service.TimeNow;

            while (true)
            {
                if (this.sendBuffer.Count <= 0)
                {
                    break;
                }

                KcpWaitPacket buffer = this.sendBuffer.Dequeue();
                this.KcpSend(buffer);
            }
        }