1 // Copyright 2022 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <grpc/support/port_platform.h>
15 
16 #include "src/core/lib/iomgr/event_engine_shims/tcp_client.h"
17 
18 #include "absl/status/status.h"
19 #include "absl/strings/string_view.h"
20 
21 #include <grpc/event_engine/event_engine.h>
22 #include <grpc/support/time.h>
23 
24 #include "src/core/lib/address_utils/sockaddr_utils.h"
25 #include "src/core/lib/event_engine/default_event_engine.h"
26 #include "src/core/lib/event_engine/resolved_address_internal.h"
27 #include "src/core/lib/event_engine/trace.h"
28 #include "src/core/lib/iomgr/closure.h"
29 #include "src/core/lib/iomgr/endpoint.h"
30 #include "src/core/lib/iomgr/error.h"
31 #include "src/core/lib/iomgr/event_engine_shims/endpoint.h"
32 #include "src/core/lib/iomgr/exec_ctx.h"
33 #include "src/core/lib/resource_quota/resource_quota.h"
34 #include "src/core/lib/transport/error_utils.h"
35 
36 namespace grpc_event_engine {
37 namespace experimental {
38 
event_engine_tcp_client_connect(grpc_closure * on_connect,grpc_endpoint ** endpoint,const grpc_event_engine::experimental::EndpointConfig & config,const grpc_resolved_address * addr,grpc_core::Timestamp deadline)39 int64_t event_engine_tcp_client_connect(
40     grpc_closure* on_connect, grpc_endpoint** endpoint,
41     const grpc_event_engine::experimental::EndpointConfig& config,
42     const grpc_resolved_address* addr, grpc_core::Timestamp deadline) {
43   auto resource_quota = reinterpret_cast<grpc_core::ResourceQuota*>(
44       config.GetVoidPointer(GRPC_ARG_RESOURCE_QUOTA));
45   auto addr_uri = grpc_sockaddr_to_uri(addr);
46   EventEngine* engine_ptr = reinterpret_cast<EventEngine*>(
47       config.GetVoidPointer(GRPC_INTERNAL_ARG_EVENT_ENGINE));
48   // Keeps the engine alive for some tests that have not otherwise instantiated
49   // an EventEngine
50   std::shared_ptr<EventEngine> keeper;
51   if (engine_ptr == nullptr) {
52     keeper = GetDefaultEventEngine();
53     engine_ptr = keeper.get();
54   }
55   EventEngine::ConnectionHandle handle = engine_ptr->Connect(
56       [on_connect,
57        endpoint](absl::StatusOr<std::unique_ptr<EventEngine::Endpoint>> ep) {
58         grpc_core::ApplicationCallbackExecCtx app_ctx;
59         grpc_core::ExecCtx exec_ctx;
60         absl::Status conn_status = ep.ok() ? absl::OkStatus() : ep.status();
61         if (ep.ok()) {
62           *endpoint = grpc_event_engine_endpoint_create(std::move(*ep));
63         } else {
64           *endpoint = nullptr;
65         }
66         GRPC_EVENT_ENGINE_TRACE("EventEngine::Connect Status: %s",
67                                 ep.status().ToString().c_str());
68         grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_connect,
69                                 absl_status_to_grpc_error(conn_status));
70       },
71       CreateResolvedAddress(*addr), config,
72       resource_quota != nullptr
73           ? resource_quota->memory_quota()->CreateMemoryOwner(
74                 absl::StrCat("tcp-client:", addr_uri.value()))
75           : grpc_event_engine::experimental::MemoryAllocator(),
76       std::max(grpc_core::Duration::Milliseconds(1),
77                deadline - grpc_core::Timestamp::Now()));
78   GRPC_EVENT_ENGINE_TRACE("EventEngine::Connect Peer: %s, handle: %" PRId64,
79                           (*addr_uri).c_str(),
80                           static_cast<int64_t>(handle.keys[0]));
81   return handle.keys[0];
82 }
83 
event_engine_tcp_client_cancel_connect(int64_t connection_handle)84 bool event_engine_tcp_client_cancel_connect(int64_t connection_handle) {
85   GRPC_EVENT_ENGINE_TRACE("EventEngine::CancelConnect handle: %" PRId64,
86                           connection_handle);
87   return GetDefaultEventEngine()->CancelConnect(
88       {static_cast<intptr_t>(connection_handle), 0});
89 }
90 }  // namespace experimental
91 }  // namespace grpc_event_engine
92