bool TCPConnection::write(uint8_t *buf, int len) { if (stopped) { return false; } assert(buf); assert(len > 0); boost::shared_array<uint8_t> bufCpy(new uint8_t[len]); memcpy(bufCpy.get(), buf, len); pendingWrites.increment(); beetle.writers.schedule(getId(), [this, bufCpy, len] { uint8_t bufLen = len; if (SSL_write_all(ssl, &bufLen, 1) != 1 || SSL_write_all(ssl, bufCpy.get(), len) != len) { if (debug_socket) { std::stringstream ss; ss << "socket write failed : " << strerror(errno); pdebug(ss.str()); } stopInternal(); } else { if (debug_socket) { pdebug("wrote " + std::to_string(len) + " bytes to " + getName()); phex(bufCpy.get(), len); } } pendingWrites.decrement(); }); return true; }
static void benchmark(SSL *ssl, uint64_t send_bytes, uint64_t recv_bytes) { uint64_t bytes = 0; char buf[BENCH_CHUNK]; record_time_t t0, t1, t2; int sd; memset(buf, 'a', BENCH_CHUNK); record_time(&t0); if (SSL_connect(ssl) != 1) { ERR_print_errors_fp(stderr); goto out; } printf("cipher: %s\n", SSL_CIPHER_get_name(SSL_get_current_cipher(ssl))); record_time(&t1); if (send_bytes) { while (bytes < send_bytes) { int to_send = (send_bytes - bytes > BENCH_CHUNK) ? BENCH_CHUNK : send_bytes - bytes; if (SSL_write_all(ssl, buf, to_send)) break; bytes += to_send; } } else { while (bytes < recv_bytes) { int to_recv = (recv_bytes - bytes > BENCH_CHUNK) ? BENCH_CHUNK : recv_bytes - bytes; int recved; recved = SSL_read(ssl, buf, sizeof(to_recv)); if (recved > 0) bytes += recved; else break; } } record_time(&t2); print_time((send_bytes > 0) ? "sending" : "receiving", (send_bytes > 0) ? send_bytes : recv_bytes, &t1, &t2); SSL_shutdown(ssl); out: sd = SSL_get_fd(ssl); SSL_free(ssl); close(sd); }