Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2008 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 // MacAsyncSocket is a kind of AsyncSocket. It only creates sockets
     11 // of the TCP type, and does not (yet) support listen and accept. It works
     12 // asynchronously, which means that users of this socket should connect to
     13 // the various events declared in asyncsocket.h to receive notifications about
     14 // this socket.
     15 
     16 #ifndef WEBRTC_BASE_MACASYNCSOCKET_H__
     17 #define WEBRTC_BASE_MACASYNCSOCKET_H__
     18 
     19 #include <CoreFoundation/CoreFoundation.h>
     20 
     21 #include "webrtc/base/asyncsocket.h"
     22 #include "webrtc/base/nethelpers.h"
     23 
     24 namespace rtc {
     25 
     26 class MacBaseSocketServer;
     27 
     28 class MacAsyncSocket : public AsyncSocket, public sigslot::has_slots<> {
     29  public:
     30   MacAsyncSocket(MacBaseSocketServer* ss, int family);
     31   virtual ~MacAsyncSocket();
     32 
     33   bool valid() const { return source_ != NULL; }
     34 
     35   // Socket interface
     36   virtual SocketAddress GetLocalAddress() const;
     37   virtual SocketAddress GetRemoteAddress() const;
     38   virtual int Bind(const SocketAddress& addr);
     39   virtual int Connect(const SocketAddress& addr);
     40   virtual int Send(const void* buffer, size_t length);
     41   virtual int SendTo(const void* buffer, size_t length,
     42                      const SocketAddress& addr);
     43   virtual int Recv(void* buffer, size_t length);
     44   virtual int RecvFrom(void* buffer, size_t length, SocketAddress* out_addr);
     45   virtual int Listen(int backlog);
     46   virtual MacAsyncSocket* Accept(SocketAddress* out_addr);
     47   virtual int Close();
     48   virtual int GetError() const;
     49   virtual void SetError(int error);
     50   virtual ConnState GetState() const;
     51   virtual int EstimateMTU(uint16* mtu);
     52   virtual int GetOption(Option opt, int* value);
     53   virtual int SetOption(Option opt, int value);
     54 
     55   // For the MacBaseSocketServer to disable callbacks when process_io is false.
     56   void EnableCallbacks();
     57   void DisableCallbacks();
     58 
     59  protected:
     60   void OnResolveResult(SignalThread* thread);
     61   int DoConnect(const SocketAddress& addr);
     62 
     63  private:
     64   // Creates an async socket from an existing bsd socket
     65   MacAsyncSocket(MacBaseSocketServer* ss, int family, int native_socket);
     66 
     67    // Attaches the socket to the CFRunloop and sets the wrapped bsd socket
     68   // to async mode
     69   void Initialize(int family);
     70 
     71   // Translate the SocketAddress into a CFDataRef to pass to CF socket
     72   // functions. Caller must call CFRelease on the result when done.
     73   static CFDataRef CopyCFAddress(const SocketAddress& address);
     74 
     75   // Callback for the underlying CFSocketRef.
     76   static void MacAsyncSocketCallBack(CFSocketRef s,
     77                                      CFSocketCallBackType callbackType,
     78                                      CFDataRef address,
     79                                      const void* data,
     80                                      void* info);
     81 
     82   MacBaseSocketServer* ss_;
     83   CFSocketRef socket_;
     84   int native_socket_;
     85   CFRunLoopSourceRef source_;
     86   int current_callbacks_;
     87   bool disabled_;
     88   int error_;
     89   ConnState state_;
     90   AsyncResolver* resolver_;
     91 
     92   DISALLOW_EVIL_CONSTRUCTORS(MacAsyncSocket);
     93 };
     94 
     95 }  // namespace rtc
     96 
     97 #endif  // WEBRTC_BASE_MACASYNCSOCKET_H__
     98