Rename bytes_sent to stream_pos, because that is what it really is.
[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 {
22         request.reserve(1024);
23
24         // Find the remote address, and convert it to ASCII.
25         sockaddr_in6 addr;
26         socklen_t addr_len = sizeof(addr);
27
28         if (getpeername(sock, reinterpret_cast<sockaddr *>(&addr), &addr_len) == -1) {
29                 perror("getpeername");
30                 remote_addr = "";
31         } else {
32                 char buf[INET6_ADDRSTRLEN];
33                 if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == NULL) {
34                         perror("inet_ntop");
35                         remote_addr = "";
36                 } else {
37                         remote_addr = buf;
38                 }
39         }
40 }
41         
42 Client::Client(const ClientProto &serialized, Stream *stream)
43         : sock(serialized.sock()),
44           remote_addr(serialized.remote_addr()),
45           connect_time(serialized.connect_time()),
46           state(State(serialized.state())),
47           request(serialized.request()),
48           stream_id(serialized.stream_id()),
49           stream(stream),
50           header_or_error(serialized.header_or_error()),
51           header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()),
52           stream_pos(serialized.stream_pos())
53 {
54         if (stream->mark_pool != NULL) {
55                 fwmark = stream->mark_pool->get_mark();
56         } else {
57                 fwmark = 0;  // No mark.
58         }
59         if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) {
60                 if (fwmark != 0) {
61                         perror("setsockopt(SO_MARK)");
62                 }
63         }
64 }
65
66 ClientProto Client::serialize() const
67 {
68         ClientProto serialized;
69         serialized.set_sock(sock);
70         serialized.set_remote_addr(remote_addr);
71         serialized.set_connect_time(connect_time);
72         serialized.set_state(state);
73         serialized.set_request(request);
74         serialized.set_stream_id(stream_id);
75         serialized.set_header_or_error(header_or_error);
76         serialized.set_header_or_error_bytes_sent(serialized.header_or_error_bytes_sent());
77         serialized.set_stream_pos(stream_pos);
78         return serialized;
79 }
80         
81 ClientStats Client::get_stats() const
82 {
83         ClientStats stats;
84         stats.stream_id = stream_id;
85         stats.remote_addr = remote_addr;
86         stats.connect_time = connect_time;
87         stats.bytes_sent = stream_pos;
88         return stats;
89 }