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_UDPV4SOCKET_H 00031 00032 #define NUT_UDPV4SOCKET_H 00033 00034 #include "nutconfig.h" 00035 #include "udpsocket.h" 00036 #include "ipv4address.h" 00037 00038 #if defined(WIN32) && !defined(_WIN32_WCE) 00039 #include <mswsock.h> 00040 #endif // WIN32 00041 00042 namespace nut 00043 { 00044 00046 class ENUT_IMPORTEXPORT UDPv4Socket : public UDPSocket 00047 { 00048 public: 00050 UDPv4Socket(); 00051 00053 UDPv4Socket(const std::string &objName); 00054 00055 ~UDPv4Socket(); 00056 00057 bool create(uint16_t portNumber = 0, bool obtainDestination = false); 00058 bool create(NetworkLayerAddress &bindAddress, uint16_t portNumber = 0, bool obtainDestination = false); 00059 bool destroy(); 00060 00061 bool setNonBlocking(bool f = true); 00062 00063 uint16_t getLocalPortNumber() const { return m_localPort; } 00064 00065 bool joinMulticastGroup(const NetworkLayerAddress &groupAddress); 00066 bool leaveMulticastGroup(const NetworkLayerAddress &groupAddress); 00067 bool setMulticastTTL(uint8_t ttl); 00068 00069 bool write(const void *data, size_t &length, const NetworkLayerAddress &destinationAddress, 00070 uint16_t destinationPort); 00071 bool getAvailableDataLength(size_t &length, bool &available); 00072 bool read(void *buffer, size_t &bufferSize); 00073 const NetworkLayerAddress *getLastSourceAddress() const { return &m_srcIP; } 00074 uint16_t getLastSourcePort() const { return m_srcPort; } 00075 const NetworkLayerAddress *getLastDestinationAddress() const { return &m_dstIP; } 00076 protected: 00077 #if (defined(WIN32) || defined(_WIN32_WCE)) 00078 SOCKET getSocketDescriptor() { return m_sock; } 00079 #else 00080 int getSocketDescriptor() { return m_sock; } 00081 #endif // WIN32 || _WIN32_WCE 00082 private: 00083 // make sure we can't copy the socket 00084 UDPv4Socket(const UDPv4Socket &s) : UDPSocket(s) { } 00085 UDPv4Socket &operator=(const UDPv4Socket &s) { return *this; } 00086 00087 void zeroAll(); 00088 bool internalCreate(uint32_t ip, uint16_t port, bool obtainDestination); 00089 std::string getSocketErrorString(); 00090 00091 #if (defined(WIN32) || defined(_WIN32_WCE)) 00092 SOCKET m_sock; 00093 00094 #ifndef _WIN32_WCE 00095 LPFN_WSARECVMSG WSARecvMsg; 00096 #endif // _WIN32_WCE 00097 #else 00098 int m_sock; 00099 #endif // WIN32 || _WIN32_WCE 00100 00101 bool m_isBlocking; 00102 uint16_t m_localPort; 00103 uint32_t m_bindIP; 00104 IPv4Address m_srcIP; 00105 uint16_t m_srcPort; 00106 IPv4Address m_dstIP; 00107 bool m_obtainDest; 00108 }; 00109 00110 } // end namespace 00111 00112 #endif // NUT_UDPV4SOCKET_H 00113