[asterisk-commits] file: branch file/netsock2 r96445 - in /team/file/netsock2: include/asterisk/...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jan 3 22:42:34 CST 2008


Author: file
Date: Thu Jan  3 22:42:33 2008
New Revision: 96445

URL: http://svn.digium.com/view/asterisk?view=rev&rev=96445
Log:
Add an ast_netsock2_disconnect API call for TCP and TLS transport sockets, and finish up the shutting down of the child socket thread.

Modified:
    team/file/netsock2/include/asterisk/netsock2.h
    team/file/netsock2/main/netsock2.c

Modified: team/file/netsock2/include/asterisk/netsock2.h
URL: http://svn.digium.com/view/asterisk/team/file/netsock2/include/asterisk/netsock2.h?view=diff&rev=96445&r1=96444&r2=96445
==============================================================================
--- team/file/netsock2/include/asterisk/netsock2.h (original)
+++ team/file/netsock2/include/asterisk/netsock2.h Thu Jan  3 22:42:33 2008
@@ -174,6 +174,12 @@
  */
 ssize_t ast_netsock2_writeto(struct ast_netsock2_socket *socket, void *buf, size_t len, struct ast_netsock2_addr *addr);
 
+/*! \brief Disconnect a socket
+ * \param socket Socket to disconnect
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_netsock2_disconnect(struct ast_netsock2_socket *socket);
+
 /*! \brief Get the transport type of a network socket
  * \param socket Socket to get the transport of
  * \return Returns transport

Modified: team/file/netsock2/main/netsock2.c
URL: http://svn.digium.com/view/asterisk/team/file/netsock2/main/netsock2.c?view=diff&rev=96445&r1=96444&r2=96445
==============================================================================
--- team/file/netsock2/main/netsock2.c (original)
+++ team/file/netsock2/main/netsock2.c Thu Jan  3 22:42:33 2008
@@ -147,6 +147,15 @@
 	}
 #endif
 
+	/* It is our responsibility to remove ourselves from the parent, nobody else will do it */
+	AST_LIST_REMOVE(&parent->child_sockets, child_socket, list);
+
+	/* Close out the actual socket */
+	close(child_socket->fd);
+
+	/* And as a last step free ourselves */
+	ast_free(child_socket);
+
 	return NULL;
 }
 
@@ -248,14 +257,14 @@
 		/* If this transport is capable of having child sockets drop those one firsts */
 		if (socket->transport == AST_NETSOCK2_TRANSPORT_TCP || socket->transport == AST_NETSOCK2_TRANSPORT_TLS) {
 			struct ast_netsock2_socket *child_socket = NULL;
-			while ((child_socket = AST_LIST_REMOVE_HEAD(&socket->child_sockets, list))) {
+			/* We essentially go through each child socket telling it to stop and waiting for it to do so */
+			AST_LIST_TRAVERSE_SAFE_BEGIN(&socket->child_sockets, child_socket, list) {
 				pthread_t child_thread = child_socket->thread;
 				child_socket->thread = AST_PTHREADT_STOP;
 				pthread_kill(child_thread, SIGURG);
-				ast_debug(1, "Stopping child socket thread for %p\n", child_socket);
 				pthread_join(child_thread, NULL);
-				ast_debug(1, "Child socket thread for %p has been stopped\n", child_socket);
 			}
+			AST_LIST_TRAVERSE_SAFE_END;
 		} else {
 			/* Other transports do not have their own thread so we can destroy them here */
 			close(socket->fd);
@@ -685,6 +694,29 @@
 	return res;
 }
 
+/*! \brief Disconnect a socket
+ * \param socket Socket to disconnect
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_netsock2_disconnect(struct ast_netsock2_socket *socket)
+{
+	pthread_t thread;
+
+	/* Make sure this is only called on TCP and TLS sockets */
+	if (socket->transport != AST_NETSOCK2_TRANSPORT_TCP && socket->transport != AST_NETSOCK2_TRANSPORT_TLS)
+		return -1;
+
+	/* This is relatively easy, we basically set the thread pointer on the socket to stop and it's thread stops */
+	thread = socket->thread;
+	socket->thread = AST_PTHREADT_STOP;
+
+	/* If we aren't the thread from above then poke it to wake up just in case */
+	if (!pthread_equal(thread, pthread_self()))
+		pthread_kill(thread, SIGURG);
+
+	return 0;
+}
+
 /*! \brief Get the transport type of a network socket
  * \param socket Socket to get the transport of
  * \return Returns transport




More information about the asterisk-commits mailing list