jsocket.hpp 24 KB

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