00001 /* 00002 Ishtar - A network transparent read/write interface with 00003 probing and enumeration support. 00004 network part : convenient cross-platform packaging for tcp/ip networking 00005 Copyright (C) 2003-2005 Stephane Magnenat <stephane at magnenat dot net> 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 */ 00021 00022 #ifndef __ISHTAR_NETWORK_H 00023 #define __ISHTAR_NETWORK_H 00024 00025 #include "stream.h" 00026 #ifndef WIN32 00027 #include <arpa/inet.h> 00028 #include <netinet/in.h> 00029 #else 00030 #ifndef INADDR_ANY 00031 #define INADDR_ANY 0x00000000 00032 #endif 00033 #endif 00034 #include <string> 00035 #include <list> 00036 00041 namespace Ishtar 00042 { 00044 class TCPIPAddress 00045 { 00046 public: 00048 00050 UInt32 address; 00051 00053 00055 UInt16 port; 00056 00057 public: 00059 TCPIPAddress(UInt32 addr = INADDR_ANY, UInt16 prt = 0) 00060 { 00061 address = addr; 00062 port = prt; 00063 } 00064 00066 bool operator==(const TCPIPAddress& o) const 00067 { 00068 return address==o.address && port==o.port; 00069 } 00071 bool operator<(const TCPIPAddress& o) const 00072 { 00073 return address<o.address || (address==o.address && port<o.port); 00074 } 00076 std::string format() const; 00078 bool valid() const { return address != INADDR_ANY && port != 0; } 00079 }; 00080 00082 class SocketHelper 00083 { 00084 private: 00085 #ifndef WIN32 00086 00087 void (*oldHandler)(int); 00088 #endif 00089 00090 public: 00092 SocketHelper(); 00094 ~SocketHelper(); 00095 00097 static TCPIPAddress resolve(const std::string &name, UInt16 defPort); 00098 }; 00099 00101 extern SocketHelper socketHelper; 00102 00104 class Socket: public OutputStream, public InputStream 00105 { 00106 protected: 00107 friend class NetworkClient; 00108 friend class NetworkServer; 00110 TCPIPAddress remoteAddress; 00112 int socket; 00114 unsigned char *sendBuffer; 00116 size_t bufferSize; 00118 size_t bufferPos; 00120 bool connected; 00121 00122 protected: 00124 void send(const void *data, Size size); 00125 00126 public: 00128 Socket(); 00130 virtual ~Socket(); 00131 virtual void write(const void *data, const Size size); 00132 virtual void flush(void); 00133 virtual void read(void *data, Size size); 00135 std::string getRemoteName(void) { return remoteAddress.format(); } 00137 bool isConnected(void) { return connected; } 00139 void disconnect(void) { connected = false; } 00140 }; 00141 00143 class NetworkClient 00144 { 00145 protected: 00147 Socket *socket; 00148 00149 public: 00151 NetworkClient(const std::string &host, UInt16 port); 00153 virtual ~NetworkClient(); 00154 00156 void run(void); 00158 bool step(bool block = false, long timeout = 0); 00159 00160 protected: 00162 virtual void incomingData(Socket *socket) = 0; 00164 virtual void connectionClosed(Socket *socket) = 0; 00165 }; 00166 00168 class NetworkServer 00169 { 00170 private: 00172 Socket *serverSocket; 00174 std::list<Socket *> clients; 00175 00176 private: 00178 void closeSocket(Socket *s); 00179 00180 public: 00182 NetworkServer(UInt16 port); 00184 virtual ~NetworkServer() { } 00185 00187 void run(void); 00189 void step(bool block = false, long timeout = 0); 00190 00191 protected: 00193 virtual void incomingData(Socket *socket) = 0; 00195 virtual void incomingConnection(Socket *socket) = 0; 00197 virtual void connectionClosed(Socket *socket) = 0; 00198 }; 00199 } 00200 00201 #endif