blob: 1996b351c8a9d84924f92214a2bdef676b7a07e2 [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;
Stefan Holmer9131efd2016-05-23 18:19:26 +020045 int Recv(void* buffer, size_t length, int64_t* timestamp) override;
46 int RecvFrom(void* buffer,
47 size_t length,
48 SocketAddress* out_addr,
49 int64_t* timestamp) override;
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000050 int Listen(int backlog) override;
51 MacAsyncSocket* Accept(SocketAddress* out_addr) override;
52 int Close() override;
53 int GetError() const override;
54 void SetError(int error) override;
55 ConnState GetState() const override;
Peter Boström0c4e06b2015-10-07 12:23:21 +020056 int EstimateMTU(uint16_t* mtu) override;
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000057 int GetOption(Option opt, int* value) override;
58 int SetOption(Option opt, int value) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059
60 // For the MacBaseSocketServer to disable callbacks when process_io is false.
61 void EnableCallbacks();
62 void DisableCallbacks();
63
64 protected:
65 void OnResolveResult(SignalThread* thread);
66 int DoConnect(const SocketAddress& addr);
67
68 private:
69 // Creates an async socket from an existing bsd socket
70 MacAsyncSocket(MacBaseSocketServer* ss, int family, int native_socket);
71
72 // Attaches the socket to the CFRunloop and sets the wrapped bsd socket
73 // to async mode
74 void Initialize(int family);
75
76 // Translate the SocketAddress into a CFDataRef to pass to CF socket
77 // functions. Caller must call CFRelease on the result when done.
78 static CFDataRef CopyCFAddress(const SocketAddress& address);
79
80 // Callback for the underlying CFSocketRef.
81 static void MacAsyncSocketCallBack(CFSocketRef s,
82 CFSocketCallBackType callbackType,
83 CFDataRef address,
84 const void* data,
85 void* info);
86
87 MacBaseSocketServer* ss_;
88 CFSocketRef socket_;
89 int native_socket_;
90 CFRunLoopSourceRef source_;
91 int current_callbacks_;
92 bool disabled_;
93 int error_;
94 ConnState state_;
95 AsyncResolver* resolver_;
96
henrikg3c089d72015-09-16 05:37:44 -070097 RTC_DISALLOW_COPY_AND_ASSIGN(MacAsyncSocket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000098};
99
100} // namespace rtc
101
102#endif // WEBRTC_BASE_MACASYNCSOCKET_H__