Beispiel #1
0
 /// <summary>
 /// 关闭链接
 /// </summary>
 public void Close()
 {
     if (mReceiveThread != null)
     {
         mUpdateWork = false;
         mReceiveThread.Join();
         mReceiveThread = null;
     }
     if (client != null)
     {
         if (client.IsConnected)
         {
             client.Close();
         }
         client = null;
         Array.Clear(byteBuffer, 0, byteBuffer.Length);   //清空数组
     }
     OnRemove();
 }
Beispiel #2
0
        public UDPStatus RecvKcp(byte[] data, int index, int length)
        {
            // 上次剩下的部分
            if (mRecvBuffer.ReadableBytes > 0)
            {
                var recvBytes = Math.Min(mRecvBuffer.ReadableBytes, length);
                Buffer.BlockCopy(mRecvBuffer.RawBuffer, mRecvBuffer.ReaderIndex, data, index, recvBytes);
                mRecvBuffer.ReaderIndex += recvBytes;
                // 读完重置读写指针
                if (mRecvBuffer.ReaderIndex == mRecvBuffer.WriterIndex)
                {
                    mRecvBuffer.Clear();
                }
                return(UDPStatus.eUDPStatus_TRANSMIT);
            }

            if (mSocket == null)
            {
                return(UDPStatus.eUDPStatus_UNKNOWN);
            }

            if (!mSocket.Poll(0, SelectMode.SelectRead))
            {
                return(0);
            }

            var rn = 0;

            try
            {
                rn = mSocket.Receive(mRecvBuffer.RawBuffer, mRecvBuffer.WriterIndex, mRecvBuffer.WritableBytes, SocketFlags.None);

                byte[] headBytes = new[]
                {
                    mRecvBuffer.RawBuffer[0],
                    mRecvBuffer.RawBuffer[1],
                    mRecvBuffer.RawBuffer[2],
                    mRecvBuffer.RawBuffer[3]
                };
                UDPProtocolHead head = UDPSession.ByteToStructure <UDPProtocolHead>(headBytes);
                switch ((UDPStatus)head.p_type)
                {
                case UDPStatus.eUDPStatus_KEEP_LIVE:
                    return(UDPStatus.eUDPStatus_KEEP_LIVE);

                case UDPStatus.eUDPStatus_TRANSMIT:
                {
                    Buffer.BlockCopy(mRecvBuffer.RawBuffer, 8, mRecvBuffer.RawBuffer, 0, rn - 8);
                }
                break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                rn = -1;
            }

            if (rn <= 0)
            {
                return(UDPStatus.eUDPStatus_UNKNOWN);
            }
            mRecvBuffer.WriterIndex += rn;

            var inputN = mKCP.Input(mRecvBuffer.RawBuffer, mRecvBuffer.ReaderIndex, mRecvBuffer.ReadableBytes, true, true);

            if (inputN < 0)
            {
                mRecvBuffer.Clear();
                return(UDPStatus.eUDPStatus_UNKNOWN);
            }
            mRecvBuffer.Clear();

            // 读完所有完整的消息
            for (; ;)
            {
                var size = mKCP.PeekSize();
                if (size <= 0)
                {
                    break;
                }

                mRecvBuffer.EnsureWritableBytes(size);

                var n = mKCP.Recv(mRecvBuffer.RawBuffer, mRecvBuffer.WriterIndex, size);
                if (n > 0)
                {
                    mRecvBuffer.WriterIndex += n;
                }
            }

            // 有数据待接收
            if (mRecvBuffer.ReadableBytes > 0)
            {
                return(RecvKcp(data, index, length));
            }

            return(0);
        }