Server::Server()
{
- pthread_mutex_init(&mutex, NULL);
- pthread_mutex_init(&queued_clients_mutex, NULL);
+ pthread_mutex_init(&mutex, nullptr);
+ pthread_mutex_init(&queued_clients_mutex, nullptr);
epoll_fd = epoll_create(1024); // Size argument is ignored.
if (epoll_fd == -1) {
Server::~Server()
{
- for (Stream *stream : streams) {
- delete stream;
- }
-
safe_close(epoll_fd);
}
// Process each client where its stream has new data,
// even if there was no socket activity.
- for (Stream *stream : streams) {
+ for (unique_ptr<Stream> &stream : streams) {
vector<Client *> to_process;
swap(stream->to_process, to_process);
for (Client *client : to_process) {
for (const auto &fd_and_client : clients) {
serialized.add_clients()->MergeFrom(fd_and_client.second.serialize());
}
- for (Stream *stream : streams) {
+ for (unique_ptr<Stream> &stream : streams) {
serialized.add_streams()->MergeFrom(stream->serialize());
}
return serialized;
if (is_tls) {
assert(tls_server_contexts.count(acceptor));
client_ptr->tls_context = tls_accept(tls_server_contexts[acceptor]);
- if (client_ptr->tls_context == NULL) {
+ if (client_ptr->tls_context == nullptr) {
log(ERROR, "tls_accept() failed");
close_client(client_ptr);
return;
int stream_index = lookup_stream_by_url(client.url());
if (stream_index == -1) {
assert(client.state() != Client::SENDING_DATA);
- stream = NULL;
+ stream = nullptr;
} else {
- stream = streams[stream_index];
+ stream = streams[stream_index].get();
}
auto inserted = clients.insert(make_pair(client.sock(), Client(client, stream)));
assert(inserted.second == true); // Should not already exist.
{
MutexLock lock(&mutex);
stream_url_map.insert(make_pair(url, streams.size()));
- streams.push_back(new Stream(url, backlog_size, prebuffering_bytes, encoding, src_encoding));
+ streams.emplace_back(new Stream(url, backlog_size, prebuffering_bytes, encoding, src_encoding));
return streams.size() - 1;
}
{
MutexLock lock(&mutex);
stream_url_map.insert(make_pair(stream.url(), streams.size()));
- streams.push_back(new Stream(stream, data_fd));
+ streams.emplace_back(new Stream(stream, data_fd));
return streams.size() - 1;
}
{
switch (client->state) {
case Client::READING_REQUEST: {
- if (client->tls_context != NULL) {
+ if (client->tls_context != nullptr) {
if (send_pending_tls_data(client)) {
// send_pending_tls_data() hit postconditions #1 or #4.
return;
// Try to read more of the request.
char buf[1024];
int ret;
- if (client->tls_context == NULL) {
+ if (client->tls_context == nullptr) {
ret = read_nontls_data(client, buf, sizeof(buf));
if (ret == -1) {
// read_nontls_data() hit postconditions #1 or #2.
bool Server::send_pending_tls_data(Client *client)
{
// See if there's data from the TLS library to write.
- if (client->tls_data_to_send == NULL) {
+ if (client->tls_data_to_send == nullptr) {
client->tls_data_to_send = tls_get_write_buffer(client->tls_context, &client->tls_data_left_to_send);
- if (client->tls_data_to_send == NULL) {
+ if (client->tls_data_to_send == nullptr) {
// Really no data to send.
return false;
}
if (ret > 0 && size_t(ret) == client->tls_data_left_to_send) {
// All data has been sent, so we don't need to go to sleep.
tls_buffer_clear(client->tls_context);
- client->tls_data_to_send = NULL;
+ client->tls_data_to_send = nullptr;
return false;
}
void Server::skip_lost_data(Client *client)
{
Stream *stream = client->stream;
- if (stream == NULL) {
+ if (stream == nullptr) {
return;
}
size_t bytes_to_send = stream->bytes_received - client->stream_pos;
}
}
- Stream *stream = streams[stream_url_map_it->second];
+ Stream *stream = streams[stream_url_map_it->second].get();
if (stream->http_header.empty()) {
return 503; // Service unavailable.
}
void Server::close_client(Client *client)
{
- if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->sock, NULL) == -1) {
+ if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->sock, nullptr) == -1) {
log_perror("epoll_ctl(EPOLL_CTL_DEL)");
exit(1);
}
// This client could be sleeping, so we'll need to fix that. (Argh, O(n).)
- if (client->stream != NULL) {
+ if (client->stream != nullptr) {
delete_from(&client->stream->sleeping_clients, client);
delete_from(&client->stream->to_process, client);
}
queued_add_clients.clear();
}
- for (Stream *stream : streams) {
+ for (unique_ptr<Stream> &stream : streams) {
stream->process_queued_data();
}
}