public override int Read()

in csharp/Adapter/Microsoft.Spark.CSharp/Network/SocketStream.cs [164:208]


        public override int Read(byte[] buffer, int offset, int count)
        {
            try
            {
                if (recvDataCache == null)
                {
                    recvDataCache = streamSocket.Receive();
                }

                if (!recvDataCache.IsReadable())
                {
                    recvDataCache.Release();
                    recvDataCache = null;
                    return 0;
                }

                var bytesRemaining = count;
                while (recvDataCache != null && recvDataCache.IsReadable() && bytesRemaining > 0)
                {
                    var bytesToRead = Math.Min(bytesRemaining, recvDataCache.ReadableBytes);
                    bytesRemaining -= recvDataCache.ReadBytes(buffer, offset + count - bytesRemaining, bytesToRead);
                    if (recvDataCache.IsReadable()) continue;

                    recvDataCache.Release();
                    recvDataCache = null;
                    if (streamSocket.HasData)
                    {
                        recvDataCache = streamSocket.Receive();
                    }
                }

                return count - bytesRemaining;
            }
            catch (Exception e)
            {
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                {
                    throw;
                }

                // some sort of error occurred on the socket call,
                // set the SocketException as InnerException and throw
                throw new IOException(string.Format("Unable to read data from the transport connection: {0}.", e.Message), e);
            }
        }