00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #if !defined(__LRT_NETWORK__) && !defined(__SYMBIAN32__) // && !(defined(__WIN32) && !defined(__MSVC__))
00017 #define __LRT_NETWORK__
00018 #define __NETWORK__ // alternate macro
00019
00028 #include "rtstreams.h"
00029 #include "rtmap.h"
00030
00031 namespace lrt {
00032
00033 class String;
00034 template<class T> class StringMap;
00035 class Time;
00036
00037 class Socket;
00038 class ServerSocket;
00039 class NetworkInputStream;
00040 class NetworkOutputStream;
00041
00043 class Network {
00044 public:
00046
00048
00049 Page() : headers(false), code(0), text() {}
00050 bool isError() { return (code / 100 != 2); }
00051 StringMap<String> headers;
00052 String text;
00053 int code;
00054 String message;
00055 };
00056 public:
00061
00065
00066
00068 static const Time& getTimeout();
00072 static void setTimeout(const Time&);
00073
00076 static void splitURL(const String& url, String& proto, String& server, int& port, String& file);
00084 static bool httpRequest(const String& url, HttpMethod method, Page& page);
00086 static bool httpRequest(const String& server, int port, HttpMethod method, const String& file, Page& page);
00087
00090 static String urlEncode(const StringMap<String>& data);
00092 static String urlEncode(const String& str);
00093
00094 private:
00095 static Time timeout;
00096 };
00097
00102 class Socket {
00103 public:
00104 virtual ~Socket();
00105
00107 NetworkInputStream* getInput();
00109 NetworkOutputStream* getOutput();
00111 void close();
00113 bool isClosed();
00115 String getPeerName();
00116
00117 protected:
00118 Socket(void* sock);
00119 friend class ServerSocket;
00120 friend class Network;
00121 friend class NetworkInputStream;
00122 friend class NetworkOutputStream;
00123
00124 void* sock;
00125 NetworkInputStream* in;
00126 NetworkOutputStream* out;
00127 };
00128
00132 class ServerSocket : public lrt::Socket {
00133 public:
00134 ~ServerSocket();
00136 Socket* accept();
00137
00138 protected:
00139 ServerSocket(void* sock);
00140 friend class Network;
00141 };
00142
00144 class NetworkInputStream : public InputStream {
00145 public:
00146 ~NetworkInputStream();
00147
00148 virtual int read();
00149 virtual int read(Array<char> &b, int off, int len);
00150 virtual bool eos();
00152 virtual void close();
00153
00154
00155 protected:
00156 NetworkInputStream(Socket* sock);
00157 friend class Socket;
00158
00159 Socket* sock;
00160 };
00161
00163 class NetworkOutputStream: public OutputStream {
00164 public:
00165 virtual ~NetworkOutputStream();
00166
00167 virtual bool write(int b);
00168 virtual bool write(const Array<char> &b, int off, int len);
00169 virtual bool write(const Array<char> &b);
00171 virtual void close();
00172
00173 protected:
00174 NetworkOutputStream(Socket* sock);
00175 friend class Socket;
00176
00177 Socket* sock;
00178 };
00179
00180 }
00181
00182 #endif
00183