blob: 7c6a9b85f530c5d99ecfd0046aabbcc06c233ebb [file] [log] [blame]
murgatroid999030c812016-09-16 13:25:08 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2016 gRPC authors.
murgatroid999030c812016-09-16 13:25:08 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
murgatroid999030c812016-09-16 13:25:08 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
murgatroid999030c812016-09-16 13:25:08 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
murgatroid999030c812016-09-16 13:25:08 -070016 *
17 */
18
19#include "src/core/lib/iomgr/port.h"
20
21#ifdef GRPC_UV
22
23#include <limits.h>
24#include <string.h>
25
murgatroid99e2672c92016-11-09 15:12:22 -080026#include <grpc/slice_buffer.h>
27
murgatroid999030c812016-09-16 13:25:08 -070028#include <grpc/support/alloc.h>
29#include <grpc/support/log.h>
murgatroid999030c812016-09-16 13:25:08 -070030#include <grpc/support/string_util.h>
31
32#include "src/core/lib/iomgr/error.h"
33#include "src/core/lib/iomgr/network_status_tracker.h"
murgatroid99e2672c92016-11-09 15:12:22 -080034#include "src/core/lib/iomgr/resource_quota.h"
murgatroid999030c812016-09-16 13:25:08 -070035#include "src/core/lib/iomgr/tcp_uv.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080036#include "src/core/lib/slice/slice_internal.h"
murgatroid99e2672c92016-11-09 15:12:22 -080037#include "src/core/lib/slice/slice_string_helpers.h"
murgatroid999030c812016-09-16 13:25:08 -070038#include "src/core/lib/support/string.h"
39
ncteisen06bce6e2017-07-10 07:58:49 -070040grpc_tracer_flag grpc_tcp_trace = GRPC_TRACER_INITIALIZER(false, "tcp");
murgatroid999030c812016-09-16 13:25:08 -070041
42typedef struct {
43 grpc_endpoint base;
44 gpr_refcount refcount;
45
murgatroid9969259d42016-10-31 14:34:10 -070046 uv_write_t write_req;
47 uv_shutdown_t shutdown_req;
48
murgatroid999030c812016-09-16 13:25:08 -070049 uv_tcp_t *handle;
50
51 grpc_closure *read_cb;
52 grpc_closure *write_cb;
53
murgatroid99e2672c92016-11-09 15:12:22 -080054 grpc_slice read_slice;
55 grpc_slice_buffer *read_slices;
56 grpc_slice_buffer *write_slices;
murgatroid999030c812016-09-16 13:25:08 -070057 uv_buf_t *write_buffers;
58
murgatroid99e2672c92016-11-09 15:12:22 -080059 grpc_resource_user *resource_user;
murgatroid9969259d42016-10-31 14:34:10 -070060
murgatroid992c287ca2016-10-07 09:55:35 -070061 bool shutting_down;
murgatroid9969259d42016-10-31 14:34:10 -070062
murgatroid999030c812016-09-16 13:25:08 -070063 char *peer_string;
64 grpc_pollset *pollset;
65} grpc_tcp;
66
murgatroid99e2672c92016-11-09 15:12:22 -080067static void tcp_free(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
murgatroid99eb74c922017-06-26 09:42:04 -070068 grpc_slice_unref_internal(exec_ctx, tcp->read_slice);
murgatroid99e2672c92016-11-09 15:12:22 -080069 grpc_resource_user_unref(exec_ctx, tcp->resource_user);
murgatroid99cd4508f2017-07-07 14:44:56 -070070 gpr_free(tcp->handle);
71 gpr_free(tcp->peer_string);
murgatroid9969259d42016-10-31 14:34:10 -070072 gpr_free(tcp);
murgatroid9969259d42016-10-31 14:34:10 -070073}
murgatroid999030c812016-09-16 13:25:08 -070074
ncteisen0e3aee32017-06-08 16:32:24 -070075#ifndef NDEBUG
murgatroid992e012342016-11-10 18:24:08 -080076#define TCP_UNREF(exec_ctx, tcp, reason) \
77 tcp_unref((exec_ctx), (tcp), (reason), __FILE__, __LINE__)
murgatroid99a1137ce2017-05-09 14:21:12 -070078#define TCP_REF(tcp, reason) tcp_ref((tcp), (reason), __FILE__, __LINE__)
murgatroid992e012342016-11-10 18:24:08 -080079static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp,
80 const char *reason, const char *file, int line) {
ncteisen0e3aee32017-06-08 16:32:24 -070081 if (GRPC_TRACER_ON(grpc_tcp_trace)) {
82 gpr_atm val = gpr_atm_no_barrier_load(&tcp->refcount.count);
ncteisend39010e2017-06-08 17:08:07 -070083 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
84 "TCP unref %p : %s %" PRIdPTR " -> %" PRIdPTR, tcp, reason, val,
85 val - 1);
ncteisen0e3aee32017-06-08 16:32:24 -070086 }
murgatroid999030c812016-09-16 13:25:08 -070087 if (gpr_unref(&tcp->refcount)) {
murgatroid99e2672c92016-11-09 15:12:22 -080088 tcp_free(exec_ctx, tcp);
murgatroid999030c812016-09-16 13:25:08 -070089 }
90}
91
92static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file,
93 int line) {
ncteisen0e3aee32017-06-08 16:32:24 -070094 if (GRPC_TRACER_ON(grpc_tcp_trace)) {
95 gpr_atm val = gpr_atm_no_barrier_load(&tcp->refcount.count);
ncteisend39010e2017-06-08 17:08:07 -070096 gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
97 "TCP ref %p : %s %" PRIdPTR " -> %" PRIdPTR, tcp, reason, val,
98 val + 1);
ncteisen0e3aee32017-06-08 16:32:24 -070099 }
murgatroid999030c812016-09-16 13:25:08 -0700100 gpr_ref(&tcp->refcount);
101}
102#else
murgatroid99e2672c92016-11-09 15:12:22 -0800103#define TCP_UNREF(exec_ctx, tcp, reason) tcp_unref((exec_ctx), (tcp))
murgatroid999030c812016-09-16 13:25:08 -0700104#define TCP_REF(tcp, reason) tcp_ref((tcp))
murgatroid99e2672c92016-11-09 15:12:22 -0800105static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
murgatroid999030c812016-09-16 13:25:08 -0700106 if (gpr_unref(&tcp->refcount)) {
murgatroid99e2672c92016-11-09 15:12:22 -0800107 tcp_free(exec_ctx, tcp);
murgatroid999030c812016-09-16 13:25:08 -0700108 }
109}
110
111static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); }
112#endif
113
murgatroid991191b722017-02-08 11:56:52 -0800114static void uv_close_callback(uv_handle_t *handle) {
115 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
116 grpc_tcp *tcp = handle->data;
117 TCP_UNREF(&exec_ctx, tcp, "destroy");
118 grpc_exec_ctx_finish(&exec_ctx);
119}
120
murgatroid99bb8b1c92017-06-23 15:10:18 -0700121static grpc_slice alloc_read_slice(grpc_exec_ctx *exec_ctx,
122 grpc_resource_user *resource_user) {
123 return grpc_resource_user_slice_malloc(exec_ctx, resource_user,
124 GRPC_TCP_DEFAULT_READ_SLICE_SIZE);
125}
126
murgatroid99dedb9232016-09-26 13:54:04 -0700127static void alloc_uv_buf(uv_handle_t *handle, size_t suggested_size,
128 uv_buf_t *buf) {
murgatroid9969259d42016-10-31 14:34:10 -0700129 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
murgatroid999030c812016-09-16 13:25:08 -0700130 grpc_tcp *tcp = handle->data;
131 (void)suggested_size;
Craig Tiller618e67d2016-10-26 21:08:10 -0700132 buf->base = (char *)GRPC_SLICE_START_PTR(tcp->read_slice);
133 buf->len = GRPC_SLICE_LENGTH(tcp->read_slice);
murgatroid9969259d42016-10-31 14:34:10 -0700134 grpc_exec_ctx_finish(&exec_ctx);
murgatroid999030c812016-09-16 13:25:08 -0700135}
136
murgatroid99dedb9232016-09-26 13:54:04 -0700137static void read_callback(uv_stream_t *stream, ssize_t nread,
138 const uv_buf_t *buf) {
murgatroid99e2672c92016-11-09 15:12:22 -0800139 grpc_slice sub;
murgatroid999030c812016-09-16 13:25:08 -0700140 grpc_error *error;
141 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
142 grpc_tcp *tcp = stream->data;
143 grpc_closure *cb = tcp->read_cb;
144 if (nread == 0) {
145 // Nothing happened. Wait for the next callback
146 return;
147 }
murgatroid99e2672c92016-11-09 15:12:22 -0800148 TCP_UNREF(&exec_ctx, tcp, "read");
murgatroid999030c812016-09-16 13:25:08 -0700149 tcp->read_cb = NULL;
150 // TODO(murgatroid99): figure out what the return value here means
151 uv_read_stop(stream);
152 if (nread == UV_EOF) {
ncteisen4b36a3d2017-03-13 19:08:06 -0700153 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("EOF");
murgatroid999030c812016-09-16 13:25:08 -0700154 } else if (nread > 0) {
155 // Successful read
murgatroid99e2672c92016-11-09 15:12:22 -0800156 sub = grpc_slice_sub_no_ref(tcp->read_slice, 0, (size_t)nread);
157 grpc_slice_buffer_add(tcp->read_slices, sub);
murgatroid99bb8b1c92017-06-23 15:10:18 -0700158 tcp->read_slice = alloc_read_slice(&exec_ctx, tcp->resource_user);
murgatroid999030c812016-09-16 13:25:08 -0700159 error = GRPC_ERROR_NONE;
Craig Tiller341bcc52017-05-09 08:37:44 -0700160 if (GRPC_TRACER_ON(grpc_tcp_trace)) {
murgatroid999030c812016-09-16 13:25:08 -0700161 size_t i;
162 const char *str = grpc_error_string(error);
163 gpr_log(GPR_DEBUG, "read: error=%s", str);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800164
murgatroid999030c812016-09-16 13:25:08 -0700165 for (i = 0; i < tcp->read_slices->count; i++) {
murgatroid99e2672c92016-11-09 15:12:22 -0800166 char *dump = grpc_dump_slice(tcp->read_slices->slices[i],
murgatroid992e012342016-11-10 18:24:08 -0800167 GPR_DUMP_HEX | GPR_DUMP_ASCII);
murgatroid99dedb9232016-09-26 13:54:04 -0700168 gpr_log(GPR_DEBUG, "READ %p (peer=%s): %s", tcp, tcp->peer_string,
169 dump);
murgatroid999030c812016-09-16 13:25:08 -0700170 gpr_free(dump);
171 }
172 }
173 } else {
174 // nread < 0: Error
ncteisen4b36a3d2017-03-13 19:08:06 -0700175 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("TCP Read failed");
murgatroid999030c812016-09-16 13:25:08 -0700176 }
ncteisen969b46e2017-06-08 14:57:11 -0700177 GRPC_CLOSURE_SCHED(&exec_ctx, cb, error);
murgatroid999030c812016-09-16 13:25:08 -0700178 grpc_exec_ctx_finish(&exec_ctx);
179}
180
181static void uv_endpoint_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
murgatroid99e2672c92016-11-09 15:12:22 -0800182 grpc_slice_buffer *read_slices, grpc_closure *cb) {
murgatroid999030c812016-09-16 13:25:08 -0700183 grpc_tcp *tcp = (grpc_tcp *)ep;
184 int status;
185 grpc_error *error = GRPC_ERROR_NONE;
186 GPR_ASSERT(tcp->read_cb == NULL);
187 tcp->read_cb = cb;
188 tcp->read_slices = read_slices;
Craig Tillerab7b2d82016-12-07 07:26:34 -0800189 grpc_slice_buffer_reset_and_unref_internal(exec_ctx, read_slices);
murgatroid999030c812016-09-16 13:25:08 -0700190 TCP_REF(tcp, "read");
191 // TODO(murgatroid99): figure out what the return value here means
murgatroid99dedb9232016-09-26 13:54:04 -0700192 status =
193 uv_read_start((uv_stream_t *)tcp->handle, alloc_uv_buf, read_callback);
murgatroid999030c812016-09-16 13:25:08 -0700194 if (status != 0) {
ncteisen4b36a3d2017-03-13 19:08:06 -0700195 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("TCP Read failed at start");
murgatroid99dedb9232016-09-26 13:54:04 -0700196 error =
ncteisen4b36a3d2017-03-13 19:08:06 -0700197 grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR,
198 grpc_slice_from_static_string(uv_strerror(status)));
ncteisen969b46e2017-06-08 14:57:11 -0700199 GRPC_CLOSURE_SCHED(exec_ctx, cb, error);
murgatroid999030c812016-09-16 13:25:08 -0700200 }
Craig Tiller341bcc52017-05-09 08:37:44 -0700201 if (GRPC_TRACER_ON(grpc_tcp_trace)) {
murgatroid999030c812016-09-16 13:25:08 -0700202 const char *str = grpc_error_string(error);
203 gpr_log(GPR_DEBUG, "Initiating read on %p: error=%s", tcp, str);
204 }
205}
206
207static void write_callback(uv_write_t *req, int status) {
208 grpc_tcp *tcp = req->data;
209 grpc_error *error;
210 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
211 grpc_closure *cb = tcp->write_cb;
212 tcp->write_cb = NULL;
murgatroid99e2672c92016-11-09 15:12:22 -0800213 TCP_UNREF(&exec_ctx, tcp, "write");
murgatroid999030c812016-09-16 13:25:08 -0700214 if (status == 0) {
215 error = GRPC_ERROR_NONE;
216 } else {
ncteisen4b36a3d2017-03-13 19:08:06 -0700217 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("TCP Write failed");
murgatroid999030c812016-09-16 13:25:08 -0700218 }
Craig Tiller341bcc52017-05-09 08:37:44 -0700219 if (GRPC_TRACER_ON(grpc_tcp_trace)) {
murgatroid999030c812016-09-16 13:25:08 -0700220 const char *str = grpc_error_string(error);
221 gpr_log(GPR_DEBUG, "write complete on %p: error=%s", tcp, str);
222 }
223 gpr_free(tcp->write_buffers);
murgatroid99e2672c92016-11-09 15:12:22 -0800224 grpc_resource_user_free(&exec_ctx, tcp->resource_user,
murgatroid9969259d42016-10-31 14:34:10 -0700225 sizeof(uv_buf_t) * tcp->write_slices->count);
ncteisen969b46e2017-06-08 14:57:11 -0700226 GRPC_CLOSURE_SCHED(&exec_ctx, cb, error);
murgatroid999030c812016-09-16 13:25:08 -0700227 grpc_exec_ctx_finish(&exec_ctx);
228}
229
230static void uv_endpoint_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
murgatroid99e2672c92016-11-09 15:12:22 -0800231 grpc_slice_buffer *write_slices,
murgatroid999030c812016-09-16 13:25:08 -0700232 grpc_closure *cb) {
233 grpc_tcp *tcp = (grpc_tcp *)ep;
234 uv_buf_t *buffers;
235 unsigned int buffer_count;
236 unsigned int i;
murgatroid99e2672c92016-11-09 15:12:22 -0800237 grpc_slice *slice;
murgatroid999030c812016-09-16 13:25:08 -0700238 uv_write_t *write_req;
239
Craig Tiller341bcc52017-05-09 08:37:44 -0700240 if (GRPC_TRACER_ON(grpc_tcp_trace)) {
murgatroid99c36f6ea2016-10-03 09:24:09 -0700241 size_t j;
murgatroid999030c812016-09-16 13:25:08 -0700242
murgatroid99c36f6ea2016-10-03 09:24:09 -0700243 for (j = 0; j < write_slices->count; j++) {
murgatroid99e2672c92016-11-09 15:12:22 -0800244 char *data = grpc_dump_slice(write_slices->slices[j],
murgatroid992e012342016-11-10 18:24:08 -0800245 GPR_DUMP_HEX | GPR_DUMP_ASCII);
murgatroid999030c812016-09-16 13:25:08 -0700246 gpr_log(GPR_DEBUG, "WRITE %p (peer=%s): %s", tcp, tcp->peer_string, data);
247 gpr_free(data);
248 }
249 }
250
251 if (tcp->shutting_down) {
ncteisen969b46e2017-06-08 14:57:11 -0700252 GRPC_CLOSURE_SCHED(exec_ctx, cb, GRPC_ERROR_CREATE_FROM_STATIC_STRING(
ncteisen4b36a3d2017-03-13 19:08:06 -0700253 "TCP socket is shutting down"));
murgatroid999030c812016-09-16 13:25:08 -0700254 return;
255 }
256
257 GPR_ASSERT(tcp->write_cb == NULL);
258 tcp->write_slices = write_slices;
259 GPR_ASSERT(tcp->write_slices->count <= UINT_MAX);
260 if (tcp->write_slices->count == 0) {
261 // No slices means we don't have to do anything,
262 // and libuv doesn't like empty writes
ncteisen969b46e2017-06-08 14:57:11 -0700263 GRPC_CLOSURE_SCHED(exec_ctx, cb, GRPC_ERROR_NONE);
murgatroid999030c812016-09-16 13:25:08 -0700264 return;
265 }
266
267 tcp->write_cb = cb;
268 buffer_count = (unsigned int)tcp->write_slices->count;
269 buffers = gpr_malloc(sizeof(uv_buf_t) * buffer_count);
murgatroid99e2672c92016-11-09 15:12:22 -0800270 grpc_resource_user_alloc(exec_ctx, tcp->resource_user,
murgatroid9969259d42016-10-31 14:34:10 -0700271 sizeof(uv_buf_t) * buffer_count, NULL);
murgatroid999030c812016-09-16 13:25:08 -0700272 for (i = 0; i < buffer_count; i++) {
273 slice = &tcp->write_slices->slices[i];
Craig Tiller618e67d2016-10-26 21:08:10 -0700274 buffers[i].base = (char *)GRPC_SLICE_START_PTR(*slice);
275 buffers[i].len = GRPC_SLICE_LENGTH(*slice);
murgatroid999030c812016-09-16 13:25:08 -0700276 }
murgatroid9969259d42016-10-31 14:34:10 -0700277 tcp->write_buffers = buffers;
278 write_req = &tcp->write_req;
murgatroid999030c812016-09-16 13:25:08 -0700279 write_req->data = tcp;
280 TCP_REF(tcp, "write");
281 // TODO(murgatroid99): figure out what the return value here means
282 uv_write(write_req, (uv_stream_t *)tcp->handle, buffers, buffer_count,
283 write_callback);
284}
285
286static void uv_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
287 grpc_pollset *pollset) {
288 // No-op. We're ignoring pollsets currently
murgatroid99dedb9232016-09-26 13:54:04 -0700289 (void)exec_ctx;
290 (void)ep;
291 (void)pollset;
292 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid999030c812016-09-16 13:25:08 -0700293 tcp->pollset = pollset;
294}
295
296static void uv_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
297 grpc_pollset_set *pollset) {
298 // No-op. We're ignoring pollsets currently
murgatroid99dedb9232016-09-26 13:54:04 -0700299 (void)exec_ctx;
300 (void)ep;
301 (void)pollset;
murgatroid999030c812016-09-16 13:25:08 -0700302}
303
murgatroid9969259d42016-10-31 14:34:10 -0700304static void shutdown_callback(uv_shutdown_t *req, int status) {}
305
Craig Tiller22f13fb2017-01-27 11:43:25 -0800306static void uv_endpoint_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
307 grpc_error *why) {
murgatroid999030c812016-09-16 13:25:08 -0700308 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid992c287ca2016-10-07 09:55:35 -0700309 if (!tcp->shutting_down) {
310 tcp->shutting_down = true;
murgatroid9969259d42016-10-31 14:34:10 -0700311 uv_shutdown_t *req = &tcp->shutdown_req;
murgatroid992c287ca2016-10-07 09:55:35 -0700312 uv_shutdown(req, (uv_stream_t *)tcp->handle, shutdown_callback);
murgatroid99a1137ce2017-05-09 14:21:12 -0700313 grpc_resource_user_shutdown(exec_ctx, tcp->resource_user);
murgatroid992c287ca2016-10-07 09:55:35 -0700314 }
Craig Tiller22f13fb2017-01-27 11:43:25 -0800315 GRPC_ERROR_UNREF(why);
murgatroid999030c812016-09-16 13:25:08 -0700316}
317
318static void uv_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
319 grpc_network_status_unregister_endpoint(ep);
320 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid999030c812016-09-16 13:25:08 -0700321 uv_close((uv_handle_t *)tcp->handle, uv_close_callback);
murgatroid999030c812016-09-16 13:25:08 -0700322}
323
324static char *uv_get_peer(grpc_endpoint *ep) {
325 grpc_tcp *tcp = (grpc_tcp *)ep;
326 return gpr_strdup(tcp->peer_string);
327}
328
murgatroid9969259d42016-10-31 14:34:10 -0700329static grpc_resource_user *uv_get_resource_user(grpc_endpoint *ep) {
330 grpc_tcp *tcp = (grpc_tcp *)ep;
murgatroid99e2672c92016-11-09 15:12:22 -0800331 return tcp->resource_user;
murgatroid9969259d42016-10-31 14:34:10 -0700332}
333
murgatroid992e012342016-11-10 18:24:08 -0800334static int uv_get_fd(grpc_endpoint *ep) { return -1; }
335
murgatroid9969259d42016-10-31 14:34:10 -0700336static grpc_endpoint_vtable vtable = {
Craig Tiller00a8c0b2017-06-06 09:35:58 -0700337 uv_endpoint_read, uv_endpoint_write, uv_add_to_pollset,
338 uv_add_to_pollset_set, uv_endpoint_shutdown, uv_destroy,
339 uv_get_resource_user, uv_get_peer, uv_get_fd};
murgatroid999030c812016-09-16 13:25:08 -0700340
murgatroid9969259d42016-10-31 14:34:10 -0700341grpc_endpoint *grpc_tcp_create(uv_tcp_t *handle,
342 grpc_resource_quota *resource_quota,
343 char *peer_string) {
murgatroid999030c812016-09-16 13:25:08 -0700344 grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp));
murgatroid99bb8b1c92017-06-23 15:10:18 -0700345 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
murgatroid999030c812016-09-16 13:25:08 -0700346
Craig Tiller341bcc52017-05-09 08:37:44 -0700347 if (GRPC_TRACER_ON(grpc_tcp_trace)) {
murgatroid999030c812016-09-16 13:25:08 -0700348 gpr_log(GPR_DEBUG, "Creating TCP endpoint %p", tcp);
349 }
350
murgatroid9904d28292016-10-27 14:36:57 -0700351 /* Disable Nagle's Algorithm */
murgatroid9912e57752016-10-27 14:30:41 -0700352 uv_tcp_nodelay(handle, 1);
353
murgatroid999030c812016-09-16 13:25:08 -0700354 memset(tcp, 0, sizeof(grpc_tcp));
355 tcp->base.vtable = &vtable;
356 tcp->handle = handle;
357 handle->data = tcp;
358 gpr_ref_init(&tcp->refcount, 1);
359 tcp->peer_string = gpr_strdup(peer_string);
murgatroid992c287ca2016-10-07 09:55:35 -0700360 tcp->shutting_down = false;
murgatroid99e2672c92016-11-09 15:12:22 -0800361 tcp->resource_user = grpc_resource_user_create(resource_quota, peer_string);
murgatroid99bb8b1c92017-06-23 15:10:18 -0700362 tcp->read_slice = alloc_read_slice(&exec_ctx, tcp->resource_user);
murgatroid999030c812016-09-16 13:25:08 -0700363 /* Tell network status tracking code about the new endpoint */
364 grpc_network_status_register_endpoint(&tcp->base);
365
366#ifndef GRPC_UV_TCP_HOLD_LOOP
367 uv_unref((uv_handle_t *)handle);
368#endif
369
murgatroid99bb8b1c92017-06-23 15:10:18 -0700370 grpc_exec_ctx_finish(&exec_ctx);
murgatroid999030c812016-09-16 13:25:08 -0700371 return &tcp->base;
372}
373
374#endif /* GRPC_UV */