Only use one sending socket; auto-determine ports
authorThomas Perl <thp@perli.net>
Wed, 5 Aug 2009 13:07:50 +0000 (5 15:07 +0200)
committerThomas Perl <thp@perli.net>
Wed, 5 Aug 2009 13:11:09 +0000 (5 15:11 +0200)
For sending UDP packets, we only need one socket to be
open - both types of packets (input and state) can be
sent through the same socket.

Simplify the configuration of multi-player by setting
the pre-defined ports based on which machine is the
master and which one is the slave. This way, we only
have to specify for each instance the type (slave or
master) and the hostname/ip of the opponent.

README.Multiplayer
network.c
network.h
tennix.c
tennix.h

index 035d9f0..c4194eb 100644 (file)
@@ -4,16 +4,16 @@ and the second one is the slower one. If both computers are
 equally powerful, just chose the faster one randomly.
 
 On the faster computer, use:
-  ./tennix -n REMOTEHOST LOCALPORT REMOTEPORT -m
+  ./tennix -m REMOTEHOST
 
 On the slower computer, use:
-  ./tennix -n REMOTEHOST LOCALPORT REMOTEPORT
+  ./tennix -s REMOTEHOST
 
 So, if your faster computer is called "fast.lan" and the slower
 computer is called "slow.lan", you could use the following setup:
 
-  user@fast:~$ ./tennix -n slow.lan 1234 4321 -m
-  user@slow:~$ ./tennix -n fast.lan 4321 1234
+  user@fast:~$ ./tennix -m slow.lan
+  user@slow:~$ ./tennix -s fast.lan
 
- -- thp, Wed, 05 Aug 2009 01:26:42 +0200
+     -- thp, Wed Aug  5 15:09:59 CEST 2009
 
index e2f6aa6..444d6cd 100644 (file)
--- a/network.c
+++ b/network.c
@@ -56,25 +56,22 @@ uninit_network()
 }
 
 TennixNet*
-network_connect(const char* host, Uint16 local_port, Uint16 remote_port)
+network_connect(const char* host, bool master)
 {
     TennixNet* connection = (TennixNet*)malloc(sizeof(TennixNet));
     assert(connection != NULL);
 
-    fprintf(stderr, "ports: %d and %d\n", local_port, remote_port);
-
     assert(SDLNet_ResolveHost(&(connection->peer), (const char*)host, 0) == 0);
-    connection->base_port_local = local_port;
-    connection->base_port_remote = remote_port;
+    connection->base_port_local = (master)?(TENNIXNET_PORT_MASTER):(TENNIXNET_PORT_SLAVE);
+    connection->base_port_remote = (master)?(TENNIXNET_PORT_SLAVE):(TENNIXNET_PORT_MASTER);
     connection->input_packet = SDLNet_AllocPacket(sizeof(NetworkInputData));
     connection->state_packet = SDLNet_AllocPacket(sizeof(NetworkGameState));
     connection->input_available = false;
     connection->state_available = false;
-    connection->send_input_socket = SDLNet_UDP_Open(0);
-    connection->send_state_socket = SDLNet_UDP_Open(0);
+    connection->send_socket = SDLNet_UDP_Open(0);
     connection->recv_input_socket = SDLNet_UDP_Open(connection->base_port_local);
     connection->recv_state_socket = SDLNet_UDP_Open(connection->base_port_local+1);
-    connection->master = false;
+    connection->master = master;
 
     return connection;
 }
@@ -84,8 +81,7 @@ network_disconnect(TennixNet* connection)
 {
     if (connection != NULL)
     {
-        SDLNet_UDP_Close(connection->send_input_socket);
-        SDLNet_UDP_Close(connection->send_state_socket);
+        SDLNet_UDP_Close(connection->send_socket);
         SDLNet_UDP_Close(connection->recv_input_socket);
         SDLNet_UDP_Close(connection->recv_state_socket);
         SDLNet_FreePacket(connection->input_packet);
@@ -95,12 +91,6 @@ network_disconnect(TennixNet* connection)
 }
 
 void
-network_set_master(TennixNet* connection, bool master)
-{
-    connection->master = master;
-}
-
-void
 network_send_input(TennixNet* connection, NetworkInputData* src)
 {
     assert(src != NULL);
@@ -110,7 +100,7 @@ network_send_input(TennixNet* connection, NetworkInputData* src)
         SDLNet_Write16(connection->base_port_remote, &(connection->input_packet->address.port));
         connection->input_packet->channel = -1;
         connection->input_packet->len = sizeof(NetworkInputData);
-        SDLNet_UDP_Send(connection->send_input_socket, -1, connection->input_packet);
+        SDLNet_UDP_Send(connection->send_socket, -1, connection->input_packet);
     }
 }
 
