Exemplo n.º 1
0
        //------------------------------------------------------------
        #region 构造相关
        public void Init(uint sid)
        {
            Debuger.Log("sid:{0}", sid);
            m_sid = sid;
            m_TempSendData.sid = sid;
            m_TempSendData.msgs.Add(new FSPMessage());

            m_Kcp = new KCP(m_sid, HandleKcpSend);
            m_Kcp.NoDelay(1, 10, 2, 1);
            m_Kcp.WndSize(128, 128);
        }
Exemplo n.º 2
0
    void init_kcp(UInt32 conv)
    {
        m_Kcp = new KCP(conv, null);
        m_Kcp.SetOutput(SendAction);

        // fast mode.
        m_Kcp.NoDelay(1, 20, 2, 1);
        m_Kcp.WndSize(80, 80);
        m_Kcp.SetMinRTO(10);
        m_Kcp.SetFastResend(1);
    }
Exemplo n.º 3
0
        void init_kcp(UInt32 conv)
        {
            mKcp = new KCP(conv, (byte[] buf, int size) =>
            {
                mUdpClient.Send(buf, size);
            });

            // fast mode.
            mKcp.NoDelay(1, 20, 2, 1);
            mKcp.WndSize(128, 128);
        }
Exemplo n.º 4
0
        void init_kcp(UInt32 conv)
        {
            mKcp = new KCP(conv, (byte[] buf, int size) =>
            {
                mUdpClient.Send(buf, size);
            });

            // fast mode.
            mKcp.NoDelay(1, 10, 2, 1);
            mKcp.WndSize(128, 128);
        }
Exemplo n.º 5
0
        public KCPSession(uint sid, Action <ISession, byte[], int> sender, ISessionListener listener)
        {
            Debuger.Log("sid:{0}", sid);

            m_id       = sid;
            m_sender   = sender;
            m_listener = listener;

            m_Kcp = new KCP(sid, HandleKcpSend);
            m_Kcp.NoDelay(1, 10, 2, 1);
            m_Kcp.WndSize(128, 128);
        }
Exemplo n.º 6
0
    void init_kcp(UInt32 conv)
    {
        conv = 1;
        mKcp = new KCP(conv, (byte[] buf, int size) =>
        {
            base.Send(buf, 0, buf.Length);
        });

        // fast mode.
        mKcp.NoDelay(1, 10, 2, 1);
        mKcp.WndSize(128, 128);
    }
Exemplo n.º 7
0
    void init_kcp(UInt32 conv)
    {
        m_Kcp = new KCP(conv, null);
        m_Kcp.SetOutput((byte[] buf, int size, object user) =>
        {
            m_KCPServer.Send(this, buf, size);
        });

        // fast mode.
        m_Kcp.NoDelay(1, 20, 2, 1);
        m_Kcp.WndSize(128, 128);
    }
Exemplo n.º 8
0
        public FSPSession(uint sid, Action <IPEndPoint, byte[], int> sender)
        {
            Debuger.Log();
            m_id     = sid;
            m_sender = sender;

            m_Kcp = new KCP(sid, HandleKcpSend);
            m_Kcp.NoDelay(1, 10, 2, 1);
            m_Kcp.WndSize(128, 128);

            m_active = true;
        }
Exemplo n.º 9
0
 public void Init(uint sessionid)
 {
     sessionID              = sessionid;
     sendBufferTemp         = new byte[4096];
     recvBufferTemp         = new byte[4096];
     tempSendData           = new FSPDataCToS();
     recvBuffQueue          = new SwitchQueue <byte[]>();
     tempSendData.sessionID = sessionid;
     tempSendData.msg       = new FSPMessage();
     kcp = new KCP(sessionid, HandleKcpSend);
     kcp.NoDelay(1, 10, 2, 1);
     kcp.WndSize(128, 128);
 }
