Exemplo n.º 1
0
        // accept
        public KChannel(uint localConn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
        {
            this.InstanceId   = IdGenerater.GenerateId();
            this.memoryStream = this.GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);

            this.LocalConn      = localConn;
            this.RemoteConn     = remoteConn;
            this.remoteEndPoint = remoteEndPoint;
            this.socket         = socket;
            this.kcp            = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.LocalConn));
            Kcp.KcpSetoutput(
                this.kcp,
                (bytes, len, k, user) =>
            {
                KService.Output(bytes, len, user);
                return(len);
            }
                );
            Kcp.KcpNodelay(this.kcp, 1, 10, 1, 1);
            Kcp.KcpWndsize(this.kcp, 256, 256);
            Kcp.KcpSetmtu(this.kcp, 470);
            this.lastRecvTime = kService.TimeNow;
            this.createTime   = kService.TimeNow;
            this.Accept();
        }
Exemplo n.º 2
0
        // accept
        public KChannel(uint localConn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
        {
            this.InstanceId = IdGenerater.GenerateId();

            this.LocalConn      = localConn;
            this.RemoteConn     = remoteConn;
            this.remoteEndPoint = remoteEndPoint;
            this.socket         = socket;
            this.kcp            = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.LocalConn));
            Kcp.KcpSetoutput(
                this.kcp,
                (bytes, len, k, user) =>
            {
                KService.Output(bytes, len, user);
                return(len);
            }
                );
            Kcp.KcpNodelay(this.kcp, 1, 10, 1, 1);
            Kcp.KcpWndsize(this.kcp, 256, 256);
            Kcp.KcpSetmtu(this.kcp, 470);
            this.isConnected           = true;
            this.isRecvFirstKcpMessage = false;
            this.lastRecvTime          = kService.TimeNow;
            this.CreateTime            = kService.TimeNow;
        }
Exemplo n.º 3
0
        /// <summary>
        /// 处理2次握手连接
        /// </summary>
        public void HandleConnnect(uint remoteConn)
        {
            if (this.isConnected)                //发现已经是连接状态  不用处理
            {
                return;
            }

            this.RemoteConn = remoteConn;

            this.kcp = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.LocalConn));
            Kcp.KcpSetoutput(
                this.kcp,
                (bytes, len, k, user) =>
            {
                KService.Output(bytes, len, user);
                return(len);
            }
                );
            Kcp.KcpNodelay(this.kcp, 1, 10, 1, 1);
            Kcp.KcpWndsize(this.kcp, 256, 256);
            Kcp.KcpSetmtu(this.kcp, 470);

            this.isConnected  = true;
            this.lastRecvTime = this.GetService().TimeNow;

            HandleSend();               //握手连接后 把缓存的消息发过去
        }
Exemplo n.º 4
0
        // accept
        public KChannel(uint localConn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService _kService) : base(_kService, ChannelType.Accept)
        {
            kService          = _kService;
            this.memoryStream = this.GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);

            this.LocalConn      = localConn;
            this.RemoteConn     = remoteConn;
            this.remoteEndPoint = remoteEndPoint;
            this.socket         = socket;
            this.kcp            = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.LocalConn));

            SetOutput();
            Kcp.KcpNodelay(this.kcp, 1, 10, 1, 1);
            Kcp.KcpWndsize(this.kcp, 256, 256);
            Kcp.KcpSetmtu(this.kcp, 470);
            this.lastRecvTime = kService.TimeNow;
            this.createTime   = kService.TimeNow;
            this.Accept();

            this.RemoteAddress = remoteEndPoint;

            //if(!_kService.CheckKcpWaitsnd) // yuxj debug
            //{
            //    int dd= 0;
            //}
        }
Exemplo n.º 5
0
        // connect
        public KChannel(uint conn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Connect)
        {
            this.Id     = conn;
            this.Conn   = conn;
            this.socket = socket;

            this.remoteEndPoint = remoteEndPoint;
            this.lastRecvTime   = kService.TimeNow;
            this.Connect(kService.TimeNow);
        }
Exemplo n.º 6
0
        // connect
        public KChannel(uint localConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Connect)
        {
            this.memoryStream = this.GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);

            this.LocalConn      = localConn;
            this.socket         = socket;
            this.remoteEndPoint = remoteEndPoint;
            this.lastRecvTime   = kService.TimeNow;
            this.createTime     = kService.TimeNow;
            this.Connect();
        }
