|
|
|
@ -73,7 +73,8 @@ struct custom_tcp_endpoint { |
|
|
|
|
std::string local_address; |
|
|
|
|
}; |
|
|
|
|
static void tcp_free(grpc_custom_socket* s) { |
|
|
|
|
custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)s->endpoint; |
|
|
|
|
custom_tcp_endpoint* tcp = |
|
|
|
|
reinterpret_cast<custom_tcp_endpoint*>(s->endpoint); |
|
|
|
|
grpc_resource_user_unref(tcp->resource_user); |
|
|
|
|
delete tcp; |
|
|
|
|
s->refs--; |
|
|
|
@ -149,18 +150,19 @@ static void custom_read_callback(grpc_custom_socket* socket, size_t nread, |
|
|
|
|
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
|
|
|
|
grpc_core::ExecCtx exec_ctx; |
|
|
|
|
grpc_slice_buffer garbage; |
|
|
|
|
custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)socket->endpoint; |
|
|
|
|
custom_tcp_endpoint* tcp = |
|
|
|
|
reinterpret_cast<custom_tcp_endpoint*>(socket->endpoint); |
|
|
|
|
if (error == GRPC_ERROR_NONE && nread == 0) { |
|
|
|
|
error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("EOF"); |
|
|
|
|
} |
|
|
|
|
if (error == GRPC_ERROR_NONE) { |
|
|
|
|
// Successful read
|
|
|
|
|
if ((size_t)nread < tcp->read_slices->length) { |
|
|
|
|
if (nread < tcp->read_slices->length) { |
|
|
|
|
/* TODO(murgatroid99): Instead of discarding the unused part of the read
|
|
|
|
|
* buffer, reuse it as the next read buffer. */ |
|
|
|
|
grpc_slice_buffer_init(&garbage); |
|
|
|
|
grpc_slice_buffer_trim_end( |
|
|
|
|
tcp->read_slices, tcp->read_slices->length - (size_t)nread, &garbage); |
|
|
|
|
grpc_slice_buffer_trim_end(tcp->read_slices, |
|
|
|
|
tcp->read_slices->length - nread, &garbage); |
|
|
|
|
grpc_slice_buffer_reset_and_unref_internal(&garbage); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
@ -170,7 +172,7 @@ static void custom_read_callback(grpc_custom_socket* socket, size_t nread, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void tcp_read_allocation_done(void* tcpp, grpc_error* error) { |
|
|
|
|
custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)tcpp; |
|
|
|
|
custom_tcp_endpoint* tcp = static_cast<custom_tcp_endpoint*>(tcpp); |
|
|
|
|
if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) { |
|
|
|
|
gpr_log(GPR_INFO, "TCP:%p read_allocation_done: %s", tcp->socket, |
|
|
|
|
grpc_error_string(error)); |
|
|
|
@ -179,7 +181,8 @@ static void tcp_read_allocation_done(void* tcpp, grpc_error* error) { |
|
|
|
|
/* Before calling read, we allocate a buffer with exactly one slice
|
|
|
|
|
* to tcp->read_slices and wait for the callback indicating that the |
|
|
|
|
* allocation was successful. So slices[0] should always exist here */ |
|
|
|
|
char* buffer = (char*)GRPC_SLICE_START_PTR(tcp->read_slices->slices[0]); |
|
|
|
|
char* buffer = reinterpret_cast<char*>( |
|
|
|
|
GRPC_SLICE_START_PTR(tcp->read_slices->slices[0])); |
|
|
|
|
size_t len = GRPC_SLICE_LENGTH(tcp->read_slices->slices[0]); |
|
|
|
|
grpc_custom_socket_vtable->read(tcp->socket, buffer, len, |
|
|
|
|
custom_read_callback); |
|
|
|
@ -195,7 +198,7 @@ static void tcp_read_allocation_done(void* tcpp, grpc_error* error) { |
|
|
|
|
|
|
|
|
|
static void endpoint_read(grpc_endpoint* ep, grpc_slice_buffer* read_slices, |
|
|
|
|
grpc_closure* cb, bool /*urgent*/) { |
|
|
|
|
custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)ep; |
|
|
|
|
custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep); |
|
|
|
|
GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD(); |
|
|
|
|
GPR_ASSERT(tcp->read_cb == nullptr); |
|
|
|
|
tcp->read_cb = cb; |
|
|
|
@ -213,7 +216,8 @@ static void custom_write_callback(grpc_custom_socket* socket, |
|
|
|
|
grpc_error* error) { |
|
|
|
|
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
|
|
|
|
grpc_core::ExecCtx exec_ctx; |
|
|
|
|
custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)socket->endpoint; |
|
|
|
|
custom_tcp_endpoint* tcp = |
|
|
|
|
reinterpret_cast<custom_tcp_endpoint*>(socket->endpoint); |
|
|
|
|
grpc_closure* cb = tcp->write_cb; |
|
|
|
|
tcp->write_cb = nullptr; |
|
|
|
|
if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) { |
|
|
|
@ -226,7 +230,7 @@ static void custom_write_callback(grpc_custom_socket* socket, |
|
|
|
|
|
|
|
|
|
static void endpoint_write(grpc_endpoint* ep, grpc_slice_buffer* write_slices, |
|
|
|
|
grpc_closure* cb, void* /*arg*/) { |
|
|
|
|
custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)ep; |
|
|
|
|
custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep); |
|
|
|
|
GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD(); |
|
|
|
|
|
|
|
|
|
if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) { |
|
|
|
@ -284,7 +288,7 @@ static void endpoint_delete_from_pollset_set(grpc_endpoint* ep, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void endpoint_shutdown(grpc_endpoint* ep, grpc_error* why) { |
|
|
|
|
custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)ep; |
|
|
|
|
custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep); |
|
|
|
|
if (!tcp->shutting_down) { |
|
|
|
|
if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) { |
|
|
|
|
const char* str = grpc_error_string(why); |
|
|
|
@ -309,28 +313,29 @@ static void custom_close_callback(grpc_custom_socket* socket) { |
|
|
|
|
} else if (socket->endpoint) { |
|
|
|
|
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
|
|
|
|
grpc_core::ExecCtx exec_ctx; |
|
|
|
|
custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)socket->endpoint; |
|
|
|
|
custom_tcp_endpoint* tcp = |
|
|
|
|
reinterpret_cast<custom_tcp_endpoint*>(socket->endpoint); |
|
|
|
|
TCP_UNREF(tcp, "destroy"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void endpoint_destroy(grpc_endpoint* ep) { |
|
|
|
|
custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)ep; |
|
|
|
|
custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep); |
|
|
|
|
grpc_custom_socket_vtable->close(tcp->socket, custom_close_callback); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static absl::string_view endpoint_get_peer(grpc_endpoint* ep) { |
|
|
|
|
custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)ep; |
|
|
|
|
custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep); |
|
|
|
|
return tcp->peer_string; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static absl::string_view endpoint_get_local_address(grpc_endpoint* ep) { |
|
|
|
|
custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)ep; |
|
|
|
|
custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep); |
|
|
|
|
return tcp->local_address; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static grpc_resource_user* endpoint_get_resource_user(grpc_endpoint* ep) { |
|
|
|
|
custom_tcp_endpoint* tcp = (custom_tcp_endpoint*)ep; |
|
|
|
|
custom_tcp_endpoint* tcp = reinterpret_cast<custom_tcp_endpoint*>(ep); |
|
|
|
|
return tcp->resource_user; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -362,7 +367,7 @@ grpc_endpoint* custom_tcp_endpoint_create(grpc_custom_socket* socket, |
|
|
|
|
gpr_log(GPR_INFO, "Creating TCP endpoint %p", socket); |
|
|
|
|
} |
|
|
|
|
socket->refs++; |
|
|
|
|
socket->endpoint = (grpc_endpoint*)tcp; |
|
|
|
|
socket->endpoint = reinterpret_cast<grpc_endpoint*>(tcp); |
|
|
|
|
tcp->socket = socket; |
|
|
|
|
tcp->base.vtable = &vtable; |
|
|
|
|
gpr_ref_init(&tcp->refcount, 1); |
|
|
|
|