pgm_socket.hpp 3.64 KB
Newer Older
malosek's avatar
malosek committed
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3 4
    Copyright (c) 2007-2009 iMatix Corporation
    Copyright (c) 2010-2011 Miru Limited
5
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
malosek's avatar
malosek committed
6 7 8 9

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
10
    the terms of the GNU Lesser General Public License as published by
malosek's avatar
malosek committed
11 12 13 14 15 16
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
    GNU Lesser General Public License for more details.
malosek's avatar
malosek committed
18

19
    You should have received a copy of the GNU Lesser General Public License
malosek's avatar
malosek committed
20 21 22 23 24 25 26 27
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __PGM_SOCKET_HPP_INCLUDED__
#define __PGM_SOCKET_HPP_INCLUDED__

#include "platform.hpp"

malosek's avatar
malosek committed
28
#if defined ZMQ_HAVE_OPENPGM
malosek's avatar
malosek committed
29

30 31
#ifdef ZMQ_HAVE_WINDOWS
#include "windows.hpp"
32
#define __PGM_WININT_H__
malosek's avatar
malosek committed
33 34
#endif

35 36
#include <pgm/pgm.h>

37 38 39 40
#ifdef ZMQ_HAVE_OSX
#include <pgm/in.h>
#endif

41
#include "fd.hpp"
malosek's avatar
malosek committed
42 43 44 45 46 47 48 49 50
#include "options.hpp"

namespace zmq
{
    //  Encapsulates PGM socket.
    class pgm_socket_t
    {

    public:
51

malosek's avatar
malosek committed
52 53 54 55 56 57 58
        //  If receiver_ is true PGM transport is not generating SPM packets.
        pgm_socket_t (bool receiver_, const options_t &options_);

        //  Closes the transport.
        ~pgm_socket_t ();

        //  Initialize PGM network structures (GSI, GSRs).
59
        int init (bool udp_encapsulation_, const char *network_);
60 61 62

        //  Resolve PGM socket address.
        static int init_address(const char *network_, struct pgm_addrinfo_t **addr, uint16_t *port_number);
malosek's avatar
malosek committed
63 64
        
        //   Get receiver fds and store them into user allocated memory.
65
        void get_receiver_fds (fd_t *receive_fd_, fd_t *waiting_pipe_fd_);
malosek's avatar
malosek committed
66 67 68

        //   Get sender and receiver fds and store it to user allocated 
        //   memory. Receive fd is used to process NAKs from peers.
69 70
        void get_sender_fds (fd_t *send_fd_, fd_t *receive_fd_,
            fd_t *rdata_notify_fd_, fd_t *pending_notify_fd_);
malosek's avatar
malosek committed
71 72 73 74

        //  Send data as one APDU, transmit window owned memory.
        size_t send (unsigned char *data_, size_t data_len_);

Martin Sustrik's avatar
Martin Sustrik committed
75 76
        //  Returns max tsdu size without fragmentation.
        size_t get_max_tsdu_size ();
malosek's avatar
malosek committed
77 78

        //  Receive data from pgm socket.
malosek's avatar
malosek committed
79
        ssize_t receive (void **data_, const pgm_tsi_t **tsi_);
malosek's avatar
malosek committed
80

81 82 83
        long get_rx_timeout ();
        long get_tx_timeout ();

malosek's avatar
malosek committed
84 85
        //  POLLIN on sender side should mean NAK or SPMR receiving. 
        //  process_upstream function is used to handle such a situation.
86
        void process_upstream ();
malosek's avatar
malosek committed
87

88
    private:
89 90 91

        //  Compute size of the buffer based on rate and recovery interval.
        int compute_sqns (int tpdu_);
malosek's avatar
malosek committed
92
    
93
        //  OpenPGM transport.
Steven McCoy's avatar
Steven McCoy committed
94
        pgm_sock_t* sock;
malosek's avatar
malosek committed
95

96 97
        int last_rx_status, last_tx_status;

malosek's avatar
malosek committed
98 99 100
        //  Associated socket options.
        options_t options;
       
malosek's avatar
malosek committed
101 102 103
        //  true when pgm_socket should create receiving side.
        bool receiver;

Martin Sustrik's avatar
Martin Sustrik committed
104
        //  Array of pgm_msgv_t structures to store received data
malosek's avatar
malosek committed
105 106 107
        //  from the socket (pgm_transport_recvmsgv).
        pgm_msgv_t *pgm_msgv;

Martin Sustrik's avatar
Martin Sustrik committed
108 109 110
        //  Size of pgm_msgv array.
        size_t pgm_msgv_len;

malosek's avatar
malosek committed
111
        // How many bytes were read from pgm socket.
malosek's avatar
malosek committed
112
        size_t nbytes_rec;
malosek's avatar
malosek committed
113 114

        //  How many bytes were processed from last pgm socket read.
malosek's avatar
malosek committed
115
        size_t nbytes_processed;
malosek's avatar
malosek committed
116 117
        
        //  How many messages from pgm_msgv were already sent up.
malosek's avatar
malosek committed
118
        size_t pgm_msgv_processed;
malosek's avatar
malosek committed
119 120 121 122 123
    };
}
#endif

#endif
124