Log bytes sent and lost better.
[cubemap] / client.cpp
1 #include <arpa/inet.h>
2 #include <netinet/in.h>
3 #include <stdio.h>
4 #include <sys/socket.h>
5
6 #include "client.h"
7 #include "markpool.h"
8 #include "state.pb.h"
9 #include "stream.h"
10
11 using namespace std;
12
13 Client::Client(int sock)
14         : sock(sock),
15           fwmark(0),
16           connect_time(time(NULL)),
17           state(Client::READING_REQUEST),
18           stream(NULL),
19           header_or_error_bytes_sent(0),
20           stream_pos(0),
21           bytes_sent(0),
22           bytes_lost(0),
23           num_loss_events(0)
24 {
25         request.reserve(1024);
26
27         // Find the remote address, and convert it to ASCII.
28         sockaddr_in6 addr;
29         socklen_t addr_len = sizeof(addr);
30
31         if (getpeername(sock, reinterpret_cast<sockaddr *>(&addr), &addr_len) == -1) {
32                 perror("getpeername");
33                 remote_addr = "";
34         } else {
35                 char buf[INET6_ADDRSTRLEN];
36                 if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == NULL) {
37                         perror("inet_ntop");
38                         remote_addr = "";
39                 } else {
40                         remote_addr = buf;
41                 }
42         }
43 }
44         
45 Client::Client(const ClientProto &serialized, Stream *stream)
46         : sock(serialized.sock()),
47           remote_addr(serialized.remote_addr()),
48           connect_time(serialized.connect_time()),
49           state(State(serialized.state())),
50           request(serialized.request()),
51           stream_id(serialized.stream_id()),
52           stream(stream),
53           header_or_error(serialized.header_or_error()),
54           header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()),
55           stream_pos(serialized.stream_pos()),
56           bytes_sent(serialized.bytes_sent()),
57           bytes_lost(serialized.bytes_lost()),
58           num_loss_events(serialized.num_loss_events())
59 {
60         if (stream != NULL && stream->mark_pool != NULL) {
61                 fwmark = stream->mark_pool->get_mark();
62         } else {
63                 fwmark = 0;  // No mark.
64         }
65         if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) {
66                 if (fwmark != 0) {
67                         perror("setsockopt(SO_MARK)");
68                 }
69         }
70 }
71
72 ClientProto Client::serialize() const
73 {
74         ClientProto serialized;
75         serialized.set_sock(sock);
76         serialized.set_remote_addr(remote_addr);
77         serialized.set_connect_time(connect_time);
78         serialized.set_state(state);
79         serialized.set_request(request);
80         serialized.set_stream_id(stream_id);
81         serialized.set_header_or_error(header_or_error);
82         serialized.set_header_or_error_bytes_sent(serialized.header_or_error_bytes_sent());
83         serialized.set_stream_pos(stream_pos);
84         serialized.set_bytes_sent(bytes_sent);
85         serialized.set_bytes_lost(bytes_lost);
86         serialized.set_num_loss_events(num_loss_events);
87         return serialized;
88 }
89         
90 ClientStats Client::get_stats() const
91 {
92         ClientStats stats;
93         stats.stream_id = stream_id;
94         stats.remote_addr = remote_addr;
95         stats.connect_time = connect_time;
96         stats.bytes_sent = bytes_sent;
97         stats.bytes_lost = bytes_lost;
98         stats.num_loss_events = num_loss_events;
99         return stats;
100 }