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 | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_LIB_IOMGR_ENDPOINT_H |
| 35 | #define GRPC_CORE_LIB_IOMGR_ENDPOINT_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
Craig Tiller | b37d53e | 2016-10-26 16:16:35 -0700 | [diff] [blame] | 37 | #include <grpc/slice.h> |
| 38 | #include <grpc/slice_buffer.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc/support/time.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 40 | #include "src/core/lib/iomgr/pollset.h" |
| 41 | #include "src/core/lib/iomgr/pollset_set.h" |
Craig Tiller | afcc875 | 2016-10-18 16:10:06 -0700 | [diff] [blame] | 42 | #include "src/core/lib/iomgr/resource_quota.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | |
| 44 | /* An endpoint caps a streaming channel between two communicating processes. |
| 45 | Examples may be: a tcp socket, <stdin+stdout>, or some shared memory. */ |
| 46 | |
| 47 | typedef struct grpc_endpoint grpc_endpoint; |
| 48 | typedef struct grpc_endpoint_vtable grpc_endpoint_vtable; |
| 49 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 50 | struct grpc_endpoint_vtable { |
| 51 | void (*read)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 52 | grpc_slice_buffer *slices, grpc_closure *cb); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 53 | void (*write)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 54 | grpc_slice_buffer *slices, grpc_closure *cb); |
Craig Tiller | 70bd483 | 2016-06-30 14:20:46 -0700 | [diff] [blame] | 55 | grpc_workqueue *(*get_workqueue)(grpc_endpoint *ep); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 56 | void (*add_to_pollset)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 57 | grpc_pollset *pollset); |
| 58 | void (*add_to_pollset_set)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 59 | grpc_pollset_set *pollset); |
Craig Tiller | cda759d | 2017-01-27 11:37:37 -0800 | [diff] [blame] | 60 | void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, grpc_error *why); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 61 | void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep); |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 62 | grpc_resource_user *(*get_resource_user)(grpc_endpoint *ep); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 63 | char *(*get_peer)(grpc_endpoint *ep); |
Yuchen Zeng | 68413c2 | 2016-11-02 11:57:37 -0700 | [diff] [blame] | 64 | int (*get_fd)(grpc_endpoint *ep); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 67 | /* When data is available on the connection, calls the callback with slices. |
| 68 | Callback success indicates that the endpoint can accept more reads, failure |
| 69 | indicates the endpoint is closed. |
Mark D. Roth | 4623e1c | 2016-07-18 14:09:18 -0700 | [diff] [blame] | 70 | Valid slices may be placed into \a slices even when the callback is |
| 71 | invoked with error != GRPC_ERROR_NONE. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 72 | void grpc_endpoint_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 73 | grpc_slice_buffer *slices, grpc_closure *cb); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 74 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 75 | char *grpc_endpoint_get_peer(grpc_endpoint *ep); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 76 | |
Yuchen Zeng | e942434 | 2016-11-04 15:40:20 -0700 | [diff] [blame] | 77 | /* Get the file descriptor used by \a ep. Return -1 if \a ep is not using an fd. |
Yuchen Zeng | 68413c2 | 2016-11-02 11:57:37 -0700 | [diff] [blame] | 78 | */ |
| 79 | int grpc_endpoint_get_fd(grpc_endpoint *ep); |
Yuchen Zeng | e5ec9ac | 2016-10-24 14:43:12 -0700 | [diff] [blame] | 80 | |
Craig Tiller | 70bd483 | 2016-06-30 14:20:46 -0700 | [diff] [blame] | 81 | /* Retrieve a reference to the workqueue associated with this endpoint */ |
| 82 | grpc_workqueue *grpc_endpoint_get_workqueue(grpc_endpoint *ep); |
| 83 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | /* Write slices out to the socket. |
| 85 | |
| 86 | If the connection is ready for more data after the end of the call, it |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 87 | returns GRPC_ENDPOINT_DONE. |
| 88 | Otherwise it returns GRPC_ENDPOINT_PENDING and calls cb when the |
| 89 | connection is ready for more data. |
| 90 | \a slices may be mutated at will by the endpoint until cb is called. |
| 91 | No guarantee is made to the content of slices after a write EXCEPT that |
| 92 | it is a valid slice buffer. |
| 93 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 94 | void grpc_endpoint_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 95 | grpc_slice_buffer *slices, grpc_closure *cb); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 96 | |
Craig Tiller | 52f2312 | 2016-06-15 09:34:14 -0700 | [diff] [blame] | 97 | /* Causes any pending and future read/write callbacks to run immediately with |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 98 | success==0 */ |
Craig Tiller | cda759d | 2017-01-27 11:37:37 -0800 | [diff] [blame] | 99 | void grpc_endpoint_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 100 | grpc_error *why); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 101 | void grpc_endpoint_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 102 | |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 103 | /* Add an endpoint to a pollset, so that when the pollset is polled, events from |
| 104 | this endpoint are considered */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 105 | void grpc_endpoint_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 106 | grpc_pollset *pollset); |
| 107 | void grpc_endpoint_add_to_pollset_set(grpc_exec_ctx *exec_ctx, |
| 108 | grpc_endpoint *ep, |
| 109 | grpc_pollset_set *pollset_set); |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 110 | |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 111 | grpc_resource_user *grpc_endpoint_get_resource_user(grpc_endpoint *endpoint); |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 112 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 113 | struct grpc_endpoint { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | const grpc_endpoint_vtable *vtable; |
| 115 | }; |
| 116 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 117 | #endif /* GRPC_CORE_LIB_IOMGR_ENDPOINT_H */ |