Exemple #1
0
        public int Recv(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(recvBytes);
            }

            if (mSocket == null)
            {
                return(-1);
            }

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

            var rn = 0;

            try
            {
                rn = mSocket.Receive(mRecvBuffer.RawBuffer, mRecvBuffer.WriterIndex, mRecvBuffer.WritableBytes, SocketFlags.None);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                rn = -1;
            }

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

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

            if (inputN < 0)
            {
                mRecvBuffer.Clear();
                return(inputN);
            }
            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(Recv(data, index, length));
            }

            return(0);
        }
Exemple #2
0
        public int Recv(byte[] data, int index, int length)
        {
            // The remaining part from last time
            if (mRecvBuffer.ReadableBytes > 0)
            {
                var recvBytes = Math.Min(mRecvBuffer.ReadableBytes, length);
                Buffer.BlockCopy(mRecvBuffer.RawBuffer, mRecvBuffer.ReaderIndex, data, index, recvBytes);
                mRecvBuffer.ReaderIndex += recvBytes;
                // Reset the read and write pointer after reading
                if (mRecvBuffer.ReaderIndex == mRecvBuffer.WriterIndex)
                {
                    mRecvBuffer.Clear();
                }

                return(recvBytes);
            }

            if (this.listener == null)
            {
                return(-1);
            }

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

            var rn = 0;

            try
            {
                rn = this.listener.Receive(mRecvBuffer.RawBuffer, mRecvBuffer.WriterIndex, mRecvBuffer.WritableBytes,
                                           SocketFlags.None);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                rn = -1;
            }

            if (rn <= 0)
            {
                return(rn);
            }

            mRecvBuffer.WriterIndex += rn;

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

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

            mRecvBuffer.Clear();

            // Read all the complete messages
            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;
                }
            }

            // There is data to receive
            if (mRecvBuffer.ReadableBytes > 0)
            {
                return(Recv(data, index, length));
            }

            return(0);
        }