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_SIPC_INCLUDED
25 #define NN_SIPC_INCLUDED
26 
27 #include "../../transport.h"
28 
29 #include "../../aio/fsm.h"
30 #include "../../aio/usock.h"
31 
32 #include "../utils/streamhdr.h"
33 
34 #include "../../utils/msg.h"
35 
36 /*  This state machine handles IPC connection from the point where it is
37     established to the point when it is broken. */
38 
39 #define NN_SIPC_ERROR 1
40 #define NN_SIPC_STOPPED 2
41 
42 struct nn_sipc {
43 
44     /*  The state machine. */
45     struct nn_fsm fsm;
46     int state;
47 
48     /*  The underlying socket. */
49     struct nn_usock *usock;
50 
51     /*  Child state machine to do protocol header exchange. */
52     struct nn_streamhdr streamhdr;
53 
54     /*  The original owner of the underlying socket. */
55     struct nn_fsm_owner usock_owner;
56 
57     /*  Pipe connecting this IPC connection to the nanomsg core. */
58     struct nn_pipebase pipebase;
59 
60     /*  State of inbound state machine. */
61     int instate;
62 
63     /*  Buffer used to store the header of incoming message. */
64     uint8_t inhdr [9];
65 
66     /*  Message being received at the moment. */
67     struct nn_msg inmsg;
68 
69     /*  State of the outbound state machine. */
70     int outstate;
71 
72     /*  Buffer used to store the header of outgoing message. */
73     uint8_t outhdr [9];
74 
75     /*  Message being sent at the moment. */
76     struct nn_msg outmsg;
77 
78     /*  Event raised when the state machine ends. */
79     struct nn_fsm_event done;
80 };
81 
82 void nn_sipc_init (struct nn_sipc *self, int src,
83     struct nn_ep *ep, struct nn_fsm *owner);
84 void nn_sipc_term (struct nn_sipc *self);
85 
86 int nn_sipc_isidle (struct nn_sipc *self);
87 void nn_sipc_start (struct nn_sipc *self, struct nn_usock *usock);
88 void nn_sipc_stop (struct nn_sipc *self);
89 
90 #endif
91