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_ENETSOCKET_H 00031 00032 #define NUT_ENETSOCKET_H 00033 00034 #include "nutconfig.h" 00035 00036 #ifdef NUTCONFIG_SUPPORT_ENET 00037 00038 #include "socket.h" 00039 #include "ipv4address.h" 00040 #include <enet/enet.h> 00041 #include <map> 00042 #include <list> 00043 00044 namespace nut 00045 { 00046 00053 class ENUT_IMPORTEXPORT ENETSocket : public Socket 00054 { 00055 public: 00057 ENETSocket(); 00058 00060 ENETSocket(const std::string &objName); 00061 00062 ~ENETSocket(); 00063 00065 static bool ENETStartup(); 00066 00068 static void ENETCleanup(); 00069 00076 bool createServer(int maxConnections, uint16_t port = 0); 00077 00085 bool createServer(const IPv4Address &bindAddr, int maxConnections, uint16_t port = 0); 00086 00092 bool createClient(int maxConnections); 00093 00095 bool isServer() const { return m_server; } 00096 00098 bool close(); 00099 00101 uint16_t getLocalPort() const { return m_localPort; } 00102 00109 bool waitForEvent(int milliseconds); 00110 00115 bool poll(); 00116 00123 bool requestConnection(const IPv4Address &destAddress, uint16_t destPort, uint8_t channelCount); 00124 00126 bool closeConnection(uint32_t connID); 00127 00129 int getNumberOfConnections() const { return (int)m_connectionMap.size(); } 00130 00132 const uint32_t *getConnectionIDs() const { return m_pConnectionIDs; } 00133 00140 bool getConnectionInfo(uint32_t connID, IPv4Address &addr, uint16_t &port) const; 00141 00153 bool write(const void *pData, size_t dataLength, bool reliable = false, uint8_t channel = 0, uint32_t connID = 0, bool allExceptConnID = false); 00154 00161 bool getAvailableDataLength(size_t &length, bool &available); 00162 00176 bool read(void *buffer, size_t &bufferSize, uint8_t *channel = 0, uint32_t *connID = 0); 00177 protected: 00186 virtual void onNewConnection(uint32_t connID, const IPv4Address &sourceAddress, uint16_t sourcePort) { } 00187 00192 virtual void onCloseConnection(uint32_t connID) { } 00193 private: 00194 // make sure we can't copy the socket 00195 ENETSocket(const ENETSocket &s) : Socket(s) { } 00196 ENETSocket &operator=(const ENETSocket &s) { } 00197 00198 bool createServerInternal(const ENetAddress &bindAddress, int maxConnections); 00199 bool createClientInternal(uint32_t bindIP, uint16_t bindPort, int maxConnections); 00200 void buildNewConnectionArray(); 00201 00202 ENetHost *m_pHost; 00203 bool m_server; 00204 uint16_t m_localPort; 00205 00206 class ENETPacket 00207 { 00208 public: 00209 ENETPacket(ENetPacket *pPacket, uint8_t channel, uint32_t connID) 00210 { 00211 m_pPacket = pPacket; 00212 m_channel = channel; 00213 m_connID = connID; 00214 } 00215 00216 ENetPacket *getPacket() { return m_pPacket; } 00217 uint8_t getChannel() const { return m_channel; } 00218 uint32_t getConnectionID() const { return m_connID; } 00219 private: 00220 ENetPacket *m_pPacket; 00221 uint8_t m_channel; 00222 uint32_t m_connID; 00223 }; 00224 00225 std::list<ENETPacket> m_packetQueue; 00226 00227 uint32_t m_nextConnID; 00228 std::map<uint32_t, ENetPeer *> m_connectionMap; 00229 uint32_t *m_pConnectionIDs; 00230 uint32_t m_maxConnections; 00231 00232 friend class MulticastTunnelSocket; 00233 00234 protected: 00235 // Should probably only be used by the socket waiter 00236 ENetSocket getSocketDescriptor() { return m_pHost->socket; } 00237 }; 00238 00239 } // end namespace 00240 00241 #endif // NUTCONFIG_SUPPORT_ENET 00242 00243 #endif // NUT_ENETSOCKET_H 00244