Debian Codegraph @

src/core/lib/iomgr/tcp_client_cfstream.cc

raw
1
2 /*
3 *
4 * Copyright 2018 gRPC authors.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 #include <grpc/support/port_platform.h>
21
22 #include "src/core/lib/iomgr/port.h"
23
24 #ifdef GRPC_CFSTREAM_CLIENT
25
26 #include <CoreFoundation/CoreFoundation.h>
27 #include <netinet/in.h>
28 #include <string.h>
29
30 #include <grpc/event_engine/endpoint_config.h>
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/sync.h>
34
35 #include "src/core/lib/address_utils/sockaddr_utils.h"
36 #include "src/core/lib/gprpp/host_port.h"
37 #include "src/core/lib/iomgr/cfstream_handle.h"
38 #include "src/core/lib/iomgr/closure.h"
39 #include "src/core/lib/iomgr/endpoint_cfstream.h"
40 #include "src/core/lib/iomgr/error.h"
41 #include "src/core/lib/iomgr/error_cfstream.h"
42 #include "src/core/lib/iomgr/tcp_client.h"
43 #include "src/core/lib/iomgr/timer.h"
44
45 extern grpc_core::TraceFlag grpc_tcp_trace;
46
47 struct CFStreamConnect {
48 gpr_mu mu;
49 gpr_refcount refcount;
50
51 CFReadStreamRef read_stream;
52 CFWriteStreamRef write_stream;
53 CFStreamHandle* stream_handle;
54
55 grpc_timer alarm;
56 grpc_closure on_alarm;
57 grpc_closure on_open;
58
59 bool read_stream_open;
60 bool write_stream_open;
61 bool failed;
62
63 grpc_closure* closure;
64 grpc_endpoint** endpoint;
65 int refs;
66 std::string addr_name;
67 };
68
69 static void CFStreamConnectCleanup(CFStreamConnect* connect) {
70 CFSTREAM_HANDLE_UNREF(connect->stream_handle, "async connect clean up");
71 CFRelease(connect->read_stream);
72 CFRelease(connect->write_stream);
73 gpr_mu_destroy(&connect->mu);
74 delete connect;
75 }
76
77 static void OnAlarm(void* arg, grpc_error_handle error) {
78 CFStreamConnect* connect = static_cast<CFStreamConnect*>(arg);
79 if (grpc_tcp_trace.enabled()) {
80 gpr_log(GPR_DEBUG, "CLIENT_CONNECT :%p OnAlarm, error:%s", connect,
81 grpc_core::StatusToString(error).c_str());
82 }
83 gpr_mu_lock(&connect->mu);
84 grpc_closure* closure = connect->closure;
85 connect->closure = nil;
86 const bool done = (--connect->refs == 0);
87 gpr_mu_unlock(&connect->mu);
88 // Only schedule a callback once, by either OnAlarm or OnOpen. The
89 // first one issues callback while the second one does cleanup.
90 if (done) {
91 CFStreamConnectCleanup(connect);
92 } else {
93 grpc_error_handle error = GRPC_ERROR_CREATE("connect() timed out");
94 grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, error);
95 }
96 }
97
98 static void OnOpen(void* arg, grpc_error_handle error) {
99 CFStreamConnect* connect = static_cast<CFStreamConnect*>(arg);
100 if (grpc_tcp_trace.enabled()) {
101 gpr_log(GPR_DEBUG, "CLIENT_CONNECT :%p OnOpen, error:%s", connect,
102 grpc_core::StatusToString(error).c_str());
103 }
104 gpr_mu_lock(&connect->mu);
105 grpc_timer_cancel(&connect->alarm);
106 grpc_closure* closure = connect->closure;
107 connect->closure = nil;
108
109 bool done = (--connect->refs == 0);
110 grpc_endpoint** endpoint = connect->endpoint;
111
112 // Only schedule a callback once, by either OnAlarm or OnOpen. The
113 // first one issues callback while the second one does cleanup.
114 if (done) {
115 gpr_mu_unlock(&connect->mu);
116 CFStreamConnectCleanup(connect);
117 } else {
118 if (error.ok()) {
119 CFErrorRef stream_error = CFReadStreamCopyError(connect->read_stream);
120 if (stream_error == NULL) {
121 stream_error = CFWriteStreamCopyError(connect->write_stream);
122 }
123 if (stream_error) {
124 error = GRPC_ERROR_CREATE_FROM_CFERROR(stream_error, "connect() error");
125 CFRelease(stream_error);
126 }
127 if (error.ok()) {
128 *endpoint = grpc_cfstream_endpoint_create(
129 connect->read_stream, connect->write_stream,
130 connect->addr_name.c_str(), connect->stream_handle);
131 }
132 }
133 gpr_mu_unlock(&connect->mu);
134 grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, error);
135 }
136 }
137
138 static void ParseResolvedAddress(const grpc_resolved_address* addr,
139 CFStringRef* host, int* port) {
140 std::string host_port = grpc_sockaddr_to_string(addr, true).value();
141 std::string host_string;
142 std::string port_string;
143 grpc_core::SplitHostPort(host_port, &host_string, &port_string);
144 *host = CFStringCreateWithCString(NULL, host_string.c_str(),
145 kCFStringEncodingUTF8);
146 *port = grpc_sockaddr_get_port(addr);
147 }
148
149 static int64_t CFStreamClientConnect(
150 grpc_closure* closure, grpc_endpoint** ep,
151 grpc_pollset_set* interested_parties,
152 const grpc_event_engine::experimental::EndpointConfig& /*config*/,
153 const grpc_resolved_address* resolved_addr, grpc_core::Timestamp deadline) {
154 auto addr_uri = grpc_sockaddr_to_uri(resolved_addr);
155 if (!addr_uri.ok()) {
156 grpc_error_handle error = GRPC_ERROR_CREATE(addr_uri.status().ToString());
157 grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, error);
158 return 0;
159 }
160
161 CFStreamConnect* connect = new CFStreamConnect();
162 connect->closure = closure;
163 connect->endpoint = ep;
164 connect->addr_name = addr_uri.value();
165 connect->refs = 2; // One for the connect operation, one for the timer.
166 gpr_ref_init(&connect->refcount, 1);
167 gpr_mu_init(&connect->mu);
168
169 if (grpc_tcp_trace.enabled()) {
170 gpr_log(GPR_DEBUG, "CLIENT_CONNECT: %p, %s: asynchronously connecting",
171 connect, connect->addr_name.c_str());
172 }
173
174 CFReadStreamRef read_stream;
175 CFWriteStreamRef write_stream;
176
177 CFStringRef host;
178 int port;
179 ParseResolvedAddress(resolved_addr, &host, &port);
180 CFStreamCreatePairWithSocketToHost(NULL, host, port, &read_stream,
181 &write_stream);
182 CFRelease(host);
183 connect->read_stream = read_stream;
184 connect->write_stream = write_stream;
185 connect->stream_handle =
186 CFStreamHandle::CreateStreamHandle(read_stream, write_stream);
187 GRPC_CLOSURE_INIT(&connect->on_open, OnOpen, static_cast<void*>(connect),
188 grpc_schedule_on_exec_ctx);
189 connect->stream_handle->NotifyOnOpen(&connect->on_open);
190 GRPC_CLOSURE_INIT(&connect->on_alarm, OnAlarm, connect,
191 grpc_schedule_on_exec_ctx);
192 gpr_mu_lock(&connect->mu);
193 CFReadStreamOpen(read_stream);
194 CFWriteStreamOpen(write_stream);
195 grpc_timer_init(&connect->alarm, deadline, &connect->on_alarm);
196 gpr_mu_unlock(&connect->mu);
197 return 0;
198 }
199
200 static bool CFStreamClientCancelConnect(int64_t /*connection_handle*/) {
201 return false;
202 }
203
204 grpc_tcp_client_vtable grpc_cfstream_client_vtable = {
205 CFStreamClientConnect, CFStreamClientCancelConnect};
206
207 #endif /* GRPC_CFSTREAM_CLIENT */