1 /*
2     Copyright (c) 2013 Martin Sustrik  All rights reserved.
3     Copyright 2016 Garrett D'Amore <garrett@damore.org>
4 
5     Permission is hereby granted, free of charge, to any person obtaining a copy
6     of this software and associated documentation files (the "Software"),
7     to deal in the Software without restriction, including without limitation
8     the rights to use, copy, modify, merge, publish, distribute, sublicense,
9     and/or sell copies of the Software, and to permit persons to whom
10     the Software is furnished to do so, subject to the following conditions:
11 
12     The above copyright notice and this permission notice shall be included
13     in all copies or substantial portions of the Software.
14 
15     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21     IN THE SOFTWARE.
22 */
23 
24 #ifndef NN_AIPC_INCLUDED
25 #define NN_AIPC_INCLUDED
26 
27 #include "sipc.h"
28 
29 #include "../../transport.h"
30 #include "../../ipc.h"
31 
32 #include "../../aio/fsm.h"
33 #include "../../aio/usock.h"
34 
35 #include "../../utils/list.h"
36 
37 /*  State machine handling accepted IPC sockets. */
38 
39 /*  In bipc, some events are just *assumed* to come from a child aipc object.
40     By using non-trivial event codes, we can do more reliable sanity checking
41     in such scenarios. */
42 #define NN_AIPC_ACCEPTED 34231
43 #define NN_AIPC_ERROR 34232
44 #define NN_AIPC_STOPPED 34233
45 
46 struct nn_aipc {
47 
48     /*  The state machine. */
49     struct nn_fsm fsm;
50     int state;
51 
52     /*  Pointer to the associated endpoint. */
53     struct nn_ep *ep;
54 
55     /*  Underlying socket. */
56     struct nn_usock usock;
57 
58     /*  Listening socket. Valid only while accepting new connection. */
59     struct nn_usock *listener;
60     struct nn_fsm_owner listener_owner;
61 
62     /*  State machine that takes care of the connection in the active state. */
63     struct nn_sipc sipc;
64 
65     /*  Events generated by aipc state machine. */
66     struct nn_fsm_event accepted;
67     struct nn_fsm_event done;
68 
69     /*  This member can be used by owner to keep individual aipcs in a list. */
70     struct nn_list_item item;
71 };
72 
73 void nn_aipc_init (struct nn_aipc *self, int src,
74     struct nn_ep *ep, struct nn_fsm *owner);
75 void nn_aipc_term (struct nn_aipc *self);
76 
77 int nn_aipc_isidle (struct nn_aipc *self);
78 void nn_aipc_start (struct nn_aipc *self, struct nn_usock *listener);
79 void nn_aipc_stop (struct nn_aipc *self);
80 
81 #endif
82