Otp.AbstractConnection.readSock C# (CSharp) Method

readSock() protected method

protected readSock ( System s, byte b, int sz, bool readingPayload ) : int
s System
b byte
sz int
readingPayload bool
return int
        protected internal virtual int readSock(System.Net.Sockets.TcpClient s, byte[] b, int sz, bool readingPayload)
        {
            int got = 0;
            int len = sz;
            int i;
            System.IO.Stream is_Renamed = null;
            
            lock (this)
            {
                if (s == null)
                {
                    throw new System.IO.IOException("expected " + len + " bytes, socket was closed");
                }
                is_Renamed = (System.IO.Stream)s.GetStream();
            }

            while (got < len && is_Renamed.CanRead)
            {
                i = is_Renamed.Read(b, got, len - got);

                if (i < 0)
                {
                    throw new System.IO.IOException("Expected " + len + " bytes, got EOF after " + got + " bytes");
                }
                else if (i == 0)
                {
                    throw new System.IO.IOException("Remote connection closed");
                }

                got += i;
            }

            receivedBytes += got;
            if (readingPayload)
            {
                receivedMsgs++;
                if (onReadWrite != null)
                    onReadWrite(this, Operation.Read, got + 4 /* header len */, receivedBytes, receivedMsgs);
            }
            return got;
        }