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