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