FileFind.Meshwork.Transport.TcpTransport.Receive C# (CSharp) Method

Receive() public method

public Receive ( byte buffer, int offset, int size ) : int
buffer byte
offset int
size int
return int
        public override int Receive(byte[] buffer, int offset, int size)
        {
            if (size <= 0) {
                throw new ArgumentException("Cannot receive <= 0 bytes");
            }

            int totalReceived = 0;
            while (totalReceived < size) {
                int count = 0;
                if (socket == null) {
                    // We were disconnected!
                    return 0;
                }
                lock (receiveLock) {
                    count = socket.Receive(buffer, offset + totalReceived, size - totalReceived, SocketFlags.None);
                }
                if (count == 0) {
                    // This means the connection was closed.
                    Disconnect();
                    return 0;
                } else {
                    totalReceived += count;

                    if (totalReceived > size) {
                        throw new Exception("Somehow received too much! This shouldn't ever happen!");
                    }
                }
            }
            return totalReceived;
        }