Пример #1
0
        private void ReceiveAsync(IAsyncResult ar)
        {
            Socket nSocket = (Socket)ar.AsyncState;
            int    nLen    = nSocket.EndReceive(ar);

            if (nLen <= 0)
            {
                Debug.LogWarning("Receive length <=0;");
            }
            else
            {
                if (nLen >= MIN_PACKET_LEN)
                {
                    mMessageCounter.RecvPacketCount(nLen);
                    UInt32 nOperSession = 0;
                    KCP.ikcp_decode32u(udpRecvBuffer, 0, ref nOperSession);
                    byte[] nMsg = new byte[nLen];
                    Array.Copy(udpRecvBuffer, nMsg, nLen);
                    if (nOperSession < OPER_RANGE)
                    {
                        lock (mUdpMessageQueue)
                        {
                            mUdpMessageQueue.Enqueue(nMsg);
                        }
                    }
                    else
                    {
                        lock (mKcpLock)
                        {
                            if (mKcp != null)
                            {
                                mKcp.Input(nMsg);
                                //mNeedUpdateFlag = true;
                                mKcp.flush();
                                int nRecvLen = mKcp.Recv(kcpRecvBuffer);
                                if (nRecvLen > 0)
                                {
                                    mUnpackTool.UnpackMessage(kcpRecvBuffer, nRecvLen);
                                }
                                else if (nRecvLen == -3)
                                {
                                    Debug.LogError("recv buffer length not enough");
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        public void StartKcp(int vFd, int vToken, System.Object vKcpRecvCall)
        {
            mFd         = (UInt32)vFd;
            mToken      = (UInt32)vToken;
            kcpRecvCall = vKcpRecvCall;

            lock (mKcpLock)
            {
                mUnpackTool.Clear();
                // create kcp
                mKcp = new KCP(mToken, (byte[] buf, int size) =>
                {
                    LittleEndian.encode32u(sendBuffer, 0, mFd);
                    Array.Copy(buf, 0, sendBuffer, 4, size);
                    UdpSend(sendBuffer, size + 4);
                });

                // default setting
                mKcp.NoDelay(1, KCP_UPDATE_INTERVAL, 2, 1);
                //mKcp.SetMinrto(80);
            }
        }