3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_BASE_PHYSICALSOCKETSERVER_H__
29 #define TALK_BASE_PHYSICALSOCKETSERVER_H__
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"
44 // Event constants for the Dispatcher class.
45 enum DispatcherEvent {
55 class PosixSignalDispatcher;
60 virtual ~Dispatcher() {}
61 virtual uint32 GetRequestedEvents() = 0;
62 virtual void OnPreEvent(uint32 ff) = 0;
63 virtual void OnEvent(uint32 ff, int err) = 0;
65 virtual WSAEVENT GetWSAEvent() = 0;
66 virtual SOCKET GetSocket() = 0;
67 virtual bool CheckSignalClose() = 0;
69 virtual int GetDescriptor() = 0;
70 virtual bool IsDescriptorClosed() = 0;
74 // A socket server that provides the real sockets of the underlying OS.
75 class PhysicalSocketServer : public SocketServer {
77 PhysicalSocketServer();
78 virtual ~PhysicalSocketServer();
81 virtual Socket* CreateSocket(int type);
82 virtual Socket* CreateSocket(int family, int type);
84 virtual AsyncSocket* CreateAsyncSocket(int type);
85 virtual AsyncSocket* CreateAsyncSocket(int family, int type);
87 // Internal Factory for Accept
88 AsyncSocket* WrapSocket(SOCKET s);
91 virtual bool Wait(int cms, bool process_io);
92 virtual void WakeUp();
94 void Add(Dispatcher* dispatcher);
95 void Remove(Dispatcher* dispatcher);
98 AsyncFile* CreateFile(int fd);
100 // Sets the function to be executed in response to the specified POSIX signal.
101 // The function is executed from inside Wait() using the "self-pipe trick"--
102 // regardless of which thread receives the signal--and hence can safely
103 // manipulate user-level data structures.
104 // "handler" may be SIG_IGN, SIG_DFL, or a user-specified function, just like
106 // Only one PhysicalSocketServer should have user-level signal handlers.
107 // Dispatching signals on multiple PhysicalSocketServers is not reliable.
108 // The signal mask is not modified. It is the caller's responsibily to
109 // maintain it as desired.
110 virtual bool SetPosixSignalHandler(int signum, void (*handler)(int));
113 Dispatcher* signal_dispatcher();
117 typedef std::vector<Dispatcher*> DispatcherList;
118 typedef std::vector<size_t*> IteratorList;
121 static bool InstallSignal(int signum, void (*handler)(int));
123 scoped_ptr<PosixSignalDispatcher> signal_dispatcher_;
125 DispatcherList dispatchers_;
126 IteratorList iterators_;
127 Signaler* signal_wakeup_;
128 CriticalSection crit_;
130 uint32 last_tick_tracked_;
131 int last_tick_dispatch_count_;
137 } // namespace talk_base
139 #endif // TALK_BASE_PHYSICALSOCKETSERVER_H__