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_TCPPACKETSOCKET_H 00031 00032 #define NUT_TCPPACKETSOCKET_H 00033 00034 #include "nutconfig.h" 00035 #include "tcpsocket.h" 00036 #include <time.h> 00037 #include <list> 00038 00039 namespace nut 00040 { 00041 00042 class TCPSocket; 00043 class Packet; 00044 00046 class ENUT_IMPORTEXPORT TCPPacketSocket : public Socket 00047 { 00048 public: 00069 TCPPacketSocket(TCPSocket *pSock, bool deleteSocket, bool sixteenBit = false, uint32_t maxReceiveLength = 0xffffffff, size_t idLength = 0, uint32_t id = 0); 00070 00071 ~TCPPacketSocket(); 00072 00076 bool poll(); 00077 00079 bool getAvailableDataLength(size_t &length) { if (!m_pBaseSocket->getAvailableDataLength(length)) { setErrorString(m_pBaseSocket->getErrorString()); return false; } return true; } 00080 00086 bool isPacketAvailable() { return !m_packetQueue.empty(); } 00087 00094 bool read(Packet &packet); 00095 00097 bool write(const void *pData, size_t length); 00098 00099 time_t getLastReadTime() const { return m_lastReadTime; } 00100 time_t getLastWriteTime() const { return m_lastWriteTime; } 00101 TCPSocket *getBaseSocket() { return m_pBaseSocket; } 00102 protected: 00103 #if (defined(WIN32) || defined(_WIN32_WCE)) 00104 SOCKET getSocketDescriptor() { return m_pBaseSocket->getSocketDescriptor(); } 00105 #else 00106 int getSocketDescriptor() { return m_pBaseSocket->getSocketDescriptor(); } 00107 #endif // WIN32 || _WIN32_WCE 00108 private: 00109 // make sure we can't copy the socket 00110 TCPPacketSocket(const TCPPacketSocket &s) : Socket(s) { } 00111 TCPPacketSocket &operator=(const TCPPacketSocket &s) { return *this; } 00112 00113 TCPSocket *m_pBaseSocket; 00114 uint32_t m_maximumSendLength; 00115 uint32_t m_maximumReceiveLength; 00116 uint32_t m_id, m_idNBO; 00117 int m_idLength; 00118 int m_lengthBytes; 00119 int m_prefixLength; 00120 bool m_deleteSocket; 00121 00122 std::list<Packet *> m_packetQueue; 00123 00124 uint8_t m_sizeBuffer[sizeof(uint32_t)*2]; // one for the length field, the other one for an optional 32-bit identifier 00125 size_t m_sizeBufferPosition; 00126 00127 uint8_t *m_pBuffer; 00128 uint32_t m_bufferLength; 00129 uint32_t m_bufferPosition; 00130 00131 time_t m_lastReadTime; 00132 time_t m_lastWriteTime; 00133 }; 00134 00135 } // end namespace 00136 00137 #endif // NUT_TCPPACKETSOCKET_H 00138