Exemplo n.º 7
0
        // connect
        public KChannel(uint localConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Connect)
        {
            this.InstanceId = IdGenerater.GenerateId();

            this.LocalConn             = localConn;
            this.socket                = socket;
            this.remoteEndPoint        = remoteEndPoint;
            this.isRecvFirstKcpMessage = false;
            this.lastRecvTime          = kService.TimeNow;
            this.Connect();
        }
Exemplo n.º 8
0
        // connect
        public KChannel(uint conn, UdpClient socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Connect)
        {
            this.Id     = conn;
            this.Conn   = conn;
            this.socket = socket;
            this.parser = new PacketParser(this.recvBuffer);

            this.remoteEndPoint = remoteEndPoint;
            this.lastRecvTime   = kService.TimeNow;
            this.Connect(kService.TimeNow);
        }
Exemplo n.º 9
0
        // connect
        public KChannel(uint localConn, Socket socket, IPEndPoint remoteEndPoint, KService _kService) : base(_kService, ChannelType.Connect)
        {
            kService          = _kService;
            this.memoryStream = this.GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);

            this.LocalConn      = localConn;
            this.socket         = socket;
            this.remoteEndPoint = remoteEndPoint;
            this.lastRecvTime   = kService.TimeNow;
            this.createTime     = kService.TimeNow;
            this.Connect();

            this.RemoteAddress = remoteEndPoint;

            //if (!_kService.CheckKcpWaitsnd) // yuxj debug
            //{
            //    int dd = 0;
            //}
        }
Exemplo n.º 10
0
        public override void Dispose()
        {
            bool IsDisposeLocal = this.IsDispose;

            base.Dispose();
            this.IsDispose = true;
            if (IsDisposeLocal)
            {
                return;
            }

            this.OnError(ErrorCode.ERR_SocketError);

            try
            {
                if (this.Error == ErrorCode.ERR_Success)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        this.Disconnect();
                    }
                }
            }
            catch (Exception)
            {
                // ignored
            }

            if (this.kcp != IntPtr.Zero)
            {
                Kcp.KcpRelease(this.kcp);
                this.kcp = IntPtr.Zero;
            }
            this.socket = null;
            this.memoryStream.Dispose();
            kService = null;
        }
Exemplo n.º 11
0
        // accept
        public KChannel(uint conn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
        {
            this.Id             = conn;
            this.Conn           = conn;
            this.RemoteConn     = remoteConn;
            this.remoteEndPoint = remoteEndPoint;
            this.socket         = socket;
            kcp = new KCP(this.RemoteConn, this);
            kcp.SetOutput(this.Output);
            kcp.NoDelay(1, 10, 2, 1);              //fast
            kcp.SetMTU(470);
            kcp.WndSize(256, 256);
            this.isConnected = true;

            this.lastRecvTime = kService.TimeNow;
        }
Exemplo n.º 12
0
 public static int KcpOutput(IntPtr bytes, int len, IntPtr kcp, IntPtr user)
 {
     KService.Output(bytes, len, user);
     return(len);
 }
Exemplo n.º 13
0
 // accept
 public KChannel(uint conn, uint remoteConn, UdpClient socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
 {
     this.Id             = conn;
     this.Conn           = conn;
     this.RemoteConn     = remoteConn;
     this.remoteEndPoint = remoteEndPoint;
     this.socket         = socket;
     this.parser         = new PacketParser(this.recvBuffer);
     kcp = new Kcp(this.RemoteConn, this.Output);
     kcp.SetMtu(512);
     kcp.NoDelay(1, 10, 2, 1);              //fast
     this.isConnected  = true;
     this.lastRecvTime = kService.TimeNow;
 }
Exemplo n.º 14
0
        // accept
        public KChannel(uint localConn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
        {
            this.memoryStream = this.GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);

            this.LocalConn      = localConn;
            this.RemoteConn     = remoteConn;
            this.RemoteAddress  = remoteEndPoint;       //赋值一下端口 这是本人后期改的
            this.remoteEndPoint = remoteEndPoint;
            this.socket         = socket;
            this.kcp            = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.LocalConn));

            SetOutput();
            Kcp.KcpNodelay(this.kcp, 1, 10, 1, 1);
            Kcp.KcpWndsize(this.kcp, 256, 256);
            Kcp.KcpSetmtu(this.kcp, 470);
            this.lastRecvTime = kService.TimeNow;
            this.createTime   = kService.TimeNow;
            this.Accept();
        }