Reopen the port if it changes between runs.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 7 Apr 2013 13:43:36 +0000 (15:43 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 7 Apr 2013 13:43:36 +0000 (15:43 +0200)
cubemap.cpp
state.proto

index f682472..8dff03b 100644 (file)
@@ -282,7 +282,7 @@ int main(int argc, char **argv)
        // Create the servers.
        servers = new Server[NUM_SERVERS];
 
-       int server_sock;
+       int server_sock = -1, old_port = -1;
        if (argc == 4 && strcmp(argv[2], "-state") == 0) {
                fprintf(stderr, "Deserializing state from previous process... ");
                int state_fd = atoi(argv[3]);
@@ -303,17 +303,27 @@ int main(int argc, char **argv)
 
                // Deserialize the server socket.
                server_sock = loaded_state.server_sock();
+               old_port = loaded_state.port();
 
                fprintf(stderr, "done.\n");
-       } else {
-               server_sock = create_server_socket(port);
-
+       } else{
                // TODO: This should come from the config file.
                for (int i = 0; i < NUM_SERVERS; ++i) {
                        servers[i].add_stream(STREAM_ID);
                }
        }
 
+       // Open a new server socket if we do not already have one, or if we changed ports.
+       if (server_sock != -1 && port != old_port) {
+               fprintf(stderr, "NOTE: Port changed from %d to %d; opening new socket.\n", old_port, port);
+               close(server_sock);
+               server_sock = -1;
+       }
+       if (server_sock == -1) {
+               server_sock = create_server_socket(port);
+       }
+
+       // Start up all the servers!
        for (int i = 0; i < NUM_SERVERS; ++i) {
                servers[i].run();
        }
@@ -335,6 +345,7 @@ int main(int argc, char **argv)
 
        CubemapStateProto state;
        state.set_server_sock(server_sock);
+       state.set_port(port);
        for (int i = 0; i < NUM_SERVERS; ++i) {
                servers[i].stop();
 
index 19f2617..d33d689 100644 (file)
@@ -21,4 +21,5 @@ message CubemapStateProto {
        repeated ClientProto clients = 1;
        repeated StreamProto streams = 2;
        optional int32 server_sock = 3;
+       optional int32 port = 4;
 };