[asterisk-commits] rizzo: trunk r48081 - /trunk/main/manager.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Nov 28 04:20:40 MST 2006


Author: rizzo
Date: Tue Nov 28 05:20:39 2006
New Revision: 48081

URL: http://svn.digium.com/view/asterisk?view=rev&rev=48081
Log:
Move the code to purge stale sessions to a function,
to simplify the body of the main loop of the accepting thread.
Rename purge_unused() to purge_events() so one knows what the
function does.


Modified:
    trunk/main/manager.c

Modified: trunk/main/manager.c
URL: http://svn.digium.com/view/asterisk/trunk/main/manager.c?view=diff&rev=48081&r1=48080&r2=48081
==============================================================================
--- trunk/main/manager.c (original)
+++ trunk/main/manager.c Tue Nov 28 05:20:39 2006
@@ -271,7 +271,7 @@
  * Purge unused events. Remove elements from the head
  * as long as their usecount is 0 and there is a next element.
  */
-static void purge_unused(void)
+static void purge_events(void)
 {
 	struct eventqent *ev;
 
@@ -2136,6 +2136,32 @@
 	return NULL;
 }
 
+/*! \brief remove at most n_max stale session from the list. */
+static void purge_sessions(int n_max)
+{
+	struct mansession *s;
+	time_t now = time(NULL);
+
+	AST_LIST_LOCK(&sessions);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&sessions, s, list) {
+		if (s->sessiontimeout && (now > s->sessiontimeout) && !s->inuse) {
+			ast_verbose("destroy session[2] %lx now %lu to %lu\n",
+				s->managerid, (unsigned long)now, (unsigned long)s->sessiontimeout);
+			AST_LIST_REMOVE_CURRENT(&sessions, list);
+			ast_atomic_fetchadd_int(&num_sessions, -1);
+			if (s->authenticated && (option_verbose > 1) && displayconnects) {
+				ast_verbose(VERBOSE_PREFIX_2 "HTTP Manager '%s' timed out from %s\n",
+					s->username, ast_inet_ntoa(s->sin.sin_addr));
+			}
+			free_session(s);	/* XXX outside ? */
+			if (--n_max <= 0)
+				break;
+		}
+	}
+	AST_LIST_TRAVERSE_SAFE_END
+	AST_LIST_UNLOCK(&sessions);
+}
+
 /*! \brief The thread accepting connections on the manager interface port.
  * As a side effect, it purges stale sessions, one per each iteration,
  * which is at least every 5 seconds.
@@ -2149,40 +2175,20 @@
 
 	for (;;) {
 		struct mansession *s;
-		time_t now = time(NULL);
 		int as;
 		struct sockaddr_in sin;
 		socklen_t sinlen;
 		struct protoent *p;
 		int flags;
-		struct pollfd pfds[1];
-
-		AST_LIST_LOCK(&sessions);
-		AST_LIST_TRAVERSE_SAFE_BEGIN(&sessions, s, list) {
-			if (s->sessiontimeout && (now > s->sessiontimeout) && !s->inuse) {
-				ast_verbose("destroy session[2] %lx now %lu to %lu\n",
-					s->managerid, (unsigned long)now, (unsigned long)s->sessiontimeout);
-				AST_LIST_REMOVE_CURRENT(&sessions, list);
-				ast_atomic_fetchadd_int(&num_sessions, -1);
-				if (s->authenticated && (option_verbose > 1) && displayconnects) {
-					ast_verbose(VERBOSE_PREFIX_2 "HTTP Manager '%s' timed out from %s\n",
-						s->username, ast_inet_ntoa(s->sin.sin_addr));
-				}
-				free_session(s);	/* XXX outside ? */
-				break;
-			}
-		}
-		AST_LIST_TRAVERSE_SAFE_END
-		AST_LIST_UNLOCK(&sessions);
-		purge_unused();
-
-		sinlen = sizeof(sin);
-		pfds[0].fd = asock;
-		pfds[0].events = POLLIN;
+
+		purge_sessions(1);
+		purge_events();
+
 		/* Wait for something to happen, but timeout every few seconds so
 		   we can ditch any old manager sessions */
-		if (poll(pfds, 1, 5000) < 1)
+		if (ast_wait_for_input(asock, 5000) < 1)
 			continue;
+		sinlen = sizeof(sin);
 		as = accept(asock, (struct sockaddr *)&sin, &sinlen);
 		if (as < 0) {
 			ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));



More information about the asterisk-commits mailing list