jsocket.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. // Socket abstraction
  15. #ifndef __JSOCKIO_H__
  16. #define __JSOCKIO_H__
  17. #ifndef _VER_C5
  18. #include <time.h>
  19. #endif
  20. #include "jexpdef.hpp"
  21. #include "jexcept.hpp"
  22. #include "jthread.hpp"
  23. #define DEFAULT_LISTEN_QUEUE_SIZE 200 // maximum for windows 2000 server
  24. #define DEFAULT_LINGER_TIME 1000 // seconds
  25. #ifndef WAIT_FOREVER
  26. #define WAIT_FOREVER ((unsigned)-1)
  27. #endif
  28. enum JSOCKET_ERROR_CODES {
  29. JSOCKERR_ok = 0,
  30. JSOCKERR_not_opened = -1, // accept,name,peer_name,read,write
  31. JSOCKERR_bad_address = -2, // connect
  32. JSOCKERR_connection_failed = -3, // connect
  33. JSOCKERR_broken_pipe = -4, // read,write
  34. JSOCKERR_invalid_access_mode = -5, // accept
  35. JSOCKERR_timeout_expired = -6, // read
  36. JSOCKERR_port_in_use = -7, // create
  37. JSOCKERR_cancel_accept = -8, // accept
  38. JSOCKERR_connectionless_socket = -9, // accept, cancel_accept
  39. JSOCKERR_graceful_close = -10, // read,send
  40. JSOCKERR_handle_too_large = -11, // select, connect etc (linux only)
  41. JSOCKERR_bad_netaddr = -12, // get/set net address
  42. JSOCKERR_ipv6_not_implemented = -13 // various
  43. };
  44. // Block operation flags
  45. #define BF_ASYNC_TRANSFER 0 // send_block sends immediately (default)
  46. #define BF_SYNC_TRANSFER_PULL 1 // send_block waits until receiver ready (i.e. receives first)
  47. #define BF_LZW_COMPRESS 2 // compress using LZW compression
  48. #define BF_REC_COMPRESS 4 // compress using record difference compression
  49. #define BF_RELIABLE_TRANSFER 8 // retries on socket failure
  50. #define BF_SYNC_TRANSFER_PUSH 16 // send_block pushes that has data (i.e. sends first)
  51. // shutdown options
  52. #define SHUTDOWN_READ 0
  53. #define SHUTDOWN_WRITE 1
  54. #define SHUTDOWN_READWRITE 2
  55. //
  56. // Abstract socket interface
  57. //
  58. class jlib_decl IpAddress
  59. {
  60. unsigned netaddr[4];
  61. public:
  62. IpAddress() { ipset(NULL); }
  63. IpAddress(const IpAddress& other) { ipset(other); }
  64. IpAddress(const char *text) { ipset(text); }
  65. bool ipset(const char *text); // sets to NULL if fails or text=NULL
  66. void ipset(const IpAddress& other) { memcpy(&netaddr,&other.netaddr,sizeof(netaddr)); }
  67. bool ipequals(const IpAddress & other) const;
  68. int ipcompare(const IpAddress & other) const; // depreciated
  69. unsigned iphash(unsigned prev=0) const;
  70. bool isNull() const; // is null
  71. bool isHost() const; // is primary host NIC ip
  72. bool isLoopBack() const; // is loopback (localhost: 127.0.0.1 or ::1)
  73. bool isLocal() const; // matches local interface
  74. bool isLinkLocal() const;
  75. bool isSiteLocal() const; // depreciated
  76. bool isIp4() const;
  77. StringBuffer &getIpText(StringBuffer & out) const;
  78. void ipserialize(MemoryBuffer & out) const;
  79. void ipdeserialize(MemoryBuffer & in);
  80. unsigned ipdistance(const IpAddress &ip,unsigned offset=0) const; // network order distance (offset: 0-3 word (leat sig.), 0=Ipv4)
  81. bool ipincrement(unsigned count,byte minoctet=0,byte maxoctet=255,unsigned short minipv6piece=0,unsigned maxipv6piece=0xffff);
  82. unsigned ipsetrange( const char *text); // e.g. 10.173.72.1-65 ('-' may be omitted)
  83. // returns number in range (use ipincrement to iterate through)
  84. size32_t getNetAddress(size32_t maxsz,void *dst) const; // for internal use - returns 0 if address doesn't fit
  85. void setNetAddress(size32_t sz,const void *src); // for internal use
  86. inline IpAddress & operator = ( const IpAddress &other )
  87. {
  88. ipset(other);
  89. return *this;
  90. }
  91. };
  92. inline IpAddress &Array__Member2Param(IpAddress &src) { return src; }
  93. inline void Array__Assign(IpAddress & dest, IpAddress &src) { dest=src; }
  94. inline bool Array__Equal(IpAddress &m, IpAddress &p) { return m.ipequals(p); }
  95. inline void Array__Destroy(IpAddress &p) { }
  96. class jlib_decl IpAddressArray : public ArrayOf<IpAddress, IpAddress &>
  97. {
  98. public:
  99. StringBuffer &getText(StringBuffer &text);
  100. void fromText(const char *s,unsigned defport);
  101. };
  102. extern jlib_decl IpAddress & queryHostIP();
  103. extern jlib_decl IpAddress & queryLocalIP();
  104. extern jlib_decl const char * GetCachedHostName();
  105. inline StringBuffer & GetHostName(StringBuffer &str) { return str.append(GetCachedHostName()); }
  106. extern jlib_decl IpAddress &GetHostIp(IpAddress &ip);
  107. extern jlib_decl IpAddress &localHostToNIC(IpAddress &ip);
  108. class jlib_decl SocketEndpoint : extends IpAddress
  109. {
  110. public:
  111. SocketEndpoint() { set(NULL,0); };
  112. SocketEndpoint(const char *name,unsigned short _port=0) { set(name,_port); };
  113. SocketEndpoint(unsigned short _port) { setLocalHost(_port); };
  114. SocketEndpoint(unsigned short _port, const IpAddress & _ip) { set(_port,_ip); };
  115. SocketEndpoint(const SocketEndpoint &other) { set(other); }
  116. void deserialize(MemoryBuffer & in);
  117. void serialize(MemoryBuffer & out) const;
  118. bool set(const char *name,unsigned short _port=0);
  119. inline void set(const SocketEndpoint & value) { ipset(value); port = value.port; }
  120. inline void setLocalHost(unsigned short _port) { port = _port; GetHostIp(*this); } // NB *not* localhost(127.0.0.1)
  121. inline void set(unsigned short _port, const IpAddress & _ip) { ipset(_ip); port = _port; };
  122. inline bool equals(const SocketEndpoint &ep) const { return ((port==ep.port)&&ipequals(ep)); }
  123. void getUrlStr(char * str, size32_t len) const; // in form ip4:port or [ip6]:port
  124. StringBuffer &getUrlStr(StringBuffer &str) const; // in form ip4:port or [ip6]:port
  125. inline SocketEndpoint & operator = ( const SocketEndpoint &other )
  126. {
  127. ipset(other);
  128. port = other.port;
  129. return *this;
  130. }
  131. bool operator == (const SocketEndpoint &other) const { return equals(other); }
  132. unsigned hash(unsigned prev) const;
  133. unsigned short port;
  134. };
  135. inline SocketEndpoint &Array__Member2Param(SocketEndpoint &src) { return src; }
  136. inline void Array__Assign(SocketEndpoint & dest, SocketEndpoint &src) { dest=src; }
  137. inline bool Array__Equal(SocketEndpoint &m, SocketEndpoint &p) { return m.equals(p); }
  138. inline void Array__Destroy(SocketEndpoint &p) { }
  139. class jlib_decl SocketEndpointArray : public ArrayOf<SocketEndpoint, SocketEndpoint &>
  140. {
  141. public:
  142. StringBuffer &getText(StringBuffer &text);
  143. void fromText(const char *s,unsigned defport);
  144. };
  145. interface ISocketEndpointHashTable: implements IInterface
  146. {
  147. virtual void add(const SocketEndpoint &ep, IInterface *i)=0; // takes ownership
  148. virtual void remove(const SocketEndpoint &ep)=0; // releases
  149. virtual IInterface *find(const SocketEndpoint &ep)=0; // does not link
  150. };
  151. extern jlib_decl ISocketEndpointHashTable *createSocketEndpointHashTable();
  152. class jlib_decl IpSubNet
  153. {
  154. unsigned net[4];
  155. unsigned mask[4];
  156. public:
  157. IpSubNet() {set(NULL,NULL); }
  158. IpSubNet(const char *_net,const char *_mask) { set(_net,_mask); }
  159. bool set(const char *_net,const char *_mask); // _net NULL means match everything
  160. // _mask NULL means match exact
  161. bool test(const IpAddress &ip);
  162. StringBuffer getNetText(StringBuffer &text);
  163. StringBuffer getMaskText(StringBuffer &text);
  164. bool isNull();
  165. };
  166. class jlib_decl ISocket : extends IInterface
  167. {
  168. public:
  169. //
  170. // Create client socket connected to a TCP server socket
  171. static ISocket* connect( const SocketEndpoint &ep );
  172. // general connect
  173. static ISocket* connect_timeout( const SocketEndpoint &ep , unsigned timeout);
  174. // connect where should must take longer than timeout (in ms) to connect
  175. static ISocket* connect_wait( const SocketEndpoint &ep, unsigned timems);
  176. // connect where should try connecting for *at least* time specified
  177. // (e.g. if don't know that server listening yet)
  178. // if 0 specified for time then does single (blocking) connect try
  179. // Create client socket connected to a UDP server socket
  180. //
  181. static ISocket* udp_connect( unsigned short port, char const* host);
  182. static ISocket* udp_connect( const SocketEndpoint &ep);
  183. //
  184. // Create server TCP socket
  185. //
  186. static ISocket* create( unsigned short port,
  187. int listen_queue_size = DEFAULT_LISTEN_QUEUE_SIZE);
  188. //
  189. // Create server TCP socket listening a specific IP
  190. //
  191. static ISocket* create_ip( unsigned short port,
  192. const char *host,
  193. int listen_queue_size = DEFAULT_LISTEN_QUEUE_SIZE);
  194. //
  195. // Create server UDP socket
  196. //
  197. static ISocket* udp_create( unsigned short port);
  198. // Create client socket connected to a multicast server socket
  199. //
  200. static ISocket* multicast_connect( unsigned short port, const char *mcgroupip, unsigned ttl);
  201. static ISocket* multicast_connect( const SocketEndpoint &ep, unsigned ttl);
  202. //
  203. // Create server multicast socket
  204. //
  205. static ISocket* multicast_create( unsigned short port, const char *mcgroupip);
  206. static ISocket* multicast_create( unsigned short port, const IpAddress &mcgroupip);
  207. //
  208. // Creates an ISocket for an already created socket
  209. //
  210. static ISocket* attach(int s,bool tcpip=true);
  211. virtual void read(void* buf, size32_t min_size, size32_t max_size, size32_t &size_read,
  212. unsigned timeoutsecs = WAIT_FOREVER) = 0;
  213. virtual void readtms(void* buf, size32_t min_size, size32_t max_size, size32_t &size_read,
  214. unsigned timeout) = 0;
  215. virtual void read(void* buf, size32_t size) = 0;
  216. virtual size32_t write(void const* buf, size32_t size) = 0; // returns amount written normally same as in size (see set_nonblock)
  217. virtual size32_t get_max_send_size() = 0;
  218. //
  219. // This method is called by server to accept client connection
  220. //
  221. virtual ISocket* accept(bool allowcancel=false) = 0; // not needed for UDP
  222. //
  223. // This method is called to check whether a socket has data ready
  224. //
  225. virtual int wait_read(unsigned timeout) = 0;
  226. //
  227. // This method is called to check whether a socket is ready to write (i.e. some free buffer space)
  228. //
  229. virtual int wait_write(unsigned timeout) = 0;
  230. //
  231. // can be used with write to allow it to return if it would block
  232. // be sure and restore to old state before calling other functions on this socket
  233. //
  234. virtual bool set_nonblock(bool on) = 0; // returns old state
  235. // enable 'nagling' - small packet coalescing (implies delayed transmission)
  236. //
  237. virtual bool set_nagle(bool on) = 0; // returns old state
  238. // set 'linger' time - time close will linger so that outstanding unsent data will be transmitted
  239. //
  240. virtual void set_linger(int lingersecs) = 0;
  241. //
  242. // Cancel accept operation and close socket
  243. //
  244. virtual void cancel_accept() = 0; // not needed for UDP
  245. //
  246. // Shutdown socket: prohibit write and/or read operations on socket
  247. //
  248. virtual void shutdown(unsigned mode=SHUTDOWN_READWRITE) = 0; // not needed for UDP
  249. // Get name of accepted socket and returns port
  250. virtual int name(char *name,size32_t namemax)=0;
  251. // Get peer name of socket and returns port - in UDP returns return addr
  252. virtual int peer_name(char *name,size32_t namemax)=0;
  253. // Get peer endpoint of socket - in UDP returns return addr
  254. virtual SocketEndpoint &getPeerEndpoint(SocketEndpoint &ep)=0;
  255. // Get peer ip of socket - in UDP returns return addr
  256. virtual IpAddress &getPeerAddress(IpAddress &addr)=0;
  257. //
  258. // Close socket
  259. //
  260. virtual bool connectionless()=0; // true if accept need not be called (i.e. UDP)
  261. virtual void set_return_addr(int port,const char *name) = 0; // used for UDP servers only
  262. // Block functions
  263. virtual void set_block_mode ( // must be called before block operations
  264. unsigned flags, // BF_* flags (must match receive_block)
  265. size32_t recsize=0, // record size (required for rec compression)
  266. unsigned timeoutms=0 // timeout in milisecs (0 for no timeout)
  267. )=0;
  268. virtual bool send_block(
  269. const void *blk, // data to send
  270. size32_t sz // size to send (0 for eof)
  271. )=0;
  272. virtual size32_t receive_block_size ()=0; // get size of next block (always must call receive_block after)
  273. virtual size32_t receive_block(
  274. void *blk, // receive pointer
  275. size32_t sz // max size to read (0 for sync eof)
  276. // if less than block size truncates block
  277. )=0;
  278. virtual void close() = 0;
  279. virtual unsigned OShandle() = 0; // for internal use
  280. virtual size32_t avail_read() = 0; // called after wait_read to see how much data available
  281. virtual size32_t write_multiple(unsigned num,void const**buf, size32_t *size) = 0; // same as write except writes multiple blocks
  282. virtual size32_t get_send_buffer_size() =0; // get OS send buffer
  283. virtual void set_send_buffer_size(size32_t sz) =0; // set OS send buffer size
  284. virtual bool join_multicast_group(SocketEndpoint &ep) = 0; // for udp multicast
  285. virtual bool leave_multicast_group(SocketEndpoint &ep) = 0; // for udp multicast
  286. virtual size32_t get_receive_buffer_size() =0; // get OS receive buffer
  287. virtual void set_receive_buffer_size(size32_t sz) =0; // set OS receive buffer size
  288. virtual void set_keep_alive(bool set)=0; // set option SO_KEEPALIVE
  289. virtual size32_t udp_write_to(SocketEndpoint &ep,void const* buf, size32_t size)=0;
  290. virtual bool check_connection() = 0;
  291. /*
  292. Exceptions raised: (when set_raise_exceptions(TRUE))
  293. create
  294. sys:(socket, bind, listen)
  295. udp_create
  296. sys:(socket, bind, listen)
  297. accept
  298. JSOCKERR_not_opened, sys:(accept,setsockopt), JSOCKERR_invalid_access_mode, JSOCKERR_cancel_accept, JSOCKERR_connectionless_socket
  299. name
  300. JSOCKERR_not_opened, sys:(getsockname)
  301. peer_name
  302. JSOCKERR_not_opened, sys:(getpeername)
  303. cancel_accept
  304. {connect}, sys:(gethostname), JSOCKERR_connectionless_socket
  305. connect
  306. JSOCKERR_bad_address, JSOCKERR_connection_failed, sys:(socket, connect, setsockopt)
  307. udp_connect
  308. JSOCKERR_bad_address, sys:(socket, connect, setsockopt)
  309. read (timeout)
  310. JSOCKERR_not_opened, JSOCKERR_broken_pipe, JSOCKERR_timeout_expired ,sys:(select, read), JSOCKERR_graceful_close
  311. read (no timeout)
  312. JSOCKERR_not_opened, JSOCKERR_broken_pipe, sys:(read), JSOCKERR_graceful_close
  313. write
  314. JSOCKERR_not_opened, JSOCKERR_broken_pipe, sys:(write), JSOCKERR_graceful_close
  315. close
  316. sys:(write)
  317. shutdown
  318. sys:(shutdown),JSOCKERR_broken_pipe
  319. */
  320. };
  321. interface jlib_thrown_decl IJSOCK_Exception: extends IException
  322. {
  323. };
  324. extern jlib_decl IJSOCK_Exception *IPv6NotImplementedException(const char *filename,unsigned lineno);
  325. #define IPV6_NOT_IMPLEMENTED() throw IPv6NotImplementedException(__FILE__, __LINE__)
  326. //---------------------------------------------------------------------------
  327. // These classes are useful for compressing a list of ip:ports to pass around.
  328. class jlib_decl SocketListCreator
  329. {
  330. public:
  331. SocketListCreator();
  332. void addSocket(const SocketEndpoint &ep);
  333. void addSocket(const char * ip, unsigned port);
  334. const char * getText();
  335. void addSockets(SocketEndpointArray &array);
  336. protected:
  337. StringBuffer fullText;
  338. StringAttr lastIp;
  339. unsigned lastPort;
  340. };
  341. class jlib_decl SocketListParser
  342. // This class depreciated - new code should use SocketEndpointArray::fromText and getText
  343. {
  344. public:
  345. SocketListParser(const char * text);
  346. void first(unsigned defport=0);
  347. bool get(StringAttr & ip, unsigned & port, unsigned index, unsigned defport=0); // alternative to iterating..
  348. bool next(StringAttr & ip, unsigned & port);
  349. unsigned getSockets(SocketEndpointArray &array,unsigned defport=0);
  350. protected:
  351. StringAttr fullText;
  352. StringAttr lastIp;
  353. const char * cursor;
  354. unsigned lastPort;
  355. };
  356. struct JSocketStatistics
  357. {
  358. unsigned connects; // successful
  359. unsigned connecttime; // all times in microsecs
  360. unsigned failedconnects;
  361. unsigned failedconnecttime;
  362. unsigned reads;
  363. unsigned readtime;
  364. __int64 readsize; // all sizes in bytes
  365. unsigned writes;
  366. unsigned writetime;
  367. __int64 writesize;
  368. unsigned activesockets;
  369. unsigned numblockrecvs;
  370. unsigned numblocksends;
  371. __int64 blockrecvsize;
  372. __int64 blocksendsize;
  373. unsigned blockrecvtime; // not including initial handshake
  374. unsigned blocksendtime;
  375. unsigned longestblocksend;
  376. unsigned longestblocksize;
  377. };
  378. extern jlib_decl void getSocketStatistics(JSocketStatistics &stats);
  379. extern jlib_decl void resetSocketStatistics();
  380. extern jlib_decl StringBuffer &getSocketStatisticsString(JSocketStatistics &stats,StringBuffer &buf);
  381. // Select Thread
  382. #define SELECTMODE_READ 1
  383. #define SELECTMODE_WRITE 2
  384. #define SELECTMODE_EXCEPT 4
  385. interface ISocketSelectNotify: extends IInterface
  386. {
  387. virtual bool notifySelected(ISocket *sock,unsigned selected)=0; // return false to continue to next selected, true to re-select
  388. };
  389. interface ISocketSelectHandler: extends IInterface
  390. {
  391. public:
  392. virtual void start()=0;
  393. virtual void add(ISocket *sock,unsigned mode,ISocketSelectNotify *nfy)=0;
  394. virtual void remove(ISocket *sock)=0;
  395. virtual void stop(bool wait)=0;
  396. };
  397. extern jlib_decl ISocketSelectHandler *createSocketSelectHandler(const char *trc=NULL);
  398. class MemoryBuffer;
  399. // sends/receives length as well as contents.
  400. extern jlib_decl void readBuffer(ISocket * socket, MemoryBuffer & buffer);
  401. extern jlib_decl void readBuffer(ISocket * socket, MemoryBuffer & buffer, unsigned timeoutms);
  402. extern jlib_decl void writeBuffer(ISocket * socket, MemoryBuffer & buffer);
  403. // ditto but catches any exceptions
  404. extern jlib_decl bool catchReadBuffer(ISocket * socket, MemoryBuffer & buffer);
  405. extern jlib_decl bool catchReadBuffer(ISocket * socket, MemoryBuffer & buffer, unsigned timeoutms);
  406. extern jlib_decl bool catchWriteBuffer(ISocket * socket, MemoryBuffer & buffer);
  407. // utility interface for simple conversations
  408. // conversation is always between two ends,
  409. // at any given time one end must be receiving and other sending (though these may swap during the conversation)
  410. interface IConversation: extends IInterface
  411. {
  412. virtual bool accept(unsigned timeoutms)=0; // one side accepts
  413. virtual bool connect(unsigned timeoutms)=0; // other side connects
  414. virtual bool send(MemoryBuffer &mb)=0; // 0 length buffer can be sent
  415. virtual bool recv(MemoryBuffer &mb, unsigned timeoutms)=0; // up to protocol to terminate conversation (e.g. by zero length buffer)
  416. virtual void cancel()=0; // cancels above methods (from separate thread)
  417. virtual unsigned short setRandomPort(unsigned short base, unsigned num)=0; // sets a random unique port for accept use
  418. };
  419. extern jlib_decl IConversation *createSingletonSocketConnection(unsigned short port,SocketEndpoint *ep=NULL);
  420. // the end that listens may omit ep
  421. // this function does not connect so raises no socket exceptions
  422. // interface for reading from multiple sockets using the BF_SYNC_TRANSFER_PUSH protocol
  423. interface ISocketBufferReader: extends IInterface
  424. {
  425. public:
  426. virtual void init(unsigned num,ISocket **sockets,size32_t buffermax=(unsigned)-1)=0;
  427. virtual unsigned get(MemoryBuffer &mb)=0;
  428. virtual void done(bool wait)=0;
  429. };
  430. extern jlib_decl ISocketBufferReader *createSocketBufferReader(const char *trc=NULL);
  431. extern jlib_decl void markNodeCentral(SocketEndpoint &ep); // random delay for linux
  432. interface ISocketConnectNotify
  433. {
  434. public:
  435. virtual void connected(unsigned idx,const SocketEndpoint &ep,ISocket *socket)=0; // must link socket if kept
  436. virtual void failed(unsigned idx,const SocketEndpoint &ep,int err)=0;
  437. };
  438. extern jlib_decl void multiConnect(const SocketEndpointArray &eps,ISocketConnectNotify &inotify,unsigned timeout);
  439. extern jlib_decl void multiConnect(const SocketEndpointArray &eps,PointerIArrayOf<ISocket> &retsockets,unsigned timeout);
  440. interface ISocketConnectWait: extends IInterface
  441. {
  442. public:
  443. virtual ISocket *wait(unsigned waittimems)=0; // return NULL if time expired, throws exception if connect failed
  444. // releasing ISocketConnectWait cancels the connect iff wait has never returned socket
  445. };
  446. extern jlib_decl ISocketConnectWait *nonBlockingConnect(SocketEndpoint &ep,unsigned connectimeoutms=0);
  447. // buffered socket
  448. interface IBufferedSocket : implements IInterface
  449. {
  450. virtual int readline(char* buf, int maxlen, IMultiException *me) = 0;
  451. virtual int read(char* buf, int maxlen) = 0;
  452. virtual int readline(char* buf, int maxlen, bool keepcrlf, IMultiException *me) = 0;
  453. virtual void setReadTimeout(unsigned int timeout) = 0;
  454. };
  455. #define BSOCKET_READ_TIMEOUT 600
  456. #define BSOCKET_CLIENT_READ_TIMEOUT 7200
  457. extern jlib_decl IBufferedSocket* createBufferedSocket(ISocket* socket);
  458. #define MAX_NET_ADDRESS_SIZE (16)
  459. extern jlib_decl IpSubNet &queryPreferredSubnet(); // preferred subnet when resolving multiple NICs
  460. extern jlib_decl bool setPreferredSubnet(const char *ip,const char *mask); // also resets cached host IP
  461. extern jlib_decl StringBuffer lookupHostName(const IpAddress &ip,StringBuffer &ret);
  462. extern jlib_decl bool isInterfaceIp(const IpAddress &ip, const char *ifname);
  463. extern jlib_decl bool getInterfaceIp(IpAddress &ip, const char *ifname);
  464. #endif