| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2019-2022 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #include "grn.h" |
| 23 | #include "grn_str.h" |
| 24 | #include "grn_hash.h" |
| 25 | |
| 26 | #ifdef HAVE_NETDB_H |
| 27 | #include <netdb.h> |
| 28 | #endif /* HAVE_NETDB_H */ |
| 29 | |
| 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
| 34 | /******* grn_com_queue ********/ |
| 35 | |
| 36 | typedef struct _grn_com_queue grn_com_queue; |
| 37 | typedef struct _grn_com_queue_entry grn_com_queue_entry; |
| 38 | |
| 39 | #define GRN_COM_QUEUE_BINSIZE (0x100) |
| 40 | |
| 41 | struct _grn_com_queue_entry { |
| 42 | grn_obj obj; |
| 43 | struct _grn_com_queue_entry *next; |
| 44 | }; |
| 45 | |
| 46 | struct _grn_com_queue { |
| 47 | grn_com_queue_entry *next; |
| 48 | grn_com_queue_entry **tail; |
| 49 | uint64_t size; |
| 50 | grn_critical_section cs; |
| 51 | }; |
| 52 | |
| 53 | #define GRN_COM_QUEUE_INIT(q) do {\ |
| 54 | (q)->next = NULL;\ |
| 55 | (q)->tail = &(q)->next;\ |
| 56 | (q)->size = 0;\ |
| 57 | CRITICAL_SECTION_INIT((q)->cs);\ |
| 58 | } while (0) |
| 59 | |
| 60 | #define GRN_COM_QUEUE_EMPTYP(q) ((q)->size == 0) |
| 61 | |
| 62 | GRN_API grn_rc grn_com_queue_enque(grn_ctx *ctx, grn_com_queue *q, grn_com_queue_entry *e); |
| 63 | GRN_API grn_com_queue_entry *grn_com_queue_deque(grn_ctx *ctx, grn_com_queue *q); |
| 64 | uint64_t grn_com_queue_size(grn_ctx *ctx, grn_com_queue *q); |
| 65 | |
| 66 | GRN_API void grn_job_queue_current_set(grn_ctx *ctx, grn_com_queue *queue); |
| 67 | grn_com_queue *grn_job_queue_current_get(grn_ctx *ctx); |
| 68 | |
| 69 | /******* grn_com ********/ |
| 70 | |
| 71 | #ifdef USE_SELECT |
| 72 | # ifdef HAVE_SYS_SELECT_H |
| 73 | # include <sys/select.h> |
| 74 | # endif /* HAVE_SYS_SELECT_H */ |
| 75 | # define GRN_COM_POLLIN 1 |
| 76 | # define GRN_COM_POLLOUT 2 |
| 77 | #else /* USE_SELECT */ |
| 78 | # ifdef USE_EPOLL |
| 79 | # include <sys/epoll.h> |
| 80 | # define GRN_COM_POLLIN EPOLLIN |
| 81 | # define GRN_COM_POLLOUT EPOLLOUT |
| 82 | # else /* USE_EPOLL */ |
| 83 | # ifdef USE_KQUEUE |
| 84 | # include <sys/event.h> |
| 85 | # define GRN_COM_POLLIN EVFILT_READ |
| 86 | # define GRN_COM_POLLOUT EVFILT_WRITE |
| 87 | # else /* USE_KQUEUE */ |
| 88 | # include <poll.h> |
| 89 | # define GRN_COM_POLLIN POLLIN |
| 90 | # define GRN_COM_POLLOUT POLLOUT |
| 91 | # endif /* USE_KQUEUE */ |
| 92 | # endif /* USE_EPOLL */ |
| 93 | #endif /* USE_SELECT */ |
| 94 | |
| 95 | #define GRN_COM_EVENT_LISTEN_BACKLOG_DEFAULT 0x1000 |
| 96 | |
| 97 | typedef struct _grn_com grn_com; |
| 98 | typedef struct _grn_com_event grn_com_event; |
| 99 | typedef struct _grn_com_addr grn_com_addr; |
| 100 | typedef void grn_com_callback(grn_ctx *ctx, grn_com_event *, grn_com *); |
| 101 | typedef void grn_msg_handler(grn_ctx *ctx, grn_obj *msg); |
| 102 | |
| 103 | struct _grn_com_addr { |
| 104 | uint32_t addr; |
| 105 | uint16_t port; |
| 106 | uint16_t sid; |
| 107 | }; |
| 108 | |
| 109 | struct _grn_com { |
| 110 | grn_sock fd; |
| 111 | int events; |
| 112 | uint16_t sid; |
| 113 | uint8_t has_sid; |
| 114 | uint8_t closed; |
| 115 | grn_com_queue new_; |
| 116 | grn_com_event *ev; |
| 117 | void *opaque; |
| 118 | bool accepting; |
| 119 | }; |
| 120 | |
| 121 | struct _grn_com_event { |
| 122 | struct _grn_hash *hash; |
| 123 | int max_nevents; |
| 124 | grn_ctx *ctx; |
| 125 | grn_critical_section critical_section; |
| 126 | grn_cond cond; |
| 127 | grn_com_queue recv_old; |
| 128 | grn_msg_handler *msg_handler; |
| 129 | grn_com_addr curr_edge_id; |
| 130 | grn_com *acceptor; |
| 131 | int listen_backlog; |
| 132 | void *opaque; |
| 133 | #ifndef USE_SELECT |
| 134 | #ifdef USE_EPOLL |
| 135 | int epfd; |
| 136 | struct epoll_event *events; |
| 137 | #else /* USE_EPOLL */ |
| 138 | #ifdef USE_KQUEUE |
| 139 | int kqfd; |
| 140 | struct kevent *events; |
| 141 | #else /* USE_KQUEUE */ |
| 142 | int dummy; /* dummy */ |
| 143 | struct pollfd *events; |
| 144 | #endif /* USE_KQUEUE */ |
| 145 | #endif /* USE_EPOLL */ |
| 146 | #endif /* USE_SELECT */ |
| 147 | }; |
| 148 | |
| 149 | grn_rc grn_com_init(void); |
| 150 | void grn_com_fin(void); |
| 151 | GRN_API grn_rc grn_com_event_init(grn_ctx *ctx, grn_com_event *ev, int max_nevents, int data_size); |
| 152 | GRN_API grn_rc grn_com_event_fin(grn_ctx *ctx, grn_com_event *ev); |
| 153 | GRN_API void grn_com_event_set_listen_backlog(grn_ctx *ctx, |
| 154 | grn_com_event *ev, |
| 155 | int backlog); |
| 156 | GRN_API int grn_com_event_get_listen_backlog(grn_ctx *ctx, grn_com_event *ev); |
| 157 | GRN_API grn_rc grn_com_event_start_accept(grn_ctx *ctx, grn_com_event *ev); |
| 158 | grn_rc grn_com_event_stop_accept(grn_ctx *ctx, grn_com_event *ev); |
| 159 | grn_rc grn_com_event_add(grn_ctx *ctx, grn_com_event *ev, grn_sock fd, int events, grn_com **com); |
| 160 | grn_rc grn_com_event_mod(grn_ctx *ctx, grn_com_event *ev, grn_sock fd, int events, grn_com **com); |
| 161 | GRN_API grn_rc grn_com_event_del(grn_ctx *ctx, grn_com_event *ev, grn_sock fd); |
| 162 | GRN_API grn_rc grn_com_event_poll(grn_ctx *ctx, grn_com_event *ev, int timeout); |
| 163 | grn_rc grn_com_event_each(grn_ctx *ctx, grn_com_event *ev, grn_com_callback *func); |
| 164 | |
| 165 | /******* grn_com_gqtp ********/ |
| 166 | |
| 167 | #define GRN_COM_PROTO_HTTP 0x47 |
| 168 | #define GRN_COM_PROTO_GQTP 0xc7 |
| 169 | #define GRN_COM_PROTO_MBREQ 0x80 |
| 170 | #define GRN_COM_PROTO_MBRES 0x81 |
| 171 | |
| 172 | typedef struct _grn_com_header grn_com_header; |
| 173 | |
| 174 | struct _grn_com_header { |
| 175 | uint8_t proto; |
| 176 | uint8_t qtype; |
| 177 | uint16_t keylen; |
| 178 | uint8_t level; |
| 179 | uint8_t flags; |
| 180 | uint16_t status; |
| 181 | uint32_t size; |
| 182 | uint32_t opaque; |
| 183 | uint64_t cas; |
| 184 | }; |
| 185 | |
| 186 | GRN_API grn_com *grn_com_copen(grn_ctx *ctx, grn_com_event *ev, const char *dest, int port); |
| 187 | GRN_API grn_rc grn_com_sopen(grn_ctx *ctx, grn_com_event *ev, |
| 188 | const char *bind_address, uint16_t port, |
| 189 | grn_msg_handler *func, struct hostent *he); |
| 190 | |
| 191 | GRN_API void grn_com_close_(grn_ctx *ctx, grn_com *com); |
| 192 | GRN_API grn_rc grn_com_close(grn_ctx *ctx, grn_com *com); |
| 193 | |
| 194 | GRN_API grn_rc grn_com_send(grn_ctx *ctx, grn_com *cs, |
| 195 | grn_com_header *header, const char *body, uint32_t size, int flags); |
| 196 | grn_rc grn_com_recv(grn_ctx *ctx, grn_com *cs, grn_com_header *header, grn_obj *buf); |
| 197 | GRN_API grn_rc grn_com_send_http(grn_ctx *ctx, grn_com *cs, const char *path, uint32_t path_len, int flags); |
| 198 | |
| 199 | /******* grn_msg ********/ |
| 200 | |
| 201 | typedef struct _grn_msg grn_msg; |
| 202 | |
| 203 | struct _grn_msg { |
| 204 | grn_com_queue_entry qe; |
| 205 | union { |
| 206 | grn_com *peer; |
| 207 | grn_sock fd; |
| 208 | } u; |
| 209 | grn_ctx *ctx; |
| 210 | grn_com_queue *old; |
| 211 | grn_com_header header; |
| 212 | grn_com_addr edge_id; |
| 213 | grn_com *acceptor; |
| 214 | }; |
| 215 | |
| 216 | GRN_API grn_rc grn_msg_send(grn_ctx *ctx, grn_obj *msg, int flags); |
| 217 | GRN_API grn_obj *grn_msg_open_for_reply(grn_ctx *ctx, grn_obj *query, grn_com_queue *old); |
| 218 | GRN_API grn_obj *grn_msg_open(grn_ctx *ctx, grn_com *com, grn_com_queue *old); |
| 219 | GRN_API grn_rc grn_msg_set_property(grn_ctx *ctx, grn_obj *obj, |
| 220 | uint16_t status, uint16_t key_size, uint8_t extra_size); |
| 221 | GRN_API grn_rc grn_msg_close(grn_ctx *ctx, grn_obj *msg); |
| 222 | |
| 223 | /******* grn_edge ********/ |
| 224 | |
| 225 | #define GRN_EDGE_WORKER 0 |
| 226 | #define GRN_EDGE_COMMUNICATOR 1 |
| 227 | |
| 228 | typedef struct { |
| 229 | grn_com_queue_entry eq; |
| 230 | grn_ctx ctx; |
| 231 | grn_com_queue recv_new; |
| 232 | grn_com_queue send_old; |
| 233 | grn_com *com; |
| 234 | grn_com_addr *addr; |
| 235 | grn_msg *msg; |
| 236 | uint8_t stat; |
| 237 | uint8_t flags; |
| 238 | grn_id id; |
| 239 | } grn_edge; |
| 240 | |
| 241 | GRN_VAR grn_hash *grn_edges; |
| 242 | GRN_API void grn_edges_init(grn_ctx *ctx, void (*dispatcher)(grn_ctx *ctx, grn_edge *edge)); |
| 243 | GRN_API void grn_edges_fin(grn_ctx *ctx); |
| 244 | GRN_API grn_edge *grn_edges_add(grn_ctx *ctx, grn_com_addr *addr, int *added); |
| 245 | grn_edge *grn_edges_add_communicator(grn_ctx *ctx, grn_com_addr *addr); |
| 246 | GRN_API void grn_edges_delete(grn_ctx *ctx, grn_edge *edge); |
| 247 | void grn_edge_dispatch(grn_ctx *ctx, grn_edge *edge, grn_obj *msg); |
| 248 | |
| 249 | #ifdef __cplusplus |
| 250 | } |
| 251 | #endif |