Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_BASE_PHYSICALSOCKETSERVER_H__
     12 #define WEBRTC_BASE_PHYSICALSOCKETSERVER_H__
     13 
     14 #include <vector>
     15 
     16 #include "webrtc/base/asyncfile.h"
     17 #include "webrtc/base/scoped_ptr.h"
     18 #include "webrtc/base/socketserver.h"
     19 #include "webrtc/base/criticalsection.h"
     20 
     21 #if defined(WEBRTC_POSIX)
     22 typedef int SOCKET;
     23 #endif // WEBRTC_POSIX
     24 
     25 namespace rtc {
     26 
     27 // Event constants for the Dispatcher class.
     28 enum DispatcherEvent {
     29   DE_READ    = 0x0001,
     30   DE_WRITE   = 0x0002,
     31   DE_CONNECT = 0x0004,
     32   DE_CLOSE   = 0x0008,
     33   DE_ACCEPT  = 0x0010,
     34 };
     35 
     36 class Signaler;
     37 #if defined(WEBRTC_POSIX)
     38 class PosixSignalDispatcher;
     39 #endif
     40 
     41 class Dispatcher {
     42  public:
     43   virtual ~Dispatcher() {}
     44   virtual uint32 GetRequestedEvents() = 0;
     45   virtual void OnPreEvent(uint32 ff) = 0;
     46   virtual void OnEvent(uint32 ff, int err) = 0;
     47 #if defined(WEBRTC_WIN)
     48   virtual WSAEVENT GetWSAEvent() = 0;
     49   virtual SOCKET GetSocket() = 0;
     50   virtual bool CheckSignalClose() = 0;
     51 #elif defined(WEBRTC_POSIX)
     52   virtual int GetDescriptor() = 0;
     53   virtual bool IsDescriptorClosed() = 0;
     54 #endif
     55 };
     56 
     57 // A socket server that provides the real sockets of the underlying OS.
     58 class PhysicalSocketServer : public SocketServer {
     59  public:
     60   PhysicalSocketServer();
     61   virtual ~PhysicalSocketServer();
     62 
     63   // SocketFactory:
     64   virtual Socket* CreateSocket(int type);
     65   virtual Socket* CreateSocket(int family, int type);
     66 
     67   virtual AsyncSocket* CreateAsyncSocket(int type);
     68   virtual AsyncSocket* CreateAsyncSocket(int family, int type);
     69 
     70   // Internal Factory for Accept
     71   AsyncSocket* WrapSocket(SOCKET s);
     72 
     73   // SocketServer:
     74   virtual bool Wait(int cms, bool process_io);
     75   virtual void WakeUp();
     76 
     77   void Add(Dispatcher* dispatcher);
     78   void Remove(Dispatcher* dispatcher);
     79 
     80 #if defined(WEBRTC_POSIX)
     81   AsyncFile* CreateFile(int fd);
     82 
     83   // Sets the function to be executed in response to the specified POSIX signal.
     84   // The function is executed from inside Wait() using the "self-pipe trick"--
     85   // regardless of which thread receives the signal--and hence can safely
     86   // manipulate user-level data structures.
     87   // "handler" may be SIG_IGN, SIG_DFL, or a user-specified function, just like
     88   // with signal(2).
     89   // Only one PhysicalSocketServer should have user-level signal handlers.
     90   // Dispatching signals on multiple PhysicalSocketServers is not reliable.
     91   // The signal mask is not modified. It is the caller's responsibily to
     92   // maintain it as desired.
     93   virtual bool SetPosixSignalHandler(int signum, void (*handler)(int));
     94 
     95  protected:
     96   Dispatcher* signal_dispatcher();
     97 #endif
     98 
     99  private:
    100   typedef std::vector<Dispatcher*> DispatcherList;
    101   typedef std::vector<size_t*> IteratorList;
    102 
    103 #if defined(WEBRTC_POSIX)
    104   static bool InstallSignal(int signum, void (*handler)(int));
    105 
    106   scoped_ptr<PosixSignalDispatcher> signal_dispatcher_;
    107 #endif
    108   DispatcherList dispatchers_;
    109   IteratorList iterators_;
    110   Signaler* signal_wakeup_;
    111   CriticalSection crit_;
    112   bool fWait_;
    113 #if defined(WEBRTC_WIN)
    114   WSAEVENT socket_ev_;
    115 #endif
    116 };
    117 
    118 } // namespace rtc
    119 
    120 #endif // WEBRTC_BASE_PHYSICALSOCKETSERVER_H__
    121