00001
00002
00003 #ifndef BASE_SOCKET_H
00004 #define BASE_SOCKET_H
00005
00006 #ifdef _WIN32
00007 #define WIN32_LEAN_AND_MEAN
00008 #include <winsock2.h>
00009 #else
00010 #include <sys/socket.h>
00011 #include <sys/un.h>
00012 #include <netinet/in.h>
00013 #include <arpa/inet.h>
00014 #endif
00015 #include "String.h"
00016 #include "IOException.h"
00017
00018 namespace base {
00020
00024 class PPBASE_EXPORT Socket {
00025 public:
00026 #ifdef _WIN32
00027 typedef SOCKET socket_type;
00028 static const socket_type invalid_socket = INVALID_SOCKET;
00029 typedef int socklen_type;
00030 #else
00031 typedef int socket_type;
00032 static const socket_type invalid_socket = -1;
00033 typedef socklen_t socklen_type;
00034 #endif
00035
00037
00044 Socket(socket_type sd = invalid_socket, int tout_acc = 60, int tout_conn = 60, int tout_recv = 60, int tout_send = 60);
00045
00047 ~Socket();
00048
00050
00054 Socket *accept();
00055
00057 void close();
00058
00060
00064 void connect(struct sockaddr *addr, socklen_type len);
00065
00067
00071 void connect(const String &host, int port = 0);
00072
00074
00077 bool connected() const;
00078
00080
00086 void discard();
00087
00089
00092 bool eof() const;
00093
00095
00098 static void finit();
00099
00101 void flush();
00102
00104
00107 socket_type getSD() const;
00108
00110
00114 String getLocalAddr(bool full = false);
00115
00117
00120 String getLocalIP();
00121
00123
00126 String getLocalIPPort();
00127
00129
00132 int getLocalPort();
00133
00135
00139 String getRemoteAddr(bool full = false);
00140
00142
00145 String getRemoteIP();
00146
00148
00151 String getRemoteIPPort();
00152
00154
00157 int getRemotePort();
00158
00160
00163 static void init();
00164
00166
00174 static socket_type openInet(int type = SOCK_STREAM, int port = -1, int listenq = -1, const char *ip = NULL);
00175
00176 #ifndef _WIN32
00177
00178
00185 static socket_type openUnix(int type, int port = -1, int listenq = -1);
00186
00188
00195 static socket_type openUnix(int type = SOCK_STREAM, const char *path = NULL, int listenq = -1);
00196 #endif
00197
00199
00206 void open(int type = SOCK_STREAM, int port = -1, int listenq = -1, const char *ip = NULL);
00207
00209
00215 void open(int type, const char *addr, int listenq = -1);
00216
00218
00223 size_t read(char *buf, size_t len);
00224
00226
00231 size_t read(String &buf, size_t len);
00232
00234
00239 static void readEOF(int fd, String &ret, int timeout = 0);
00240
00242
00245 void readEOF(String &ret);
00246
00248
00253 bool readln(String &line, bool strip = false);
00254
00256
00263 static size_t readto(socket_type sd, char *buf, size_t len, int timeout);
00264
00266
00270 void relay(socket_type sd, int timeout = 0);
00271
00273
00277 void relay(Socket &sock, int timeout = 0);
00278
00280
00284 static void setBlocking(socket_type sd, bool blocking);
00285
00287
00290 void setBlocking(bool blocking);
00291
00293
00296 void setSD(socket_type sd);
00297
00299
00303 void setReadBuffer(size_t len);
00304
00306
00312 void setWriteBuffer(size_t len);
00313
00315
00321 void setTimeout(int tout_acc, int tout_conn, int tout_recv, int tout_send);
00322
00324
00328 void write(const char *buf, size_t len);
00329
00331
00334 void write(const String &s);
00335
00337
00344 static size_t writeto(socket_type sd, const char *buf, size_t len, int timeout);
00345
00347
00352 size_t writef(const char *fmt, ...)
00353 #if __GNUC__
00354 __attribute__((format(printf, 2, 3)))
00355 #endif
00356 ;
00357
00359
00364 size_t writevf(const char *fmt, va_list ap);
00365
00366 private:
00367 bool checkLocalAddr();
00368 bool checkRemoteAddr();
00369
00370 bool _connected;
00371 bool _eof;
00372 union {
00373 struct sockaddr sa;
00374 struct sockaddr_in sin;
00375 #ifndef _WIN32
00376 struct sockaddr_un sun;
00377 #endif
00378 } _localaddr;
00379 char *_rdbuf;
00380 size_t _rdlen;
00381 char *_rdptr;
00382 size_t _rdsize;
00383 union {
00384 struct sockaddr sa;
00385 struct sockaddr_in sin;
00386 #ifndef _WIN32
00387 struct sockaddr_un sun;
00388 #endif
00389 } _remoteaddr;
00390 socket_type _sd;
00391 bool _server;
00392 int _tout_acc;
00393 int _tout_conn;
00394 int _tout_recv;
00395 int _tout_send;
00396 char *_wrbuf;
00397 size_t _wrlen;
00398 size_t _wrsize;
00399
00400 DISALLOW_COPY_CONSTRUCTOR_AND_ASSIGNMENT(Socket);
00401 };
00402 }
00403
00404 #endif