'Args:
destination(str): The name of the remote server.
last_stream_id(int|long): The last position of the device message stream
that the server sent up to.
current_stream_id(int|long): The current position of the device
message stream.
Returns:
Deferred ([dict], int|long): List of messages for the device and where
in the stream the messages got to.'

def get_new_device_msgs_for_remote(self, destination, last_stream_id, current_stream_id, limit=100):

    has_changed = self._device_federation_outbox_stream_cache.has_entity_changed(destination, last_stream_id)
    if ((not has_changed) or (last_stream_id == current_stream_id)):
        return defer.succeed(([], current_stream_id))
    def get_new_messages_for_remote_destination_txn(txn):
        sql = 'SELECT    stream_id,    messages_json    FROM    device_federation_outbox    WHERE    destination    =    ?    AND    ?    <    stream_id    AND    stream_id    <=    ?    ORDER    BY    stream_id    ASC    LIMIT    ?'
        txn.execute(sql, (destination, last_stream_id, current_stream_id, limit))
        messages = []
        for row in txn:
            stream_pos = row[0]
            messages.append(ujson.loads(row[1]))
        if (len(messages) < limit):
            stream_pos = current_stream_id
        return (messages, stream_pos)
    return self.runInteraction('get_new_device_msgs_for_remote', get_new_messages_for_remote_destination_txn)
