Lines Matching refs:conn
13 typedef void (app_connect_cb)(APP_CONN *conn, int status, void *arg);
14 typedef void (app_write_cb)(APP_CONN *conn, int status, void *arg);
15 typedef void (app_read_cb)(APP_CONN *conn, void *buf, size_t buf_len, void *arg);
18 static void set_timer(APP_CONN *conn);
23 static int handshake_ssl(APP_CONN *conn);
24 static void flush_write_buf(APP_CONN *conn);
25 static void set_rx(APP_CONN *conn);
26 static int try_write(APP_CONN *conn, UPPER_WRITE_OP *op);
27 static void handle_pending_writes(APP_CONN *conn);
28 static int write_deferred(APP_CONN *conn, const void *buf, size_t buf_len, app_write_cb *cb, void *…
30 static int setup_ssl(APP_CONN *conn, const char *hostname);
48 APP_CONN *conn; member
64 APP_CONN *conn; member
136 APP_CONN *conn = NULL; in new_conn() local
138 conn = calloc(1, sizeof(APP_CONN)); in new_conn()
139 if (!conn) in new_conn()
143 uv_udp_init(uv_default_loop(), &conn->udp); in new_conn()
144 conn->udp.data = conn; in new_conn()
146 uv_timer_init(uv_default_loop(), &conn->timer); in new_conn()
147 conn->timer.data = conn; in new_conn()
149 uv_tcp_init(uv_default_loop(), &conn->tcp); in new_conn()
150 conn->tcp.data = conn; in new_conn()
152 conn->stream = (uv_stream_t *)&conn->tcp; in new_conn()
155 conn->app_connect_cb = cb; in new_conn()
156 conn->app_connect_arg = arg; in new_conn()
158 rc = uv_udp_connect(&conn->udp, sa); in new_conn()
160 conn->tcp_connect.data = conn; in new_conn()
161 rc = uv_tcp_connect(&conn->tcp_connect, &conn->tcp, sa, tcp_connect_done); in new_conn()
165 uv_close((uv_handle_t *)&conn->udp, net_connect_fail_close_done); in new_conn()
167 uv_close((uv_handle_t *)&conn->tcp, net_connect_fail_close_done); in new_conn()
172 conn->ctx = ctx; in new_conn()
173 conn->hostname = hostname; in new_conn()
176 rc = setup_ssl(conn, hostname); in new_conn()
178 uv_close((uv_handle_t *)&conn->udp, net_connect_fail_close_done); in new_conn()
183 return conn; in new_conn()
190 int app_read_start(APP_CONN *conn, app_read_cb *cb, void *arg) in app_read_start() argument
192 conn->app_read_cb = cb; in app_read_start()
193 conn->app_read_arg = arg; in app_read_start()
194 set_rx(conn); in app_read_start()
202 int app_write(APP_CONN *conn, const void *buf, size_t buf_len, app_write_cb *cb, void *arg) in app_write() argument
204 write_deferred(conn, buf, buf_len, cb, arg); in app_write()
205 handle_pending_writes(conn); in app_write()
213 void teardown(APP_CONN *conn) in teardown() argument
217 if (conn == NULL) in teardown()
220 BIO_free_all(conn->net_bio); in teardown()
221 SSL_free(conn->ssl); in teardown()
224 uv_cancel((uv_req_t *)&conn->tcp_connect); in teardown()
227 conn->teardown_done = &teardown_done; in teardown()
229 uv_close((uv_handle_t *)&conn->udp, teardown_continued); in teardown()
230 uv_close((uv_handle_t *)&conn->timer, teardown_continued); in teardown()
232 uv_close((uv_handle_t *)conn->stream, teardown_continued); in teardown()
257 static void enqueue_upper_write_op(APP_CONN *conn, UPPER_WRITE_OP *op) in enqueue_upper_write_op() argument
259 op->prev = conn->pending_upper_write_tail; in enqueue_upper_write_op()
263 conn->pending_upper_write_tail = op; in enqueue_upper_write_op()
264 if (conn->pending_upper_write_head == NULL) in enqueue_upper_write_op()
265 conn->pending_upper_write_head = op; in enqueue_upper_write_op()
268 static void dequeue_upper_write_op(APP_CONN *conn) in dequeue_upper_write_op() argument
270 if (conn->pending_upper_write_head == NULL) in dequeue_upper_write_op()
273 if (conn->pending_upper_write_head->next == NULL) { in dequeue_upper_write_op()
274 conn->pending_upper_write_head = NULL; in dequeue_upper_write_op()
275 conn->pending_upper_write_tail = NULL; in dequeue_upper_write_op()
277 conn->pending_upper_write_head = conn->pending_upper_write_head->next; in dequeue_upper_write_op()
278 conn->pending_upper_write_head->prev = NULL; in dequeue_upper_write_op()
294 static void on_rx_push(APP_CONN *conn) in on_rx_push() argument
300 if (!conn->app_read_cb) in on_rx_push()
307 srd = SSL_read(conn->ssl, buf, buf_len); in on_rx_push()
308 flush_write_buf(conn); in on_rx_push()
310 rc = SSL_get_error(conn->ssl, srd); in on_rx_push()
317 conn->app_read_cb(conn, buf, srd, conn->app_read_arg); in on_rx_push()
321 static void net_error(APP_CONN *conn) in net_error() argument
323 conn->closed = 1; in net_error()
324 set_rx(conn); in net_error()
326 if (conn->app_read_cb) in net_error()
327 conn->app_read_cb(conn, NULL, 0, conn->app_read_arg); in net_error()
330 static void handle_pending_writes(APP_CONN *conn) in handle_pending_writes() argument
334 if (conn->pending_upper_write_head == NULL) in handle_pending_writes()
338 UPPER_WRITE_OP *op = conn->pending_upper_write_head; in handle_pending_writes()
339 rc = try_write(conn, op); in handle_pending_writes()
343 dequeue_upper_write_op(conn); in handle_pending_writes()
345 } while (conn->pending_upper_write_head != NULL); in handle_pending_writes()
347 set_rx(conn); in handle_pending_writes()
358 APP_CONN *conn = (APP_CONN *)stream->data; in net_read_done() local
362 net_error(conn); in net_read_done()
367 int wr = BIO_write(conn->net_bio, buf->base, nr); in net_read_done()
373 if (!conn->done_handshake) { in net_read_done()
374 rc = handshake_ssl(conn); in net_read_done()
380 if (!conn->done_handshake) in net_read_done()
384 handle_pending_writes(conn); in net_read_done()
385 on_rx_push(conn); in net_read_done()
388 static void set_rx(APP_CONN *conn) in set_rx() argument
391 if (!conn->closed) in set_rx()
392 uv_udp_recv_start(&conn->udp, net_read_alloc, net_read_done); in set_rx()
394 uv_udp_recv_stop(&conn->udp); in set_rx()
396 …if (!conn->closed && (conn->app_read_cb || (!conn->done_handshake && conn->init_handshake) || conn… in set_rx()
397 uv_read_start(conn->stream, net_read_alloc, net_read_done); in set_rx()
399 uv_read_stop(conn->stream); in set_rx()
410 APP_CONN *conn = op->conn; in net_write_done() local
420 flush_write_buf(conn); in net_write_done()
423 static void flush_write_buf(APP_CONN *conn) in flush_write_buf() argument
433 rd = BIO_read(conn->net_bio, buf, 4096); in flush_write_buf()
444 op->conn = conn; in flush_write_buf()
450 rc = uv_udp_send(&op->w, &conn->udp, &op->b, 1, NULL, net_write_done); in flush_write_buf()
452 rc = uv_write(&op->w, conn->stream, &op->b, 1, net_write_done); in flush_write_buf()
462 static void handshake_done_ssl(APP_CONN *conn) in handshake_done_ssl() argument
465 set_timer(conn); in handshake_done_ssl()
467 conn->app_connect_cb(conn, 0, conn->app_connect_arg); in handshake_done_ssl()
470 static int handshake_ssl(APP_CONN *conn) in handshake_ssl() argument
474 conn->init_handshake = 1; in handshake_ssl()
476 rc = SSL_do_handshake(conn->ssl); in handshake_ssl()
478 conn->done_handshake = 1; in handshake_ssl()
479 handshake_done_ssl(conn); in handshake_ssl()
480 set_rx(conn); in handshake_ssl()
484 flush_write_buf(conn); in handshake_ssl()
485 rcx = SSL_get_error(conn->ssl, rc); in handshake_ssl()
487 set_rx(conn); in handshake_ssl()
495 static int setup_ssl(APP_CONN *conn, const char *hostname) in setup_ssl() argument
503 ssl = SSL_new(conn->ctx); in setup_ssl()
542 conn->net_bio = net_bio; in setup_ssl()
543 conn->ssl = ssl; in setup_ssl()
544 return handshake_ssl(conn); in setup_ssl()
551 APP_CONN *conn = (APP_CONN *)tcp_connect->data; in tcp_connect_done() local
558 rc = setup_ssl(conn, conn->hostname); in tcp_connect_done()
569 APP_CONN *conn = (APP_CONN *)handle->data; in net_connect_fail_close_done() local
571 free(conn); in net_connect_fail_close_done()
578 APP_CONN *conn = (APP_CONN *)timer->data; in timer_done() local
580 SSL_handle_events(conn->ssl); in timer_done()
581 handle_pending_writes(conn); in timer_done()
582 flush_write_buf(conn); in timer_done()
583 set_rx(conn); in timer_done()
584 set_timer(conn); /* repeat timer */ in timer_done()
587 static void set_timer(APP_CONN *conn) in set_timer() argument
592 if (!SSL_get_event_timeout(conn->ssl, &tv, &is_infinite)) in set_timer()
597 uv_timer_start(&conn->timer, timer_done, ms, 0); in set_timer()
602 static int try_write(APP_CONN *conn, UPPER_WRITE_OP *op) in try_write() argument
608 rc = SSL_write(conn->ssl, op->buf + written, op->buf_len - written); in try_write()
610 rcx = SSL_get_error(conn->ssl, rc); in try_write()
616 op->cb(conn, -rcx, op->cb_arg); in try_write()
625 op->cb(conn, 0, op->cb_arg); in try_write()
627 flush_write_buf(conn); in try_write()
631 static int write_deferred(APP_CONN *conn, const void *buf, size_t buf_len, app_write_cb *cb, void *… in write_deferred() argument
639 op->conn = conn; in write_deferred()
643 enqueue_upper_write_op(conn, op); in write_deferred()
644 set_rx(conn); in write_deferred()
645 flush_write_buf(conn); in write_deferred()
651 APP_CONN *conn = (APP_CONN *)handle->data; in teardown_continued() local
653 char *teardown_done = conn->teardown_done; in teardown_continued()
660 for (op=conn->pending_upper_write_head; op; op=next_op) { in teardown_continued()
665 free(conn); in teardown_continued()
676 static void post_read(APP_CONN *conn, void *buf, size_t buf_len, void *arg) in post_read() argument
688 static void post_write_get(APP_CONN *conn, int status, void *arg) in post_write_get() argument
695 app_read_start(conn, post_read, NULL); in post_write_get()
701 static void post_connect(APP_CONN *conn, int status, void *arg) in post_connect() argument
711 wr = app_write(conn, tx_msg, mlen, post_write_get, NULL); in post_connect()
722 APP_CONN *conn = NULL; in main() local
746 conn = new_conn(ctx, argv[1], result->ai_addr, result->ai_addrlen, post_connect, NULL); in main()
747 if (!conn) in main()
754 teardown(conn); in main()