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 NTCPSOCKET_H 00023 #define NTCPSOCKET_H 00024 00033 #include "ndefs.h" 00034 #include "ndatastream.h" 00035 #include "nstring.h" 00036 #include "nabstractsocket.h" 00037 #include "nexception.h" 00038 #include "ndebug.h" 00039 00040 #ifndef WIN32_LEAN_AND_MEAN 00041 #define WIN32_LEAN_AND_MEAN 00042 #endif 00043 #include <winsock2.h> 00044 #include <ws2tcpip.h> 00045 00046 00051 class DllDeclSpec NTcpSocket: public NAbstractSocket { 00052 public: 00056 NTcpSocket(void); 00057 00062 NTcpSocket(NHostAddress &host); 00063 00069 NTcpSocket(const NString &host, nuint16 port); 00070 00074 ~NTcpSocket(void); 00075 00080 void setPort(nint16 port); 00081 void setSocketOptions(nint32 opt); 00082 00087 NHostAddress peerAddress(void) const; 00088 00093 NString peerName(void) const; 00094 00099 nuint16 peerPort(void) const; 00100 00101 void open(void); 00102 void closeDevice(void); 00103 00104 void disconnectFromHost(void); 00105 void connectToHost(); 00106 void execListen(void); 00107 00113 NTcpSocket *waitForConnected(void); 00114 00115 nint32 read(NDataStream *buffer); 00116 nint32 read(NDataStream *buffer, nuint32 bytes); 00117 nint32 readLine(NDataStream *buffer); 00118 nint32 write(const NDataStream &data, nuint32 bytes = 0); 00119 nint32 writeLine(const NDataStream &data, nuint32 bytes = 0); 00120 00121 private: 00122 struct addrinfo *m_addr; 00123 SOCKET m_sock; 00124 00125 NTcpSocket(nuint16 timeout, nint32 sock); 00126 00127 void init(void); 00128 void execBind(void); 00129 00130 void setPeer(struct sockaddr_in addr); 00131 void setSock(int sock); 00132 void setupNewSocket(NTcpSocket *sock, struct sockaddr_in addr); 00133 00134 void throwSocketError(int err) const; 00135 }; 00136 00137 #endif // NTCPSOCKET_H