]> git.sesse.net Git - cubemap/blob - client.cpp
Split HTTP header parsing into a common function.
[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         connect_time.tv_sec = serialized.connect_time_sec();
85         connect_time.tv_nsec = serialized.connect_time_nsec();
86 }
87
88 ClientProto Client::serialize() const
89 {
90         ClientProto serialized;
91         serialized.set_sock(sock);
92         serialized.set_remote_addr(remote_addr);
93         serialized.set_connect_time_sec(connect_time.tv_sec);
94         serialized.set_connect_time_nsec(connect_time.tv_nsec);
95         serialized.set_state(state);
96         serialized.set_request(request);
97         serialized.set_url(url);
98         serialized.set_header_or_error(header_or_error);
99         serialized.set_header_or_error_bytes_sent(serialized.header_or_error_bytes_sent());
100         serialized.set_stream_pos(stream_pos);
101         serialized.set_bytes_sent(bytes_sent);
102         serialized.set_bytes_lost(bytes_lost);
103         serialized.set_num_loss_events(num_loss_events);
104         return serialized;
105 }
106         
107 ClientStats Client::get_stats() const
108 {
109         ClientStats stats;
110         if (url.empty()) {
111                 stats.url = "-";
112         } else {
113                 stats.url = url;
114         }
115         stats.sock = sock;
116         stats.remote_addr = remote_addr;
117         stats.connect_time = connect_time;
118         stats.bytes_sent = bytes_sent;
119         stats.bytes_lost = bytes_lost;
120         stats.num_loss_events = num_loss_events;
121         return stats;
122 }