'Sends outgoing messages to a waiting client.
Can block long-polling style for a while to wait
until it has some to send.'

def tx(self, wfile, seq, batch_size):

    ack = True
    if (seq is None):
        seq = (self._sent_tx_seq + 1)
    elif (seq > (self._sent_tx_seq + 1)):
        log.debug(('Client    is    living    in    the    future    (they    sent    seq    %s    but    ' + 'we    are    only    at    %s)'), seq, (self._sent_tx_seq + 1))
        ack = False
    if ack:
        while len(self._tx_buffer):
            if (self._tx_buffer[0][0] >= seq):
                break
            del self._tx_buffer[0]
    with self._cond:
        data = self._get_tx_batch(seq=seq, batch_size=batch_size)
        if (len(data) == 0):
            start_time = time.time()
            while True:
                self._cond.wait(2)
                data = self._get_tx_batch(seq=seq, batch_size=batch_size)
                if (len(data) > 0):
                    self._cond.wait(0.05)
                    data = self._get_tx_batch(seq=seq, batch_size=batch_size)
                    break
                if self._quitting:
                    break
                (r, w, x) = select.select([wfile], [], [wfile], 0)
                if (len(r) or len(x)):
                    return ABORT
                if ((time.time() - start_time) > CONNECTION_TIMEOUT):
                    return _result({'seq': seq, 'messages': []})
        if self._quitting:
            return _result({'messages': [], 'failure': 'quit', 'seq': self._sent_tx_seq})
        if (len(data) > 0):
            if (seq < data[0][0]):
                return _result({'messages': [], 'failure': 'expired', 'seq': self._sent_tx_seq})
            if (data[0][0] != seq):
                log.info('First    sequence    sent    is    not    the    one    asked    for')
                seq = data[0][0]
            last = data[(-1)][0]
            if (last > self._sent_tx_seq):
                self._sent_tx_seq = last
        data = [d[1] for d in data]
        return _result({'seq': seq, 'messages': data})
