[asterisk-commits] wedhorn: branch wedhorn/skinny-session r391039 - /team/wedhorn/skinny-session...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Jun 9 15:48:21 CDT 2013


Author: wedhorn
Date: Sun Jun  9 15:48:19 2013
New Revision: 391039

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=391039
Log:
skinny: move keepalives to sched arrangement

Modified:
    team/wedhorn/skinny-session/channels/chan_skinny.c

Modified: team/wedhorn/skinny-session/channels/chan_skinny.c
URL: http://svnview.digium.com/svn/asterisk/team/wedhorn/skinny-session/channels/chan_skinny.c?view=diff&rev=391039&r1=391038&r2=391039
==============================================================================
--- team/wedhorn/skinny-session/channels/chan_skinny.c (original)
+++ team/wedhorn/skinny-session/channels/chan_skinny.c Sun Jun  9 15:48:19 2013
@@ -1607,7 +1607,7 @@
 struct skinnysession {
 	pthread_t t;
 	ast_mutex_t lock;
-	time_t start;
+	struct timeval start;
 	struct sockaddr_in sin;
 	int fd;
 	char outbuf[SKINNY_MAX_PACKET];
@@ -1616,6 +1616,7 @@
 	int lockstate; /* Only for use in the skinny_session thread */
 	int auth_timeout_sched;
 	int keepalive_timeout_sched;
+	struct timeval last_keepalive;
 };
 
 static struct ast_channel *skinny_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *dest, int *cause);
@@ -1639,6 +1640,7 @@
 static void dumpsub(struct skinny_subchannel *sub, int forcehangup);
 static void activatesub(struct skinny_subchannel *sub, int state);
 static void dialandactivatesub(struct skinny_subchannel *sub, char exten[AST_MAX_EXTENSION]);
+static int skinny_nokeepalive_cb(const void *data);
 
 static struct ast_channel_tech skinny_tech = {
 	.type = "Skinny",
@@ -6158,6 +6160,23 @@
 	return 1;
 }
 
+static void handle_keepalive_message(struct skinny_req *req, struct skinnysession *s)
+{
+	if (ast_sched_del(sched, s->keepalive_timeout_sched)) {
+		return;
+	}
+	
+	if (ast_tvzero(s->last_keepalive)) {
+		SKINNY_DEBUG(DEBUG_PACKET, 3, "Received first keep_alive after %ldms\n", (long) ast_tvdiff_ms(ast_tvnow(), s->start));
+	} else {
+		SKINNY_DEBUG(DEBUG_PACKET, 3, "Received keep_alive %ldms\n", (long) ast_tvdiff_ms(ast_tvnow(), ast_tvadd(s->last_keepalive, ast_tv(keep_alive, 0))));
+	}
+
+	s->keepalive_timeout_sched = ast_sched_add(sched, keep_alive*3000, skinny_nokeepalive_cb, s);
+	s->last_keepalive = ast_tvnow();
+	transmit_keepaliveack(s);
+}
+
 static int handle_keypad_button_message(struct skinny_req *req, struct skinnysession *s)
 {
 	struct skinny_subchannel *sub = NULL;
@@ -7202,7 +7221,7 @@
 	switch(letohl(req->e)) {
 	case KEEP_ALIVE_MESSAGE:
 		SKINNY_DEBUG(DEBUG_PACKET, 3, "Received KEEP_ALIVE_MESSAGE from %s\n", (d ? d->name : "unregistered"));
-		transmit_keepaliveack(s);
+		handle_keepalive_message(req, s);
 		break;
 	case REGISTER_MESSAGE:
 		SKINNY_DEBUG(DEBUG_PACKET, 3, "Received REGISTER_MESSAGE from %s, name %s, type %d, protovers %d\n",
@@ -7348,6 +7367,15 @@
 	return 0;
 }
 
+static int skinny_nokeepalive_cb(const void *data)
+{
+	struct skinnysession *s = (struct skinnysession *)data;
+	ast_log(LOG_WARNING, "Skinny Client failed to send keepalive in last %d seconds (SCHED %d)\n", keep_alive*3, s->keepalive_timeout_sched);
+	s->keepalive_timeout_sched = 0;
+	end_session(s);
+	return 0;
+}
+
 static void skinny_session_cleanup(void *data)
 {
 	struct skinnysession *s = (struct skinnysession *)data;
@@ -7395,7 +7423,6 @@
 	struct skinnysession *s = data;
 	
 	int dlen = 0;
-	int timeout;
 	struct pollfd fds[1];
 
 	if (!s) {
@@ -7406,10 +7433,8 @@
 	ast_verb(3, "Starting Skinny session from %s\n", ast_inet_ntoa(s->sin.sin_addr));
 
 	pthread_cleanup_push(skinny_session_cleanup, s);
-	
-	if(time(&s->start) == -1) {
-		ast_log(LOG_ERROR, "error executing time(): %s; continuing without start time\n", strerror(errno));
-	}
+
+	s->start = ast_tvnow();
 
 	ast_mutex_init(&s->lock);
 	s->lockstate = 0;
@@ -7419,31 +7444,20 @@
 	AST_LIST_UNLOCK(&sessions);
 
 	s->auth_timeout_sched = ast_sched_add(sched, auth_timeout*1000, skinny_noauth_cb, s);
+	s->keepalive_timeout_sched = ast_sched_add(sched, keep_alive*3000, skinny_nokeepalive_cb, s);
 
 	for (;;) {
-
-		timeout = keep_alive * 1100;
 
 		fds[0].fd = s->fd;
 		fds[0].events = POLLIN;
 		fds[0].revents = 0;
-		res = ast_poll(fds, 1, timeout); /* If nothing has happen, client is dead */
-								/* we add 10% to the keep_alive to deal */
-								/* with network delays, etc */
+		res = ast_poll(fds, 1, -1); /* block */
 		if (res < 0) {
 			if (errno != EINTR) {
 				ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
 				ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
 				break;
 			}
-		} else if (res == 0) {
-			if (s->device) {
-				ast_log(LOG_WARNING, "Skinny Client was lost, unregistering\n");
-			} else {
-				ast_log(LOG_WARNING, "Skinny Client failed to authenticate in %d seconds\n", auth_timeout);
-			}
-			ast_verb(3, "Ending Skinny session from %s (bad input)\n", ast_inet_ntoa(s->sin.sin_addr));
-			break;
 		}
 
 		if (fds[0].revents) {




More information about the asterisk-commits mailing list