00001 // LICENSE: (Please see the file COPYING for details) 00002 // 00003 // NUS - Nemesis Utilities System: A C++ application development framework 00004 // Copyright (C) 2006, 2008 Otavio Rodolfo Piske 00005 // 00006 // This file is part of NUS 00007 // 00008 // This library is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU Lesser General Public 00010 // License as published by the Free Software Foundation version 2.1 00011 // of the License. 00012 // 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 // Lesser General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU Lesser General Public 00019 // License along with this library; if not, write to the Free Software 00020 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 // 00022 #ifndef NABSTRACTSOCKET_H 00023 #define NABSTRACTSOCKET_H 00024 00033 #include "ndefs.h" 00034 #include "nstring.h" 00035 #include "ndatastream.h" 00036 #include "nhostaddress.h" 00037 #include "nsignal.h" 00038 #include "niodevice.h" 00039 00040 #ifndef _MSC_VER 00041 #include <sys/socket.h> 00042 #endif // _MSC_VER 00043 00044 00048 class DllDeclSpec NAbstractSocket: public NIODevice { 00049 public: 00054 enum SocketType { 00055 Unknown = -1, 00056 TcpSocket, 00057 UdpSocket 00058 }; 00059 00061 static const nint32 DEFAULT_SOCK_OPTS; 00063 static const nint32 REUSE_ADDRESS; 00065 static const nint32 KEEPALIVE; 00066 00070 NAbstractSocket(void); 00071 00076 NAbstractSocket(const NHostAddress &host); 00077 00083 NAbstractSocket(const NHostAddress &host, nuint16 port); 00084 00090 NAbstractSocket(const NString &host, nuint16 port); 00091 00096 virtual ~NAbstractSocket(void); 00097 00098 00103 virtual void setHost(const NHostAddress &host); 00104 00109 virtual void setHost(const NString &host); 00110 00115 NHostAddress getHost(void) const; 00116 00121 virtual void setPort(nuint16 port); 00122 00127 nuint16 getPort(void) const; 00128 00129 00135 void setTimeout(nuint16 timeout); 00136 00141 nuint16 getTimeout(void) const; 00142 00148 void setMaxPendingConnections(nint32 max); 00149 00154 nint32 getMaxPendingConnections(void) const; 00155 00159 virtual void setSocketOptions(nint32 opt); 00160 00161 00165 virtual void open(void) = 0; 00166 00173 void open(Mode mode) { open(); }; 00174 00178 virtual void closeDevice(void) = 0; 00179 00183 virtual void disconnectFromHost(void) = 0; 00184 00188 virtual void execListen(void) = 0; 00189 00193 virtual void connectToHost(void) = 0; 00194 00195 00201 virtual nint32 read(NDataStream *buffer) = 0; 00202 00209 virtual nint32 read(NDataStream *buffer, nuint32 bytes) = 0; 00210 00216 virtual nint32 readLine(NDataStream *buffer) = 0; 00217 00224 virtual nint32 write(const NDataStream &data, nuint32 bytes) = 0; 00225 00226 00236 virtual nint32 writeLine(const NDataStream &data, nuint32 bytes) = 0; 00237 00238 protected: 00239 NHostAddress m_host; 00240 nuint16 m_port; 00242 nuint16 m_timeout; 00243 nint32 m_opt; 00244 nint32 m_maxPendingConn; 00245 }; 00246 00247 #endif // NABSTRACTSOCKET_H