]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
make multicast event handler nonblocking
authorAnthony Minessale <anthony.minessale@gmail.com>
Fri, 15 Feb 2008 21:16:39 +0000 (21:16 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Fri, 15 Feb 2008 21:16:39 +0000 (21:16 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@7633 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/mod/event_handlers/mod_event_multicast/mod_event_multicast.c

index 14669230c19523756ad8d5859f0fb29c41d478cc..d639d177b071dfd83d5d82348ab1e8fb75bcbf0c 100644 (file)
@@ -237,6 +237,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_event_multicast_load)
                return SWITCH_STATUS_GENERR;
        }
 
+       switch_socket_opt_set(globals.udp_socket, SWITCH_SO_NONBLOCK, TRUE);
 
        /* indicate that the module should continue to be loaded */
        return SWITCH_STATUS_SUCCESS;
@@ -247,10 +248,6 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_event_multicast_shutdown)
 {
        int x = 0;
 
-       if (globals.udp_socket) {
-               switch_socket_shutdown(globals.udp_socket, SWITCH_SHUTDOWN_READWRITE);
-       }
-
        if (globals.running == 1) {
                globals.running = -1;
                while (x < 100000 && globals.running) {
@@ -282,53 +279,62 @@ SWITCH_MODULE_RUNTIME_FUNCTION(mod_event_multicast_runtime)
        while (globals.running == 1) {
                char *myaddr;
                size_t len = MULTICAST_BUFFSIZE;
+               char *packet;
+               uint64_t host_hash = 0;
+               switch_status_t status;
                memset(buf, 0, len);
-
+               
                switch_sockaddr_ip_get(&myaddr, globals.addr);
+               status = switch_socket_recvfrom(addr, globals.udp_socket, 0, buf, &len);
+               
+               if (!len) {
+                       if (SWITCH_STATUS_IS_BREAK(status)) {
+                               switch_yield(100000);
+                               continue;
+                       }
 
-               if (switch_socket_recvfrom(addr, globals.udp_socket, 0, buf, &len) == SWITCH_STATUS_SUCCESS) {
-                       char *packet;
-                       uint64_t host_hash = 0;
+                       break;
+               }
 
-                       memcpy(&host_hash, buf, sizeof(host_hash));
-                       packet = buf + sizeof(host_hash);
+               memcpy(&host_hash, buf, sizeof(host_hash));
+               packet = buf + sizeof(host_hash);
 
-                       if (host_hash == globals.host_hash) {
-                               continue;
-                       }
-                       //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\nEVENT %d\n--------------------------------\n%s\n", (int) len, packet);
-                       if (switch_event_create_subclass(&local_event, SWITCH_EVENT_CUSTOM, MULTICAST_EVENT) == SWITCH_STATUS_SUCCESS) {
-                               char *var, *val, *term = NULL, tmpname[128];
-                               switch_event_add_header(local_event, SWITCH_STACK_BOTTOM, "Multicast", "yes");
-                               var = packet;
-                               while (*var) {
-                                       if ((val = strchr(var, ':')) != 0) {
-                                               *val++ = '\0';
-                                               while (*val == ' ') {
-                                                       val++;
-                                               }
-                                               if ((term = strchr(val, '\r')) != 0 || (term = strchr(val, '\n')) != 0) {
-                                                       *term = '\0';
-                                                       while (*term == '\r' || *term == '\n') {
-                                                               term++;
-                                                       }
+               if (host_hash == globals.host_hash) {
+                       continue;
+               }
+               //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\nEVENT %d\n--------------------------------\n%s\n", (int) len, packet);
+               if (switch_event_create_subclass(&local_event, SWITCH_EVENT_CUSTOM, MULTICAST_EVENT) == SWITCH_STATUS_SUCCESS) {
+                       char *var, *val, *term = NULL, tmpname[128];
+                       switch_event_add_header(local_event, SWITCH_STACK_BOTTOM, "Multicast", "yes");
+                       var = packet;
+                       while (*var) {
+                               if ((val = strchr(var, ':')) != 0) {
+                                       *val++ = '\0';
+                                       while (*val == ' ') {
+                                               val++;
+                                       }
+                                       if ((term = strchr(val, '\r')) != 0 || (term = strchr(val, '\n')) != 0) {
+                                               *term = '\0';
+                                               while (*term == '\r' || *term == '\n') {
+                                                       term++;
                                                }
-                                               switch_snprintf(tmpname, sizeof(tmpname), "Orig-%s", var);
-                                               switch_event_add_header(local_event, SWITCH_STACK_BOTTOM, tmpname, "%s", val);
-                                               var = term + 1;
-                                       } else {
-                                               break;
                                        }
+                                       switch_snprintf(tmpname, sizeof(tmpname), "Orig-%s", var);
+                                       switch_event_add_header(local_event, SWITCH_STACK_BOTTOM, tmpname, "%s", val);
+                                       var = term + 1;
+                               } else {
+                                       break;
                                }
+                       }
 
-                               if (var && strlen(var) > 1) {
-                                       switch_event_add_body(local_event, var);
-                               }
+                       if (var && strlen(var) > 1) {
+                               switch_event_add_body(local_event, var);
+                       }
 
-                               switch_event_fire(&local_event);
+                       switch_event_fire(&local_event);
 
-                       }
                }
+               
        }
 
        globals.running = 0;