Back to index

texmacs  1.0.7.15
socket_notifier.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : socket_notifier.cpp
00004 * DESCRIPTION: Notifiers for socket activity
00005 * COPYRIGHT  : (C) 2009 Massimiliano Gubinelli
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "config.h"
00013 
00014 #ifndef __MINGW32__
00015 #include <unistd.h>
00016 #include <fcntl.h>
00017 #include <sys/wait.h>
00018 #include <sys/types.h>
00019 #include <sys/socket.h>
00020 #include <netinet/in.h>
00021 #include <netdb.h>
00022 #endif
00023 #include <errno.h>
00024 
00025 #ifdef QTTEXMACS
00026 #include "Qt/qt_gui.hpp"
00027 #endif
00028 
00029 #include "socket_notifier.hpp"
00030 #include "list.hpp"
00031 #include "iterator.hpp"
00032 
00033 static hashset<socket_notifier> notifiers;
00034 
00035 void
00036 add_notifier (socket_notifier sn)  {
00037   //cout << "enable notifier " << LF;
00038   notifiers->insert (sn);
00039 #ifdef QTTEXMACS
00040   the_gui->add_notifier (sn);
00041 #endif
00042 } 
00043 
00044 void
00045 remove_notifier (socket_notifier sn)  {
00046   //cout << "disable notifier " << LF;
00047   notifiers->remove (sn);
00048 #ifdef QTTEXMACS
00049   the_gui->remove_notifier (sn);
00050 #endif
00051 }
00052 
00053 void 
00054 perform_select () {
00055 #ifndef __MINGW32__
00056   //FIXME: this can be optimizied
00057   while (true) {
00058     fd_set rfds;
00059     FD_ZERO (&rfds);
00060     int max_fd= 0;
00061     iterator<socket_notifier> it = iterate (notifiers);
00062     while (it->busy ()) {
00063       socket_notifier sn= it->next ();
00064       FD_SET (sn->fd, &rfds);
00065       if (sn->fd >= max_fd) max_fd= sn->fd+1;
00066     }
00067     if (max_fd == 0) break;
00068     
00069     struct timeval tv;
00070     tv.tv_sec  = 0;
00071     tv.tv_usec = 0;
00072     int nr = select (max_fd, &rfds, NULL, NULL, &tv);
00073     if (nr==0) break;
00074     
00075     it = iterate (notifiers);
00076     while (it->busy ()) {
00077       socket_notifier sn=  it->next ();
00078       if (FD_ISSET (sn->fd, &rfds)) sn->notify ();
00079     }
00080   }  
00081 #endif  
00082 }