Change the connected time from time_t to timespec.
[cubemap] / client.cpp
1 #include <arpa/inet.h>
2 #include <netinet/in.h>
3 #include <stdint.h>
4 #include <sys/socket.h>
5
6 #include "client.h"
7 #include "log.h"
8 #include "state.pb.h"
9 #include "stream.h"
10
11 #ifndef SO_MAX_PACING_RATE
12 #define SO_MAX_PACING_RATE 47
13 #endif
14
15 using namespace std;
16
17 Client::Client(int sock)
18         : sock(sock),
19           state(Client::READING_REQUEST),
20           stream(NULL),
21           header_or_error_bytes_sent(0),
22           stream_pos(0),
23           bytes_sent(0),
24           bytes_lost(0),
25           num_loss_events(0)
26 {
27         request.reserve(1024);
28
29         if (clock_gettime(CLOCK_MONOTONIC_COARSE, &connect_time) == -1) {
30                 log_perror("clock_gettime(CLOCK_MONOTONIC_COARSE)");
31                 return;
32         }
33
34         // Find the remote address, and convert it to ASCII.
35         sockaddr_in6 addr;
36         socklen_t addr_len = sizeof(addr);
37
38         if (getpeername(sock, reinterpret_cast<sockaddr *>(&addr), &addr_len) == -1) {
39                 log_perror("getpeername");
40                 remote_addr = "";
41                 return;
42         }
43
44         char buf[INET6_ADDRSTRLEN];
45         if (IN6_IS_ADDR_V4MAPPED(&addr.sin6_addr)) {
46                 // IPv4 address, really.
47                 if (inet_ntop(AF_INET, &addr.sin6_addr.s6_addr32[3], buf, sizeof(buf)) == NULL) {
48                         log_perror("inet_ntop");
49                         remote_addr = "";
50                 } else {
51                         remote_addr = buf;
52                 }
53         } else {
54                 if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == NULL) {
55                         log_perror("inet_ntop");
56                         remote_addr = "";
57                 } else {
58                         remote_addr = buf;
59                 }
60         }
61 }
62         
63 Client::Client(const ClientProto &serialized, Stream *stream)
64         : sock(serialized.sock()),
65           remote_addr(serialized.remote_addr()),
66           state(State(serialized.state())),
67           request(serialized.request()),
68           url(serialized.url()),
69           stream(stream),
70           header_or_error(serialized.header_or_error()),
71           header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()),
72           stream_pos(serialized.stream_pos()),
73           bytes_sent(serialized.bytes_sent()),
74           bytes_lost(serialized.bytes_lost()),
75           num_loss_events(serialized.num_loss_events())
76 {
77         if (stream != NULL) {
78                 if (setsockopt(sock, SOL_SOCKET, SO_MAX_PACING_RATE, &stream->pacing_rate, sizeof(stream->pacing_rate)) == -1) {
79                         if (stream->pacing_rate != ~0U) {
80                                 log_perror("setsockopt(SO_MAX_PACING_RATE)");
81                         }
82                 }
83         }
84         if (serialized.has_connect_time_old()) {
85                 // Do a rough conversion from time() to monotonic time.
86                 if (clock_gettime(CLOCK_MONOTONIC_COARSE, &connect_time) == -1) {
87                         log_perror("clock_gettime(CLOCK_MONOTONIC_COARSE)");
88                         return;
89                 }
90                 connect_time.tv_sec += serialized.connect_time_old() - time(NULL);
91         } else {
92                 connect_time.tv_sec = serialized.connect_time_sec();
93                 connect_time.tv_nsec = serialized.connect_time_nsec();
94         }
95 }
96
97 ClientProto Client::serialize() const
98 {
99         ClientProto serialized;
100         serialized.set_sock(sock);
101         serialized.set_remote_addr(remote_addr);
102         serialized.set_connect_time_sec(connect_time.tv_sec);
103         serialized.set_connect_time_nsec(connect_time.tv_nsec);
104         serialized.set_state(state);
105         serialized.set_request(request);
106         serialized.set_url(url);
107         serialized.set_header_or_error(header_or_error);
108         serialized.set_header_or_error_bytes_sent(serialized.header_or_error_bytes_sent());
109         serialized.set_stream_pos(stream_pos);
110         serialized.set_bytes_sent(bytes_sent);
111         serialized.set_bytes_lost(bytes_lost);
112         serialized.set_num_loss_events(num_loss_events);
113         return serialized;
114 }
115         
116 ClientStats Client::get_stats() const
117 {
118         ClientStats stats;
119         if (url.empty()) {
120                 stats.url = "-";
121         } else {
122                 stats.url = url;
123         }
124         stats.sock = sock;
125         stats.remote_addr = remote_addr;
126         stats.connect_time = connect_time;
127         stats.bytes_sent = bytes_sent;
128         stats.bytes_lost = bytes_lost;
129         stats.num_loss_events = num_loss_events;
130         return stats;
131 }