[asterisk-commits] may: branch may/smpp r410963 - /team/may/smpp/branches/10/addons/res_smpp.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Mar 20 13:43:08 CDT 2014


Author: may
Date: Thu Mar 20 13:43:01 2014
New Revision: 410963

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=410963
Log:
fixes on esme/smsc thread logic
restart smsc thread if it stopped
use keepalive on socket with linux

Modified:
    team/may/smpp/branches/10/addons/res_smpp.c

Modified: team/may/smpp/branches/10/addons/res_smpp.c
URL: http://svnview.digium.com/svn/asterisk/team/may/smpp/branches/10/addons/res_smpp.c?view=diff&rev=410963&r1=410962&r2=410963
==============================================================================
--- team/may/smpp/branches/10/addons/res_smpp.c (original)
+++ team/may/smpp/branches/10/addons/res_smpp.c Thu Mar 20 13:43:01 2014
@@ -230,6 +230,11 @@
 static int smsc_connect(struct smpp_smsc *smsc)
 {
 	int sock;
+	int keepalive = 1;
+#ifdef __linux__
+	int keepcnt = 24, keepidle = 120, keepintvl = 30;
+#endif
+
 
 	sock = socket(AF_INET, SOCK_STREAM, 0);
 	if (!sock) {
@@ -249,6 +254,15 @@
 					smsc->name, strerror(errno));
 		return 0;
 	}
+
+	setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE, (const char *)&keepalive,
+								sizeof(keepalive));
+#ifdef __linux__
+	setsockopt (sock, SOL_TCP, TCP_KEEPCNT, &keepcnt, sizeof(keepcnt));
+	setsockopt (sock, SOL_TCP, TCP_KEEPIDLE, &keepidle, sizeof(keepidle));
+	setsockopt (sock, SOL_TCP, TCP_KEEPINTVL, &keepintvl, sizeof(keepintvl));
+#endif
+
 	smsc->seq = 1;
 	return sock;
 }
@@ -684,6 +698,10 @@
 	bind.interface_version = 0x34;
 	bind.addr_ton = 1;
 	bind.addr_npi = 1;
+	if (!strcmp(smsc->name, "astragsm")) {
+		bind.addr_ton = 0;
+		bind.addr_npi = 0;
+	}
 	snprintf((char*)bind.address_range, sizeof(bind.address_range), "%s", "");
 
 	ret = smpp34_pack(BIND_TRANSCEIVER, pdu, sizeof(pdu), &pdulen, (void *)&bind);
@@ -755,6 +773,7 @@
 	struct pollfd pfds;
 	int to;
 	int nfds;
+	// struct timeval tv;
 
 	struct smpp_buff *freedbuf, *smpp_buf;
 
@@ -782,6 +801,7 @@
 				continue;	// reconnect if bind was unsuccessfull
 			} else {
 				smsc->lastenq = time(NULL);
+				smsc->sentenq = 0;
 			}
 		}
 
@@ -793,12 +813,18 @@
 			pfds.events = POLLIN;
 			nfds += 1;
 			if (!AST_LIST_EMPTY(&smsc->outq)) {
-				pfds.events |= POLLOUT;
-			}
-		}
-		// to = (smsc->enquire > 0) ? smsc->enquire * 1000 : 12000;
-		to = 300;
-
+				pfds.events |= POLLOUT | POLLERR;
+			}
+
+			pfds.events |= POLLRDHUP | POLLNVAL;
+		}
+
+		to = (smsc->enquire > 0) ? smsc->enquire : 300;
+
+		// tv = ast_tv(300, 0);
+
+
+		// if (ast_poll2(&pfds, nfds, &tv) == -1) {
 		if (ast_poll(&pfds, nfds, to) == -1) {
 			if (smsc->socket) {
 				ast_log(LOG_WARNING, "smsc %s poll error, closing connection\n", smsc->name);
@@ -854,7 +880,7 @@
 			}
 			AST_LIST_UNLOCK(&smsc->outq);
 		}
-		if ((pfds.revents & POLLERR)) {
+		if ((pfds.revents & (POLLERR | POLLRDHUP | POLLHUP | POLLNVAL))) {
 			close(smsc->socket);
 			pthread_testcancel();
 			smsc->socket = 0;
@@ -864,12 +890,14 @@
 
 	if (smsc->socket) {
 		close(smsc->socket);
+		smsc->socket = 0;
 	}
 	ast_sched_context_destroy(sched);
 	if (smppDebug) {
 		ast_verbose("smsc thread stop for %s\n", smsc->name);
 	}
 	smsc->stop = 0;
+	smsc->monitor = AST_PTHREADT_NULL;
 	return NULL;
 }
 
@@ -910,6 +938,7 @@
 	}
 
 	smsc->lastenq = time(NULL);
+	smsc->sentenq = 0;
 
 	if (!(sched = ast_sched_context_create())) {
 		ast_log(LOG_ERROR, "Unable to create schedule context on smsc %s\n", smsc->name);
@@ -919,7 +948,7 @@
 
 	smsc->monitor = pthread_self();
 
-	while (!smsc->stop) {
+	while (!smsc->stop && smsc->socket) {
 		nfds = 0;
 		pfds.fd = smsc->socket;
 		pfds.events = POLLIN;
@@ -927,7 +956,7 @@
 		if (!AST_LIST_EMPTY(&smsc->outq)) {
 			pfds.events |= POLLOUT;
 		}
-		to = 300;
+		to = (smsc->enquire > 0) ? smsc->enquire : 300;
 
 		if (ast_poll(&pfds, nfds, to) == -1) {
 			if (smsc->socket) {
@@ -1001,6 +1030,7 @@
 		ast_verbose("esme thread stop for %s\n", smsc->name);
 	}
 	smsc->stop = 0;
+	smsc->monitor = AST_PTHREADT_NULL;
 	return NULL;
 }
 
@@ -1045,6 +1075,22 @@
 	}
 
 	while (!smpplistener_stop) {
+
+		struct smpp_smsc *smsc;
+		ast_mutex_lock(&smpp_lock);
+		// Monitor smsc outgoing connections, restart if need
+		pthread_attr_init(&attr);
+		smsc = smsc_list;
+		while (smsc) {
+			if (!smsc->esme && smsc->monitor == AST_PTHREADT_NULL) {
+				if(ast_pthread_create_detached_background(&smsc->monitor, &attr, smsc_thread, smsc) < 0) {
+					ast_log(LOG_ERROR, "Unable to start monitor thread for smsc %s\n", smsc->name);
+				}
+			}
+			smsc = smsc->next;
+		}
+		ast_mutex_unlock(&smpp_lock);
+
 		nfds = 0;
 		pfds.fd = smppSocket;
 		pfds.events = POLLIN;
@@ -1076,8 +1122,9 @@
 			ast_log(LOG_ERROR, "Unable to start monitor thread for new connection from %s\n", 
 									ast_sockaddr_stringify_addr(&addr));
 		}
+
+		pthread_testcancel();
 		
-		pthread_testcancel();
 	}
 	if (smppDebug) {
 		ast_verb(2, "SMPP listener stop\n");




More information about the asterisk-commits mailing list