Exemplo n.º 10
0
    public void Connect(string host, int port)
    {
        var endpoint = IPAddress.Parse(host);

        mSocket = new Socket(endpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
        mSocket.Connect(endpoint, port);
        mKCP = new KCP((uint)(new Random().Next(1, Int32.MaxValue)), rawSend);
        // normal:  0, 40, 2, 1
        // fast:    0, 30, 2, 1
        // fast2:   1, 20, 2, 1
        // fast3:   1, 10, 2, 1
        mKCP.NoDelay(0, 30, 2, 1);
        mRecvBuffer.Clear();
    }
Exemplo n.º 11
0
        //======================================================================
        //连接与断开连接相关
        //======================================================================
        public void Active(Socket socket, IPEndPoint remoteEndPoint)
        {
            RemoteEndPoint = (IPEndPoint)remoteEndPoint;

            if (Interlocked.CompareExchange(ref this.m_actived, 1, 0) == 0)
            {
                m_socket = socket;

                LocalEndPoint = (IPEndPoint)socket.LocalEndPoint;

                m_Kcp = new KCP(Id, HandleKcpSend);
                m_Kcp.NoDelay(1, 10, 2, 1);
                m_Kcp.WndSize(128, 128);
            }
        }
Exemplo n.º 12
0
        private KCPNet() : base()
        {
            mNetTag   = "KCP";
            kcpObject = new KCP(1, HandleKcpSend);
            kcpObject.NoDelay(1, 10, 2, 1);
            kcpObject.WndSize(128, 128);
            sendCallBack = SendAsyncCallback;

            for (int i = 0; i < 60; i++)
            {
                cacheBytesQue.Enqueue(new CacheByteObject()
                {
                    bytes = new byte[cacheByteLen]
                });
            }
        }
Exemplo n.º 13
0
    void init_kcp(UInt32 conv)
    {
        mKcp = new KCP(conv, (byte[] buf, int size) =>
        {
            try
            {
                mUdpClient.Send(buf, size);
            }catch(Exception e)
            {
                Debug.LogError(e);
            }
        });

        mKcp.NoDelay(1, 10, 2, 1);
        mKcp.WndSize(128, 128);
    }
Exemplo n.º 14
0
    public static bool Connect(int port, int playerId)
    {
        if (Connected)
        {
            return(false);
        }

        Client = new UdpClient(TcpClientProxy.server.Address.ToString(), port);
        Kcp    = new KCP((uint)playerId, SendWrap);
        Kcp.NoDelay(1, 10, 2, 1);
        Kcp.WndSize(128, 128);
        Receive();
        UpdateTime = 0;
        Connected  = true;
        return(true);
    }
Exemplo n.º 15
0
 private void createSocket(int conv, string host, int port)
 {
     _remoteEP = new IPEndPoint(IPAddress.Parse(host), port);
     _listenEP = new IPEndPoint(IPAddress.Any, port);
     // 主要一定要设置port为0,不然无法接收到服务器的消息
     _socket = new UdpClient(0);
     _socket.Connect(_remoteEP);
     _kcp = new KCP((uint)conv, (byte[] buf, int size) =>
     {
         //Log4U.LogDebug("ASynKcpUdpClientSocket:createSocket Message send data=", Encoding.ASCII.GetString(buf, 0, size));
         _socket.Send(buf, size);
     });
     // fast mode.
     _kcp.NoDelay(1, 10, 2, 1);
     _kcp.WndSize(128, 128);
     _socket.BeginReceive(ReceiveAsyn, this);
 }
Exemplo n.º 16
0
        public void Initialize(object user, KCPSetting setting)
        {
            _kcp = new KCP(setting.conv, user);
            _kcp.SetOutput(OutputKCP);

            // fast mode
            _kcp.NoDelay(setting.nodelay,
                         setting.interval,
                         setting.resend,
                         setting.nc);

            _kcp.WndSize(setting.sndwnd,
                         setting.rcvwnd);

            _recvTimeoutSec = setting.timeoutSec;
            kcpRcvBuf       = new byte[(KCP.IKCP_MTU_DEF + KCP.IKCP_OVERHEAD) * 3];
        }
Exemplo n.º 17
0
 public void Connect(string ip, int port)
 {
     Debuger.Log("连接服务端 IP {0} Port {1} ", ip, port);
     IP            = ip;
     remoteIp      = ip;
     remotePort    = port;
     remotEndPoint = IPUtility.GetHostEndPoint(ip, port);
     currentSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     currentSocket.Bind(new IPEndPoint(IPAddress.Any, Port));
     kcp = new KCP(0, HandleKcpSend);
     kcp.NoDelay(1, 10, 2, 1);
     kcp.WndSize(128, 128);
     Connected  = true;
     threadRecv = new Thread(ThreadRecv)
     {
         IsBackground = true
     };
     threadRecv.Start();
 }
Exemplo n.º 18
0
        public KcpChannel(int port, Action <byte[]> reveiveHandler)
        {
            _recEndPoint = new IPEndPoint(IPAddress.Any, 0);

            _reveiveHandler = reveiveHandler;

            _udpClient = new UdpClient(new IPEndPoint(IPAddress.Any, port));
            _kcp       = new KCP((UInt32) new Random((int)DateTime.Now.Ticks).Next(1, Int32.MaxValue),
                                 UdpSendData);

            //kcp fast mode.
            _kcp.NoDelay(1, 10, 2, 1);
            _kcp.WndSize(128, 128);

            _receiveMeesages = new Queue <byte[]>();

            _udpClient.BeginReceive(UdpReceiveMessage, this);

            new Task(Update).Start();
        }
Exemplo n.º 19
0
        public void Connect(string ip, int port)
        {
            Debuger.Log("ip:{0}, port:{1}", ip, port);
            m_ip             = ip;
            m_port           = port;
            m_RemoteEndPoint = IPUtils.GetHostEndPoint(ip, port);

            m_SystemSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            m_SystemSocket.Bind(new IPEndPoint(IPAddress.Any, bindPort));

            m_Kcp = new KCP(0, HandleKcpSend);
            m_Kcp.NoDelay(1, 10, 2, 1);
            m_Kcp.WndSize(128, 128);

            Connected = true;

            m_ThreadRecv = new Thread(Thread_Recv)
            {
                IsBackground = true
            };
            m_ThreadRecv.Start();
        }
Exemplo n.º 20
0
        void init_kcp(UInt32 conv)
        {
            mKcp = new KCP(conv, (byte[] buf, int size) =>
            {
                try
                {
                    mUdpClient.Send(buf, size);
                }
                catch (Exception e)
                {
                    // itfantasy added
                    if (this.errHandler != null)
                    {
                        this.errHandler.Invoke(e.Message);
                    }
                }
            });

            // fast mode.
            mKcp.NoDelay(1, 10, 2, 1);
            mKcp.WndSize(128, 128);
        }
Exemplo n.º 21
0
        public KcpChannel(string ip, int port, Action <byte[]> reveiveHandler)
        {
            _allEndPoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), port);
            _recEndPoint = new IPEndPoint(IPAddress.Any, 0);

            _reveiveHandler = reveiveHandler;

            _udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse(ip), port));
            _kcp       = new KCP((UInt32) new Random((int)DateTime.Now.Ticks).Next(1, Int32.MaxValue),
                                 UdpSendData);

            //kcp fast mode.
            _kcp.NoDelay(1, 10, 2, 1);
            _kcp.WndSize(128, 128);

            _receiveMeesages = new Queue <byte[]>();

            Thread revThread = new Thread(ReceiveMessage);

            revThread.Start();
            Thread updataThread = new Thread(Update);

            updataThread.Start();
        }
