using namespace std;
Client::Client(int sock)
- : sock(sock),
- state(Client::READING_REQUEST),
- stream(nullptr),
- header_or_short_response_bytes_sent(0),
- stream_pos(0),
- bytes_sent(0),
- bytes_lost(0),
- num_loss_events(0),
- tls_context(nullptr),
- tls_data_to_send(nullptr),
- tls_data_left_to_send(0),
- in_ktls_mode(false)
+ : sock(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);
}
}
-Client::Client(const ClientProto &serialized, Stream *stream)
+Client::Client(const ClientProto &serialized, const vector<shared_ptr<const string>> &short_responses, Stream *stream)
: sock(serialized.sock()),
remote_addr(serialized.remote_addr()),
referer(serialized.referer()),
request(serialized.request()),
url(serialized.url()),
stream(stream),
- header_or_short_response(serialized.header_or_short_response()),
header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent()),
stream_pos(serialized.stream_pos()),
+ stream_pos_end(serialized.stream_pos_end()),
bytes_sent(serialized.bytes_sent()),
bytes_lost(serialized.bytes_lost()),
num_loss_events(serialized.num_loss_events())
}
}
}
+
+ if (serialized.has_header_or_short_response_old()) {
+ // Pre-1.4.0.
+ header_or_short_response_holder = serialized.header_or_short_response_old();
+ header_or_short_response = &header_or_short_response_holder;
+ } else if (serialized.has_header_or_short_response_index()) {
+ assert(size_t(serialized.header_or_short_response_index()) < short_responses.size());
+ header_or_short_response_ref = short_responses[serialized.header_or_short_response_index()];
+ header_or_short_response = header_or_short_response_ref.get();
+ }
connect_time.tv_sec = serialized.connect_time_sec();
connect_time.tv_nsec = serialized.connect_time_nsec();
}
}
-ClientProto Client::serialize() const
+ClientProto Client::serialize(unordered_map<const string *, size_t> *short_response_pool) const
{
ClientProto serialized;
serialized.set_sock(sock);
serialized.set_state(state);
serialized.set_request(request);
serialized.set_url(url);
- serialized.set_header_or_short_response(header_or_short_response);
+
+ if (header_or_short_response != nullptr) {
+ // See if this string is already in the pool (deduplicated by the pointer); if not, insert it.
+ auto iterator_and_inserted = short_response_pool->emplace(
+ header_or_short_response, short_response_pool->size());
+ serialized.set_header_or_short_response_index(iterator_and_inserted.first->second);
+ }
+
serialized.set_header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent());
serialized.set_stream_pos(stream_pos);
+ serialized.set_stream_pos_end(stream_pos_end);
serialized.set_bytes_sent(bytes_sent);
serialized.set_bytes_lost(bytes_lost);
serialized.set_num_loss_events(num_loss_events);