Don't send input events when nothing has changed
authorThomas Perl <thp@thpinfo.com>
Mon, 17 Aug 2009 22:09:54 +0000 (18 00:09 +0200)
committerThomas Perl <thp@thpinfo.com>
Mon, 17 Aug 2009 22:09:54 +0000 (18 00:09 +0200)
This should save bandwidth and speed up on slow links
and machines. Might introduce some bugs when the UDP
packet sent isn't received. Does not matter so much,
because input data often changes, which will lead to
a new packet to be sent.

input.h
network.c
tennix.h

diff --git a/input.h b/input.h
index 98eb471..1547056 100644 (file)
--- a/input.h
+++ b/input.h
@@ -68,12 +68,6 @@ enum {
     NET_KEY_SMASH   = 1 << INPUT_KEY_SMASH
 };
 
-typedef struct {
-    unsigned char x;
-    unsigned char y;
-    unsigned char keys; /* bitfield (0 = key pressed, 1 = not pressed) */
-} NetworkInputData;
-
 #define INPUT_DEVICE_NAME_MAX 100
 
 typedef struct {
index 444d6cd..7e16587 100644 (file)
--- a/network.c
+++ b/network.c
@@ -71,6 +71,12 @@ network_connect(const char* host, bool master)
     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);
+
+    /* Fill with invalid data for first-time update */
+    connection->input_data.x = 0xFF;
+    connection->input_data.y = 0xFF;
+    connection->input_data.keys = 0xFF;
+
     connection->master = master;
 
     return connection;
@@ -94,13 +100,16 @@ void
 network_send_input(TennixNet* connection, NetworkInputData* src)
 {
     assert(src != NULL);
-    if (connection != NULL) {
+    if (connection != NULL && memcmp(&(connection->input_data),
+                src, sizeof(NetworkInputData)) != 0) {
         memcpy(connection->input_packet->data, src, sizeof(NetworkInputData));
         connection->input_packet->address.host = connection->peer.host;
         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_socket, -1, connection->input_packet);
+        /* Remember what we sent (what the remote end "sees") right now */
+        memcpy(&(connection->input_data), src, sizeof(NetworkInputData));
     }
 }
 
index 1c8ada7..a951a9b 100644 (file)
--- a/tennix.h
+++ b/tennix.h
@@ -36,6 +36,13 @@ enum {
 };
 
 typedef struct {
+    unsigned char x;
+    unsigned char y;
+    unsigned char keys; /* bitfield (0 = key pressed, 1 = not pressed) */
+} NetworkInputData;
+
+
+typedef struct {
     UDPsocket send_socket;
     UDPsocket recv_input_socket;
     UDPsocket recv_state_socket;
@@ -51,6 +58,8 @@ typedef struct {
     Uint16 base_port_local;
     Uint16 base_port_remote;
 
+    NetworkInputData input_data;
+
     bool master;
 } TennixNet;