Helios.Channels.Sockets.TcpSocketChannel.DoReadBytes C# (CSharp) Method

DoReadBytes() protected method

protected DoReadBytes ( IByteBuf buf ) : int
buf IByteBuf
return int
        protected override int DoReadBytes(IByteBuf buf)
        {
            if (!buf.HasArray)
            {
                throw new NotImplementedException("Only IByteBuffer implementations backed by array are supported.");
            }

            SocketError errorCode;
            int received = 0;
            try
            {
                received = Socket.Receive(buf.Array, buf.ArrayOffset + buf.WriterIndex, buf.WritableBytes,
                    SocketFlags.None, out errorCode);
            }
            catch (ObjectDisposedException)
            {
                errorCode = SocketError.Shutdown;
            }

            switch (errorCode)
            {
                case SocketError.Success:
                    if (received == 0)
                    {
                        return -1; // indicate that socket was closed
                    }
                    break;
                case SocketError.WouldBlock:
                    if (received == 0)
                    {
                        return 0;
                    }
                    break;
                case SocketError.Shutdown:
                    return -1; // socket was closed
                default:
                    throw new SocketException((int) errorCode);
            }

            buf.SetWriterIndex(buf.WriterIndex + received);

            return received;
        }