Exemple #1
0
        /// <summary>
        ///     Sends a protobuf message over the underlying stream
        /// </summary>
        /// <param name="message">The message to send</param>
        /// <returns></returns>
        /// <exception cref="Exception">Throws an exception if the byte conversion didn't work as expected</exception>
        private async Task <int> SendMessage(RequestWrapper message, Connection connection)
        {
            // Generate a reference id for the message, so we can catch the response
            // later on
            var refId = GetNextRefId();

            message.RefId = refId;

            // Convert the message into bytes we can send across the wire
            var bytes     = message.ToByteArray();
            var size      = bytes.Length;
            var sizeBytes = IntToByteArray(size);

            if (sizeBytes.Length != 4)
            {
                throw new Exception("Expected an int to be converted into 4 bytes. That was not the case.");
            }

            // Gather the message into one big message, so the message can be send in one go, without something disturbing us
            var all = new byte[size + 4];

            Buffer.BlockCopy(sizeBytes, 0, all, 0, sizeBytes.Length);
            Buffer.BlockCopy(bytes, 0, all, sizeBytes.Length, bytes.Length);

            // Actually send the message
            await connection.WriteAsync(all);

            return(refId);
        }