[mw-devel] MW3 r1303 - in trunk/src: . server

arthur at sucs.org arthur at sucs.org
Thu Nov 29 16:48:57 GMT 2012


Author: arthur
Date: 2012-11-29 16:48:56 +0000 (Thu, 29 Nov 2012)
New Revision: 1303

Modified:
   trunk/src/Makefile
   trunk/src/ipc.c
   trunk/src/ipc.h
   trunk/src/server/mwserv.c
   trunk/src/server/servsock.c
   trunk/src/socket.c
   trunk/src/socket.h
   trunk/src/talker.c
Log:
when we arent connected yet, queue up any messages generated


Modified: trunk/src/Makefile
===================================================================
--- trunk/src/Makefile	2012-11-29 15:20:32 UTC (rev 1302)
+++ trunk/src/Makefile	2012-11-29 16:48:56 UTC (rev 1303)
@@ -11,10 +11,14 @@
 CFLAGS+=-I/usr/include/mozjs
 
 # cflags for standard 'cc' compiler
-CFLAGS+= -Werror -Wall -Wshadow -Wmissing-prototypes -Wstrict-prototypes -Wpointer-arith -Wwrite-strings -Wcast-align -Wbad-function-cast -Wmissing-format-attribute -Wformat=2 -Wformat-security -Wformat-nonliteral -Wno-long-long -Wno-strict-aliasing -pedantic -fpie -std=gnu99 -D_GNU_SOURCE
-LDFLAGS+= -pie
+CFLAGS+= -Werror -Wall -Wshadow -Wmissing-prototypes -Wstrict-prototypes -Wpointer-arith -Wwrite-strings -Wcast-align -Wbad-function-cast -Wmissing-format-attribute -Wformat=2 -Wformat-security -Wformat-nonliteral -Wno-long-long -Wno-strict-aliasing -pedantic -std=gnu99 -D_GNU_SOURCE
 LDLIBS+= -lreadline -ltermcap -lcrypt -l$(JSLIB) -lsqlite3 -lcurl -lpthread -lgnutls-openssl -ljansson
 
+ifdef WITHPIE
+CFLAGS += -fpie
+LDFLAGS += -pie
+endif
+
 # info strings, do not edit.
 DEFS:= -DBUILD_DATE=\"$(shell date +%Y%m%d)\"
 DEFS+= -DBUILD_USER=\"$(shell whoami | awk -f capitalise.awk)\"

Modified: trunk/src/ipc.c
===================================================================
--- trunk/src/ipc.c	2012-11-29 15:20:32 UTC (rev 1302)
+++ trunk/src/ipc.c	2012-11-29 16:48:56 UTC (rev 1303)
@@ -41,10 +41,14 @@
 
 	if (target == NULL) host = ipc_parent;
 
+	/* already connected, why are we here again ? */
+	if (ipcsock->fd != -1) return;
+
+	/* we dont have anywhere to connect to yet */
+	if (host == NULL) return;
+
 	int fd = ipcconn_connect(host);
-	if (fd < -1) {
-		free(ipcsock);
-		ipcsock = NULL;
+	if (fd < 0) {
 		fprintf(stderr, "Connection to server failed.\n");
 		return;
 	}
@@ -54,7 +58,6 @@
 		ipc_parent = strdup(target);
 	}
 
-	ipcsock = ipcconn_create();
 	ipcsock->fd = fd;
 	ipcsock->state = IPCSTATE_CONNECTED;
 
@@ -64,13 +67,25 @@
 	const char *nonce = get_nonce();
 	ipcmsg_append(msg, nonce, strlen(nonce));
 	ipcmsg_send(msg, ipcsock);
-	ipcmsg_destroy(msg);
+
+	/* now send any queued up messages */
+	if (!list_empty(&(ipcsock->outq))) {
+		struct list_head *pos, *q;
+		list_for_each_safe(pos, q, &ipcsock->outq) {
+			outq_msg_t * oq = list_entry(pos, outq_msg_t, list);
+			list_del(&(oq->list));
+			oq->msg->refcount--;
+			ipcmsg_summary("BACKLOG", oq->msg);
+			ipcmsg_send(oq->msg, ipcsock);
+			free(oq);
+		}
+	}
 }
 
 void ipc_check(void)
 {
 	/* not connected, try again */
-	if (ipc_connected() == 0)
+	if (ipc_connected())
 		ipc_connect(NULL);
 }
 
@@ -125,7 +140,6 @@
 			ipcmsg_destination(msg, dest);
 			ipcmsg_append(msg, buff, dgram_len);
 			ipcmsg_send(msg, ipcsock);
