PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/ptypes-2.1.1/include/pinet.h

http://github.com/tito/Movid
C Header | 400 lines | 253 code | 95 blank | 52 comment | 5 complexity | df234d6bee2fd68ba987ac44d219cc64 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0
  1. /*
  2. *
  3. * C++ Portable Types Library (PTypes)
  4. * Version 2.1.1 Released 27-Jun-2007
  5. *
  6. * Copyright (C) 2001-2007 Hovik Melikyan
  7. *
  8. * http://www.melikyan.com/ptypes/
  9. *
  10. */
  11. #ifndef __PINET_H__
  12. #define __PINET_H__
  13. #ifndef __PPORT_H__
  14. #include "pport.h"
  15. #endif
  16. #ifndef __PTYPES_H__
  17. #include "ptypes.h"
  18. #endif
  19. #ifndef __PSTREAMS_H__
  20. #include "pstreams.h"
  21. #endif
  22. #ifdef WIN32
  23. # include <winsock2.h>
  24. #else
  25. # include <netdb.h> // for socklen_t
  26. # include <sys/types.h>
  27. # include <sys/socket.h>
  28. #endif
  29. PTYPES_BEGIN
  30. #ifdef _MSC_VER
  31. #pragma pack(push, 4)
  32. #endif
  33. //
  34. // BSD-compatible socket error codes for Win32
  35. //
  36. #if defined(WSAENOTSOCK) && !defined(ENOTSOCK)
  37. #define EWOULDBLOCK WSAEWOULDBLOCK
  38. #define EINPROGRESS WSAEINPROGRESS
  39. #define EALREADY WSAEALREADY
  40. #define ENOTSOCK WSAENOTSOCK
  41. #define EDESTADDRREQ WSAEDESTADDRREQ
  42. #define EMSGSIZE WSAEMSGSIZE
  43. #define EPROTOTYPE WSAEPROTOTYPE
  44. #define ENOPROTOOPT WSAENOPROTOOPT
  45. #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
  46. #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
  47. #define EOPNOTSUPP WSAEOPNOTSUPP
  48. #define EPFNOSUPPORT WSAEPFNOSUPPORT
  49. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  50. #define EADDRINUSE WSAEADDRINUSE
  51. #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
  52. #define ENETDOWN WSAENETDOWN
  53. #define ENETUNREACH WSAENETUNREACH
  54. #define ENETRESET WSAENETRESET
  55. #define ECONNABORTED WSAECONNABORTED
  56. #define ECONNRESET WSAECONNRESET
  57. #define ENOBUFS WSAENOBUFS
  58. #define EISCONN WSAEISCONN
  59. #define ENOTCONN WSAENOTCONN
  60. #define ESHUTDOWN WSAESHUTDOWN
  61. #define ETOOMANYREFS WSAETOOMANYREFS
  62. #define ETIMEDOUT WSAETIMEDOUT
  63. #define ECONNREFUSED WSAECONNREFUSED
  64. #define ELOOP WSAELOOP
  65. // #define ENAMETOOLONG WSAENAMETOOLONG
  66. #define EHOSTDOWN WSAEHOSTDOWN
  67. #define EHOSTUNREACH WSAEHOSTUNREACH
  68. // #define ENOTEMPTY WSAENOTEMPTY
  69. #define EPROCLIM WSAEPROCLIM
  70. #define EUSERS WSAEUSERS
  71. #define EDQUOT WSAEDQUOT
  72. #define ESTALE WSAESTALE
  73. #define EREMOTE WSAEREMOTE
  74. // NOTE: these are not errno constants in UNIX!
  75. #define HOST_NOT_FOUND WSAHOST_NOT_FOUND
  76. #define TRY_AGAIN WSATRY_AGAIN
  77. #define NO_RECOVERY WSANO_RECOVERY
  78. #define NO_DATA WSANO_DATA
  79. #endif
  80. // shutdown() constants
  81. #if defined(SD_RECEIVE) && !defined(SHUT_RD)
  82. # define SHUT_RD SD_RECEIVE
  83. # define SHUT_WR SD_SEND
  84. # define SHUT_RDWR SD_BOTH
  85. #endif
  86. // max backlog value for listen()
  87. #ifndef SOMAXCONN
  88. # define SOMAXCONN -1
  89. #endif
  90. typedef char* sockval_t;
  91. #ifndef WIN32
  92. # define closesocket close
  93. #endif
  94. #if (defined(__DARWIN__) && !defined(_SOCKLEN_T)) || defined(WIN32) || defined(__hpux)
  95. typedef int psocklen;
  96. #else
  97. typedef socklen_t psocklen;
  98. #endif
  99. // -------------------------------------------------------------------- //
  100. // --- IP address class and DNS utilities ---------------------------- //
  101. // -------------------------------------------------------------------- //
  102. //
  103. // IP address
  104. //
  105. struct ptpublic ipaddress
  106. {
  107. public:
  108. union
  109. {
  110. uchar data[4];
  111. ulong ldata;
  112. };
  113. ipaddress() {}
  114. ipaddress(ulong a) { ldata = a; }
  115. ipaddress(const ipaddress& a) { ldata = a.ldata; }
  116. ipaddress(int a, int b, int c, int d);
  117. ipaddress& operator= (ulong a) { ldata = a; return *this; }
  118. ipaddress& operator= (const ipaddress& a) { ldata = a.ldata; return *this; }
  119. uchar& operator [] (int i) { return data[i]; }
  120. operator ulong() const { return ldata; }
  121. };
  122. ptpublic extern ipaddress ipnone;
  123. ptpublic extern ipaddress ipany;
  124. ptpublic extern ipaddress ipbcast;
  125. //
  126. // IP peer info: host name, IP and the port name
  127. // used internally in ipstream and ipmessage
  128. //
  129. class ptpublic ippeerinfo: public noncopyable
  130. {
  131. protected:
  132. ipaddress ip; // target IP
  133. string host; // target host name; either IP or hostname must be specified
  134. int port; // target port number
  135. void notfound(); // throws a (estream*) exception
  136. ptpublic friend bool ptdecl psockname(int, ippeerinfo&);
  137. public:
  138. ippeerinfo();
  139. ippeerinfo(ipaddress iip, const string& ihost, int iport);
  140. ipaddress get_ip(); // resolves the host name if necessary (only once)
  141. string get_host(); // performs reverse-lookup if necessary (only once)
  142. int get_port() { return port; }
  143. void clear();
  144. string asstring(bool showport) const;
  145. };
  146. ptpublic string ptdecl iptostring(ipaddress ip);
  147. ptpublic ipaddress ptdecl phostbyname(const char* name);
  148. ptpublic string ptdecl phostbyaddr(ipaddress ip);
  149. ptpublic string ptdecl phostcname(const char* name);
  150. // internal utilities
  151. ptpublic int ptdecl usockerrno();
  152. ptpublic const char* ptdecl usockerrmsg(int code);
  153. ptpublic bool ptdecl psockwait(int handle, int timeout);
  154. ptpublic bool ptdecl psockname(int handle, ippeerinfo&);
  155. // -------------------------------------------------------------------- //
  156. // --- TCP socket classes -------------------------------------------- //
  157. // -------------------------------------------------------------------- //
  158. // additional IO status codes
  159. const int IO_RESOLVING = 10;
  160. const int IO_RESOLVED = 11;
  161. const int IO_CONNECTING = 20;
  162. const int IO_CONNECTED = 21;
  163. //
  164. // ipstream
  165. //
  166. class ptpublic ipstream: public fdxstm, public ippeerinfo
  167. {
  168. friend class ipstmserver;
  169. protected:
  170. int svsocket; // server socket descriptor, used internally by ipstmserver
  171. #ifdef WIN32
  172. // sockets are not compatible with file handles on Windows
  173. virtual int dorawread(char* buf, int count);
  174. virtual int dorawwrite(const char* buf, int count);
  175. #endif
  176. virtual int uerrno();
  177. virtual const char* uerrmsg(int code);
  178. virtual void doopen();
  179. virtual large doseek(large newpos, ioseekmode mode);
  180. virtual void doclose();
  181. virtual void sockopt(int socket);
  182. void closehandle();
  183. public:
  184. ipstream();
  185. ipstream(ipaddress ip, int port);
  186. ipstream(const char* host, int port);
  187. ipstream(const string& host, int port);
  188. virtual ~ipstream();
  189. virtual int classid();
  190. virtual string get_streamname();
  191. bool waitfor(int timeout);
  192. ipaddress get_myip();
  193. int get_myport();
  194. void set_ip(ipaddress);
  195. void set_host(const string&);
  196. void set_host(const char*);
  197. void set_port(int);
  198. };
  199. //
  200. // common internal interfaces for ipstmserver and ipmsgserver
  201. //
  202. class ipbindinfo: public unknown, public ippeerinfo
  203. {
  204. public:
  205. int handle;
  206. ipbindinfo(ipaddress iip, const string& ihost, int iport);
  207. virtual ~ipbindinfo();
  208. };
  209. class ptpublic ipsvbase: public unknown
  210. {
  211. protected:
  212. int socktype;
  213. bool active;
  214. tobjlist<ipbindinfo> addrlist; // list of local socket addresses to bind to
  215. void error(ippeerinfo& peer, int code, const char* defmsg);
  216. bool dopoll(int* i, int timeout);
  217. void setupfds(void* set, int i);
  218. virtual void open();
  219. virtual void close();
  220. virtual void dobind(ipbindinfo*) = 0;
  221. virtual void sockopt(int socket);
  222. public:
  223. ipsvbase(int isocktype);
  224. virtual ~ipsvbase();
  225. int bind(ipaddress ip, int port);
  226. int bindall(int port);
  227. int get_addrcount() { return addrlist.get_count(); }
  228. const ipbindinfo& get_addr(int i) { return *addrlist[i]; }
  229. void clear();
  230. };
  231. //
  232. // ipstmserver
  233. //
  234. class ptpublic ipstmserver: public ipsvbase
  235. {
  236. protected:
  237. virtual void dobind(ipbindinfo*);
  238. public:
  239. ipstmserver();
  240. virtual ~ipstmserver();
  241. bool poll(int i = -1, int timeout = 0);
  242. bool serve(ipstream& client, int i = -1, int timeout = -1);
  243. };
  244. // -------------------------------------------------------------------- //
  245. // --- UDP socket classes -------------------------------------------- //
  246. // -------------------------------------------------------------------- //
  247. //
  248. // ipmessage
  249. //
  250. class ptpublic ipmessage: public unknown, public ippeerinfo
  251. {
  252. protected:
  253. int handle;
  254. void error(int code, const char* msg);
  255. void open();
  256. void close();
  257. virtual void sockopt(int socket);
  258. public:
  259. ipmessage();
  260. ipmessage(ipaddress ip, int port);
  261. ipmessage(const char* host, int port);
  262. ipmessage(const string& host, int port);
  263. virtual ~ipmessage();
  264. void set_ip(ipaddress iip);
  265. void set_host(const string&);
  266. void set_host(const char*);
  267. void set_port(int);
  268. ipaddress get_myip();
  269. int get_myport();
  270. int get_handle() { return handle; }
  271. bool waitfor(int timeout);
  272. int receive(char* buf, int count, ipaddress& src);
  273. int receive(char* buf, int count);
  274. string receive(int max, ipaddress& src);
  275. string receive(int max);
  276. void send(const char* buf, int count);
  277. void send(const string& s) { send(s, length(s)); }
  278. };
  279. //
  280. // ipmsgserver
  281. //
  282. class ptpublic ipmsgserver: public ipsvbase, public ippeerinfo
  283. {
  284. protected:
  285. int handle;
  286. virtual void close();
  287. virtual void dobind(ipbindinfo*);
  288. public:
  289. ipmsgserver();
  290. virtual ~ipmsgserver();
  291. int get_handle() { return handle; }
  292. bool poll(int i = -1, int timeout = 0);
  293. int receive(char* buf, int count);
  294. string receive(int max);
  295. void send(const char* buf, int count);
  296. void send(const string& s) { send(s, length(s)); }
  297. void sendto(const char* buf, int count, ipaddress ip, int port);
  298. void sendto(const string& s, ipaddress ip, int port)
  299. { sendto(s, length(s), ip, port); }
  300. };
  301. #ifdef _MSC_VER
  302. #pragma pack(pop)
  303. #endif
  304. PTYPES_END
  305. #endif // __PINET_H__