Exemplo n.º 22
0
        void Start()
        {
            kcpObject = new KCP(1, HandleKcpSend);
            kcpObject.NoDelay(1, 10, 2, 1);
            kcpObject.WndSize(128, 128);

            IPAddress tip = IPAddress.Parse("127.0.0.1");

            //var mTargetPoint = new IPEndPoint(tip, 200236);
            testServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            mRecPoint = new IPEndPoint(IPAddress.Any, mLocalPort);
            IPEndPoint tMypoint = new IPEndPoint(IPAddress.Any, mLocalPort);

            testServer.Bind(tMypoint);

            taskStart = true;
            recTask   = Task.Run(RecThread);

            Application.runInBackground = true;

            tetstdata.AddInt(3);
            Debug.Log("测试KCP服务器启动");
        }
Exemplo n.º 23
0
        void init_kcp(UInt32 conv)
        {
            mKcp = new KCP(conv, (byte[] buf, int size) =>
            {
                mUdpClient.Send(buf, size);
            });

            mKcp.NoDelay(1, 10, 2, 1);
        }
Exemplo n.º 24
0
 private void ResetKcp()
 {
     m_Kcp = new KCP(0, HandleKcpSend);
     m_Kcp.NoDelay(1, 10, 2, 1);
     m_Kcp.WndSize(128, 128);
 }