-			ipcmsg_destroy(msg);
 		} else {
 			fprintf(stderr, "Failed to utf8_cleanup the message\n"); 
 		}

Modified: trunk/src/ipc.h
===================================================================
--- trunk/src/ipc.h	2012-11-29 15:20:32 UTC (rev 1302)
+++ trunk/src/ipc.h	2012-11-29 16:48:56 UTC (rev 1303)
@@ -51,6 +51,6 @@
 unsigned int ipc_send_to_username(const char * dest, enum ipc_types msgtype, const char * data);
 unsigned int ipc_send_to_all(enum ipc_types msgtype, const char * data, send_filter * filter, const void * filterinfo);
 void ipcmsg_transmit(ipc_message_t * msg);
+int msg_queue(ipc_message_t *msg, ipc_connection_t *conn);
 
-
 #endif

Modified: trunk/src/server/mwserv.c
===================================================================
--- trunk/src/server/mwserv.c	2012-11-29 15:20:32 UTC (rev 1302)
+++ trunk/src/server/mwserv.c	2012-11-29 16:48:56 UTC (rev 1303)
@@ -7,6 +7,7 @@
 #include <sys/types.h>
 #include <pwd.h>
 #include <string.h>
+#include <time.h>
 #include "../socket.h"
 #include "servsock.h"
 #include "../files.h"

Modified: trunk/src/server/servsock.c
===================================================================
--- trunk/src/server/servsock.c	2012-11-29 15:20:32 UTC (rev 1302)
+++ trunk/src/server/servsock.c	2012-11-29 16:48:56 UTC (rev 1303)
@@ -21,15 +21,6 @@
 static int pollfd = -1;
 int mainsock_die = 0;
 
-typedef struct {
-	struct list_head list;
-
-	ipc_message_t * msg;
-
-	int headsent;
-	int used;
-} outq_msg_t;
-
 int open_mainsock(uint16_t port)
 {
 	int fd = -1;
@@ -125,7 +116,7 @@
 			outq_msg_t * oq = list_entry(pos, outq_msg_t, list);
 			list_del(&(oq->list));
 			oq->msg->refcount--;
-			if (oq->msg->refcount <= 0) ipcmsg_destroy(oq->msg);
+			ipcmsg_destroy(oq->msg);
 			free(oq);
 		}
 	}
@@ -223,9 +214,8 @@
 	outq_msg_t * mtx = list_entry(pos, outq_msg_t, list);
 	list_del(pos);
 
-	ipcmsg_send(mtx->msg, conn);
 	mtx->msg->refcount--;
-	if (mtx->msg->refcount <= 0) ipcmsg_destroy(mtx->msg);
+	ipcmsg_send(mtx->msg, conn);
 	free(mtx);
 }
 
@@ -233,10 +223,7 @@
 
 void process_msg(ipc_connection_t *conn, ipc_message_t *msg)
 {
-	if (msg->head.type < 255)
-		printf("src: %X, dst: %X, Type: %4d, len=%d\n", msg->head.src, msg->head.dst, msg->head.type, msg->head.len);
-	else
-		printf("src: %X, dst: %X, Type: '%4.4s', len=%d\n", msg->head.src, msg->head.dst, (char *)&msg->head.type, msg->head.len);
+	ipcmsg_summary("PROCESS", msg);
 
 	/* client just told us who they are */
 	if (msg->head.type == FOURCC("HELO")) {
@@ -276,7 +263,7 @@
 		json_addstring(j, "version", version);
 		ipcmsg_json_encode(msg, j);
 		msg_attach(msg, conn);
-		if (msg->refcount <= 0) ipcmsg_destroy(msg);
+		ipcmsg_destroy(msg);
 		return;
 	}
 
@@ -295,7 +282,7 @@
 			}
 		}
 	}
-	if (msg->refcount <= 0) ipcmsg_destroy(msg);
+	ipcmsg_destroy(msg);
 }
 
 void msg_attach(ipc_message_t *msg, ipc_connection_t *conn)
@@ -303,14 +290,8 @@
 	if (conn == NULL) return;
 	/* add to end of tx queue */
 
-	int wasempty = list_empty(&conn->outq);
+	int wasempty = msg_queue(msg, conn);
 
-	outq_msg_t * new = malloc(sizeof(outq_msg_t));
-	bzero(new, sizeof(outq_msg_t));
-	new->msg = msg;
-	new->msg->refcount++;
-	list_add_tail(&(new->list), &(conn->outq));
-
 	/* nothing was queueed, switch on write notifications */
 	if (wasempty) {
 		struct epoll_event ev;

Modified: trunk/src/socket.c
===================================================================
--- trunk/src/socket.c	2012-11-29 15:20:32 UTC (rev 1302)
+++ trunk/src/socket.c	2012-11-29 16:48:56 UTC (rev 1303)
@@ -36,6 +36,15 @@
 	msg->body[ msg->bodylen ] = 0;
 }
 
