- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / physicalsocketserver.h
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 Socket* CreateSocket(int family, int type);
83
84   virtual AsyncSocket* CreateAsyncSocket(int type);
85   virtual AsyncSocket* CreateAsyncSocket(int family, int type);
86
87   // Internal Factory for Accept
88   AsyncSocket* WrapSocket(SOCKET s);
89
90   // SocketServer:
91   virtual bool Wait(int cms, bool process_io);
92   virtual void WakeUp();
93
94   void Add(Dispatcher* dispatcher);
95   void Remove(Dispatcher* dispatcher);
96
97 #ifdef POSIX
98   AsyncFile* CreateFile(int fd);
99
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
105   // with signal(2).
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));
111
112  protected:
113   Dispatcher* signal_dispatcher();
114 #endif
115
116  private:
117   typedef std::vector<Dispatcher*> DispatcherList;
118   typedef std::vector<size_t*> IteratorList;
119
120 #ifdef POSIX
121   static bool InstallSignal(int signum, void (*handler)(int));
122
123   scoped_ptr<PosixSignalDispatcher> signal_dispatcher_;
124 #endif
125   DispatcherList dispatchers_;
126   IteratorList iterators_;
127   Signaler* signal_wakeup_;
128   CriticalSection crit_;
129   bool fWait_;
130   uint32 last_tick_tracked_;
131   int last_tick_dispatch_count_;
132 #ifdef WIN32
133   WSAEVENT socket_ev_;
134 #endif
135 };
136
137 } // namespace talk_base
138
139 #endif // TALK_BASE_PHYSICALSOCKETSERVER_H__