void h2o_socket_write(h2o_socket_t *sock, h2o_iovec_t *bufs, size_t bufcnt, h2o_socket_cb cb) { size_t i, prev_bytes_written = sock->bytes_written; for (i = 0; i != bufcnt; ++i) { sock->bytes_written = bufs[i].len; #if H2O_SOCKET_DUMP_WRITE fprintf(stderr, "writing %zu bytes to fd:%d\n", bufs[i].len, h2o_socket_get_fd(sock)); h2o_dump_memory(stderr, bufs[i].base, bufs[i].len); #endif } if (sock->ssl == NULL) { do_write(sock, bufs, bufcnt, cb); } else { assert(sock->ssl->output.bufs.size == 0); /* fill in the data */ size_t ssl_record_size; switch (sock->_latency_optimization.state) { case H2O_SOCKET_LATENCY_OPTIMIZATION_STATE_TBD: case H2O_SOCKET_LATENCY_OPTIMIZATION_STATE_DISABLED: ssl_record_size = prev_bytes_written < 200 * 1024 ? calc_suggested_tls_payload_size(sock, 1400) : 16384; break; case H2O_SOCKET_LATENCY_OPTIMIZATION_STATE_DETERMINED: sock->_latency_optimization.state = H2O_SOCKET_LATENCY_OPTIMIZATION_STATE_NEEDS_UPDATE; /* fallthru */ default: ssl_record_size = sock->_latency_optimization.suggested_tls_payload_size; break; } for (; bufcnt != 0; ++bufs, --bufcnt) { size_t off = 0; while (off != bufs[0].len) { int ret; size_t sz = bufs[0].len - off; if (sz > ssl_record_size) sz = ssl_record_size; ret = SSL_write(sock->ssl->ssl, bufs[0].base + off, (int)sz); if (ret != sz) { /* The error happens if SSL_write is called after SSL_read returns a fatal error (e.g. due to corrupt TCP packet * being received). We need to take care of this since some protocol implementations send data after the read- * side of the connection gets closed (note that protocol implementations are (yet) incapable of distinguishing * a normal shutdown and close due to an error using the `status` value of the read callback). */ clear_output_buffer(sock->ssl); flush_pending_ssl(sock, cb); #ifndef H2O_USE_LIBUV ((struct st_h2o_evloop_socket_t *)sock)->_flags |= H2O_SOCKET_FLAG_IS_WRITE_ERROR; #endif return; } off += sz; } } flush_pending_ssl(sock, cb); } }
void h2o_socket_write(h2o_socket_t *sock, h2o_iovec_t *bufs, size_t bufcnt, h2o_socket_cb cb) { #if H2O_SOCKET_DUMP_WRITE { size_t i; for (i = 0; i != bufcnt; ++i) { fprintf(stderr, "writing %zu bytes to fd:%d\n", bufs[i].len, #if H2O_USE_LIBUV ((struct st_h2o_uv_socket_t *)sock)->uv.stream->io_watcher.fd #else ((struct st_h2o_evloop_socket_t *)sock)->fd #endif ); h2o_dump_memory(stderr, bufs[i].base, bufs[i].len); } } #endif if (sock->ssl == NULL) { do_write(sock, bufs, bufcnt, cb); } else { assert(sock->ssl->output.bufs.size == 0); /* fill in the data */ for (; bufcnt != 0; ++bufs, --bufcnt) { size_t off = 0; while (off != bufs[0].len) { int ret; size_t sz = bufs[0].len - off; if (sz > 1400) sz = 1400; ret = SSL_write(sock->ssl->ssl, bufs[0].base + off, (int)sz); if (ret != sz) { /* The error happens if SSL_write is called after SSL_read returns a fatal error (e.g. due to corrupt TCP packet * being received). We need to take care of this since some protocol implementations send data after the read- * side of the connection gets closed (note that protocol implementations are (yet) incapable of distinguishing * a normal shutdown and close due to an error using the `status` value of the read callback). */ clear_output_buffer(sock->ssl); flush_pending_ssl(sock, cb); #ifndef H2O_USE_LIBUV ((struct st_h2o_evloop_socket_t *)sock)->_flags |= H2O_SOCKET_FLAG_IS_WRITE_ERROR; #endif return; } off += sz; } } flush_pending_ssl(sock, cb); } }
void h2o_socket_write(h2o_socket_t *sock, h2o_iovec_t *bufs, size_t bufcnt, h2o_socket_cb cb) { #if H2O_SOCKET_DUMP_WRITE { size_t i; for (i = 0; i != bufcnt; ++i) { fprintf(stderr, "writing %zu bytes to fd:%d\n", bufs[i].len, #if H2O_USE_LIBUV ((struct st_h2o_uv_socket_t *)sock)->uv.stream->io_watcher.fd #else ((struct st_h2o_evloop_socket_t *)sock)->fd #endif ); h2o_dump_memory(stderr, bufs[i].base, bufs[i].len); } } #endif if (sock->ssl == NULL) { do_write(sock, bufs, bufcnt, cb); } else { assert(sock->ssl->output.bufs.size == 0); /* fill in the data */ for (; bufcnt != 0; ++bufs, --bufcnt) { size_t off = 0; while (off != bufs[0].len) { int ret; size_t sz = bufs[0].len - off; if (sz > 1400) sz = 1400; ret = SSL_write(sock->ssl->ssl, bufs[0].base + off, (int)sz); assert(ret == sz); off += sz; } } flush_pending_ssl(sock, cb); } }