'Return a GrrQueue message list from the queue, draining it.
This is used to get the messages going _TO_ the server when the
client connects.
Args:
max_size: The size of the returned protobuf will be at most one
message length over this size.
Returns:
A MessageList protobuf'

def Drain(self, max_size=1024):

    queue = rdf_flows.MessageList()
    length = 0
    self._out_queue.sort(key=(lambda msg: msg[0]))
    self._out_queue.reverse()
    while (self._out_queue and (length < max_size)):
        message = self._out_queue.pop()[1]
        queue.job.Append(message)
        stats.STATS.IncrementCounter('grr_client_sent_messages')
        message_length = len(message.Get('args'))
        length += message_length
        self._out_queue_size -= message_length
    self._out_queue.reverse()
    return queue
