Exemplo n.º 1
0
        /// <summary>Read response from the network stream.</summary>
        /// <remarks>If the response contains payload portion, i.e. files, they are <b>not</b> read from the stream.
        /// It's caller's responsibility to iterate through <see cref="payload" /> values and read all the data before reusing the connection for other requests.</remarks>
        public static async Task <Response> receive(Stream connection)
        {
            // Receive into a buffer
            using (var buffer = await ResponseBuffer.receive(connection))
            {
                // Parse binary JSON from that buffer
                Hash          dict        = null;
                List <object> otherValues = null;
                var           parser      = new ResponseParser(buffer);

                foreach (object obj in parser.parse())
                {
                    switch (obj)
                    {
                    case Hash h:
                        if (null == dict)
                        {
                            dict = h;
                            break;
                        }
                        goto default;

                    default:
                        if (null == otherValues)
                        {
                            otherValues = new List <object>();
                        }
                        otherValues.Add(obj);
                        break;
                    }
                }
                // Return a new response object
                return(new Response(dict, parser.payloads, otherValues));
            }
        }
Exemplo n.º 2
0
        /// <summary>Read response from the network stream.</summary>
        /// <remarks>If the response contains payload portion, i.e. files, they are <b>not</b> read from the stream.
        /// It's caller's responsibility to iterate through <see cref="payload" /> values and read all the data before reusing the connection for other requests.</remarks>
        public static async Task <Response> receive(Stream connection)
        {
            // Receive into array of bytes
            int length = BitConverter.ToInt32(await connection.read(4), 0);

            byte[] buffer = await connection.read(length);

            // Parse
            Hash          firstHash   = null;
            List <object> otherValues = null;
            List <Data>   payloads    = null;
            var           parser      = new ResponseParser(buffer);

            foreach (object obj in parser.parse())
            {
                switch (obj)
                {
                case Hash h:
                    if (null == firstHash)
                    {
                        firstHash = h;
                        break;
                    }
                    goto default;

                case Data data:
                    if (null == payloads)
                    {
                        payloads = new List <Data>();
                    }
                    payloads.Add(data);
                    break;

                default:
                    if (null == otherValues)
                    {
                        otherValues = new List <object>();
                    }
                    otherValues.Add(obj);
                    break;
                }
            }

            return(new Response(firstHash, otherValues, payloads));
        }