1 /*
2     Copyright (c) 2013 Martin Sustrik  All rights reserved.
3 
4     Permission is hereby granted, free of charge, to any person obtaining a copy
5     of this software and associated documentation files (the "Software"),
6     to deal in the Software without restriction, including without limitation
7     the rights to use, copy, modify, merge, publish, distribute, sublicense,
8     and/or sell copies of the Software, and to permit persons to whom
9     the Software is furnished to do so, subject to the following conditions:
10 
11     The above copyright notice and this permission notice shall be included
12     in all copies or substantial portions of the Software.
13 
14     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20     IN THE SOFTWARE.
21 */
22 
23 #include "fsm.h"
24 #include "worker.h"
25 
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/uio.h>
29 
30 struct nn_usock {
31 
32     /*  State machine base class. */
33     struct nn_fsm fsm;
34     int state;
35 
36     /*  The worker thread the usock is associated with. */
37     struct nn_worker *worker;
38 
39     /*  The underlying OS socket and handle that represents it in the poller. */
40     int s;
41     struct nn_worker_fd wfd;
42 
43     /*  Members related to receiving data. */
44     struct {
45 
46         /*  The buffer being filled in at the moment. */
47         uint8_t *buf;
48         size_t len;
49 
50         /*  Buffer for batch-reading inbound data. */
51         uint8_t *batch;
52 
53         /*  Size of the batch buffer. */
54         size_t batch_len;
55 
56         /*  Current position in the batch buffer. The data preceding this
57             position were already received by the user. The data that follow
58             will be received in the future. */
59         size_t batch_pos;
60 
61         /*  File descriptor received via SCM_RIGHTS, if any. */
62         int *pfd;
63     } in;
64 
65     /*  Members related to sending data. */
66     struct {
67 
68         /*  msghdr being sent at the moment. */
69         struct msghdr hdr;
70 
71         /*  List of buffers being sent at the moment. Referenced from 'hdr'. */
72         struct iovec iov [NN_USOCK_MAX_IOVCNT];
73     } out;
74 
75     /*  Asynchronous tasks for the worker. */
76     struct nn_worker_task task_connecting;
77     struct nn_worker_task task_connected;
78     struct nn_worker_task task_accept;
79     struct nn_worker_task task_send;
80     struct nn_worker_task task_recv;
81     struct nn_worker_task task_stop;
82 
83     /*  Events raised by the usock. */
84     struct nn_fsm_event event_established;
85     struct nn_fsm_event event_sent;
86     struct nn_fsm_event event_received;
87     struct nn_fsm_event event_error;
88 
89     /*  In ACCEPTING state points to the socket being accepted.
90         In BEING_ACCEPTED state points to the listener socket. */
91     struct nn_usock *asock;
92 
93     /*  Errno remembered in NN_USOCK_ERROR state  */
94     int errnum;
95 };
96