Ticket #7912: 223690.2

File 223690.2, 2.0 KB (added by cypherpunks, 6 years ago)
Line 
1int
2connection_or_flush_from_destroy_queue(or_connection_t *conn, int max)
3{
4  int n_flushed;
5  cell_queue_t *queue = &conn.destroy_queue;
6  for (n_flushed = 0; n_flushed < max && queue->head; ) {
7    packed_cell_t *cell = cell_queue_pop(queue);
8
9    connection_write_to_buf(cell->body, CELL_NETWORK_SIZE, TO_CONN(conn));
10
11    packed_cell_free_unchecked(cell);
12    ++n_flushed;
13  }
14
15  if (n_flushed)
16    conn->timestamp_last_added_nonpadding = now;
17  return n_flushed;
18}
19
20void
21append_cell_to_destroy_queue(or_connection_t *orconn,
22                             cell_t *cell)
23{
24  packed_cell_t *copy = packed_cell_copy(cell);
25  cell_queue_append(&orconn.destroy_queue, copy);
26
27  if (! connection_get_outbuf_len(TO_CONN(orconn))) {
28    /* There is no data at all waiting to be sent on the outbuf.  Add a
29     * cell, so that we can notice when it gets flushed, flushed_some can
30     * get called, and we can start putting more data onto the buffer then.
31     */
32    log_debug(LD_GENERAL, "Primed a buffer.");
33    connection_or_flush_from_destroy_queue(orconn, 1);
34  }
35}
36
37int
38connection_or_send_destroy(circid_t circ_id, or_connection_t *conn, int reason)
39{
40  cell_t cell;
41
42  memset(&cell, 0, sizeof(cell_t));
43  cell.circ_id = circ_id;
44  cell.command = CELL_DESTROY;
45  cell.payload[0] = (uint8_t) reason;
46  log_debug(LD_OR,"Sending destroy (circID %d).", circ_id);
47  append_cell_to_destroy_queue(conn, &cell)
48  return 0;
49}
50
51int
52connection_or_flushed_some(or_connection_t *conn)
53{
54  size_t datalen = connection_get_outbuf_len(TO_CONN(conn));
55  int flushed;
56  /* If we're under the low water mark, add cells until we're just over the
57   * high water mark. */
58  if (datalen < OR_CONN_LOWWATER) {
59    ssize_t n = CEIL_DIV(OR_CONN_HIGHWATER - datalen, CELL_NETWORK_SIZE);
60    time_t now = approx_time();
61    flushed = connection_or_flush_from_destroy_queue(conn, 1);
62    n -= flushed;
63   /* (64)32:1 destroy cell fair enough? */
64    while (conn->active_circuits && n > 0) {
65      flushed = connection_or_flush_from_first_active_circuit(conn, 1, now);
66      n -= flushed;
67    }
68  }
69  return 0;
70}