diff options
author | Amitay Isaacs <amitay@gmail.com> | 2015-04-04 20:23:44 +1100 |
---|---|---|
committer | Amitay Isaacs <amitay@samba.org> | 2015-10-07 14:53:29 +0200 |
commit | 1543eedb8feaa85336216aa22df2145522425184 (patch) | |
tree | 2ee364582c7a7e84c6bf2e8e2714bdd1e9910a21 /ctdb/common/comm.h | |
parent | e01c0eed38335e7b421ab4f79410f08ab1d31482 (diff) | |
download | samba-1543eedb8feaa85336216aa22df2145522425184.tar.gz |
ctdb-common: Add communication endpoint abstraction
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
Diffstat (limited to 'ctdb/common/comm.h')
-rw-r--r-- | ctdb/common/comm.h | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/ctdb/common/comm.h b/ctdb/common/comm.h new file mode 100644 index 00000000000..27021e945af --- /dev/null +++ b/ctdb/common/comm.h @@ -0,0 +1,101 @@ +/* + Communication endpoint API + + Copyright (C) Amitay Isaacs 2015 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __CTDB_COMM_H__ +#define __CTDB_COMM_H__ + +#include <talloc.h> +#include <tevent.h> + +/** + * @file comm.h + * + * @brief Communication over a socket or file descriptor + * + * This abstraction is a wrapper around a socket or file descriptor to + * send/receive complete packets. + */ + +/** + * @brief Packet handler function + * + * This function is registered while setting up communication endpoint. Any + * time packets are read, this function is called. + */ +typedef void (*comm_read_handler_fn)(uint8_t *buf, size_t buflen, + void *private_data); + +/** + * @brief Communication endpoint dead handler function + * + * This function is called when the communication endpoint is closed. + */ +typedef void (*comm_dead_handler_fn)(void *private_data); + +/** + * @brief Abstract struct to store communication endpoint details + */ +struct comm_context; + +/** + * @brief Initialize the communication endpoint + * + * This return a new communication context. Freeing this context will free all + * memory assoicated with it. + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] fd The socket or file descriptor + * @param[in] read_handler The packet handler function + * @param[in] read_private_data Private data for read handler function + * @param[in] dead_handler The communication dead handler function + * @param[in] dead_private_data Private data for dead handler function + * @param[out] result The new comm_context structure + * @return 0 on success, errno on failure + */ +int comm_setup(TALLOC_CTX *mem_ctx, struct tevent_context *ev, int fd, + comm_read_handler_fn read_handler, void *read_private_data, + comm_dead_handler_fn dead_handler, void *dead_private_data, + struct comm_context **result); + +/** + * @brief Async computation start to send a packet + * + * @param[in] mem_ctx Talloc memory context + * @param[in] ev Tevent context + * @param[in] comm Communication context + * @param[in] buf The packet data + * @param[in] buflen The size of the packet + * @return new tevent request, or NULL on failure + */ +struct tevent_req *comm_write_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct comm_context *comm, + uint8_t *buf, size_t buflen); + +/** + * @brief Async computation end to send a packet + * + * @param[in] req Tevent request + * @param[out] perr errno in case of failure + * @return true on success, false on failure + */ +bool comm_write_recv(struct tevent_req *req, int *perr); + +#endif /* __CTDB_COMM_H__ */ |