1 /* 2 * libjingle 3 * Copyright 2004--2005, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef TALK_BASE_PHYSICALSOCKETSERVER_H__ 29 #define TALK_BASE_PHYSICALSOCKETSERVER_H__ 30 31 #include <vector> 32 33 #include "talk/base/asyncfile.h" 34 #include "talk/base/scoped_ptr.h" 35 #include "talk/base/socketserver.h" 36 #include "talk/base/criticalsection.h" 37 38 #ifdef POSIX 39 typedef int SOCKET; 40 #endif // POSIX 41 42 namespace talk_base { 43 44 // Event constants for the Dispatcher class. 45 enum DispatcherEvent { 46 DE_READ = 0x0001, 47 DE_WRITE = 0x0002, 48 DE_CONNECT = 0x0004, 49 DE_CLOSE = 0x0008, 50 DE_ACCEPT = 0x0010, 51 }; 52 53 class Signaler; 54 #ifdef POSIX 55 class PosixSignalDispatcher; 56 #endif 57 58 class Dispatcher { 59 public: 60 virtual ~Dispatcher() {} 61 virtual uint32 GetRequestedEvents() = 0; 62 virtual void OnPreEvent(uint32 ff) = 0; 63 virtual void OnEvent(uint32 ff, int err) = 0; 64 #ifdef WIN32 65 virtual WSAEVENT GetWSAEvent() = 0; 66 virtual SOCKET GetSocket() = 0; 67 virtual bool CheckSignalClose() = 0; 68 #elif POSIX 69 virtual int GetDescriptor() = 0; 70 virtual bool IsDescriptorClosed() = 0; 71 #endif 72 }; 73 74 // A socket server that provides the real sockets of the underlying OS. 75 class PhysicalSocketServer : public SocketServer { 76 public: 77 PhysicalSocketServer(); 78 virtual ~PhysicalSocketServer(); 79 80 // SocketFactory: 81 virtual Socket* CreateSocket(int type); 82 virtual AsyncSocket* CreateAsyncSocket(int type); 83 84 // Internal Factory for Accept 85 AsyncSocket* WrapSocket(SOCKET s); 86 87 // SocketServer: 88 virtual bool Wait(int cms, bool process_io); 89 virtual void WakeUp(); 90 91 void Add(Dispatcher* dispatcher); 92 void Remove(Dispatcher* dispatcher); 93 94 #ifdef POSIX 95 AsyncFile* CreateFile(int fd); 96 97 // Sets the function to be executed in response to the specified POSIX signal. 98 // The function is executed from inside Wait() using the "self-pipe trick"-- 99 // regardless of which thread receives the signal--and hence can safely 100 // manipulate user-level data structures. 101 // "handler" may be SIG_IGN, SIG_DFL, or a user-specified function, just like 102 // with signal(2). 103 // Only one PhysicalSocketServer should have user-level signal handlers. 104 // Dispatching signals on multiple PhysicalSocketServers is not reliable. 105 // The signal mask is not modified. It is the caller's responsibily to 106 // maintain it as desired. 107 bool SetPosixSignalHandler(int signum, void (*handler)(int)); 108 #endif 109 110 private: 111 typedef std::vector<Dispatcher*> DispatcherList; 112 typedef std::vector<size_t*> IteratorList; 113 114 #ifdef POSIX 115 static bool InstallSignal(int signum, void (*handler)(int)); 116 117 scoped_ptr<PosixSignalDispatcher> signal_dispatcher_; 118 #endif 119 DispatcherList dispatchers_; 120 IteratorList iterators_; 121 Signaler* signal_wakeup_; 122 CriticalSection crit_; 123 bool fWait_; 124 uint32 last_tick_tracked_; 125 int last_tick_dispatch_count_; 126 #ifdef WIN32 127 WSAEVENT socket_ev_; 128 #endif 129 }; 130 131 } // namespace talk_base 132 133 #endif // TALK_BASE_PHYSICALSOCKETSERVER_H__ 134