Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
David Garcia Quintas | 3598d44 | 2016-03-15 14:53:05 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, 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 | |
| 37 | #include <grpc/support/slice.h> |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 38 | #include <grpc/support/slice_buffer.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc/support/time.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 40 | #include "src/core/iomgr/pollset.h" |
| 41 | #include "src/core/iomgr/pollset_set.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | |
| 43 | /* An endpoint caps a streaming channel between two communicating processes. |
| 44 | Examples may be: a tcp socket, <stdin+stdout>, or some shared memory. */ |
| 45 | |
| 46 | typedef struct grpc_endpoint grpc_endpoint; |
| 47 | typedef struct grpc_endpoint_vtable grpc_endpoint_vtable; |
| 48 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 49 | struct grpc_endpoint_vtable { |
| 50 | void (*read)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 51 | gpr_slice_buffer *slices, grpc_closure *cb); |
| 52 | void (*write)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 53 | gpr_slice_buffer *slices, grpc_closure *cb); |
| 54 | void (*add_to_pollset)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 55 | grpc_pollset *pollset); |
| 56 | void (*add_to_pollset_set)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 57 | grpc_pollset_set *pollset); |
| 58 | void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep); |
| 59 | void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep); |
| 60 | char *(*get_peer)(grpc_endpoint *ep); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 63 | /* When data is available on the connection, calls the callback with slices. |
| 64 | Callback success indicates that the endpoint can accept more reads, failure |
| 65 | indicates the endpoint is closed. |
| 66 | Valid slices may be placed into \a slices even on callback success == 0. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 67 | void grpc_endpoint_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 68 | gpr_slice_buffer *slices, grpc_closure *cb); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 70 | char *grpc_endpoint_get_peer(grpc_endpoint *ep); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 71 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | /* Write slices out to the socket. |
| 73 | |
| 74 | 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] | 75 | returns GRPC_ENDPOINT_DONE. |
| 76 | Otherwise it returns GRPC_ENDPOINT_PENDING and calls cb when the |
| 77 | connection is ready for more data. |
| 78 | \a slices may be mutated at will by the endpoint until cb is called. |
| 79 | No guarantee is made to the content of slices after a write EXCEPT that |
| 80 | it is a valid slice buffer. |
| 81 | */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | void grpc_endpoint_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 83 | gpr_slice_buffer *slices, grpc_closure *cb); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | |
| 85 | /* Causes any pending read/write callbacks to run immediately with |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 86 | success==0 */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 87 | void grpc_endpoint_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep); |
| 88 | void grpc_endpoint_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 89 | |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 90 | /* Add an endpoint to a pollset, so that when the pollset is polled, events from |
| 91 | this endpoint are considered */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | void grpc_endpoint_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 93 | grpc_pollset *pollset); |
| 94 | void grpc_endpoint_add_to_pollset_set(grpc_exec_ctx *exec_ctx, |
| 95 | grpc_endpoint *ep, |
| 96 | grpc_pollset_set *pollset_set); |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 97 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 98 | struct grpc_endpoint { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | const grpc_endpoint_vtable *vtable; |
| 100 | }; |
| 101 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 102 | #endif /* GRPC_CORE_LIB_IOMGR_ENDPOINT_H */ |