Revenj.Http.HttpSocketContext.ReadUntil C# (CSharp) Method

ReadUntil() private method

private ReadUntil ( Socket socket, byte match, int position ) : int
socket Socket
match byte
position int
return int
        private int ReadUntil(Socket socket, byte match, int position)
        {
            int retries = 0;
            do
            {
                for (int i = position; i < totalBytes; i++)
                {
                    if (InputTemp[i] == match)
                    {
                        positionInTmp = i;
                        return i;
                    }
                }
                position = totalBytes;
                SocketError errorCode;
                var size = socket.Receive(InputTemp, totalBytes, InputTemp.Length - totalBytes, SocketFlags.None, out errorCode);
                if (errorCode == SocketError.Success && size > 0)
                    totalBytes += size;
                else
                {
                    if (retries == 0
                        || errorCode == SocketError.ConnectionReset
                        || errorCode == SocketError.ConnectionAborted) return -1;
                    retries++;
                }
            } while (retries < 20 && totalBytes < InputTemp.Length);
            return -1;
        }