1 /*
2     Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4     This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6     libzmq is free software; you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 3 of the License, or
9     (at your option) any later version.
10 
11     As a special exception, the Contributors give you permission to link
12     this library with independent modules to produce an executable,
13     regardless of the license terms of these independent modules, and to
14     copy and distribute the resulting executable under terms of your choice,
15     provided that you also meet, for each linked independent module, the
16     terms and conditions of the license of that module. An independent
17     module is a module which is not derived from or based on this library.
18     If you modify this library, you must extend this exception to your
19     version of the library.
20 
21     libzmq is distributed in the hope that it will be useful, but WITHOUT
22     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23     FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24     License for more details.
25 
26     You should have received a copy of the GNU Lesser General Public License
27     along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #ifndef __PGM_SOCKET_HPP_INCLUDED__
31 #define __PGM_SOCKET_HPP_INCLUDED__
32 
33 #if defined ZMQ_HAVE_OPENPGM
34 
35 #ifdef ZMQ_HAVE_WINDOWS
36 #define __PGM_WININT_H__
37 #endif
38 
39 #include <pgm/pgm.h>
40 
41 #if defined(ZMQ_HAVE_OSX) || defined(ZMQ_HAVE_NETBSD)
42 #include <pgm/in.h>
43 #endif
44 
45 #include "fd.hpp"
46 #include "options.hpp"
47 
48 namespace zmq
49 {
50 //  Encapsulates PGM socket.
51 class pgm_socket_t
52 {
53   public:
54     //  If receiver_ is true PGM transport is not generating SPM packets.
55     pgm_socket_t (bool receiver_, const options_t &options_);
56 
57     //  Closes the transport.
58     ~pgm_socket_t ();
59 
60     //  Initialize PGM network structures (GSI, GSRs).
61     int init (bool udp_encapsulation_, const char *network_);
62 
63     //  Resolve PGM socket address.
64     static int init_address (const char *network_,
65                              struct pgm_addrinfo_t **addr,
66                              uint16_t *port_number);
67 
68     //   Get receiver fds and store them into user allocated memory.
69     void get_receiver_fds (fd_t *receive_fd_, fd_t *waiting_pipe_fd_);
70 
71     //   Get sender and receiver fds and store it to user allocated
72     //   memory. Receive fd is used to process NAKs from peers.
73     void get_sender_fds (fd_t *send_fd_,
74                          fd_t *receive_fd_,
75                          fd_t *rdata_notify_fd_,
76                          fd_t *pending_notify_fd_);
77 
78     //  Send data as one APDU, transmit window owned memory.
79     size_t send (unsigned char *data_, size_t data_len_);
80 
81     //  Returns max tsdu size without fragmentation.
82     size_t get_max_tsdu_size ();
83 
84     //  Receive data from pgm socket.
85     ssize_t receive (void **data_, const pgm_tsi_t **tsi_);
86 
87     long get_rx_timeout ();
88     long get_tx_timeout ();
89 
90     //  POLLIN on sender side should mean NAK or SPMR receiving.
91     //  process_upstream function is used to handle such a situation.
92     void process_upstream ();
93 
94   private:
95     //  Compute size of the buffer based on rate and recovery interval.
96     int compute_sqns (int tpdu_);
97 
98     //  OpenPGM transport.
99     pgm_sock_t *sock;
100 
101     int last_rx_status, last_tx_status;
102 
103     //  Associated socket options.
104     options_t options;
105 
106     //  true when pgm_socket should create receiving side.
107     bool receiver;
108 
109     //  Array of pgm_msgv_t structures to store received data
110     //  from the socket (pgm_transport_recvmsgv).
111     pgm_msgv_t *pgm_msgv;
112 
113     //  Size of pgm_msgv array.
114     size_t pgm_msgv_len;
115 
116     // How many bytes were read from pgm socket.
117     size_t nbytes_rec;
118 
119     //  How many bytes were processed from last pgm socket read.
120     size_t nbytes_processed;
121 
122     //  How many messages from pgm_msgv were already sent up.
123     size_t pgm_msgv_processed;
124 };
125 }
126 #endif
127 
128 #endif
129