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 #ifndef NN_USOCK_INCLUDED 24 #define NN_USOCK_INCLUDED 25 26 /* Import the definition of nn_iovec. */ 27 #include "../nn.h" 28 29 /* OS-level sockets. */ 30 31 /* Event types generated by nn_usock. */ 32 #define NN_USOCK_CONNECTED 1 33 #define NN_USOCK_ACCEPTED 2 34 #define NN_USOCK_SENT 3 35 #define NN_USOCK_RECEIVED 4 36 #define NN_USOCK_ERROR 5 37 #define NN_USOCK_ACCEPT_ERROR 6 38 #define NN_USOCK_STOPPED 7 39 #define NN_USOCK_SHUTDOWN 8 40 41 /* Maximum number of iovecs that can be passed to nn_usock_send function. */ 42 #define NN_USOCK_MAX_IOVCNT 3 43 44 /* Size of the buffer used for batch-reads of inbound data. To keep the 45 performance optimal make sure that this value is larger than network MTU. */ 46 #define NN_USOCK_BATCH_SIZE 2048 47 48 #if defined NN_HAVE_WINDOWS 49 #include "usock_win.h" 50 #else 51 #include "usock_posix.h" 52 #endif 53 54 void nn_usock_init (struct nn_usock *self, int src, struct nn_fsm *owner); 55 void nn_usock_term (struct nn_usock *self); 56 57 int nn_usock_isidle (struct nn_usock *self); 58 int nn_usock_start (struct nn_usock *self, 59 int domain, int type, int protocol); 60 void nn_usock_start_fd (struct nn_usock *self, int fd); 61 void nn_usock_stop (struct nn_usock *self); 62 63 void nn_usock_swap_owner (struct nn_usock *self, struct nn_fsm_owner *owner); 64 65 int nn_usock_setsockopt (struct nn_usock *self, int level, int optname, 66 const void *optval, size_t optlen); 67 68 int nn_usock_bind (struct nn_usock *self, const struct sockaddr *addr, 69 size_t addrlen); 70 int nn_usock_listen (struct nn_usock *self, int backlog); 71 72 /* Accept a new connection from a listener. When done, NN_USOCK_ACCEPTED 73 event will be delivered to the accepted socket. To cancel the operation, 74 stop the socket being accepted. Listening socket should not be stopped 75 while accepting a new socket is underway. */ 76 void nn_usock_accept (struct nn_usock *self, struct nn_usock *listener); 77 78 /* When all the tuning is done on the accepted socket, call this function 79 to activate standard data transfer phase. */ 80 void nn_usock_activate (struct nn_usock *self); 81 82 /* Start connecting. Prior to this call the socket has to be bound to a local 83 address. When connecting is done NN_USOCK_CONNECTED event will be reaised. 84 If connecting fails NN_USOCK_ERROR event will be raised. */ 85 void nn_usock_connect (struct nn_usock *self, const struct sockaddr *addr, 86 size_t addrlen); 87 88 void nn_usock_send (struct nn_usock *self, const struct nn_iovec *iov, 89 int iovcnt); 90 void nn_usock_recv (struct nn_usock *self, void *buf, size_t len, int *fd); 91 92 int nn_usock_geterrno (struct nn_usock *self); 93 94 #endif 95