ENUt

tcpv4socket.h

Go to the documentation of this file.
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_TCPV4SOCKET_H
00031 
00032 #define NUT_TCPV4SOCKET_H
00033 
00034 #include "nutconfig.h"
00035 #include "tcpsocket.h"
00036 #include "ipv4address.h"
00037 
00038 namespace nut
00039 {
00040 
00042 class ENUT_IMPORTEXPORT TCPv4Socket : public TCPSocket
00043 {
00044 public:
00046         TCPv4Socket();
00047 
00049         TCPv4Socket(const std::string &objName);
00050         
00051         ~TCPv4Socket();
00052 
00053         bool create(uint16_t portNumber = 0);
00054         bool create(const NetworkLayerAddress &bindAddress, uint16_t portNumber = 0);
00055         bool destroy();
00056 
00057         bool setNonBlocking(bool f = true);
00058         
00059         uint16_t getLocalPortNumber()                                                           { return m_localPort; }
00060 
00061         bool connect(const NetworkLayerAddress &hostAddress, uint16_t portNumber);
00062         bool listen(int backlog);
00063         bool accept(TCPSocket **newsock);
00064         bool isConnected()                                                                      { return m_connected; }
00065         
00066         const NetworkLayerAddress *getDestinationAddress() const                                { return m_destIP; }
00067         uint16_t getDestinationPort() const                                                     { return m_destPort; }
00068         
00069         bool write(const void *data, size_t &length);
00070         bool getAvailableDataLength(size_t &length);
00071         bool read(void *buffer, size_t &bufferSize);
00072 protected:
00073 #if (defined(WIN32) || defined(_WIN32_WCE))
00074         SOCKET getSocketDescriptor()                                                            { return m_sock; }
00075 #else
00076         int getSocketDescriptor()                                                               { return m_sock; }
00077 #endif // WIN32 || _WIN32_WCE
00078 private:
00079         // make sure we can't copy the socket
00080         TCPv4Socket(const TCPv4Socket &s) : TCPSocket(s)                                        { }
00081         TCPv4Socket &operator=(const TCPv4Socket &s)                                            { return *this; }
00082         
00083         bool internalCreate(uint32_t ip, uint16_t port);
00084         void zeroAll();
00085         std::string getSocketErrorString();
00086 
00087 #if (defined(WIN32) || defined(_WIN32_WCE))
00088         TCPv4Socket(SOCKET sock, uint16_t localPort, uint32_t destIP, uint16_t destPort);
00089 
00090         SOCKET m_sock;
00091 #else
00092         TCPv4Socket(int sock, uint16_t localPort, uint32_t destIP, uint16_t destPort);
00093 
00094         int m_sock;
00095 #endif // WIN32 || _WIN32_WCE
00096 
00097         bool m_connected, m_listening;
00098         uint16_t m_localPort;
00099         uint16_t m_destPort;
00100         IPv4Address *m_destIP;
00101 };
00102 
00103 }
00104 
00105 #endif // NUT_TCPV4SOCKET_H