@@ -124,7 +114,7 @@ network_send_state(TennixNet* connection, GameState* src)
         SDLNet_Write16(connection->base_port_remote+1, &(connection->state_packet->address.port));
         connection->state_packet->channel = -1;
         connection->state_packet->len = sizeof(NetworkGameState);
-        assert(SDLNet_UDP_Send(connection->send_state_socket, -1, connection->state_packet)!=0);
+        assert(SDLNet_UDP_Send(connection->send_socket, -1, connection->state_packet)!=0);
     }
 }
 
index 298187c..a31ed27 100644 (file)
--- a/network.h
+++ b/network.h
@@ -29,8 +29,9 @@
 
 #include <SDL/SDL_net.h>
 
-#define TENNIXNET_STATE_PORT 1448
-#define TENNIXNET_INPUT_PORT 1449
+/* Vanity ports "STNX" and "MTNX" */
+#define TENNIXNET_PORT_SLAVE 7869
+#define TENNIXNET_PORT_MASTER 6869
 
 typedef struct {
     Uint32 x;
@@ -78,15 +79,12 @@ void
 uninit_network();
 
 TennixNet*
-network_connect(const char* host, Uint16 local_port, Uint16 remote_port);
+network_connect(const char* host, bool master);
 
 void
 network_disconnect(TennixNet* connection);
 
 void
-network_set_master(TennixNet* connection, bool master);
-
-void
 network_send_input(TennixNet* connection, NetworkInputData* src);
 
 void
index 5b6d44b..9145b4e 100644 (file)
--- a/tennix.c
+++ b/tennix.c
@@ -108,8 +108,6 @@ int main( int argc, char** argv) {
     float wiggle;
     TennixNet* connection = NULL;
     char* net_host = NULL;
-    int net_port_local;
-    int net_port_remote;
     bool net_master = false;
 
     MenuButton btn_back = {
@@ -200,18 +198,16 @@ int main( int argc, char** argv) {
         else if (OPTION_SET("--benchmark", "-b")) {
             benchmark = true;
         }
-        else if (OPTION_SET("--network", "-n")) {
+        else if (OPTION_SET("--slave", "-s")) {
             net_host = OPTION_VALUE;
             assert(OPTION_VALUE != NULL);
             OPTION_VALUE_PROCESSED;
-            net_port_local = atoi(OPTION_VALUE);
-            assert(OPTION_VALUE != NULL);
-            OPTION_VALUE_PROCESSED;
-            net_port_remote = atoi(OPTION_VALUE);
-            assert(OPTION_VALUE != NULL);
-            OPTION_VALUE_PROCESSED;
+            net_master = false;
         }
         else if (OPTION_SET("--master", "-m")) {
+            net_host = OPTION_VALUE;
+            assert(OPTION_VALUE != NULL);
+            OPTION_VALUE_PROCESSED;
             net_master = true;
         }
         else {
@@ -253,8 +249,7 @@ int main( int argc, char** argv) {
     init_network();
 
     if (net_host != NULL) {
-        connection = network_connect(net_host, net_port_local, net_port_remote);
-        network_set_master(connection, net_master);
+        connection = network_connect(net_host, net_master);
     }
 
     menu_button_init(&btn_back);
index 5902f5c..1c8ada7 100644 (file)
--- a/tennix.h
+++ b/tennix.h
@@ -36,9 +36,8 @@ enum {
 };
 
 typedef struct {
-    UDPsocket send_input_socket;
+    UDPsocket send_socket;
     UDPsocket recv_input_socket;
-    UDPsocket send_state_socket;
     UDPsocket recv_state_socket;
 
     UDPpacket* input_packet;