7 #include <sys/socket.h>
10 #include <sys/ioctl.h>
11 #include <sys/epoll.h>
24 #define STREAM_ID "stream"
25 #define STREAM_URL "http://gruessi.zrh.sesse.net:4013/"
30 Server *servers = NULL;
31 volatile bool hupped = false;
38 int create_server_socket(int port)
40 int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
41 if (server_sock == -1) {
47 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
48 perror("setsockopt(SO_REUSEADDR)");
52 // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
54 if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
55 perror("setsockopt(IPV6_V6ONLY)");
60 memset(&addr, 0, sizeof(addr));
61 addr.sin6_family = AF_INET6;
62 addr.sin6_port = htons(port);
64 if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
69 if (listen(server_sock, 128) == -1) {
77 void *acceptor_thread_run(void *arg)
79 int server_sock = int(intptr_t(arg));
83 socklen_t addrlen = sizeof(addr);
86 int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
87 if (sock == -1 && errno == EINTR) {
95 // Set the socket as nonblocking.
97 if (ioctl(sock, FIONBIO, &one) == -1) {
102 // Pick a server, round-robin, and hand over the socket to it.
103 servers[num_accepted % NUM_SERVERS].add_client(sock);
108 // Serialize the given state to a file descriptor, and return the (still open)
110 int make_tempfile(const CubemapStateProto &state)
112 char tmpl[] = "/tmp/cubemapstate.XXXXXX";
113 int state_fd = mkstemp(tmpl);
114 if (state_fd == -1) {
120 state.SerializeToString(&serialized);
122 const char *ptr = serialized.data();
123 size_t to_write = serialized.size();
124 while (to_write > 0) {
125 ssize_t ret = write(state_fd, ptr, to_write);
138 // Read the state back from the file descriptor made by make_tempfile,
140 CubemapStateProto read_tempfile(int state_fd)
142 if (lseek(state_fd, 0, SEEK_SET) == -1) {
150 ssize_t ret = read(state_fd, buf, sizeof(buf));
160 serialized.append(string(buf, buf + ret));
163 close(state_fd); // Implicitly deletes the file.
165 CubemapStateProto state;
166 if (!state.ParseFromString(serialized)) {
167 fprintf(stderr, "PANIC: Failed deserialization of state.\n");
174 int main(int argc, char **argv)
176 fprintf(stderr, "\nCubemap starting.\n");
178 servers = new Server[NUM_SERVERS];
181 if (argc == 3 && strcmp(argv[1], "-state") == 0) {
182 fprintf(stderr, "Deserializing state from previous process... ");
183 int state_fd = atoi(argv[2]);
184 CubemapStateProto loaded_state = read_tempfile(state_fd);
186 // Deserialize the streams.
187 for (int i = 0; i < loaded_state.streams_size(); ++i) {
188 for (int j = 0; j < NUM_SERVERS; ++j) {
189 servers[j].add_stream_from_serialized(loaded_state.streams(i));
193 // Put back the existing clients. It doesn't matter which server we
194 // allocate them to, so just do round-robin.
195 for (int i = 0; i < loaded_state.clients_size(); ++i) {
196 servers[i % NUM_SERVERS].add_client_from_serialized(loaded_state.clients(i));
199 // Deserialize the server socket.
200 server_sock = loaded_state.server_sock();
202 fprintf(stderr, "done.\n");
204 // TODO: This should come from a config file.
205 server_sock = create_server_socket(PORT);
206 for (int i = 0; i < NUM_SERVERS; ++i) {
207 servers[i].add_stream(STREAM_ID);
211 for (int i = 0; i < NUM_SERVERS; ++i) {
215 pthread_t acceptor_thread;
216 pthread_create(&acceptor_thread, NULL, acceptor_thread_run, reinterpret_cast<void *>(server_sock));
218 Input input(STREAM_ID, STREAM_URL);
229 CubemapStateProto state;
230 state.set_server_sock(server_sock);
231 for (int i = 0; i < NUM_SERVERS; ++i) {
234 CubemapStateProto local_state = servers[i].serialize();
236 // The stream state should be identical between the servers, so we only store it once.
238 state.mutable_streams()->MergeFrom(local_state.streams());
240 for (int j = 0; j < local_state.clients_size(); ++j) {
241 state.add_clients()->MergeFrom(local_state.clients(j));
246 fprintf(stderr, "Serializing state and re-execing...\n");
247 int state_fd = make_tempfile(state);
250 sprintf(buf, "%d", state_fd);
253 execlp(argv[0], argv[0], "-state", buf, NULL);
255 fprintf(stderr, "PANIC: re-exec of %s failed. Waiting 0.2 seconds and trying again...\n", argv[0]);