![]() |
libzmq master
The Intelligent Transport Layer
|
00001 /* 00002 Copyright (c) 2009-2011 250bpm s.r.o. 00003 Copyright (c) 2007-2009 iMatix Corporation 00004 Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 00005 00006 This file is part of 0MQ. 00007 00008 0MQ is free software; you can redistribute it and/or modify it under 00009 the terms of the GNU Lesser General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 0MQ is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public License 00019 along with this program. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 00022 #ifndef __ZMQ_SELECT_HPP_INCLUDED__ 00023 #define __ZMQ_SELECT_HPP_INCLUDED__ 00024 00025 // poller.hpp decides which polling mechanism to use. 00026 #include "poller.hpp" 00027 #if defined ZMQ_USE_SELECT 00028 00029 #include "platform.hpp" 00030 00031 #include <stddef.h> 00032 #include <vector> 00033 00034 #ifdef ZMQ_HAVE_WINDOWS 00035 #include "winsock2.h" 00036 #elif defined ZMQ_HAVE_OPENVMS 00037 #include <sys/types.h> 00038 #include <sys/time.h> 00039 #else 00040 #include <sys/select.h> 00041 #endif 00042 00043 #include "fd.hpp" 00044 #include "thread.hpp" 00045 #include "poller_base.hpp" 00046 00047 namespace zmq 00048 { 00049 00050 // Implements socket polling mechanism using POSIX.1-2001 select() 00051 // function. 00052 00053 class select_t : public poller_base_t 00054 { 00055 public: 00056 00057 typedef fd_t handle_t; 00058 00059 select_t (); 00060 ~select_t (); 00061 00062 // "poller" concept. 00063 handle_t add_fd (fd_t fd_, struct i_poll_events *events_); 00064 void rm_fd (handle_t handle_); 00065 void set_pollin (handle_t handle_); 00066 void reset_pollin (handle_t handle_); 00067 void set_pollout (handle_t handle_); 00068 void reset_pollout (handle_t handle_); 00069 void start (); 00070 void stop (); 00071 00072 private: 00073 00074 // Main worker thread routine. 00075 static void worker_routine (void *arg_); 00076 00077 // Main event loop. 00078 void loop (); 00079 00080 struct fd_entry_t 00081 { 00082 fd_t fd; 00083 struct i_poll_events *events; 00084 }; 00085 00086 // Checks if an fd_entry_t is retired. 00087 static bool is_retired_fd (const fd_entry_t &entry); 00088 00089 // Set of file descriptors that are used to retreive 00090 // information for fd_set. 00091 typedef std::vector <fd_entry_t> fd_set_t; 00092 fd_set_t fds; 00093 00094 fd_set source_set_in; 00095 fd_set source_set_out; 00096 fd_set source_set_err; 00097 00098 fd_set readfds; 00099 fd_set writefds; 00100 fd_set exceptfds; 00101 00102 // Maximum file descriptor. 00103 fd_t maxfd; 00104 00105 // If true, at least one file descriptor has retired. 00106 bool retired; 00107 00108 // If true, thread is shutting down. 00109 bool stopping; 00110 00111 // Handle of the physical thread doing the I/O work. 00112 thread_t worker; 00113 00114 select_t (const select_t&); 00115 const select_t &operator = (const select_t&); 00116 }; 00117 00118 typedef select_t poller_t; 00119 00120 } 00121 00122 #endif 00123 00124 #endif