Exemplo n.º 25
0
        // Test case
        static void KCPTest(int mode)
        {
            // Create an analog network: 10% packet loss rate,Rtt 60ms~125ms
            vnet = new LatencySimulator(10, 60, 125);

            // Create a kcp object with two endpoints, the first parameter conv is the session number, the same session needs to be the same
            // The last one is the user parameter, which is used to pass the identifier.
            var kcp1 = new KCP(0x11223344, 1);
            var kcp2 = new KCP(0x11223344, 2);

            // Set the lower output of kcp, here udp_output, simulate udp network output function
            kcp1.SetOutput(udp_output);
            kcp2.SetOutput(udp_output);

            UInt32 current = Utils.iclock();
            UInt32 slap    = current + 20;
            UInt32 index   = 0;
            UInt32 next    = 0;
            Int64  sumrtt  = 0;
            int    count   = 0;
            int    maxrtt  = 0;

            // Configuration window size: average delay 200ms, send a packet every 20ms,
            // Considering the packet loss retransmission, set the maximum receiving and sending window to 128.
            kcp1.WndSize(128, 128);
            kcp2.WndSize(128, 128);

            if (mode == 0)             //Default mode
            {
                kcp1.NoDelay(0, 10, 0, 0);
                kcp2.NoDelay(0, 10, 0, 0);
            }
            else if (mode == 1)             // Normal mode, turn off flow control, etc.
            {
                kcp1.NoDelay(0, 10, 0, 1);
                kcp2.NoDelay(0, 10, 0, 1);
            }
            else             //Start fast mode
            {
                // 1st parameter nodelay-enabled after some regular acceleration will start
                // The second parameter interval is the internal processing clock. The default setting is 10ms.
                // The third parameter resend is the fast retransmission indicator, set to 2
                // The fourth parameter is whether to disable regular flow control, this is forbidden here.
                kcp1.NoDelay(1, 10, 2, 1);
                kcp2.NoDelay(1, 10, 2, 1);
                kcp1.SetMinRTO(10);
                kcp1.SetFastResend(1);
            }

            var    buffer = new byte[2000];
            int    hr     = 0;
            UInt32 ts1    = Utils.iclock();

            while (true)
            {
                Thread.Sleep(1);
                current = Utils.iclock();
                kcp1.Update(current);
                kcp2.Update(current);

                // Kcp1 sends data every 20ms
                for (; current >= slap; slap += 20)
                {
                    KCP.ikcp_encode32u(buffer, 0, index++);
                    KCP.ikcp_encode32u(buffer, 4, current);

                    // Send the upper layer protocol packet
                    kcp1.Send(buffer, 0, 8);
                }

                //Handling virtual networks: detecting if there are udp packets from p1->p2
                while (true)
                {
                    hr = vnet.Recv(1, buffer, 2000);
                    if (hr < 0)
                    {
                        break;
                    }

                    //If p2 receives udp, it is input to kcp2 as the underlying protocol.
                    hr = kcp2.Input(buffer, 0, hr);
                    Debug.Assert(hr >= 0);
                }

                // Handling virtual networks: detecting if there are udp packets from p2->p1
                while (true)
                {
                    hr = vnet.Recv(0, buffer, 2000);
                    if (hr < 0)
                    {
                        break;
                    }

                    // If p1 receives udp, it is input to kcp1 as the underlying protocol.
                    hr = kcp1.Input(buffer, 0, hr);
                    Debug.Assert(hr >= 0);
                }

                // Kcp2 receives any package and returns it.
                while (true)
                {
                    hr = kcp2.Recv(buffer, 0, 10);
                    if (hr < 0)
                    {
                        break;
                    }

                    //If you receive the package, it will return
                    hr = kcp2.Send(buffer, 0, hr);
                    Debug.Assert(hr >= 0);
                }

                // Kcp1 receives the echo data of kcp2
                while (true)
                {
                    hr = kcp1.Recv(buffer, 0, 10);
                    if (hr < 0)                     // Exit without receiving the package
                    {
                        break;
                    }

                    int    offset = 0;
                    UInt32 sn     = KCP.ikcp_decode32u(buffer, ref offset);
                    UInt32 ts     = KCP.ikcp_decode32u(buffer, ref offset);
                    UInt32 rtt    = current - ts;

                    if (sn != next)
                    {
                        // If the received packet is not continuous
                        Console.WriteLine(String.Format("ERROR sn {0}<->{1}", count, next));
                        return;
                    }
                    next++;
                    sumrtt += rtt;
                    count++;
                    if (rtt > maxrtt)
                    {
                        maxrtt = (int)rtt;
                    }

                    Console.WriteLine(String.Format("[RECV] mode={0} sn={1} rtt={2}", mode, sn, rtt));
                }
                if (next > 1000)
                {
                    break;
                }
            }
            ts1 = Utils.iclock() - ts1;
            var names = new string[3] {
                "default", "normal", "fast"
            };

            Console.WriteLine("{0} mode result ({1}ms):", names[mode], ts1);
            Console.WriteLine("avgrtt={0} maxrtt={1} tx={2}", sumrtt / count, maxrtt, vnet.tx1);
            Console.WriteLine("Press any key to next...");
            Console.Read();
        }
