X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=client.cpp;h=e7a12f6c8ef395691f3dfa86efc12d2fda228efe;hp=bb2d26cdcd4abec4516d23ae92142636a33ac341;hb=b57530552825a13a3cd1924bda99e5e237c722a4;hpb=4856c49f1b63753ce86ad759ee649a1117628a8e diff --git a/client.cpp b/client.cpp index bb2d26c..e7a12f6 100644 --- a/client.cpp +++ b/client.cpp @@ -1,20 +1,21 @@ #include #include -#include +#include #include #include "client.h" #include "log.h" -#include "markpool.h" #include "state.pb.h" #include "stream.h" +#ifndef SO_MAX_PACING_RATE +#define SO_MAX_PACING_RATE 47 +#endif + using namespace std; Client::Client(int sock) : sock(sock), - fwmark(0), - connect_time(time(NULL)), state(Client::READING_REQUEST), stream(NULL), header_or_error_bytes_sent(0), @@ -25,6 +26,11 @@ Client::Client(int sock) { request.reserve(1024); + if (clock_gettime(CLOCK_MONOTONIC_COARSE, &connect_time) == -1) { + log_perror("clock_gettime(CLOCK_MONOTONIC_COARSE)"); + return; + } + // Find the remote address, and convert it to ASCII. sockaddr_in6 addr; socklen_t addr_len = sizeof(addr); @@ -32,8 +38,19 @@ Client::Client(int sock) if (getpeername(sock, reinterpret_cast(&addr), &addr_len) == -1) { log_perror("getpeername"); remote_addr = ""; + return; + } + + char buf[INET6_ADDRSTRLEN]; + if (IN6_IS_ADDR_V4MAPPED(&addr.sin6_addr)) { + // IPv4 address, really. + if (inet_ntop(AF_INET, &addr.sin6_addr.s6_addr32[3], buf, sizeof(buf)) == NULL) { + log_perror("inet_ntop"); + remote_addr = ""; + } else { + remote_addr = buf; + } } else { - char buf[INET6_ADDRSTRLEN]; if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == NULL) { log_perror("inet_ntop"); remote_addr = ""; @@ -46,10 +63,9 @@ Client::Client(int sock) Client::Client(const ClientProto &serialized, Stream *stream) : sock(serialized.sock()), remote_addr(serialized.remote_addr()), - connect_time(serialized.connect_time()), state(State(serialized.state())), request(serialized.request()), - stream_id(serialized.stream_id()), + url(serialized.url()), stream(stream), header_or_error(serialized.header_or_error()), header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()), @@ -58,15 +74,23 @@ Client::Client(const ClientProto &serialized, Stream *stream) bytes_lost(serialized.bytes_lost()), num_loss_events(serialized.num_loss_events()) { - if (stream != NULL && stream->mark_pool != NULL) { - fwmark = stream->mark_pool->get_mark(); - } else { - fwmark = 0; // No mark. + if (stream != NULL) { + if (setsockopt(sock, SOL_SOCKET, SO_MAX_PACING_RATE, &stream->pacing_rate, sizeof(stream->pacing_rate)) == -1) { + if (stream->pacing_rate != ~0U) { + log_perror("setsockopt(SO_MAX_PACING_RATE)"); + } + } } - if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) { - if (fwmark != 0) { - log_perror("setsockopt(SO_MARK)"); + if (serialized.has_connect_time_old()) { + // Do a rough conversion from time() to monotonic time. + if (clock_gettime(CLOCK_MONOTONIC_COARSE, &connect_time) == -1) { + log_perror("clock_gettime(CLOCK_MONOTONIC_COARSE)"); + return; } + connect_time.tv_sec += serialized.connect_time_old() - time(NULL); + } else { + connect_time.tv_sec = serialized.connect_time_sec(); + connect_time.tv_nsec = serialized.connect_time_nsec(); } } @@ -75,10 +99,11 @@ ClientProto Client::serialize() const ClientProto serialized; serialized.set_sock(sock); serialized.set_remote_addr(remote_addr); - serialized.set_connect_time(connect_time); + serialized.set_connect_time_sec(connect_time.tv_sec); + serialized.set_connect_time_nsec(connect_time.tv_nsec); serialized.set_state(state); serialized.set_request(request); - serialized.set_stream_id(stream_id); + serialized.set_url(url); serialized.set_header_or_error(header_or_error); serialized.set_header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()); serialized.set_stream_pos(stream_pos); @@ -91,7 +116,12 @@ ClientProto Client::serialize() const ClientStats Client::get_stats() const { ClientStats stats; - stats.stream_id = stream_id; + if (url.empty()) { + stats.url = "-"; + } else { + stats.url = url; + } + stats.sock = sock; stats.remote_addr = remote_addr; stats.connect_time = connect_time; stats.bytes_sent = bytes_sent;