Debian Codegraph @

lib/com.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2018 Brazil
3 Copyright (C) 2018-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 #include "grn.h"
21
22 #include <stdio.h>
23 #include <string.h>
24 #include "grn_ctx_impl.h"
25
26 #ifdef WIN32
27 # include <ws2tcpip.h>
28 #else
29 # ifdef HAVE_SYS_SOCKET_H
30 # include <sys/socket.h>
31 # endif /* HAVE_SYS_SOCKET_H */
32 # include <netinet/in.h>
33 # include <netinet/tcp.h>
34 # ifdef HAVE_SIGNAL_H
35 # include <signal.h>
36 # endif /* HAVE_SIGNAL_H */
37 # include <sys/uio.h>
38 #endif /* WIN32 */
39
40 #include "grn_ctx.h"
41 #include "grn_com.h"
42
43 #ifndef PF_INET
44 # define PF_INET AF_INET
45 #endif /* PF_INET */
46
47 #ifndef SOL_TCP
48 # ifdef IPPROTO_TCP
49 # define SOL_TCP IPPROTO_TCP
50 # else
51 # define SOL_TCP 6
52 # endif /* IPPROTO_TCP */
53 #endif /* SOL_TCP */
54
55 #ifndef USE_MSG_MORE
56 # ifdef MSG_MORE
57 # undef MSG_MORE
58 # endif
59 # define MSG_MORE 0
60 #endif /* USE_MSG_MORE */
61
62 #ifndef USE_MSG_NOSIGNAL
63 # ifdef MSG_NOSIGNAL
64 # undef MSG_NOSIGNAL
65 # endif
66 # define MSG_NOSIGNAL 0
67 #endif /* USE_MSG_NOSIGNAL */
68 /******* grn_com_queue ********/
69
70 grn_rc
71 grn_com_queue_enque(grn_ctx *ctx, grn_com_queue *q, grn_com_queue_entry *e)
72 {
73 CRITICAL_SECTION_ENTER(q->cs);
74 e->next = NULL;
75 *q->tail = e;
76 q->tail = &e->next;
77 q->size++;
78 CRITICAL_SECTION_LEAVE(q->cs);
79 return GRN_SUCCESS;
80 }
81
82 grn_com_queue_entry *
83 grn_com_queue_deque(grn_ctx *ctx, grn_com_queue *q)
84 {
85 grn_com_queue_entry *e = NULL;
86
87 CRITICAL_SECTION_ENTER(q->cs);
88 if (q->next) {
89 e = q->next;
90 if (!(q->next = e->next)) {
91 q->tail = &q->next;
92 }
93 q->size--;
94 }
95 CRITICAL_SECTION_LEAVE(q->cs);
96
97 return e;
98 }
99
100 uint64_t
101 grn_com_queue_size(grn_ctx *ctx, grn_com_queue *q)
102 {
103 return q->size;
104 }
105
106 static grn_com_queue *grn_job_queue_current = NULL;
107
108 void
109 grn_job_queue_current_set(grn_ctx *ctx, grn_com_queue *queue)
110 {
111 grn_job_queue_current = queue;
112 }
113
114 grn_com_queue *
115 grn_job_queue_current_get(grn_ctx *ctx)
116 {
117 return grn_job_queue_current;
118 }
119
120 /******* grn_msg ********/
121
122 grn_obj *
123 grn_msg_open(grn_ctx *ctx, grn_com *com, grn_com_queue *old)
124 {
125 grn_msg *msg = NULL;
126 if (old && (msg = (grn_msg *)grn_com_queue_deque(ctx, old))) {
127 if (msg->ctx != ctx) {
128 ERR(GRN_INVALID_ARGUMENT, "ctx unmatch");
129 return NULL;
130 }
131 GRN_BULK_REWIND(&msg->qe.obj);
132 } else if ((msg = GRN_CALLOC(sizeof(grn_msg)))) {
133 GRN_OBJ_INIT(&msg->qe.obj, GRN_MSG, 0, GRN_DB_TEXT);
134 msg->qe.obj.header.impl_flags |= GRN_OBJ_ALLOCATED;
135 msg->ctx = ctx;
136 }
137 msg->qe.next = NULL;
138 msg->u.peer = com;
139 msg->old = old;
140 memset(&msg->header, 0, sizeof(grn_com_header));
141 return (grn_obj *)msg;
142 }
143
144 grn_obj *
145 grn_msg_open_for_reply(grn_ctx *ctx, grn_obj *query, grn_com_queue *old)
146 {
147 grn_msg *req = (grn_msg *)query, *msg = NULL;
148 if (req && (msg = (grn_msg *)grn_msg_open(ctx, req->u.peer, old))) {
149 msg->edge_id = req->edge_id;
150 msg->header.proto = req->header.proto == GRN_COM_PROTO_MBREQ
151 ? GRN_COM_PROTO_MBRES
152 : req->header.proto;
153 }
154 return (grn_obj *)msg;
155 }
156
157 grn_rc
158 grn_msg_close(grn_ctx *ctx, grn_obj *obj)
159 {
160 grn_msg *msg = (grn_msg *)obj;
161 if (ctx == msg->ctx) {
162 return grn_obj_close(ctx, obj);
163 }
164 return grn_com_queue_enque(ctx, msg->old, (grn_com_queue_entry *)msg);
165 }
166
167 grn_rc
168 grn_msg_set_property(grn_ctx *ctx,
169 grn_obj *obj,
170 uint16_t status,
171 uint16_t key_size,
172 uint8_t extra_size)
173 {
174 grn_com_header *header = &((grn_msg *)obj)->header;
175 header->status = htons(status);
176 header->keylen = htons(key_size);
177 header->level = extra_size;
178 return GRN_SUCCESS;
179 }
180
181 grn_rc
182 grn_msg_send(grn_ctx *ctx, grn_obj *msg, int flags)
183 {
184 grn_rc rc;
185 grn_msg *m = (grn_msg *)msg;
186 grn_com *peer = m->u.peer;
187 grn_com_header *header = &m->header;
188 if (GRN_COM_QUEUE_EMPTYP(&peer->new_)) {
189 switch (header->proto) {
190 case GRN_COM_PROTO_HTTP:
191 {
192 ssize_t ret;
193 ret =
194 send(peer->fd, GRN_BULK_HEAD(msg), GRN_BULK_VSIZE(msg), MSG_NOSIGNAL);
195 if (ret == -1) {
196 SOERR("send");
197 }
198 if (ctx->rc != GRN_OPERATION_WOULD_BLOCK) {
199 grn_com_queue_enque(ctx, m->old, (grn_com_queue_entry *)msg);
200 return ctx->rc;
201 }
202 }
203 break;
204 case GRN_COM_PROTO_GQTP:
205 {
206 if (flags & GRN_CTX_MORE) {
207 flags |= GRN_CTX_QUIET;
208 }
209 if (ctx->stat == GRN_CTX_QUIT) {
210 flags |= GRN_CTX_QUIT;
211 }
212 header->qtype = (uint8_t)ctx->impl->output.type;
213 header->keylen = 0;
214 header->level = 0;
215 header->flags = (uint8_t)flags;
216 header->status = htons((uint16_t)ctx->rc);
217 header->opaque = 0;
218 header->cas = 0;
219 // todo : MSG_DONTWAIT
220 rc = grn_com_send(ctx,
221 peer,
222 header,
223 GRN_BULK_HEAD(msg),
224 (uint32_t)GRN_BULK_VSIZE(msg),
225 0);
226 if (rc != GRN_OPERATION_WOULD_BLOCK) {
227 grn_com_queue_enque(ctx, m->old, (grn_com_queue_entry *)msg);
228 return rc;
229 }
230 }
231 break;
232 case GRN_COM_PROTO_MBREQ:
233 return GRN_FUNCTION_NOT_IMPLEMENTED;
234 case GRN_COM_PROTO_MBRES:
235 rc = grn_com_send(ctx,
236 peer,
237 header,
238 GRN_BULK_HEAD(msg),
239 (uint32_t)GRN_BULK_VSIZE(msg),
240 (flags & GRN_CTX_MORE) ? MSG_MORE : 0);
241 if (rc != GRN_OPERATION_WOULD_BLOCK) {
242 grn_com_queue_enque(ctx, m->old, (grn_com_queue_entry *)msg);
243 return rc;
244 }
245 break;
246 default:
247 return GRN_INVALID_ARGUMENT;
248 }
249 }
250 CRITICAL_SECTION_ENTER(peer->ev->critical_section);
251 rc = grn_com_queue_enque(ctx, &peer->new_, (grn_com_queue_entry *)msg);
252 COND_SIGNAL(peer->ev->cond);
253 CRITICAL_SECTION_LEAVE(peer->ev->critical_section);
254 return rc;
255 }
256
257 /******* grn_com ********/
258
259 grn_rc
260 grn_com_init(void)
261 {
262 #ifdef WIN32
263 WSADATA wd;
264 if (WSAStartup(MAKEWORD(2, 0), &wd) != 0) {
265 grn_ctx *ctx = &grn_gctx;
266 SOERR("WSAStartup");
267 }
268 #else /* WIN32 */
269 # ifndef USE_MSG_NOSIGNAL
270 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
271 grn_ctx *ctx = &grn_gctx;
272 SERR("signal");
273 }
274 # endif /* USE_MSG_NOSIGNAL */
275 #endif /* WIN32 */
276 return grn_gctx.rc;
277 }
278
279 void
280 grn_com_fin(void)
281 {
282 #ifdef WIN32
283 WSACleanup();
284 #endif /* WIN32 */
285 }
286
287 grn_rc
288 grn_com_event_init(grn_ctx *ctx,
289 grn_com_event *ev,
290 int max_nevents,
291 int data_size)
292 {
293 ev->max_nevents = max_nevents;
294 if ((ev->hash = grn_hash_create(ctx,
295 NULL,
296 sizeof(grn_sock),
297 (unsigned int)data_size,
298 0))) {
299 CRITICAL_SECTION_INIT(ev->critical_section);
300 COND_INIT(ev->cond);
301 GRN_COM_QUEUE_INIT(&ev->recv_old);
302 ev->msg_handler = NULL;
303 memset(&(ev->curr_edge_id), 0, sizeof(grn_com_addr));
304 ev->acceptor = NULL;
305 ev->listen_backlog = GRN_COM_EVENT_LISTEN_BACKLOG_DEFAULT;
306 ev->opaque = NULL;
307 #ifndef USE_SELECT
308 # ifdef USE_EPOLL
309 if ((ev->events =
310 GRN_MALLOC(sizeof(struct epoll_event) * (size_t)max_nevents))) {
311 if ((ev->epfd = epoll_create(max_nevents)) != -1) {
312 goto exit;
313 } else {
314 SERR("epoll_create");
315 }
316 GRN_FREE(ev->events);
317 }
318 # else /* USE_EPOLL */
319 # ifdef USE_KQUEUE
320 if ((ev->events = GRN_MALLOC(sizeof(struct kevent) * max_nevents))) {
321 if ((ev->kqfd = kqueue()) != -1) {
322 goto exit;
323 } else {
324 SERR("kqueue");
325 }
326 GRN_FREE(ev->events);
327 }
328 # else /* USE_KQUEUE */
329 if ((ev->events = GRN_MALLOC(sizeof(struct pollfd) * max_nevents))) {
330 goto exit;
331 }
332 # endif /* USE_KQUEUE*/
333 # endif /* USE_EPOLL */
334 grn_hash_close(ctx, ev->hash);
335 ev->hash = NULL;
336 ev->events = NULL;
337 #else /* USE_SELECT */
338 goto exit;
339 #endif /* USE_SELECT */
340 }
341 exit:
342 return ctx->rc;
343 }
344
345 grn_rc
346 grn_com_event_fin(grn_ctx *ctx, grn_com_event *ev)
347 {
348 grn_obj *msg;
349 while ((msg = (grn_obj *)grn_com_queue_deque(ctx, &ev->recv_old))) {
350 grn_msg_close(ctx, msg);
351 }
352 if (ev->hash) {
353 grn_hash_close(ctx, ev->hash);
354 }
355 #ifndef USE_SELECT
356 if (ev->events) {
357 GRN_FREE(ev->events);
358 }
359 # ifdef USE_EPOLL
360 grn_close(ev->epfd);
361 # endif /* USE_EPOLL */
362 # ifdef USE_KQUEUE
363 grn_close(ev->kqfd);
364 # endif /* USE_KQUEUE*/
365 #endif /* USE_SELECT */
366 CRITICAL_SECTION_FIN(ev->critical_section);
367 return GRN_SUCCESS;
368 }
369
370 void
371 grn_com_event_set_listen_backlog(grn_ctx *ctx, grn_com_event *ev, int backlog)
372 {
373 ev->listen_backlog = backlog;
374 }
375
376 int
377 grn_com_event_get_listen_backlog(grn_ctx *ctx, grn_com_event *ev)
378 {
379 return ev->listen_backlog;
380 }
381
382 grn_rc
383 grn_com_event_add(
384 grn_ctx *ctx, grn_com_event *ev, grn_sock fd, int events, grn_com **com)
385 {
386 grn_com *c;
387 /* todo : expand events */
388 if (!ev || *ev->hash->n_entries == (uint32_t)(ev->max_nevents)) {
389 if (ev) {
390 GRN_LOG(ctx, GRN_LOG_ERROR, "too many connections (%d)", ev->max_nevents);
391 }
392 return GRN_INVALID_ARGUMENT;
393 }
394 #ifdef USE_EPOLL
395 {
396 struct epoll_event e;
397 memset(&e, 0, sizeof(struct epoll_event));
398 e.data.fd = (fd);
399 e.events = (uint32_t)events;
400 if (epoll_ctl(ev->epfd, EPOLL_CTL_ADD, (fd), &e) == -1) {
401 SERR("epoll_ctl");
402 return ctx->rc;
403 }
404 }
405 #endif /* USE_EPOLL*/
406 #ifdef USE_KQUEUE
407 {
408 struct kevent e;
409 /* todo: udata should have fd */
410 EV_SET(&e, (fd), events, EV_ADD, 0, 0, NULL);
411 if (kevent(ev->kqfd, &e, 1, NULL, 0, NULL) == -1) {
412 SERR("kevent");
413 return ctx->rc;
414 }
415 }
416 #endif /* USE_KQUEUE */
417 {
418 if (grn_hash_add(ctx, ev->hash, &fd, sizeof(grn_sock), (void **)&c, NULL)) {
419 c->ev = ev;
420 c->fd = fd;
421 c->events = events;
422 if (com) {
423 *com = c;
424 }
425 }
426 }
427 return ctx->rc;
428 }
429
430 grn_rc
431 grn_com_event_mod(
432 grn_ctx *ctx, grn_com_event *ev, grn_sock fd, int events, grn_com **com)
433 {
434 grn_com *c;
435 if (!ev) {
436 return GRN_INVALID_ARGUMENT;
437 }
438 if (grn_hash_get(ctx, ev->hash, &fd, sizeof(grn_sock), (void **)&c)) {
439 if (c->fd != fd) {
440 GRN_LOG(ctx,
441 GRN_LOG_ERROR,
442 "grn_com_event_mod fd unmatch "
443 "%" GRN_FMT_SOCKET " != %" GRN_FMT_SOCKET,
444 c->fd,
445 fd);
446 return GRN_OBJECT_CORRUPT;
447 }
448 if (com) {
449 *com = c;
450 }
451 if (c->events != events) {
452 #ifdef USE_EPOLL
453 struct epoll_event e;
454 memset(&e, 0, sizeof(struct epoll_event));
455 e.data.fd = (fd);
456 e.events = (uint32_t)events;
457 if (epoll_ctl(ev->epfd, EPOLL_CTL_MOD, (fd), &e) == -1) {
458 SERR("epoll_ctl");
459 return ctx->rc;
460 }
461 #endif /* USE_EPOLL*/
462 #ifdef USE_KQUEUE
463 // experimental
464 struct kevent e[2];
465 EV_SET(&e[0],
466 (fd),
467 GRN_COM_POLLIN | GRN_COM_POLLOUT,
468 EV_DELETE,
469 0,
470 0,
471 NULL);
472 EV_SET(&e[1], (fd), events, EV_ADD, 0, 0, NULL);
473 if (kevent(ev->kqfd, e, 2, NULL, 0, NULL) == -1) {
474 SERR("kevent");
475 return ctx->rc;
476 }
477 #endif /* USE_KQUEUE */
478 c->events = events;
479 }
480 return GRN_SUCCESS;
481 }
482 return GRN_INVALID_ARGUMENT;
483 }
484
485 grn_rc
486 grn_com_event_del(grn_ctx *ctx, grn_com_event *ev, grn_sock fd)
487 {
488 if (!ev) {
489 return GRN_INVALID_ARGUMENT;
490 }
491 {
492 grn_com *c;
493 grn_id id = grn_hash_get(ctx, ev->hash, &fd, sizeof(grn_sock), (void **)&c);
494 if (id) {
495 #ifdef USE_EPOLL
496 if (!c->closed) {
497 struct epoll_event e;
498 memset(&e, 0, sizeof(struct epoll_event));
499 e.data.fd = fd;
500 e.events = (uint32_t)(c->events);
501 if (epoll_ctl(ev->epfd, EPOLL_CTL_DEL, fd, &e) == -1) {
502 SERR("epoll_ctl");
503 return ctx->rc;
504 }
505 }
506 #endif /* USE_EPOLL*/
507 #ifdef USE_KQUEUE
508 struct kevent e;
509 EV_SET(&e, (fd), c->events, EV_DELETE, 0, 0, NULL);
510 if (kevent(ev->kqfd, &e, 1, NULL, 0, NULL) == -1) {
511 SERR("kevent");
512 return ctx->rc;
513 }
514 #endif /* USE_KQUEUE */
515 return grn_hash_delete_by_id(ctx, ev->hash, id, NULL);
516 } else {
517 GRN_LOG(ctx,
518 GRN_LOG_ERROR,
519 "%04x| fd(%" GRN_FMT_SOCKET ") not found in ev(%p)",
520 grn_getpid(),
521 fd,
522 ev);
523 return GRN_INVALID_ARGUMENT;
524 }
525 }
526 }
527
528 grn_rc
529 grn_com_event_start_accept(grn_ctx *ctx, grn_com_event *ev)
530 {
531 grn_com *com = ev->acceptor;
532
533 if (com->accepting) {
534 return ctx->rc;
535 }
536
537 GRN_API_ENTER;
538 if (!grn_com_event_mod(ctx, ev, com->fd, GRN_COM_POLLIN, NULL)) {
539 if (listen(com->fd, ev->listen_backlog) == 0) {
540 com->accepting = true;
541 } else {
542 SOERR("listen - start accept");
543 }
544 }
545 GRN_API_RETURN(ctx->rc);
546 }
547
548 grn_rc
549 grn_com_event_stop_accept(grn_ctx *ctx, grn_com_event *ev)
550 {
551 grn_com *com = ev->acceptor;
552
553 if (!com->accepting) {
554 return ctx->rc;
555 }
556
557 GRN_API_ENTER;
558 if (!grn_com_event_mod(ctx, ev, com->fd, 0, NULL)) {
559 if (listen(com->fd, 0) == 0) {
560 com->accepting = false;
561 } else {
562 SOERR("listen - disable accept");
563 }
564 }
565 GRN_API_RETURN(ctx->rc);
566 }
567
568 static void
569 grn_com_receiver(grn_ctx *ctx, grn_com *com)
570 {
571 grn_com_event *ev = com->ev;
572 ERRCLR(ctx);
573 if (ev->acceptor == com) {
574 grn_com *ncs;
575 grn_sock fd = accept(com->fd, NULL, NULL);
576 if (fd == -1) {
577 if (errno == EMFILE) {
578 grn_com_event_stop_accept(ctx, ev);
579 } else {
580 SOERR("accept");
581 }
582 return;
583 }
584 if (grn_com_event_add(ctx, ev, fd, GRN_COM_POLLIN, (grn_com **)&ncs)) {
585 grn_sock_close(fd);
586 return;
587 }
588 ncs->has_sid = 0;
589 ncs->closed = 0;
590 ncs->opaque = NULL;
591 GRN_COM_QUEUE_INIT(&ncs->new_);
592 // GRN_LOG(ctx, GRN_LOG_NOTICE, "accepted (%d)", fd);
593 return;
594 } else {
595 grn_msg *msg = (grn_msg *)grn_msg_open(ctx, com, &ev->recv_old);
596 grn_com_recv(ctx, msg->u.peer, &msg->header, (grn_obj *)msg);
597 if (msg->u.peer /* is_edge_request(msg)*/) {
598 grn_memcpy(&msg->edge_id, &ev->curr_edge_id, sizeof(grn_com_addr));
599 if (!com->has_sid) {
600 com->has_sid = 1;
601 com->sid = ev->curr_edge_id.sid++;
602 }
603 msg->edge_id.sid = com->sid;
604 }
605 msg->acceptor = ev->acceptor;
606 ev->msg_handler(ctx, (grn_obj *)msg);
607 }
608 }
609
610 grn_rc
611 grn_com_event_poll(grn_ctx *ctx, grn_com_event *ev, int timeout)
612 {
613 int nevents;
614 grn_com *com;
615 #ifdef USE_SELECT
616 uint32_t dummy;
617 grn_sock *pfd;
618 int nfds = 0;
619 fd_set rfds;
620 fd_set wfds;
621 struct timeval tv;
622 if (timeout >= 0) {
623 tv.tv_sec = timeout / 1000;
624 tv.tv_usec = (timeout % 1000) * 1000;
625 }
626 FD_ZERO(&rfds);
627 FD_ZERO(&wfds);
628 ctx->errlvl = GRN_OK;
629 ctx->rc = GRN_SUCCESS;
630 {
631 grn_hash_cursor *cursor;
632 cursor = grn_hash_cursor_open(ctx, ev->hash, NULL, 0, NULL, 0, 0, -1, 0);
633 if (cursor) {
634 grn_id id;
635 while ((id = grn_hash_cursor_next(ctx, cursor))) {
636 grn_hash_cursor_get_key_value(ctx,
637 cursor,
638 (void **)(&pfd),
639 &dummy,
640 (void **)(&com));
641 if (com->events & GRN_COM_POLLIN) {
642 FD_SET(*pfd, &rfds);
643 }
644 if (com->events & GRN_COM_POLLOUT) {
645 FD_SET(*pfd, &wfds);
646 }
647 # ifndef WIN32
648 if (*pfd > nfds) {
649 nfds = *pfd;
650 }
651 # endif /* WIN32 */
652 }
653 grn_hash_cursor_close(ctx, cursor);
654 }
655 }
656 nevents = select(nfds + 1, &rfds, &wfds, NULL, (timeout >= 0) ? &tv : NULL);
657 if (nevents < 0) {
658 SOERR("select");
659 if (ctx->rc == GRN_INTERRUPTED_FUNCTION_CALL) {
660 ERRCLR(ctx);
661 }
662 return ctx->rc;
663 }
664 if (timeout < 0 && !nevents) {
665 GRN_LOG(ctx, GRN_LOG_NOTICE, "select returns 0 events");
666 }
667 GRN_HASH_EACH(ctx, ev->hash, eh, &pfd, &dummy, &com, {
668 if (FD_ISSET(*pfd, &rfds)) {
669 grn_com_receiver(ctx, com);
670 }
671 });
672 #else /* USE_SELECT */
673 # ifdef USE_EPOLL
674 struct epoll_event *ep;
675 ctx->errlvl = GRN_OK;
676 ctx->rc = GRN_SUCCESS;
677 nevents = epoll_wait(ev->epfd, ev->events, ev->max_nevents, timeout);
678 if (nevents < 0) {
679 SERR("epoll_wait");
680 }
681 # else /* USE_EPOLL */
682 # ifdef USE_KQUEUE
683 struct kevent *ep;
684 struct timespec tv;
685 if (timeout >= 0) {
686 tv.tv_sec = timeout / 1000;
687 tv.tv_nsec = (timeout % 1000) * 1000;
688 }
689 nevents = kevent(ev->kqfd, NULL, 0, ev->events, ev->max_nevents, &tv);
690 if (nevents < 0) {
691 SERR("kevent");
692 }
693 # else /* USE_KQUEUE */
694 uint32_t dummy;
695 int nfd = 0, *pfd;
696 struct pollfd *ep = ev->events;
697 ctx->errlvl = GRN_OK;
698 ctx->rc = GRN_SUCCESS;
699 GRN_HASH_EACH(ctx, ev->hash, eh, &pfd, &dummy, &com, {
700 ep->fd = *pfd;
701 // ep->events =(short) com->events;
702 ep->events = POLLIN;
703 ep->revents = 0;
704 ep++;
705 nfd++;
706 });
707 nevents = poll(ev->events, nfd, timeout);
708 if (nevents < 0) {
709 SERR("poll");
710 }
711 # endif /* USE_KQUEUE */
712 # endif /* USE_EPOLL */
713 if (ctx->rc != GRN_SUCCESS) {
714 if (ctx->rc == GRN_INTERRUPTED_FUNCTION_CALL) {
715 ERRCLR(ctx);
716 }
717 return ctx->rc;
718 }
719 if (timeout < 0 && !nevents) {
720 GRN_LOG(ctx, GRN_LOG_NOTICE, "poll returns 0 events");
721 }
722 for (ep = ev->events; nevents; ep++) {
723 int efd;
724 # ifdef USE_EPOLL
725 efd = ep->data.fd;
726 nevents--;
727 // todo : com = ep->data.ptr;
728 if (!grn_hash_get(ctx, ev->hash, &efd, sizeof(grn_sock), (void *)&com)) {
729 struct epoll_event e;
730 GRN_LOG(ctx, GRN_LOG_ERROR, "fd(%d) not found in ev->hash", efd);
731 memset(&e, 0, sizeof(struct epoll_event));
732 e.data.fd = efd;
733 e.events = ep->events;
734 if (epoll_ctl(ev->epfd, EPOLL_CTL_DEL, efd, &e) == -1) {
735 SERR("epoll_ctl");
736 }
737 if (grn_sock_close(efd) == -1) {
738 SOERR("close");
739 }
740 continue;
741 }
742 if (ep->events & GRN_COM_POLLIN) {
743 grn_com_receiver(ctx, com);
744 }
745 # else /* USE_EPOLL */
746 # ifdef USE_KQUEUE
747 efd = ep->ident;
748 nevents--;
749 // todo : com = ep->udata;
750 if (!grn_hash_get(ctx, ev->hash, &efd, sizeof(grn_sock), (void *)&com)) {
751 struct kevent e;
752 GRN_LOG(ctx, GRN_LOG_ERROR, "fd(%d) not found in ev->set", efd);
753 EV_SET(&e, efd, ep->filter, EV_DELETE, 0, 0, NULL);
754 if (kevent(ev->kqfd, &e, 1, NULL, 0, NULL) == -1) {
755 SERR("kevent");
756 }
757 if (grn_sock_close(efd) == -1) {
758 SOERR("close");
759 }
760 continue;
761 }
762 if (ep->filter == GRN_COM_POLLIN) {
763 grn_com_receiver(ctx, com);
764 }
765 # else
766 efd = ep->fd;
767 if (!(ep->events & ep->revents)) {
768 continue;
769 }
770 nevents--;
771 if (!grn_hash_get(ctx, ev->hash, &efd, sizeof(grn_sock), (void *)&com)) {
772 GRN_LOG(ctx, GRN_LOG_ERROR, "fd(%d) not found in ev->hash", efd);
773 if (grn_sock_close(efd) == -1) {
774 SOERR("close");
775 }
776 continue;
777 }
778 if (ep->revents & GRN_COM_POLLIN) {
779 grn_com_receiver(ctx, com);
780 }
781 # endif /* USE_KQUEUE */
782 # endif /* USE_EPOLL */
783 }
784 #endif /* USE_SELECT */
785 /* todo :
786 while (!(msg = (grn_com_msg *)grn_com_queue_deque(&recv_old))) {
787 grn_msg_close(ctx, msg);
788 }
789 */
790 return GRN_SUCCESS;
791 }
792
793 grn_rc
794 grn_com_send_http(
795 grn_ctx *ctx, grn_com *cs, const char *path, uint32_t path_len, int flags)
796 {
797 ssize_t ret;
798 grn_obj buf;
799 GRN_TEXT_INIT(&buf, 0);
800 GRN_TEXT_PUTS(ctx, &buf, "GET ");
801 grn_bulk_write(ctx, &buf, path, path_len);
802 GRN_TEXT_PUTS(ctx, &buf, " HTTP/1.0\r\n\r\n");
803 // todo : refine
804 if ((ret = send(cs->fd,
805 GRN_BULK_HEAD(&buf),
806 GRN_BULK_VSIZE(&buf),
807 MSG_NOSIGNAL | flags)) == -1) {
808 SOERR("send");
809 }
810 if ((size_t)ret != GRN_BULK_VSIZE(&buf)) {
811 GRN_LOG(ctx,
812 GRN_LOG_NOTICE,
813 "send %d != %d",
814 (int)ret,
815 (int)GRN_BULK_VSIZE(&buf));
816 }
817 grn_obj_close(ctx, &buf);
818 return ctx->rc;
819 }
820
821 grn_rc
822 grn_com_send(grn_ctx *ctx,
823 grn_com *cs,
824 grn_com_header *header,
825 const char *body,
826 uint32_t size,
827 int flags)
828 {
829 grn_rc rc = GRN_SUCCESS;
830 size_t whole_size = sizeof(grn_com_header) + size;
831 ssize_t ret;
832 header->size = htonl(size);
833 GRN_LOG(ctx,
834 GRN_LOG_INFO,
835 "send (%d,%x,%d,%02x,%02x,%04x)",
836 size,
837 header->flags,
838 header->proto,
839 header->qtype,
840 header->level,
841 header->status);
842
843 if (size) {
844 #ifdef WIN32
845 WSABUF wsabufs[2];
846 DWORD n_sent;
847 wsabufs[0].buf = (char *)header;
848 wsabufs[0].len = sizeof(grn_com_header);
849 wsabufs[1].buf = (char *)body;
850 wsabufs[1].len = size;
851 if (WSASend(cs->fd, wsabufs, 2, &n_sent, 0, NULL, NULL) == SOCKET_ERROR) {
852 SOERR("WSASend");
853 }
854 ret = n_sent;
855 #else /* WIN32 */
856 struct iovec msg_iov[2];
857 struct msghdr msg;
858 memset(&msg, 0, sizeof(struct msghdr));
859 msg.msg_name = NULL;
860 msg.msg_namelen = 0;
861 msg.msg_iov = msg_iov;
862 msg.msg_iovlen = 2;
863 msg_iov[0].iov_base = header;
864 msg_iov[0].iov_len = sizeof(grn_com_header);
865 msg_iov[1].iov_base = (char *)body;
866 msg_iov[1].iov_len = size;
867 if ((ret = sendmsg(cs->fd, &msg, MSG_NOSIGNAL | flags)) == -1) {
868 SOERR("sendmsg");
869 rc = ctx->rc;
870 }
871 #endif /* WIN32 */
872 } else {
873 if ((ret = send(cs->fd,
874 (const void *)header,
875 whole_size,
876 MSG_NOSIGNAL | flags)) == -1) {
877 SOERR("send");
878 rc = ctx->rc;
879 }
880 }
881 if ((size_t)ret != whole_size) {
882 GRN_LOG(ctx,
883 GRN_LOG_ERROR,
884 "sendmsg(%" GRN_FMT_SOCKET "): %" GRN_FMT_LLD " < %" GRN_FMT_LLU,
885 cs->fd,
886 (long long int)ret,
887 (unsigned long long int)whole_size);
888 rc = ctx->rc;
889 }
890 GRN_QUERY_LOG(ctx,
891 GRN_QUERY_LOG_SIZE,
892 ":",
893 "send(%" GRN_FMT_SIZE "): %" GRN_FMT_SIZE "/%" GRN_FMT_SIZE,
894 whole_size,
895 ret,
896 whole_size);
897 return rc;
898 }
899
900 #define RETRY_MAX 10
901
902 static const char *
903 scan_delimiter(const char *p, const char *e)
904 {
905 while (p + 4 <= e) {
906 if (p[3] == '\n') {
907 if (p[2] == '\r') {
908 if (p[1] == '\n') {
909 if (p[0] == '\r') {
910 return p + 4;
911 } else {
912 p += 2;
913 }
914 } else {
915 p += 2;
916 }
917 } else {
918 p += 4;
919 }
920 } else {
921 p += p[3] == '\r' ? 1 : 4;
922 }
923 }
924 return NULL;
925 }
926
927 #define BUFSIZE 4096
928
929 static grn_rc
930 grn_com_recv_text(
931 grn_ctx *ctx, grn_com *com, grn_com_header *header, grn_obj *buf, ssize_t ret)
932 {
933 const char *p;
934 int retry = 0;
935 grn_bulk_write(ctx, buf, (char *)header, (size_t)ret);
936 if ((p = scan_delimiter(GRN_BULK_HEAD(buf), GRN_BULK_CURR(buf)))) {
937 header->qtype = (uint8_t)(*GRN_BULK_HEAD(buf));
938 header->proto = GRN_COM_PROTO_HTTP;
939 header->size = (uint32_t)GRN_BULK_VSIZE(buf);
940 goto exit;
941 }
942 for (;;) {
943 if (grn_bulk_reserve(ctx, buf, BUFSIZE)) {
944 return ctx->rc;
945 }
946 if ((ret = recv(com->fd, GRN_BULK_CURR(buf), BUFSIZE, 0)) < 0) {
947 SOERR("recv text");
948 if (ctx->rc == GRN_OPERATION_WOULD_BLOCK ||
949 ctx->rc == GRN_INTERRUPTED_FUNCTION_CALL) {
950 ERRCLR(ctx);
951 continue;
952 }
953 goto exit;
954 }
955 if (ret) {
956 off_t o = (off_t)GRN_BULK_VSIZE(buf);
957 p = GRN_BULK_CURR(buf);
958 GRN_BULK_INCR_LEN(buf, ret);
959 if (scan_delimiter(p - (o > 3 ? 3 : o), p + ret)) {
960 break;
961 }
962 } else {
963 if (++retry > RETRY_MAX) {
964 // ERR(GRN_RETRY_MAX, "retry max in recv text");
965 goto exit;
966 }
967 }
968 }
969 header->qtype = (uint8_t)*GRN_BULK_HEAD(buf);
970 header->proto = GRN_COM_PROTO_HTTP;
971 header->size = (uint32_t)GRN_BULK_VSIZE(buf);
972 exit:
973 if (header->qtype == 'H') {
974 // todo : refine
975 /*
976 GRN_BULK_REWIND(buf);
977 grn_bulk_reserve(ctx, buf, BUFSIZE);
978 if ((ret = recv(com->fd, GRN_BULK_CURR(buf), BUFSIZE, 0)) < 0) {
979 SOERR("recv text body");
980 } else {
981 GRN_BULK_CURR(buf) += ret;
982 }
983 */
984 }
985 return ctx->rc;
986 }
987
988 grn_rc
989 grn_com_recv(grn_ctx *ctx, grn_com *com, grn_com_header *header, grn_obj *buf)
990 {
991 ssize_t ret;
992 int retry = 0;
993 byte *p = (byte *)header;
994 size_t rest = sizeof(grn_com_header);
995 do {
996 if ((ret = recv(com->fd, p, rest, 0)) < 0) {
997 SOERR("recv size");
998 GRN_LOG(ctx, GRN_LOG_ERROR, "recv error (%" GRN_FMT_SOCKET ")", com->fd);
999 if (ctx->rc == GRN_OPERATION_WOULD_BLOCK ||
1000 ctx->rc == GRN_INTERRUPTED_FUNCTION_CALL) {
1001 ERRCLR(ctx);
1002 continue;
1003 }
1004 goto exit;
1005 }
1006 if (ret) {
1007 if (header->proto < 0x80) {
1008 return grn_com_recv_text(ctx, com, header, buf, ret);
1009 }
1010 rest -= (size_t)ret;
1011 p += ret;
1012 } else {
1013 if (++retry > RETRY_MAX) {
1014 // ERR(GRN_RETRY_MAX, "retry max in recv header (%d)", com->fd);
1015 goto exit;
1016 }
1017 }
1018 } while (rest);
1019 GRN_LOG(ctx,
1020 GRN_LOG_INFO,
1021 "recv (%u,%x,%d,%02x,%02x,%04x)",
1022 (uint32_t)ntohl(header->size),
1023 header->flags,
1024 header->proto,
1025 header->qtype,
1026 header->level,
1027 header->status);
1028 {
1029 uint8_t proto = header->proto;
1030 size_t value_size = ntohl(header->size);
1031 GRN_BULK_REWIND(buf);
1032 switch (proto) {
1033 case GRN_COM_PROTO_GQTP:
1034 case GRN_COM_PROTO_MBREQ:
1035 if (GRN_BULK_WSIZE(buf) < value_size) {
1036 if (grn_bulk_resize(ctx, buf, value_size)) {
1037 goto exit;
1038 }
1039 }
1040 retry = 0;
1041 for (rest = value_size; rest;) {
1042 if ((ret = recv(com->fd, GRN_BULK_CURR(buf), rest, MSG_WAITALL)) < 0) {
1043 SOERR("recv body");
1044 if (ctx->rc == GRN_OPERATION_WOULD_BLOCK ||
1045 ctx->rc == GRN_INTERRUPTED_FUNCTION_CALL) {
1046 ERRCLR(ctx);
1047 continue;
1048 }
1049 goto exit;
1050 }
1051 if (ret) {
1052 rest -= (size_t)ret;
1053 GRN_BULK_INCR_LEN(buf, ret);
1054 } else {
1055 if (++retry > RETRY_MAX) {
1056 // ERR(GRN_RETRY_MAX, "retry max in recv body");
1057 goto exit;
1058 }
1059 }
1060 }
1061 break;
1062 default:
1063 GRN_LOG(ctx, GRN_LOG_ERROR, "illegal header: %d", proto);
1064 ctx->rc = GRN_INVALID_FORMAT;
1065 goto exit;
1066 }
1067 }
1068 exit:
1069 return ctx->rc;
1070 }
1071
1072 grn_com *
1073 grn_com_copen(grn_ctx *ctx, grn_com_event *ev, const char *dest, int port)
1074 {
1075 grn_sock fd = -1;
1076 grn_com *cs = NULL;
1077
1078 struct addrinfo hints, *addrinfo_list, *addrinfo_ptr;
1079 char port_string[16];
1080 int getaddrinfo_result;
1081
1082 memset(&hints, 0, sizeof(hints));
1083 hints.ai_family = AF_UNSPEC;
1084 hints.ai_socktype = SOCK_STREAM;
1085 #ifdef AI_NUMERICSERV
1086 hints.ai_flags = AI_NUMERICSERV;
1087 #endif
1088 grn_snprintf(port_string,
1089 sizeof(port_string),
1090 sizeof(port_string),
1091 "%d",
1092 port);
1093
1094 getaddrinfo_result = getaddrinfo(dest, port_string, &hints, &addrinfo_list);
1095 if (getaddrinfo_result != 0) {
1096 switch (getaddrinfo_result) {
1097 #ifdef EAI_MEMORY
1098 case EAI_MEMORY:
1099 ERR(GRN_NO_MEMORY_AVAILABLE,
1100 "getaddrinfo: <%s:%s>: %s",
1101 dest,
1102 port_string,
1103 gai_strerror(getaddrinfo_result));
1104 break;
1105 #endif
1106 #ifdef EAI_SYSTEM
1107 case EAI_SYSTEM:
1108 SOERR("getaddrinfo");
1109 break;
1110 #endif
1111 default:
1112 ERR(GRN_INVALID_ARGUMENT,
1113 "getaddrinfo: <%s:%s>: %s",
1114 dest,
1115 port_string,
1116 gai_strerror(getaddrinfo_result));
1117 break;
1118 }
1119 return NULL;
1120 }
1121
1122 for (addrinfo_ptr = addrinfo_list; addrinfo_ptr;
1123 addrinfo_ptr = addrinfo_ptr->ai_next) {
1124 fd = socket(addrinfo_ptr->ai_family,
1125 addrinfo_ptr->ai_socktype,
1126 addrinfo_ptr->ai_protocol);
1127 if (fd == GRN_INVALID_SOCKET) {
1128 SOERR("socket");
1129 continue;
1130 }
1131 #ifdef TCP_NODELAY
1132 {
1133 static const int value = 1;
1134 if (setsockopt(fd, 6, TCP_NODELAY, (const char *)&value, sizeof(value)) !=
1135 0) {
1136 SOERR("setsockopt");
1137 grn_sock_close(fd);
1138 continue;
1139 }
1140 }
1141 #endif
1142 if (connect(fd, addrinfo_ptr->ai_addr, addrinfo_ptr->ai_addrlen) != 0) {
1143 SOERR("connect");
1144 grn_sock_close(fd);
1145 continue;
1146 }
1147
1148 break;
1149 }
1150
1151 freeaddrinfo(addrinfo_list);
1152
1153 if (!addrinfo_ptr) {
1154 return NULL;
1155 }
1156 ctx->errlvl = GRN_OK;
1157 ctx->rc = GRN_SUCCESS;
1158
1159 if (ev) {
1160 grn_com_event_add(ctx, ev, fd, GRN_COM_POLLIN, &cs);
1161 } else {
1162 cs = GRN_CALLOC(sizeof(grn_com));
1163 if (cs) {
1164 cs->fd = fd;
1165 }
1166 }
1167 if (!cs) {
1168 grn_sock_close(fd);
1169 }
1170 return cs;
1171 }
1172
1173 void
1174 grn_com_close_(grn_ctx *ctx, grn_com *com)
1175 {
1176 grn_sock fd = com->fd;
1177 if (shutdown(fd, SHUT_RDWR) == -1) { /* SOERR("shutdown"); */
1178 }
1179 if (grn_sock_close(fd) == -1) {
1180 SOERR("close");
1181 } else {
1182 com->closed = 1;
1183 }
1184 }
1185
1186 grn_rc
1187 grn_com_close(grn_ctx *ctx, grn_com *com)
1188 {
1189 grn_sock fd = com->fd;
1190 grn_com_event *ev = com->ev;
1191 if (ev) {
1192 grn_com *acceptor = ev->acceptor;
1193 grn_com_event_del(ctx, ev, fd);
1194 if (acceptor) {
1195 grn_com_event_start_accept(ctx, ev);
1196 }
1197 }
1198 if (!com->closed) {
1199 grn_com_close_(ctx, com);
1200 }
1201 if (!ev) {
1202 GRN_FREE(com);
1203 }
1204 return GRN_SUCCESS;
1205 }
1206
1207 grn_rc
1208 grn_com_sopen(grn_ctx *ctx,
1209 grn_com_event *ev,
1210 const char *bind_address,
1211 uint16_t port,
1212 grn_msg_handler *func,
1213 struct hostent *he)
1214 {
1215 grn_sock lfd = -1;
1216 grn_com *cs = NULL;
1217 int getaddrinfo_result;
1218 struct addrinfo *bind_address_info = NULL;
1219 struct addrinfo hints;
1220 char port_string[6]; /* ceil(log10(65535)) + 1 ('\0')*/
1221
1222 GRN_API_ENTER;
1223 if (!bind_address) {
1224 bind_address = "0.0.0.0";
1225 }
1226 grn_snprintf(port_string,
1227 sizeof(port_string),
1228 sizeof(port_string),
1229 "%u",
1230 port);
1231 memset(&hints, 0, sizeof(struct addrinfo));
1232 hints.ai_family = PF_UNSPEC;
1233 hints.ai_socktype = SOCK_STREAM;
1234 #ifdef AI_NUMERICSERV
1235 hints.ai_flags = AI_NUMERICSERV;
1236 #endif
1237 getaddrinfo_result =
1238 getaddrinfo(bind_address, port_string, &hints, &bind_address_info);
1239 if (getaddrinfo_result != 0) {
1240 switch (getaddrinfo_result) {
1241 #ifdef EAI_MEMORY
1242 case EAI_MEMORY:
1243 ERR(GRN_NO_MEMORY_AVAILABLE,
1244 "getaddrinfo: <%s:%s>: %s",
1245 bind_address,
1246 port_string,
1247 gai_strerror(getaddrinfo_result));
1248 break;
1249 #endif
1250 #ifdef EAI_SYSTEM
1251 case EAI_SYSTEM:
1252 SOERR("getaddrinfo");
1253 break;
1254 #endif
1255 default:
1256 ERR(GRN_INVALID_ARGUMENT,
1257 "getaddrinfo: <%s:%s>: %s",
1258 bind_address,
1259 port_string,
1260 gai_strerror(getaddrinfo_result));
1261 break;
1262 }
1263 goto exit;
1264 }
1265 if ((lfd = socket(bind_address_info->ai_family, SOCK_STREAM, 0)) == -1) {
1266 SOERR("socket");
1267 goto exit;
1268 }
1269 grn_memcpy(&ev->curr_edge_id.addr, he->h_addr, (size_t)(he->h_length));
1270 ev->curr_edge_id.port = htons(port);
1271 ev->curr_edge_id.sid = 0;
1272 {
1273 int v = 1;
1274 #ifdef TCP_NODELAY
1275 if (setsockopt(lfd, SOL_TCP, TCP_NODELAY, (void *)&v, sizeof(int)) == -1) {
1276 SOERR("setsockopt");
1277 goto exit;
1278 }
1279 #endif
1280 if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (void *)&v, sizeof(int)) ==
1281 -1) {
1282 SOERR("setsockopt");
1283 goto exit;
1284 }
1285 }
1286 if (bind(lfd, bind_address_info->ai_addr, bind_address_info->ai_addrlen) <
1287 0) {
1288 SOERR("bind");
1289 goto exit;
1290 }
1291 if (listen(lfd, ev->listen_backlog) < 0) {
1292 SOERR("listen");
1293 goto exit;
1294 }
1295 if (ev) {
1296 if (grn_com_event_add(ctx, ev, lfd, GRN_COM_POLLIN, &cs)) {
1297 goto exit;
1298 }
1299 ev->acceptor = cs;
1300 ev->msg_handler = func;
1301 cs->has_sid = 0;
1302 cs->closed = 0;
1303 cs->opaque = NULL;
1304 GRN_COM_QUEUE_INIT(&cs->new_);
1305 } else {
1306 if (!(cs = GRN_CALLOC(sizeof(grn_com)))) {
1307 goto exit;
1308 }
1309 cs->fd = lfd;
1310 }
1311 cs->accepting = true;
1312 exit:
1313 if (!cs && lfd != 1) {
1314 grn_sock_close(lfd);
1315 }
1316 if (bind_address_info) {
1317 freeaddrinfo(bind_address_info);
1318 }
1319 GRN_API_RETURN(ctx->rc);
1320 }
1321
1322 grn_hash *grn_edges = NULL;
1323 void (*grn_dispatcher)(grn_ctx *ctx, grn_edge *edge);
1324
1325 void
1326 grn_edges_init(grn_ctx *ctx, void (*dispatcher)(grn_ctx *ctx, grn_edge *edge))
1327 {
1328 grn_edges =
1329 grn_hash_create(ctx, NULL, sizeof(grn_com_addr), sizeof(grn_edge), 0);
1330 grn_dispatcher = dispatcher;
1331 }
1332
1333 void
1334 grn_edges_fin(grn_ctx *ctx)
1335 {
1336 grn_hash_close(ctx, grn_edges);
1337 }
1338
1339 grn_edge *
1340 grn_edges_add(grn_ctx *ctx, grn_com_addr *addr, int *added)
1341 {
1342 if (grn_io_lock(ctx, grn_edges->io, grn_lock_timeout)) {
1343 return NULL;
1344 } else {
1345 grn_edge *edge;
1346 grn_id id = grn_hash_add(ctx,
1347 grn_edges,
1348 addr,
1349 sizeof(grn_com_addr),
1350 (void **)&edge,
1351 added);
1352 grn_io_unlock(ctx, grn_edges->io);
1353 if (id) {
1354 edge->id = id;
1355 }
1356 return edge;
1357 }
1358 }
1359
1360 void
1361 grn_edges_delete(grn_ctx *ctx, grn_edge *edge)
1362 {
1363 if (!grn_io_lock(ctx, grn_edges->io, grn_lock_timeout)) {
1364 grn_hash_delete_by_id(ctx, grn_edges, edge->id, NULL);
1365 grn_io_unlock(ctx, grn_edges->io);
1366 }
1367 }
1368
1369 grn_edge *
1370 grn_edges_add_communicator(grn_ctx *ctx, grn_com_addr *addr)
1371 {
1372 int added;
1373 grn_edge *edge = grn_edges_add(ctx, addr, &added);
1374 if (added) {
1375 grn_ctx_init(&edge->ctx, 0);
1376 GRN_COM_QUEUE_INIT(&edge->recv_new);
1377 GRN_COM_QUEUE_INIT(&edge->send_old);
1378 edge->com = NULL;
1379 edge->stat = 0 /*EDGE_IDLE*/;
1380 edge->flags = GRN_EDGE_COMMUNICATOR;
1381 }
1382 return edge;
1383 }
1384
1385 void
1386 grn_edge_dispatch(grn_ctx *ctx, grn_edge *edge, grn_obj *msg)
1387 {
1388 grn_com_queue_enque(ctx, &edge->recv_new, (grn_com_queue_entry *)msg);
1389 grn_dispatcher(ctx, edge);
1390 }