Exemplo n.º 26
0
        // 测试用例
        static void KCPTest(int mode)
        {
            // 创建模拟网络:丢包率10%,Rtt 60ms~125ms
            vnet = new LatencySimulator(10, 60, 125);

            // 创建两个端点的 kcp对象,第一个参数 conv是会话编号,同一个会话需要相同
            // 最后一个是 user参数,用来传递标识
            var kcp1 = new KCP(0x11223344, 1);
            var kcp2 = new KCP(0x11223344, 2);

            // 设置kcp的下层输出,这里为 udp_output,模拟udp网络输出函数
            kcp1.SetOutput(udp_output);
            kcp2.SetOutput(udp_output);

            UInt32 current = Utils.iclock();
            UInt32 slap    = current + 20;
            UInt32 index   = 0;
            UInt32 next    = 0;
            Int64  sumrtt  = 0;
            int    count   = 0;
            int    maxrtt  = 0;

            // 配置窗口大小:平均延迟200ms,每20ms发送一个包,
            // 而考虑到丢包重发,设置最大收发窗口为128
            kcp1.WndSize(128, 128);
            kcp2.WndSize(128, 128);

            if (mode == 0) // 默认模式
            {
                kcp1.NoDelay(0, 10, 0, 0);
                kcp2.NoDelay(0, 10, 0, 0);
            }
            else if (mode == 1) // 普通模式,关闭流控等
            {
                kcp1.NoDelay(0, 10, 0, 1);
                kcp2.NoDelay(0, 10, 0, 1);
            }
            else // 启动快速模式
            {
                // 第1个参数 nodelay-启用以后若干常规加速将启动
                // 第2个参数 interval为内部处理时钟,默认设置为 10ms
                // 第3个参数 resend为快速重传指标,设置为2
                // 第4个参数 为是否禁用常规流控,这里禁止
                kcp1.NoDelay(1, 10, 2, 1);
                kcp2.NoDelay(1, 10, 2, 1);
                kcp1.SetMinRTO(10);
                kcp1.SetFastResend(1);
            }

            var    buffer = new byte[2000];
            int    hr     = 0;
            UInt32 ts1    = Utils.iclock();

            while (true)
            {
                Thread.Sleep(1);
                current = Utils.iclock();
                kcp1.Update(current);
                kcp2.Update(current);

                // 每隔 20ms,kcp1发送数据
                for (; current >= slap; slap += 20)
                {
                    KCP.ikcp_encode32u(buffer, 0, index++);
                    KCP.ikcp_encode32u(buffer, 4, current);

                    // 发送上层协议包
                    kcp1.Send(buffer, 0, 8);
                }

                // 处理虚拟网络:检测是否有udp包从p1->p2
                while (true)
                {
                    hr = vnet.Recv(1, buffer, 2000);
                    if (hr < 0)
                    {
                        break;
                    }

                    // 如果 p2收到udp,则作为下层协议输入到kcp2
                    hr = kcp2.Input(buffer, 0, hr);
                    Debug.Assert(hr >= 0);
                }

                // 处理虚拟网络:检测是否有udp包从p2->p1
                while (true)
                {
                    hr = vnet.Recv(0, buffer, 2000);
                    if (hr < 0)
                    {
                        break;
                    }

                    // 如果 p1收到udp,则作为下层协议输入到kcp1
                    hr = kcp1.Input(buffer, 0, hr);
                    Debug.Assert(hr >= 0);
                }

                // kcp2接收到任何包都返回回去
                while (true)
                {
                    hr = kcp2.Recv(buffer, 0, 10);
                    if (hr < 0)
                    {
                        break;
                    }

                    // 如果收到包就回射
                    hr = kcp2.Send(buffer, 0, hr);
                    Debug.Assert(hr >= 0);
                }

                // kcp1收到kcp2的回射数据
                while (true)
                {
                    hr = kcp1.Recv(buffer, 0, 10);
                    if (hr < 0) // 没有收到包就退出
                    {
                        break;
                    }

                    int    offset = 0;
                    UInt32 sn     = KCP.ikcp_decode32u(buffer, ref offset);
                    UInt32 ts     = KCP.ikcp_decode32u(buffer, ref offset);
                    UInt32 rtt    = current - ts;

                    if (sn != next)
                    {
                        // 如果收到的包不连续
                        Console.WriteLine(String.Format("ERROR sn {0}<->{1}", count, next));
                        return;
                    }
                    next++;
                    sumrtt += rtt;
                    count++;
                    if (rtt > maxrtt)
                    {
                        maxrtt = (int)rtt;
                    }

                    Console.WriteLine(String.Format("[RECV] mode={0} sn={1} rtt={2}", mode, sn, rtt));
                }
                if (next > 1000)
                {
                    break;
                }
            }
            ts1 = Utils.iclock() - ts1;
            var names = new string[3] {
                "default", "normal", "fast"
            };

            Console.WriteLine("{0} mode result ({1}ms):", names[mode], ts1);
            Console.WriteLine("avgrtt={0} maxrtt={1} tx={2}", sumrtt / count, maxrtt, vnet.tx1);
            Console.WriteLine("Press any key to next...");
            Console.Read();
        }