blob: 740357ecc5480412a933388240265f8292b51ed9 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_IOMGR_ENDPOINT_H
35#define GRPC_CORE_LIB_IOMGR_ENDPOINT_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
Craig Tillerb37d53e2016-10-26 16:16:35 -070037#include <grpc/slice.h>
38#include <grpc/slice_buffer.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <grpc/support/time.h>
Craig Tiller9533d042016-03-25 17:11:06 -070040#include "src/core/lib/iomgr/pollset.h"
41#include "src/core/lib/iomgr/pollset_set.h"
Craig Tillerafcc8752016-10-18 16:10:06 -070042#include "src/core/lib/iomgr/resource_quota.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043
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
47typedef struct grpc_endpoint grpc_endpoint;
48typedef struct grpc_endpoint_vtable grpc_endpoint_vtable;
49
Craig Tillera82950e2015-09-22 12:33:20 -070050struct grpc_endpoint_vtable {
51 void (*read)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
Craig Tillerd41a4a72016-10-26 16:16:06 -070052 grpc_slice_buffer *slices, grpc_closure *cb);
Craig Tillera82950e2015-09-22 12:33:20 -070053 void (*write)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
Craig Tillerd41a4a72016-10-26 16:16:06 -070054 grpc_slice_buffer *slices, grpc_closure *cb);
Craig Tiller70bd4832016-06-30 14:20:46 -070055 grpc_workqueue *(*get_workqueue)(grpc_endpoint *ep);
Craig Tillera82950e2015-09-22 12:33:20 -070056 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 Tillercda759d2017-01-27 11:37:37 -080060 void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, grpc_error *why);
Craig Tillera82950e2015-09-22 12:33:20 -070061 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep);
Craig Tiller20afa3d2016-10-17 14:52:14 -070062 grpc_resource_user *(*get_resource_user)(grpc_endpoint *ep);
Craig Tillera82950e2015-09-22 12:33:20 -070063 char *(*get_peer)(grpc_endpoint *ep);
Yuchen Zeng68413c22016-11-02 11:57:37 -070064 int (*get_fd)(grpc_endpoint *ep);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065};
66
Craig Tillerb0298592015-08-27 07:38:01 -070067/* 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. Roth4623e1c2016-07-18 14:09:18 -070070 Valid slices may be placed into \a slices even when the callback is
71 invoked with error != GRPC_ERROR_NONE. */
Craig Tillera82950e2015-09-22 12:33:20 -070072void grpc_endpoint_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
Craig Tillerd41a4a72016-10-26 16:16:06 -070073 grpc_slice_buffer *slices, grpc_closure *cb);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074
Craig Tillera82950e2015-09-22 12:33:20 -070075char *grpc_endpoint_get_peer(grpc_endpoint *ep);
Craig Tiller1b22b9d2015-07-20 13:42:22 -070076
Yuchen Zenge9424342016-11-04 15:40:20 -070077/* Get the file descriptor used by \a ep. Return -1 if \a ep is not using an fd.
Yuchen Zeng68413c22016-11-02 11:57:37 -070078 */
79int grpc_endpoint_get_fd(grpc_endpoint *ep);
Yuchen Zenge5ec9ac2016-10-24 14:43:12 -070080
Craig Tiller70bd4832016-06-30 14:20:46 -070081/* Retrieve a reference to the workqueue associated with this endpoint */
82grpc_workqueue *grpc_endpoint_get_workqueue(grpc_endpoint *ep);
83
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080084/* Write slices out to the socket.
85
86 If the connection is ready for more data after the end of the call, it
Craig Tillerb0298592015-08-27 07:38:01 -070087 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 Tillera82950e2015-09-22 12:33:20 -070094void grpc_endpoint_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
Craig Tillerd41a4a72016-10-26 16:16:06 -070095 grpc_slice_buffer *slices, grpc_closure *cb);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080096
Craig Tiller52f23122016-06-15 09:34:14 -070097/* Causes any pending and future read/write callbacks to run immediately with
Craig Tillerb0298592015-08-27 07:38:01 -070098 success==0 */
Craig Tillercda759d2017-01-27 11:37:37 -080099void grpc_endpoint_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
100 grpc_error *why);
Craig Tillera82950e2015-09-22 12:33:20 -0700101void grpc_endpoint_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800102
ctillerd79b4862014-12-17 16:36:59 -0800103/* Add an endpoint to a pollset, so that when the pollset is polled, events from
104 this endpoint are considered */
Craig Tillera82950e2015-09-22 12:33:20 -0700105void grpc_endpoint_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
106 grpc_pollset *pollset);
107void grpc_endpoint_add_to_pollset_set(grpc_exec_ctx *exec_ctx,
108 grpc_endpoint *ep,
109 grpc_pollset_set *pollset_set);
ctillerd79b4862014-12-17 16:36:59 -0800110
Craig Tiller20afa3d2016-10-17 14:52:14 -0700111grpc_resource_user *grpc_endpoint_get_resource_user(grpc_endpoint *endpoint);
Craig Tillere34c2852016-09-23 09:43:32 -0700112
Craig Tillera82950e2015-09-22 12:33:20 -0700113struct grpc_endpoint {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800114 const grpc_endpoint_vtable *vtable;
115};
116
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700117#endif /* GRPC_CORE_LIB_IOMGR_ENDPOINT_H */