[mw-devel] MW3 r1281 - trunk/src

arthur at sucs.org arthur at sucs.org
Thu Nov 8 11:24:45 GMT 2012


Author: arthur
Date: 2012-11-08 11:24:45 +0000 (Thu, 08 Nov 2012)
New Revision: 1281

Modified:
   trunk/src/incoming.c
   trunk/src/ipc.c
   trunk/src/ipc.h
   trunk/src/main.c
Log:
fix auto-reconnect, now with exponential backoff


Modified: trunk/src/incoming.c
===================================================================
--- trunk/src/incoming.c	2012-11-08 10:45:07 UTC (rev 1280)
+++ trunk/src/incoming.c	2012-11-08 11:24:45 UTC (rev 1281)
@@ -455,7 +455,7 @@
 		ipcmsg_destroy(msg);
 		msg = read_socket(ipcsock, 0);
 	}
-	if (msg == NULL && ipcsock->fd == -1) fprintf(stderr, "Server disconnected.\n");
+	if (msg == NULL && !ipc_connected()) fprintf(stderr, "Server disconnected.\n");
 	if (cm_flags(user->chatmode,CM_ONCHAT,CM_MODE_ANY)) set_talk_rights(); else set_rights();
 }
 

Modified: trunk/src/ipc.c
===================================================================
--- trunk/src/ipc.c	2012-11-08 10:45:07 UTC (rev 1280)
+++ trunk/src/ipc.c	2012-11-08 11:24:45 UTC (rev 1281)
@@ -15,13 +15,25 @@
 #include "files.h"
 #include "socket.h"
 
-ipc_connection_t * ipcsock = NULL;
+/* client mode uses this as its connection */
+ipc_connection_t * ipcsock = NULL; 
 char *ipc_parent = NULL;
 
 extern int32_t userposn;
 
 const char *get_nonce(void);
 
+int ipc_connected(void)
+{
+	/* we have never connected */
+	if (ipc_parent == NULL) return -1;
+	/* we are not connected now */
+	if (ipcsock == NULL) return 0;
+	if (ipcsock->fd == -1) return 0;
+	/* everything must be fine */
+	return 1;
+}
+
 void ipc_connect(const char *target)
 {
 	const char * host = target;
@@ -58,7 +70,8 @@
 
 void ipc_check(void)
 {
-	if (ipcsock == NULL || ipcsock->fd == -1)
+	/* not connected, try again */
+	if (ipc_connected() == 0)
 		ipc_connect(NULL);
 }
 

Modified: trunk/src/ipc.h
===================================================================
--- trunk/src/ipc.h	2012-11-08 10:45:07 UTC (rev 1280)
+++ trunk/src/ipc.h	2012-11-08 11:24:45 UTC (rev 1281)
@@ -36,6 +36,8 @@
 typedef int (send_filter)(const struct person * usr, const struct who * who, const void * info);
 
 void ipc_connect(const char *target);
+int ipc_connected(void);
+void ipc_check(void);
 void ipc_close(void);
 int ipc_getfd(void);
 int ipc_send_to_pid(pid_t dest, enum ipc_types msgtype, const char * data);

Modified: trunk/src/main.c
===================================================================
--- trunk/src/main.c	2012-11-08 10:45:07 UTC (rev 1280)
+++ trunk/src/main.c	2012-11-08 11:24:45 UTC (rev 1281)
@@ -56,6 +56,7 @@
 #include "colour.h"
 #include "init.h"
 #include "intl.h"
+#include "ipc.h"
 
 #include <pwd.h>
 #include <grp.h>
@@ -1043,6 +1044,9 @@
 	static int fl=0;
 	int select_error;
 
+	static int reconnect_backoff = 1;
+	static time_t reconnect_last = 0;
+
 	in_idle++;
 
 	int incoming_pipe = ipc_getfd();
@@ -1081,6 +1085,21 @@
 	}
 	in_idle--;
 
+	/* we are not connected, attempt reconnect */
+	if (ipc_connected() == 0) {
+		time_t now = time(0);
+		if (now > reconnect_last + reconnect_backoff) {
+			ipc_check();
+			reconnect_last = now;
+			if (!ipc_connected()) {
+				/* failed again, backoff */
+				reconnect_backoff *= 2;
+			}
+		}
+	} else {
+		reconnect_backoff = 1;
+	}
+
 	errno = select_error;
 	if (nfds<0 && select_error == EINVAL)
 	{




More information about the mw-devel mailing list