Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
| 35 | |
| 36 | #ifdef GPR_POSIX_SOCKET |
| 37 | |
Makarand Dharmapurikar | 0579cfc | 2016-06-20 15:45:24 -0700 | [diff] [blame] | 38 | #include "src/core/lib/iomgr/network_status_tracker.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 39 | #include "src/core/lib/iomgr/tcp_posix.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | |
| 41 | #include <errno.h> |
Mark D. Roth | 808ac38 | 2016-05-13 11:14:21 -0700 | [diff] [blame] | 42 | #include <stdbool.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include <sys/socket.h> |
Craig Tiller | a8be91b | 2016-02-19 16:22:44 -0800 | [diff] [blame] | 46 | #include <sys/types.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | #include <unistd.h> |
| 48 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | #include <grpc/support/alloc.h> |
| 50 | #include <grpc/support/log.h> |
| 51 | #include <grpc/support/slice.h> |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 52 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | #include <grpc/support/sync.h> |
| 54 | #include <grpc/support/time.h> |
| 55 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 56 | #include "src/core/lib/debug/trace.h" |
Craig Tiller | 8a03448 | 2016-03-28 16:09:04 -0700 | [diff] [blame] | 57 | #include "src/core/lib/iomgr/ev_posix.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 58 | #include "src/core/lib/profiling/timers.h" |
| 59 | #include "src/core/lib/support/string.h" |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 60 | |
Craig Tiller | 2da0296 | 2015-05-06 16:14:25 -0700 | [diff] [blame] | 61 | #ifdef GPR_HAVE_MSG_NOSIGNAL |
| 62 | #define SENDMSG_FLAGS MSG_NOSIGNAL |
| 63 | #else |
| 64 | #define SENDMSG_FLAGS 0 |
| 65 | #endif |
| 66 | |
Craig Tiller | ebc7ef2 | 2015-09-10 22:19:25 -0700 | [diff] [blame] | 67 | #ifdef GPR_MSG_IOVLEN_TYPE |
| 68 | typedef GPR_MSG_IOVLEN_TYPE msg_iovlen_type; |
| 69 | #else |
| 70 | typedef size_t msg_iovlen_type; |
| 71 | #endif |
| 72 | |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 73 | int grpc_tcp_trace = 0; |
| 74 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 75 | typedef struct { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 76 | grpc_endpoint base; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 77 | grpc_fd *em_fd; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | int fd; |
Mark D. Roth | 808ac38 | 2016-05-13 11:14:21 -0700 | [diff] [blame] | 79 | bool finished_edge; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 80 | msg_iovlen_type iov_size; /* Number of slices to allocate per read attempt */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | size_t slice_size; |
| 82 | gpr_refcount refcount; |
| 83 | |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 84 | /* garbage after the last read */ |
| 85 | gpr_slice_buffer last_read_buffer; |
| 86 | |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 87 | gpr_slice_buffer *incoming_buffer; |
| 88 | gpr_slice_buffer *outgoing_buffer; |
| 89 | /** slice within outgoing_buffer to write next */ |
| 90 | size_t outgoing_slice_idx; |
| 91 | /** byte within outgoing_buffer->slices[outgoing_slice_idx] to write next */ |
| 92 | size_t outgoing_byte_idx; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 94 | grpc_closure *read_cb; |
| 95 | grpc_closure *write_cb; |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 96 | grpc_closure *release_fd_cb; |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 97 | int *release_fd; |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 98 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 99 | grpc_closure read_closure; |
| 100 | grpc_closure write_closure; |
David Garcia Quintas | 5f228f5 | 2015-05-26 19:58:50 -0700 | [diff] [blame] | 101 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 102 | char *peer_string; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 103 | } grpc_tcp; |
| 104 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 105 | static void tcp_handle_read(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 106 | grpc_error *error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 107 | static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 108 | grpc_error *error); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 110 | static void tcp_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) { |
| 111 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 112 | grpc_fd_shutdown(exec_ctx, tcp->em_fd); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 115 | static void tcp_free(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 116 | grpc_fd_orphan(exec_ctx, tcp->em_fd, tcp->release_fd_cb, tcp->release_fd, |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 117 | "tcp_unref_orphan"); |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 118 | gpr_slice_buffer_destroy(&tcp->last_read_buffer); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 119 | gpr_free(tcp->peer_string); |
| 120 | gpr_free(tcp); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /*#define GRPC_TCP_REFCOUNT_DEBUG*/ |
| 124 | #ifdef GRPC_TCP_REFCOUNT_DEBUG |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 125 | #define TCP_UNREF(cl, tcp, reason) \ |
| 126 | tcp_unref((cl), (tcp), (reason), __FILE__, __LINE__) |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 127 | #define TCP_REF(tcp, reason) tcp_ref((tcp), (reason), __FILE__, __LINE__) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp, |
| 129 | const char *reason, const char *file, int line) { |
| 130 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP unref %p : %s %d -> %d", tcp, |
| 131 | reason, tcp->refcount.count, tcp->refcount.count - 1); |
| 132 | if (gpr_unref(&tcp->refcount)) { |
| 133 | tcp_free(exec_ctx, tcp); |
| 134 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file, |
| 138 | int line) { |
| 139 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP ref %p : %s %d -> %d", tcp, |
| 140 | reason, tcp->refcount.count, tcp->refcount.count + 1); |
| 141 | gpr_ref(&tcp->refcount); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 142 | } |
| 143 | #else |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 144 | #define TCP_UNREF(cl, tcp, reason) tcp_unref((cl), (tcp)) |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 145 | #define TCP_REF(tcp, reason) tcp_ref((tcp)) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 146 | static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { |
| 147 | if (gpr_unref(&tcp->refcount)) { |
| 148 | tcp_free(exec_ctx, tcp); |
| 149 | } |
Craig Tiller | 592e7f2 | 2015-08-21 10:45:48 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 152 | static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 153 | #endif |
| 154 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 155 | static void tcp_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) { |
Makarand Dharmapurikar | 6149443 | 2016-06-22 13:34:59 -0700 | [diff] [blame^] | 156 | grpc_network_status_unregister_endpoint(ep); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 157 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 158 | TCP_UNREF(exec_ctx, tcp, "destroy"); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 161 | static void call_read_cb(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp, |
| 162 | grpc_error *error) { |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 163 | grpc_closure *cb = tcp->read_cb; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 164 | |
Craig Tiller | 449c64b | 2016-06-13 16:26:50 -0700 | [diff] [blame] | 165 | if (false && grpc_tcp_trace) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 166 | size_t i; |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 167 | const char *str = grpc_error_string(error); |
| 168 | gpr_log(GPR_DEBUG, "read: error=%s", str); |
| 169 | grpc_error_free_string(str); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 170 | for (i = 0; i < tcp->incoming_buffer->count; i++) { |
| 171 | char *dump = gpr_dump_slice(tcp->incoming_buffer->slices[i], |
| 172 | GPR_DUMP_HEX | GPR_DUMP_ASCII); |
Mark D. Roth | bc84672 | 2016-05-04 10:53:50 -0700 | [diff] [blame] | 173 | gpr_log(GPR_DEBUG, "READ %p (peer=%s): %s", tcp, tcp->peer_string, dump); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 174 | gpr_free(dump); |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 175 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 176 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 177 | |
| 178 | tcp->read_cb = NULL; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 179 | tcp->incoming_buffer = NULL; |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 180 | grpc_exec_ctx_sched(exec_ctx, cb, error, NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 183 | #define MAX_READ_IOVEC 4 |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 184 | static void tcp_continue_read(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 185 | struct msghdr msg; |
| 186 | struct iovec iov[MAX_READ_IOVEC]; |
| 187 | ssize_t read_bytes; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 188 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 189 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 190 | GPR_ASSERT(!tcp->finished_edge); |
| 191 | GPR_ASSERT(tcp->iov_size <= MAX_READ_IOVEC); |
| 192 | GPR_ASSERT(tcp->incoming_buffer->count <= MAX_READ_IOVEC); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 193 | GPR_TIMER_BEGIN("tcp_continue_read", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 194 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 195 | while (tcp->incoming_buffer->count < (size_t)tcp->iov_size) { |
| 196 | gpr_slice_buffer_add_indexed(tcp->incoming_buffer, |
| 197 | gpr_slice_malloc(tcp->slice_size)); |
| 198 | } |
| 199 | for (i = 0; i < tcp->incoming_buffer->count; i++) { |
| 200 | iov[i].iov_base = GPR_SLICE_START_PTR(tcp->incoming_buffer->slices[i]); |
| 201 | iov[i].iov_len = GPR_SLICE_LENGTH(tcp->incoming_buffer->slices[i]); |
| 202 | } |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 203 | |
| 204 | msg.msg_name = NULL; |
| 205 | msg.msg_namelen = 0; |
| 206 | msg.msg_iov = iov; |
| 207 | msg.msg_iovlen = tcp->iov_size; |
| 208 | msg.msg_control = NULL; |
| 209 | msg.msg_controllen = 0; |
| 210 | msg.msg_flags = 0; |
| 211 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 212 | GPR_TIMER_BEGIN("recvmsg", 1); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 213 | do { |
| 214 | read_bytes = recvmsg(tcp->fd, &msg, 0); |
| 215 | } while (read_bytes < 0 && errno == EINTR); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 216 | GPR_TIMER_END("recvmsg", 0); |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 217 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 218 | if (read_bytes < 0) { |
| 219 | /* NB: After calling call_read_cb a parallel call of the read handler may |
| 220 | * be running. */ |
| 221 | if (errno == EAGAIN) { |
| 222 | if (tcp->iov_size > 1) { |
| 223 | tcp->iov_size /= 2; |
| 224 | } |
| 225 | /* We've consumed the edge, request a new one */ |
| 226 | grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure); |
| 227 | } else { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 228 | gpr_slice_buffer_reset_and_unref(tcp->incoming_buffer); |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 229 | call_read_cb(exec_ctx, tcp, GRPC_OS_ERROR(errno, "recvmsg")); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 230 | TCP_UNREF(exec_ctx, tcp, "read"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 231 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 232 | } else if (read_bytes == 0) { |
| 233 | /* 0 read size ==> end of stream */ |
| 234 | gpr_slice_buffer_reset_and_unref(tcp->incoming_buffer); |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 235 | call_read_cb(exec_ctx, tcp, GRPC_ERROR_CREATE("EOF")); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 236 | TCP_UNREF(exec_ctx, tcp, "read"); |
| 237 | } else { |
| 238 | GPR_ASSERT((size_t)read_bytes <= tcp->incoming_buffer->length); |
| 239 | if ((size_t)read_bytes < tcp->incoming_buffer->length) { |
| 240 | gpr_slice_buffer_trim_end( |
| 241 | tcp->incoming_buffer, |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 242 | tcp->incoming_buffer->length - (size_t)read_bytes, |
| 243 | &tcp->last_read_buffer); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 244 | } else if (tcp->iov_size < MAX_READ_IOVEC) { |
| 245 | ++tcp->iov_size; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 246 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 247 | GPR_ASSERT((size_t)read_bytes == tcp->incoming_buffer->length); |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 248 | call_read_cb(exec_ctx, tcp, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 249 | TCP_UNREF(exec_ctx, tcp, "read"); |
| 250 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 251 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 252 | GPR_TIMER_END("tcp_continue_read", 0); |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 255 | static void tcp_handle_read(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 256 | grpc_error *error) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 257 | grpc_tcp *tcp = (grpc_tcp *)arg; |
| 258 | GPR_ASSERT(!tcp->finished_edge); |
Craig Tiller | 5c07cce | 2015-04-28 09:21:58 -0700 | [diff] [blame] | 259 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 260 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 261 | gpr_slice_buffer_reset_and_unref(tcp->incoming_buffer); |
Craig Tiller | 6a64bfd | 2016-05-09 13:05:03 -0700 | [diff] [blame] | 262 | call_read_cb(exec_ctx, tcp, GRPC_ERROR_REF(error)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 263 | TCP_UNREF(exec_ctx, tcp, "read"); |
| 264 | } else { |
| 265 | tcp_continue_read(exec_ctx, tcp); |
| 266 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 269 | static void tcp_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 270 | gpr_slice_buffer *incoming_buffer, grpc_closure *cb) { |
| 271 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 272 | GPR_ASSERT(tcp->read_cb == NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 273 | tcp->read_cb = cb; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 274 | tcp->incoming_buffer = incoming_buffer; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 275 | gpr_slice_buffer_reset_and_unref(incoming_buffer); |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 276 | gpr_slice_buffer_swap(incoming_buffer, &tcp->last_read_buffer); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 277 | TCP_REF(tcp, "read"); |
| 278 | if (tcp->finished_edge) { |
Mark D. Roth | 808ac38 | 2016-05-13 11:14:21 -0700 | [diff] [blame] | 279 | tcp->finished_edge = false; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 280 | grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure); |
| 281 | } else { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 282 | grpc_exec_ctx_sched(exec_ctx, &tcp->read_closure, GRPC_ERROR_NONE, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 283 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 286 | /* returns true if done, false if pending; if returning true, *error is set */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 287 | #define MAX_WRITE_IOVEC 16 |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 288 | static bool tcp_flush(grpc_tcp *tcp, grpc_error **error) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 289 | struct msghdr msg; |
| 290 | struct iovec iov[MAX_WRITE_IOVEC]; |
Craig Tiller | ebc7ef2 | 2015-09-10 22:19:25 -0700 | [diff] [blame] | 291 | msg_iovlen_type iov_size; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 292 | ssize_t sent_length; |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 293 | size_t sending_length; |
| 294 | size_t trailing; |
| 295 | size_t unwind_slice_idx; |
| 296 | size_t unwind_byte_idx; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 297 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 298 | for (;;) { |
| 299 | sending_length = 0; |
| 300 | unwind_slice_idx = tcp->outgoing_slice_idx; |
| 301 | unwind_byte_idx = tcp->outgoing_byte_idx; |
| 302 | for (iov_size = 0; tcp->outgoing_slice_idx != tcp->outgoing_buffer->count && |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 303 | iov_size != MAX_WRITE_IOVEC; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 304 | iov_size++) { |
| 305 | iov[iov_size].iov_base = |
| 306 | GPR_SLICE_START_PTR( |
| 307 | tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) + |
| 308 | tcp->outgoing_byte_idx; |
| 309 | iov[iov_size].iov_len = |
| 310 | GPR_SLICE_LENGTH( |
| 311 | tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) - |
| 312 | tcp->outgoing_byte_idx; |
| 313 | sending_length += iov[iov_size].iov_len; |
| 314 | tcp->outgoing_slice_idx++; |
| 315 | tcp->outgoing_byte_idx = 0; |
| 316 | } |
| 317 | GPR_ASSERT(iov_size > 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 318 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 319 | msg.msg_name = NULL; |
| 320 | msg.msg_namelen = 0; |
| 321 | msg.msg_iov = iov; |
| 322 | msg.msg_iovlen = iov_size; |
| 323 | msg.msg_control = NULL; |
| 324 | msg.msg_controllen = 0; |
| 325 | msg.msg_flags = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 326 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 327 | GPR_TIMER_BEGIN("sendmsg", 1); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 328 | do { |
| 329 | /* TODO(klempner): Cork if this is a partial write */ |
| 330 | sent_length = sendmsg(tcp->fd, &msg, SENDMSG_FLAGS); |
| 331 | } while (sent_length < 0 && errno == EINTR); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 332 | GPR_TIMER_END("sendmsg", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 333 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 334 | if (sent_length < 0) { |
| 335 | if (errno == EAGAIN) { |
| 336 | tcp->outgoing_slice_idx = unwind_slice_idx; |
| 337 | tcp->outgoing_byte_idx = unwind_byte_idx; |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 338 | return false; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 339 | } else { |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 340 | *error = GRPC_OS_ERROR(errno, "sendmsg"); |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 341 | return true; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 344 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 345 | GPR_ASSERT(tcp->outgoing_byte_idx == 0); |
| 346 | trailing = sending_length - (size_t)sent_length; |
| 347 | while (trailing > 0) { |
| 348 | size_t slice_length; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 349 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 350 | tcp->outgoing_slice_idx--; |
| 351 | slice_length = GPR_SLICE_LENGTH( |
| 352 | tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]); |
| 353 | if (slice_length > trailing) { |
| 354 | tcp->outgoing_byte_idx = slice_length - trailing; |
| 355 | break; |
| 356 | } else { |
| 357 | trailing -= slice_length; |
| 358 | } |
| 359 | } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 360 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 361 | if (tcp->outgoing_slice_idx == tcp->outgoing_buffer->count) { |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 362 | *error = GRPC_ERROR_NONE; |
| 363 | return true; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 364 | } |
| 365 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 368 | static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 369 | grpc_error *error) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 370 | grpc_tcp *tcp = (grpc_tcp *)arg; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 371 | grpc_closure *cb; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 372 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 373 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 374 | cb = tcp->write_cb; |
| 375 | tcp->write_cb = NULL; |
Mark D. Roth | b06c640 | 2016-05-13 12:39:30 -0700 | [diff] [blame] | 376 | cb->cb(exec_ctx, cb->cb_arg, error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 377 | TCP_UNREF(exec_ctx, tcp, "write"); |
| 378 | return; |
| 379 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 380 | |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 381 | if (!tcp_flush(tcp, &error)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 382 | grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure); |
| 383 | } else { |
| 384 | cb = tcp->write_cb; |
| 385 | tcp->write_cb = NULL; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 386 | GPR_TIMER_BEGIN("tcp_handle_write.cb", 0); |
Mark D. Roth | b06c640 | 2016-05-13 12:39:30 -0700 | [diff] [blame] | 387 | cb->cb(exec_ctx, cb->cb_arg, error); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 388 | GPR_TIMER_END("tcp_handle_write.cb", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 389 | TCP_UNREF(exec_ctx, tcp, "write"); |
Craig Tiller | e0d6c57 | 2016-05-13 07:23:36 -0700 | [diff] [blame] | 390 | GRPC_ERROR_UNREF(error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 391 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 394 | static void tcp_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 395 | gpr_slice_buffer *buf, grpc_closure *cb) { |
| 396 | grpc_tcp *tcp = (grpc_tcp *)ep; |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 397 | grpc_error *error = GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 398 | |
Craig Tiller | 449c64b | 2016-06-13 16:26:50 -0700 | [diff] [blame] | 399 | if (false && grpc_tcp_trace) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 400 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 401 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 402 | for (i = 0; i < buf->count; i++) { |
| 403 | char *data = |
| 404 | gpr_dump_slice(buf->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII); |
Mark D. Roth | bc84672 | 2016-05-04 10:53:50 -0700 | [diff] [blame] | 405 | gpr_log(GPR_DEBUG, "WRITE %p (peer=%s): %s", tcp, tcp->peer_string, data); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 406 | gpr_free(data); |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 407 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 408 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 409 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 410 | GPR_TIMER_BEGIN("tcp_write", 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 411 | GPR_ASSERT(tcp->write_cb == NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 412 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 413 | if (buf->length == 0) { |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 414 | GPR_TIMER_END("tcp_write", 0); |
Craig Tiller | 13c0940 | 2016-06-15 09:41:33 -0700 | [diff] [blame] | 415 | grpc_exec_ctx_sched(exec_ctx, cb, grpc_fd_is_shutdown(tcp->em_fd) |
| 416 | ? GRPC_ERROR_CREATE("EOF") |
| 417 | : GRPC_ERROR_NONE, |
| 418 | NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 419 | return; |
| 420 | } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 421 | tcp->outgoing_buffer = buf; |
| 422 | tcp->outgoing_slice_idx = 0; |
| 423 | tcp->outgoing_byte_idx = 0; |
| 424 | |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 425 | if (!tcp_flush(tcp, &error)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 426 | TCP_REF(tcp, "write"); |
| 427 | tcp->write_cb = cb; |
| 428 | grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure); |
| 429 | } else { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 430 | grpc_exec_ctx_sched(exec_ctx, cb, error, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 431 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 432 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 433 | GPR_TIMER_END("tcp_write", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 436 | static void tcp_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 437 | grpc_pollset *pollset) { |
| 438 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 439 | grpc_pollset_add_fd(exec_ctx, pollset, tcp->em_fd); |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 442 | static void tcp_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 443 | grpc_pollset_set *pollset_set) { |
| 444 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 445 | grpc_pollset_set_add_fd(exec_ctx, pollset_set, tcp->em_fd); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 448 | static char *tcp_get_peer(grpc_endpoint *ep) { |
| 449 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 450 | return gpr_strdup(tcp->peer_string); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 451 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 452 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 453 | static const grpc_endpoint_vtable vtable = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 454 | tcp_read, tcp_write, tcp_add_to_pollset, tcp_add_to_pollset_set, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 455 | tcp_shutdown, tcp_destroy, tcp_get_peer}; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 456 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 457 | grpc_endpoint *grpc_tcp_create(grpc_fd *em_fd, size_t slice_size, |
| 458 | const char *peer_string) { |
| 459 | grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 460 | tcp->base.vtable = &vtable; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 461 | tcp->peer_string = gpr_strdup(peer_string); |
Craig Tiller | 0a8a017 | 2016-02-25 07:36:27 -0800 | [diff] [blame] | 462 | tcp->fd = grpc_fd_wrapped_fd(em_fd); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 463 | tcp->read_cb = NULL; |
| 464 | tcp->write_cb = NULL; |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 465 | tcp->release_fd_cb = NULL; |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 466 | tcp->release_fd = NULL; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 467 | tcp->incoming_buffer = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 468 | tcp->slice_size = slice_size; |
Craig Tiller | 2b1fd66 | 2015-04-28 07:33:37 -0700 | [diff] [blame] | 469 | tcp->iov_size = 1; |
Mark D. Roth | 808ac38 | 2016-05-13 11:14:21 -0700 | [diff] [blame] | 470 | tcp->finished_edge = true; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 471 | /* paired with unref in grpc_tcp_destroy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 472 | gpr_ref_init(&tcp->refcount, 1); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 473 | tcp->em_fd = em_fd; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 474 | tcp->read_closure.cb = tcp_handle_read; |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 475 | tcp->read_closure.cb_arg = tcp; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 476 | tcp->write_closure.cb = tcp_handle_write; |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 477 | tcp->write_closure.cb_arg = tcp; |
Craig Tiller | 649deeb | 2015-09-24 23:19:40 -0700 | [diff] [blame] | 478 | gpr_slice_buffer_init(&tcp->last_read_buffer); |
Makarand Dharmapurikar | 0579cfc | 2016-06-20 15:45:24 -0700 | [diff] [blame] | 479 | /* Tell network status tracker about new endpoint */ |
| 480 | grpc_network_status_register_endpoint(&tcp->base); |
David Garcia Quintas | 5f228f5 | 2015-05-26 19:58:50 -0700 | [diff] [blame] | 481 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 482 | return &tcp->base; |
| 483 | } |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 484 | |
Dan Born | 43a7803 | 2016-01-05 17:17:45 -0800 | [diff] [blame] | 485 | int grpc_tcp_fd(grpc_endpoint *ep) { |
| 486 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 487 | GPR_ASSERT(ep->vtable == &vtable); |
| 488 | return grpc_fd_wrapped_fd(tcp->em_fd); |
| 489 | } |
| 490 | |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 491 | void grpc_tcp_destroy_and_release_fd(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 492 | int *fd, grpc_closure *done) { |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 493 | grpc_tcp *tcp = (grpc_tcp *)ep; |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 494 | GPR_ASSERT(ep->vtable == &vtable); |
| 495 | tcp->release_fd = fd; |
yang-g | dc21593 | 2015-11-30 14:25:01 -0800 | [diff] [blame] | 496 | tcp->release_fd_cb = done; |
| 497 | TCP_UNREF(exec_ctx, tcp, "destroy"); |
| 498 | } |
| 499 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 500 | #endif |