Change the connected time from time_t to timespec.
[cubemap] / client.cpp
index b226d5e..e7a12f6 100644 (file)
@@ -16,7 +16,6 @@ using namespace std;
 
 Client::Client(int sock)
        : sock(sock),
-         connect_time(time(NULL)),
          state(Client::READING_REQUEST),
          stream(NULL),
          header_or_error_bytes_sent(0),
@@ -27,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);
@@ -59,7 +63,6 @@ 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()),
          url(serialized.url()),
@@ -78,6 +81,17 @@ Client::Client(const ClientProto &serialized, Stream *stream)
                        }
                }
        }
+       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();
+       }
 }
 
 ClientProto Client::serialize() const
@@ -85,7 +99,8 @@ 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_url(url);