jmulticastsocket.h 4.9 KB
/***************************************************************************
 *   Copyright (C) 2005 by Jeff Ferr                                       *
 *   root@sat                                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef J_MULTICAST_SOCKET_H
#define J_MULTICAST_SOCKET_H

#include "jinetaddress.h"
#include "jmulticastsocketbuffer.h"
#include "jsocketoption.h"

#include <iostream>
#include <string>
#include <vector>

#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdint.h>

namespace jsocket {

/**
 * \brief MulticastSocket.
 *
 * \author Jeff Ferr
 */
class MulticastSocket{

    private:
        /** \brief Use to bind the socket in a free port */
        static uint16_t _used_port;
        
        /** \brief Descriptor */
        int _fds, _fdr;
        /** \brief Local socket */
        sockaddr_in _sock_s, _sock_r;
        /** \brief Buffer UDP */
        MulticastSocketBuffer *_buffer;
        /** \brief */
        SocketOption *_sock_opt;
        /** \brief Input stream */
        std::istream *_is;
        /** \brief Output stream */
        std::ostream *_os;
        /** \brief */
        uint64_t _sent_bytes;
        /** \brief */
        uint64_t _receive_bytes;
        /** \brief */
        std::vector<std::string> _groups;
        
        /**
        * \brief Create a new socket
        *
        */
        void CreateSocket();
        
        /**
        * \brief Bind socket
        *
        */
        void BindSocket(InetAddress *addr_, uint16_t local_port_);
        
        /**
        * \brief Connect socket
        *
        */
        void ConnectSocket(InetAddress *addr_, uint16_t port_);
        
        /**
        * \brief
        *
        */
        void InitStream();

    public:
        /**
        * \brief 
        *
        */
        MulticastSocket(std::string addr_, uint16_t port_);
        
        /**
        * \brief Destrutor virtual.
        *
        */
        virtual ~MulticastSocket();
        
        /**
        * \brief
        *
        */
        std::istream & GetInputStream();
        
        /**
        * \brief
        *
        */
        std::ostream & GetOutputStream();
        
        /**
        * \brief
        *
        */
        int Receive(char *data_, int size_);
        
        /**
        * \brief
        *
        */
        int Send(char *data_, int size_);

        /**
         * \brief
         *
         */
        void Join(std::string group_);

        /**
         * \brief
         *
         */
        void Join(InetAddress *group_);

        /**
         * \brief
         *
         */
        void Leave(std::string group_);

        /**
         * \brief
         *
         */
        void Leave(InetAddress *group_);

        /**
         * \brief
         *
         */
        std::vector<std::string> & GetGroupList();

        /**
        * \brief
        *
        */
        void Close();
        
        /**
        * \brief
        *
        */
        uint16_t GetLocalPort();
        
        /**
        * \brief
        *
        */
        uint64_t GetSentBytes();
        
        /**
        * \brief
        *
        */
        uint64_t GetReceiveBytes();

        /**
         * \brief
         *
         */
        void SetMulticastTTL(uint8_t ttl_);
        
        /**
         * \brief
         *
         */
        SocketOption * GetSocketOption();
        
        /**
        * \brief
        *
        */
        std::string what();

};

};

#endif