CaSharpServer.Pipe.Read C# (CSharp) Метод

Read() публичный Метод

public Read ( int size ) : byte[]
size int
Результат byte[]
        public byte[] Read(int size)
        {
            if (size == 0 || isDisposing)
                return new byte[0];

            int spare = 0;
            byte[] result = new byte[size];
            long realLength = 0;

            //if the readposition and the write osition is at the same position do
            //wait for new data
            dataSemaphore.WaitOne();
            if (writePosition == readPosition)
            {
                dataSemaphore.Release();
                waitForNewData.WaitOne();
            }
            else
                dataSemaphore.Release();

            //be sure we have enough data
            do
            {
                if (isDisposing)
                    return new byte[0];

                dataSemaphore.WaitOne();
                if (writePosition < readPosition)
                    realLength = (data.Length - readPosition) + writePosition;
                else
                    realLength = (writePosition - readPosition);
                dataSemaphore.Release();

                //if there is not enough data to read, it will wait till new arrives or
                // one second passed. because it could be, that due to some really bad luck
                // the new data just happened between the lock open and the wait.
                if (size > realLength)
                    waitForNewData.WaitOne();

            }
            while (size > realLength);

            if (isDisposing)
                return new byte[0];

            dataSemaphore.WaitOne();
            //they are on the same loop
            if (writePosition > readPosition)
            {
                Buffer.BlockCopy(data, readPosition, result, 0, size);
                readPosition += size;
            }
            else
            {
                if ((data.Length - readPosition) >= size)
                {
                    Buffer.BlockCopy(data, readPosition, result, 0, size);
                    readPosition += size;
                }
                else
                {
                    spare = (data.Length - readPosition);
                    Buffer.BlockCopy(data, readPosition, result, 0, spare);
                    Buffer.BlockCopy(data, 0, result, spare, (size - spare));
                    readPosition = (size - spare);
                }
            }
            dataSemaphore.Release();

            waitForRead.Set();
            return result;
        }

Usage Example

        public void ProcessReceivedData(Pipe dataPipe, EndPoint remoteEndPoint, int maxPacketSize = 0, bool wait = true)
        {
            string remoteAddress = remoteEndPoint.ToString();
            uint   readedBytes = 0;
            UInt16 cmdId, dataType;
            UInt32 payloadSize, dataCount, param1, param2;

            byte[] payload = null;
            byte[] header  = null;

            while (maxPacketSize == 0 || maxPacketSize >= (readedBytes + 16))
            {
                if (!wait && dataPipe.AvailableBytes == 0)
                {
                    return;
                }
                header = dataPipe.Read(16);

                //Pipe destroyed
                if (header.Length == 0)
                {
                    return;
                }

                cmdId       = header.ToUInt16(0);
                payloadSize = header.ToUInt16(2);
                dataType    = header.ToUInt16(4);
                param1      = header.ToUInt32(8);
                param2      = header.ToUInt32(12);

                if (payloadSize == 0xFFFF)
                {
                    payloadSize  = dataPipe.Read(4).ToUInt32();
                    dataCount    = dataPipe.Read(4).ToUInt32();
                    readedBytes += (payloadSize + 24);
                }
                else
                {
                    dataCount    = header.ToUInt16(6);
                    readedBytes += (payloadSize + 16);
                }

                payload = dataPipe.Read(Convert.ToInt32(payloadSize));

                HandleMessage(cmdId, dataType, ref payloadSize, ref dataCount,
                              ref param1, ref param2, ref header, ref payload, ref remoteEndPoint);
            }
        }
All Usage Examples Of CaSharpServer.Pipe::Read