tcp_client_cfstream.cc
Go to the documentation of this file.
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 
21 
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/support/alloc.h>
31 #include <grpc/support/log.h>
32 #include <grpc/support/sync.h>
33 
44 
46 
47 struct CFStreamConnect {
48  gpr_mu mu;
50 
51  CFReadStreamRef read_stream;
52  CFWriteStreamRef write_stream;
53  CFStreamHandle* stream_handle;
54 
55  grpc_timer alarm;
56  grpc_closure on_alarm;
58 
59  bool read_stream_open;
60  bool write_stream_open;
61  bool failed;
62 
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,
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 {
94  GRPC_ERROR_CREATE_FROM_STATIC_STRING("connect() timed out");
96  }
97 }
98 
99 static void OnOpen(void* arg, grpc_error_handle error) {
100  CFStreamConnect* connect = static_cast<CFStreamConnect*>(arg);
101  if (grpc_tcp_trace.enabled()) {
102  gpr_log(GPR_DEBUG, "CLIENT_CONNECT :%p OnOpen, error:%s", connect,
104  }
105  gpr_mu_lock(&connect->mu);
106  grpc_timer_cancel(&connect->alarm);
107  grpc_closure* closure = connect->closure;
108  connect->closure = nil;
109 
110  bool done = (--connect->refs == 0);
111  grpc_endpoint** endpoint = connect->endpoint;
112 
113  // Only schedule a callback once, by either OnAlarm or OnOpen. The
114  // first one issues callback while the second one does cleanup.
115  if (done) {
116  gpr_mu_unlock(&connect->mu);
117  CFStreamConnectCleanup(connect);
118  } else {
119  if (GRPC_ERROR_IS_NONE(error)) {
120  CFErrorRef stream_error = CFReadStreamCopyError(connect->read_stream);
121  if (stream_error == NULL) {
122  stream_error = CFWriteStreamCopyError(connect->write_stream);
123  }
124  if (stream_error) {
125  error = GRPC_ERROR_CREATE_FROM_CFERROR(stream_error, "connect() error");
126  CFRelease(stream_error);
127  }
128  if (GRPC_ERROR_IS_NONE(error)) {
129  *endpoint = grpc_cfstream_endpoint_create(
130  connect->read_stream, connect->write_stream,
131  connect->addr_name.c_str(), connect->stream_handle);
132  }
133  } else {
134  (void)GRPC_ERROR_REF(error);
135  }
136  gpr_mu_unlock(&connect->mu);
138  }
139 }
140 
141 static void ParseResolvedAddress(const grpc_resolved_address* addr,
142  CFStringRef* host, int* port) {
143  std::string host_port = grpc_sockaddr_to_string(addr, true).value();
144  std::string host_string;
145  std::string port_string;
146  grpc_core::SplitHostPort(host_port, &host_string, &port_string);
147  *host = CFStringCreateWithCString(NULL, host_string.c_str(),
148  kCFStringEncodingUTF8);
150 }
151 
152 static int64_t CFStreamClientConnect(grpc_closure* closure, grpc_endpoint** ep,
153  grpc_pollset_set* interested_parties,
154  const grpc_channel_args* channel_args,
155  const grpc_resolved_address* resolved_addr,
156  grpc_core::Timestamp deadline) {
157  auto addr_uri = grpc_sockaddr_to_uri(resolved_addr);
158  if (!addr_uri.ok()) {
160  GRPC_ERROR_CREATE_FROM_CPP_STRING(addr_uri.status().ToString());
162  return 0;
163  }
164 
165  CFStreamConnect* connect = new CFStreamConnect();
166  connect->closure = closure;
167  connect->endpoint = ep;
168  connect->addr_name = addr_uri.value();
169  connect->refs = 2; // One for the connect operation, one for the timer.
170  gpr_ref_init(&connect->refcount, 1);
171  gpr_mu_init(&connect->mu);
172 
173  if (grpc_tcp_trace.enabled()) {
174  gpr_log(GPR_DEBUG, "CLIENT_CONNECT: %p, %s: asynchronously connecting",
175  connect, connect->addr_name.c_str());
176  }
177 
178  CFReadStreamRef read_stream;
179  CFWriteStreamRef write_stream;
180 
181  CFStringRef host;
182  int port;
183  ParseResolvedAddress(resolved_addr, &host, &port);
184  CFStreamCreatePairWithSocketToHost(NULL, host, port, &read_stream,
185  &write_stream);
186  CFRelease(host);
187  connect->read_stream = read_stream;
188  connect->write_stream = write_stream;
189  connect->stream_handle =
190  CFStreamHandle::CreateStreamHandle(read_stream, write_stream);
191  GRPC_CLOSURE_INIT(&connect->on_open, OnOpen, static_cast<void*>(connect),
192  grpc_schedule_on_exec_ctx);
193  connect->stream_handle->NotifyOnOpen(&connect->on_open);
194  GRPC_CLOSURE_INIT(&connect->on_alarm, OnAlarm, connect,
195  grpc_schedule_on_exec_ctx);
196  gpr_mu_lock(&connect->mu);
197  CFReadStreamOpen(read_stream);
198  CFWriteStreamOpen(write_stream);
199  grpc_timer_init(&connect->alarm, deadline, &connect->on_alarm);
200  gpr_mu_unlock(&connect->mu);
201  return 0;
202 }
203 
204 static bool CFStreamClientCancelConnect(int64_t /*connection_handle*/) {
205  return false;
206 }
207 
208 grpc_tcp_client_vtable grpc_cfstream_client_vtable = {
209  CFStreamClientConnect, CFStreamClientCancelConnect};
210 
211 #endif /* GRPC_CFSTREAM_CLIENT */
GRPC_CLOSURE_INIT
#define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler)
Definition: closure.h:115
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
log.h
sockaddr_utils.h
grpc_pollset_set
struct grpc_pollset_set grpc_pollset_set
Definition: iomgr_fwd.h:23
string.h
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_resolved_address
Definition: resolved_address.h:34
closure.h
grpc_core::SplitHostPort
bool SplitHostPort(absl::string_view name, absl::string_view *host, absl::string_view *port)
Definition: host_port.cc:88
grpc_timer
Definition: iomgr/timer.h:33
stream_handle
Definition: benchmark-multi-accept.c:28
grpc_sockaddr_to_string
absl::StatusOr< std::string > grpc_sockaddr_to_string(const grpc_resolved_address *resolved_addr, bool normalize)
Definition: sockaddr_utils.cc:194
grpc_channel_args
Definition: grpc_types.h:132
gpr_refcount
Definition: impl/codegen/sync_generic.h:39
gpr_mu_destroy
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
grpc_tcp_trace
grpc_core::TraceFlag grpc_tcp_trace(false, "tcp")
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
refcount
size_t refcount
Definition: abseil-cpp/absl/strings/internal/cordz_info.cc:122
refs
std::vector< CordRep * > refs
Definition: cordz_info_statistics_test.cc:81
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
on_open
void on_open(uv_fs_t *req)
Definition: libuv/docs/code/uvcat/main.c:41
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
closure
grpc_closure closure
Definition: src/core/lib/surface/server.cc:466
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
error_cfstream.h
arg
Definition: cmdline.cc:40
cfstream_handle.h
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
error.h
host_port.h
endpoint_cfstream.h
tcp_client.h
GRPC_ERROR_CREATE_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
Definition: error.h:291
grpc_core::TraceFlag
Definition: debug/trace.h:63
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
grpc_core::TraceFlag::enabled
bool enabled()
Definition: debug/trace.h:82
grpc_timer_cancel
void grpc_timer_cancel(grpc_timer *timer)
Definition: iomgr/timer.cc:36
grpc_sockaddr_get_port
int grpc_sockaddr_get_port(const grpc_resolved_address *resolved_addr)
Definition: sockaddr_utils.cc:303
GRPC_ERROR_REF
#define GRPC_ERROR_REF(err)
Definition: error.h:261
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
port.h
grpc_error_std_string
std::string grpc_error_std_string(grpc_error_handle error)
Definition: error.cc:944
GRPC_ERROR_CREATE_FROM_CPP_STRING
#define GRPC_ERROR_CREATE_FROM_CPP_STRING(desc)
Definition: error.h:297
alloc.h
arg
struct arg arg
grpc_timer_init
void grpc_timer_init(grpc_timer *timer, grpc_core::Timestamp deadline, grpc_closure *closure)
Definition: iomgr/timer.cc:31
closure
Definition: proxy.cc:59
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
channel_args.h
timer.h
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
grpc_tcp_client_vtable
Definition: tcp_client.h:32
absl::StatusOr::value
const T & value() const &ABSL_ATTRIBUTE_LIFETIME_BOUND
Definition: abseil-cpp/absl/status/statusor.h:687
gpr_ref_init
GPRAPI void gpr_ref_init(gpr_refcount *r, int n)
Definition: sync.cc:86
grpc_error
Definition: error_internal.h:42
sync.h
grpc_closure
Definition: closure.h:56
grpc_sockaddr_to_uri
absl::StatusOr< std::string > grpc_sockaddr_to_uri(const grpc_resolved_address *resolved_addr)
Definition: sockaddr_utils.cc:260
grpc_endpoint
Definition: endpoint.h:105
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:29