blob: 12d2addafe9fde3d18d6ac3e1e7e5b5d6e57ac60 [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001// Copyright 2008 Google Inc. All Rights Reserved.
2
3//
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
8// this socket.
9
10#ifndef TALK_BASE_MACASYNCSOCKET_H__
11#define TALK_BASE_MACASYNCSOCKET_H__
12
13#include <CoreFoundation/CoreFoundation.h>
14
15#include "talk/base/asyncsocket.h"
16#include "talk/base/nethelpers.h"
17
18namespace talk_base {
19
20class MacBaseSocketServer;
21
22class MacAsyncSocket : public AsyncSocket, public sigslot::has_slots<> {
23 public:
24 MacAsyncSocket(MacBaseSocketServer* ss, int family);
25 virtual ~MacAsyncSocket();
26
27 bool valid() const { return source_ != NULL; }
28
29 // Socket interface
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);
41 virtual int Close();
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);
48
49 // For the MacBaseSocketServer to disable callbacks when process_io is false.
50 void EnableCallbacks();
51 void DisableCallbacks();
52
53 protected:
54 void OnResolveResult(SignalThread* thread);
55 int DoConnect(const SocketAddress& addr);
56
57 private:
58 // Creates an async socket from an existing bsd socket
59 MacAsyncSocket(MacBaseSocketServer* ss, int family, int native_socket);
60
61 // Attaches the socket to the CFRunloop and sets the wrapped bsd socket
62 // to async mode
63 void Initialize(int family);
64
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);
68
69 // Callback for the underlying CFSocketRef.
70 static void MacAsyncSocketCallBack(CFSocketRef s,
71 CFSocketCallBackType callbackType,
72 CFDataRef address,
73 const void* data,
74 void* info);
75
76 MacBaseSocketServer* ss_;
77 CFSocketRef socket_;
78 int native_socket_;
79 CFRunLoopSourceRef source_;
80 int current_callbacks_;
81 bool disabled_;
82 int error_;
83 ConnState state_;
84 AsyncResolver* resolver_;
85
86 DISALLOW_EVIL_CONSTRUCTORS(MacAsyncSocket);
87};
88
89} // namespace talk_base
90
91#endif // TALK_BASE_MACASYNCSOCKET_H__