ENUt
|
00001 /* 00002 00003 This file is a part of ENUt, a library containing network 00004 programming utilities. 00005 00006 Copyright (C) 2006-2008 Hasselt University - Expertise Centre for 00007 Digital Media (EDM) (http://www.edm.uhasselt.be) 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 00022 USA 00023 00024 */ 00025 00030 #ifndef NUT_MULTICASTTUNNELSOCKET_H 00031 00032 #define NUT_MULTICASTTUNNELSOCKET_H 00033 00034 #include "nutconfig.h" 00035 00036 #ifdef NUTCONFIG_SUPPORT_ENET 00037 00038 #include "datagramsocket.h" 00039 #include "enetsocket.h" 00040 #include <list> 00041 00042 namespace nut 00043 { 00044 00045 class IPv4Address; 00046 class UDPSocket; 00047 00071 class ENUT_IMPORTEXPORT MulticastTunnelSocket : public DatagramSocket 00072 { 00073 public: 00075 MulticastTunnelSocket(); 00076 00078 MulticastTunnelSocket(const std::string &objName); 00079 00080 ~MulticastTunnelSocket(); 00081 00090 bool create(const IPv4Address &serverAddr, uint16_t serverPort, uint16_t mcastRemotePortNumber); 00091 00093 bool destroy(); 00094 00096 uint16_t getLocalPortNumber() const; 00097 00098 bool joinMulticastGroup(const NetworkLayerAddress &groupAddress); 00099 bool leaveMulticastGroup(const NetworkLayerAddress &groupAddress); 00100 00101 bool write(const void *pData, size_t &length, const NetworkLayerAddress &destinationAddress, uint16_t destinationPort); 00102 00103 bool getAvailableDataLength(size_t &length, bool &available); 00104 bool read(void *buffer, size_t &bufferSize); 00105 00106 const NetworkLayerAddress *getLastSourceAddress() const; 00107 uint16_t getLastSourcePort() const; 00108 const NetworkLayerAddress *getLastDestinationAddress() const; 00109 protected: 00110 ENetSocket getSocketDescriptor() { return m_pEnetSocket->getSocketDescriptor(); } 00111 private: 00112 // Make sure we can't copy the socket 00113 MulticastTunnelSocket(const MulticastTunnelSocket &s) : DatagramSocket(s) { } 00114 MulticastTunnelSocket &operator=(const MulticastTunnelSocket &s) { } 00115 00116 class Packet 00117 { 00118 public: 00119 Packet(uint8_t *pBuf, size_t length, NetworkLayerAddress *pSrcAddr, uint16_t srcPort, 00120 NetworkLayerAddress *pDstAddr) 00121 { 00122 m_pBuf = pBuf; 00123 m_length = length; 00124 m_pSrcAddr = pSrcAddr; 00125 m_srcPort = srcPort; 00126 m_pDstAddr = pDstAddr; 00127 } 00128 00129 ~Packet() 00130 { 00131 delete [] m_pBuf; 00132 delete m_pSrcAddr; 00133 delete m_pDstAddr; 00134 } 00135 00136 uint8_t *getBuffer() const { return m_pBuf + sizeof(uint16_t)+sizeof(uint32_t)*2; } 00137 size_t getLength() const { return m_length - sizeof(uint16_t)-sizeof(uint32_t)*2; } 00138 const NetworkLayerAddress *getSourceAddress() const { return m_pSrcAddr; } 00139 uint16_t getSourcePort() const { return m_srcPort; } 00140 const NetworkLayerAddress *getDestinationAddress() const { return m_pDstAddr; } 00141 private: 00142 uint8_t *m_pBuf; 00143 size_t m_length; 00144 NetworkLayerAddress *m_pSrcAddr, *m_pDstAddr; 00145 uint16_t m_srcPort; 00146 }; 00147 00148 bool poll(); 00149 bool writeCommand(uint8_t cmdNum, uint8_t *pData = 0, size_t length = 0); 00150 00151 ENETSocket *m_pEnetSocket; 00152 std::list<Packet *> m_packets; 00153 Packet *m_pLastReadPacket; 00154 bool m_init; 00155 }; 00156 00157 } 00158 00159 #endif // NUTCONFIG_SUPPORT_ENET 00160 00161 #endif // NUT_MULTICASTTUNNELSOCKET_H