+void ipcmsg_summary(const char *intro, ipc_message_t *msg)
+{
+	if (intro != NULL) printf("%s: ", intro);
+	if (msg->head.type < 255)
+		printf("src: %X, dst: %X, Type: %4d, len=%d\n", msg->head.src, msg->head.dst, msg->head.type, msg->head.len);
+	else
+		printf("src: %X, dst: %X, Type: '%4.4s', len=%d\n", msg->head.src, msg->head.dst, (char *)&msg->head.type, msg->head.len);
+}
+
 /* set the destination (PID) of a message */
 void ipcmsg_destination(ipc_message_t *msg, uint32_t dest)
 {
@@ -47,17 +56,42 @@
 void ipcmsg_destroy(ipc_message_t * msg)
 {
 	if (msg == NULL) return;
+	if (msg->refcount > 0) return;
+
 	if (msg->body != NULL) free(msg->body);
 	bzero(msg, sizeof(ipc_message_t));
 	free(msg);
 }
 
+int msg_queue(ipc_message_t *msg, ipc_connection_t *conn)
+{
+	int wasempty = list_empty(&conn->outq);
+
+	outq_msg_t * new = malloc(sizeof(outq_msg_t));
+	bzero(new, sizeof(outq_msg_t));
+	new->msg = msg;
+	new->msg->refcount++;
+	list_add_tail(&(new->list), &(conn->outq));
+
+	ipcmsg_summary("QUEUE", msg);
+
+	return wasempty;
+}
+
 /* immediately transmit a message to a socket */
 void ipcmsg_send(ipc_message_t *msg, ipc_connection_t *conn)
 {
 	struct iovec iov[2];
 	int iovused = 0;
 
+	if (conn == NULL) { 
+		ipcmsg_summary("NO CONN", msg); 
+		abort();
+		return; 
+	}
+	if (msg == NULL) { msg_queue(msg, conn); return; }
+	if (conn->fd == -1) { msg_queue(msg, conn); return; }
+
 	bzero(&iov, sizeof(iov));
 	iov[0].iov_base = &msg->head;
 	iov[0].iov_len = sizeof(msg->head);
@@ -79,6 +113,7 @@
 	if (n < bytes) {
 		fprintf(stderr, "Short write on ipc socket.\n");
 	}
+	ipcmsg_destroy(msg);
 }
 
 /* mark this socket as bad/gone */

Modified: trunk/src/socket.h
===================================================================
--- trunk/src/socket.h	2012-11-29 15:20:32 UTC (rev 1302)
+++ trunk/src/socket.h	2012-11-29 16:48:56 UTC (rev 1303)
@@ -45,6 +45,16 @@
 
 } ipc_connection_t;
 
+/* use this to queue a message for transmission */
+typedef struct {
+	struct list_head list;
+
+	ipc_message_t * msg;
+
+	int headsent;
+	int used;
+} outq_msg_t;
+
 #define _MIN(a,b) (a<b)?a:b
 
 unsigned int FOURCC(const char *a);
@@ -52,6 +62,7 @@
 ipc_message_t *ipcmsg_create(uint32_t type,uint32_t src);
 void ipcmsg_append(ipc_message_t *msg, const void *data, int len);
 void ipcmsg_destination(ipc_message_t *msg, uint32_t dest);
+int msg_queue(ipc_message_t *msg, ipc_connection_t *conn);
 void ipcmsg_destroy(ipc_message_t *msg);
 void ipcmsg_send(ipc_message_t *msg, ipc_connection_t *conn);
 void ipcconn_bad(ipc_connection_t *conn);
@@ -65,5 +76,7 @@
 int json_addint(json_t *js, const char *key, int value);
 const char *json_getstring(json_t *js, const char *key);
 int json_getint(json_t *js, const char *key);
+void ipcmsg_summary(const char *intro, ipc_message_t *msg);
 
+
 #endif

Modified: trunk/src/talker.c
===================================================================
--- trunk/src/talker.c	2012-11-29 15:20:32 UTC (rev 1302)
+++ trunk/src/talker.c	2012-11-29 16:48:56 UTC (rev 1303)
@@ -271,7 +271,6 @@
 {
 	ipc_message_t * msg = ipcmsg_create(FOURCC("UPTM"), getpid());
 	ipcmsg_transmit(msg);
-	ipcmsg_destroy(msg);
 }
 
 void t_quit(CommandList *cm, int argc, const char **argv, char *args)




More information about the mw-devel mailing list