1 // Copyright 2008 Google Inc. All Rights Reserved.
4 // MacAsyncSocket is a kind of AsyncSocket. It only creates sockets
5 // of the TCP type, and does not (yet) support listen and accept. It works
6 // asynchronously, which means that users of this socket should connect to
7 // the various events declared in asyncsocket.h to receive notifications about
10 #ifndef TALK_BASE_MACASYNCSOCKET_H__
11 #define TALK_BASE_MACASYNCSOCKET_H__
13 #include <CoreFoundation/CoreFoundation.h>
15 #include "talk/base/asyncsocket.h"
16 #include "talk/base/nethelpers.h"
20 class MacBaseSocketServer;
22 class MacAsyncSocket : public AsyncSocket, public sigslot::has_slots<> {
24 MacAsyncSocket(MacBaseSocketServer* ss, int family);
25 virtual ~MacAsyncSocket();
27 bool valid() const { return source_ != NULL; }
30 virtual SocketAddress GetLocalAddress() const;
31 virtual SocketAddress GetRemoteAddress() const;
32 virtual int Bind(const SocketAddress& addr);
33 virtual int Connect(const SocketAddress& addr);
34 virtual int Send(const void* buffer, size_t length);
35 virtual int SendTo(const void* buffer, size_t length,
36 const SocketAddress& addr);
37 virtual int Recv(void* buffer, size_t length);
38 virtual int RecvFrom(void* buffer, size_t length, SocketAddress* out_addr);
39 virtual int Listen(int backlog);
40 virtual MacAsyncSocket* Accept(SocketAddress* out_addr);
42 virtual int GetError() const;
43 virtual void SetError(int error);
44 virtual ConnState GetState() const;
45 virtual int EstimateMTU(uint16* mtu);
46 virtual int GetOption(Option opt, int* value);
47 virtual int SetOption(Option opt, int value);
49 // For the MacBaseSocketServer to disable callbacks when process_io is false.
50 void EnableCallbacks();
51 void DisableCallbacks();
54 void OnResolveResult(SignalThread* thread);
55 int DoConnect(const SocketAddress& addr);
58 // Creates an async socket from an existing bsd socket
59 MacAsyncSocket(MacBaseSocketServer* ss, int family, int native_socket);
61 // Attaches the socket to the CFRunloop and sets the wrapped bsd socket
63 void Initialize(int family);
65 // Translate the SocketAddress into a CFDataRef to pass to CF socket
66 // functions. Caller must call CFRelease on the result when done.
67 static CFDataRef CopyCFAddress(const SocketAddress& address);
69 // Callback for the underlying CFSocketRef.
70 static void MacAsyncSocketCallBack(CFSocketRef s,
71 CFSocketCallBackType callbackType,
76 MacBaseSocketServer* ss_;
79 CFRunLoopSourceRef source_;
80 int current_callbacks_;
84 AsyncResolver* resolver_;
86 DISALLOW_EVIL_CONSTRUCTORS(MacAsyncSocket);
89 } // namespace talk_base
91 #endif // TALK_BASE_MACASYNCSOCKET_H__