blob: 735147443aad157da56ab687d931ce4a83a5cbf8 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
24namespace rtc {
25
26class MacBaseSocketServer;
27
28class MacAsyncSocket : public AsyncSocket, public sigslot::has_slots<> {
29 public:
30 MacAsyncSocket(MacBaseSocketServer* ss, int family);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000031 ~MacAsyncSocket() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032
33 bool valid() const { return source_ != NULL; }
34
35 // Socket interface
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000036 SocketAddress GetLocalAddress() const override;
37 SocketAddress GetRemoteAddress() const override;
38 int Bind(const SocketAddress& addr) override;
39 int Connect(const SocketAddress& addr) override;
40 int Send(const void* buffer, size_t length) override;
41 int SendTo(const void* buffer,
42 size_t length,
43 const SocketAddress& addr) override;
44 int Recv(void* buffer, size_t length) override;
45 int RecvFrom(void* buffer, size_t length, SocketAddress* out_addr) override;
46 int Listen(int backlog) override;
47 MacAsyncSocket* Accept(SocketAddress* out_addr) override;
48 int Close() override;
49 int GetError() const override;
50 void SetError(int error) override;
51 ConnState GetState() const override;
52 int EstimateMTU(uint16* mtu) override;
53 int GetOption(Option opt, int* value) override;
54 int SetOption(Option opt, int value) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055
56 // For the MacBaseSocketServer to disable callbacks when process_io is false.
57 void EnableCallbacks();
58 void DisableCallbacks();
59
60 protected:
61 void OnResolveResult(SignalThread* thread);
62 int DoConnect(const SocketAddress& addr);
63
64 private:
65 // Creates an async socket from an existing bsd socket
66 MacAsyncSocket(MacBaseSocketServer* ss, int family, int native_socket);
67
68 // Attaches the socket to the CFRunloop and sets the wrapped bsd socket
69 // to async mode
70 void Initialize(int family);
71
72 // Translate the SocketAddress into a CFDataRef to pass to CF socket
73 // functions. Caller must call CFRelease on the result when done.
74 static CFDataRef CopyCFAddress(const SocketAddress& address);
75
76 // Callback for the underlying CFSocketRef.
77 static void MacAsyncSocketCallBack(CFSocketRef s,
78 CFSocketCallBackType callbackType,
79 CFDataRef address,
80 const void* data,
81 void* info);
82
83 MacBaseSocketServer* ss_;
84 CFSocketRef socket_;
85 int native_socket_;
86 CFRunLoopSourceRef source_;
87 int current_callbacks_;
88 bool disabled_;
89 int error_;
90 ConnState state_;
91 AsyncResolver* resolver_;
92
henrikg3c089d72015-09-16 05:37:44 -070093 RTC_DISALLOW_COPY_AND_ASSIGN(MacAsyncSocket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094};
95
96} // namespace rtc
97
98#endif // WEBRTC_BASE_MACASYNCSOCKET_H__