NCBI C++ ToolKit
ncbi_socket_cxx.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ncbi_socket_cxx.cpp 97320 2022-07-06 17:22:27Z lavr $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Anton Lavrentiev
27  *
28  * File Description:
29  * C++ wrappers for the C "SOCK" API (UNIX, MS-Win, MacOS, Darwin)
30  * Implementation of out-of-line methods
31  *
32  */
33 
34 #include <ncbi_pch.hpp>
35 #include "ncbi_assert.h" // no _ASSERT()s, keep clean from xncbi
37 #include <limits.h> // for PATH_MAX
38 #if defined(NCBI_OS_MSWIN) && !defined(PATH_MAX)
39 # define PATH_MAX 512 // will actually use less than 32 chars
40 #endif // NCBI_OS_MSWIN && !PATH_MAX
41 
42 
44 
45 
46 /////////////////////////////////////////////////////////////////////////////
47 // CTrigger::
48 //
49 
51 {
52  if (m_Trigger)
54 }
55 
56 
57 
58 /////////////////////////////////////////////////////////////////////////////
59 // CSocket::
60 //
61 
62 
63 CSocket::CSocket(const string& host,
64  unsigned short port,
65  const STimeout* timeout,
67  : m_IsOwned(eTakeOwnership),
68  r_timeout(0/*kInfiniteTimeout*/), w_timeout(0), c_timeout(0)
69 {
70  if (timeout && timeout != kDefaultTimeout) {
71  oo_timeout = *timeout;
73  } else
74  o_timeout = 0/*kInfiniteTimeout*/;
75  SOCK_CreateEx(host.c_str(), port, o_timeout, &m_Socket, 0, 0, flags);
76 }
77 
78 
79 CSocket::CSocket(const string& hostport,
80  const STimeout* timeout,
82  : m_IsOwned(eTakeOwnership),
83  r_timeout(0/*kInfiniteTimeout*/), w_timeout(0), c_timeout(0)
84 {
85  const char* end;
86  unsigned int x_host;
87  unsigned short port;
88  char host[16/*sizeof("255.255.255.255")*/];
89  if (timeout && timeout != kDefaultTimeout) {
90  oo_timeout = *timeout;
92  } else
93  o_timeout = 0/*kInfiniteTimeout*/;
94  if (!(end = SOCK_StringToHostPort(hostport.c_str(), &x_host, &port))
95  || *end || !x_host || !port
96  || SOCK_ntoa(x_host, host, sizeof(host)) != 0) {
97  m_Socket = 0;
98  } else
99  SOCK_CreateEx(host, port, o_timeout, &m_Socket, 0, 0, flags);
100 }
101 
102 
103 CSocket::CSocket(unsigned int host,
104  unsigned short port,
105  const STimeout* timeout,
107  : m_IsOwned(eTakeOwnership),
108  r_timeout(0/*kInfiniteTimeout*/), w_timeout(0), c_timeout(0)
109 {
110  char x_host[16/*sizeof("255.255.255.255")*/];
111  if (timeout && timeout != kDefaultTimeout) {
112  oo_timeout = *timeout;
114  } else
115  o_timeout = 0/*kInfiniteTimeout*/;
116  if (SOCK_ntoa(host, x_host, sizeof(x_host)) != 0)
117  m_Socket = 0;
118  else
119  SOCK_CreateEx(x_host, port, o_timeout, &m_Socket, 0, 0, flags);
120 
121 }
122 
123 
124 CUNIXSocket::CUNIXSocket(const string& path,
125  const STimeout* timeout,
127 {
128  if (timeout && timeout != kDefaultTimeout) {
129  oo_timeout = *timeout;
131  } else
132  o_timeout = 0/*kInfiniteTimeout*/;
133  SOCK_CreateUNIX(path.c_str(), o_timeout, &m_Socket, 0, 0, flags);
134 }
135 
136 
138 {
139  if (m_Socket && m_IsOwned != eNoOwnership)
141 }
142 
143 
144 EIO_Status CSocket::Connect(const string& host,
145  unsigned short port,
146  const STimeout* timeout,
148 {
149  if ( m_Socket ) {
150  if (SOCK_Status(m_Socket, eIO_Open) != eIO_Closed)
151  return eIO_Unknown;
152  if (m_IsOwned != eNoOwnership)
154  }
155  if (timeout != kDefaultTimeout) {
156  if ( timeout ) {
157  if (&oo_timeout != timeout)
158  oo_timeout = *timeout;
160  } else
161  o_timeout = 0/*kInfiniteTimeout*/;
162  }
163  EIO_Status status = SOCK_CreateEx(host.c_str(), port, o_timeout,
164  &m_Socket, 0, 0, flags);
165  if (status == eIO_Success) {
169  } else
170  assert(!m_Socket);
171  return status;
172 }
173 
174 
175 EIO_Status CSocket::Connect(const string& hostport,
176  const STimeout* timeout,
178 {
179  const char* end;
180  unsigned int x_host;
181  unsigned short port;
182  char host[16/*sizeof("255.255.255.255")*/];
183  if ( m_Socket ) {
184  if (SOCK_Status(m_Socket, eIO_Open) != eIO_Closed)
185  return eIO_Unknown;
186  if (m_IsOwned != eNoOwnership)
188  }
189  if (timeout != kDefaultTimeout) {
190  if ( timeout ) {
191  if (&oo_timeout != timeout)
192  oo_timeout = *timeout;
194  } else
195  o_timeout = 0/*kInfiniteTimeout*/;
196  }
197  EIO_Status status;
198  if (!(end = SOCK_StringToHostPort(hostport.c_str(), &x_host, &port))
199  || *end || !x_host || !port
200  || SOCK_ntoa(x_host, host, sizeof(host)) != 0) {
201  status = eIO_Unknown;
202  m_Socket = 0;
203  } else {
204  status = SOCK_CreateEx(host, port, o_timeout,
205  &m_Socket, 0, 0, flags);
206  }
207  if (status == eIO_Success) {
211  } else
212  assert(!m_Socket);
213  return status;
214 }
215 
216 
217 EIO_Status CUNIXSocket::Connect(const string& path,
218  const STimeout* timeout,
220 {
221  if ( m_Socket ) {
222  if (SOCK_Status(m_Socket, eIO_Open) != eIO_Closed)
223  return eIO_Unknown;
224  if (m_IsOwned != eNoOwnership)
226  }
227  if (timeout != kDefaultTimeout) {
228  if ( timeout ) {
229  if (&oo_timeout != timeout)
230  oo_timeout = *timeout;
232  } else
233  o_timeout = 0/*kInfiniteTimeout*/;
234  }
235  EIO_Status status = SOCK_CreateUNIX(path.c_str(), o_timeout,
236  &m_Socket, 0, 0, flags);
237  if (status == eIO_Success) {
241  } else
242  assert(!m_Socket);
243  return status;
244 }
245 
246 
248 {
249  if (timeout != kDefaultTimeout) {
250  if ( timeout ) {
251  if (&oo_timeout != timeout)
252  oo_timeout = *timeout;
254  } else
255  o_timeout = 0/*kInfiniteTimeout*/;
256  }
257  return m_Socket ? SOCK_Reconnect(m_Socket, 0, 0, o_timeout) : eIO_Closed;
258 }
259 
260 
262 {
263  if (timeout == kDefaultTimeout)
264  return eIO_Success;
265 
266  switch (event) {
267  case eIO_Open:
268  if ( timeout ) {
269  if (&oo_timeout != timeout)
270  oo_timeout = *timeout;
272  } else
273  o_timeout = 0/*kInfiniteTimeout*/;
274  break;
275  case eIO_Read:
276  if ( timeout ) {
277  if (&rr_timeout != timeout)
278  rr_timeout = *timeout;
280  } else
281  r_timeout = 0/*kInfiniteTimeout*/;
282  break;
283  case eIO_Write:
284  if ( timeout ) {
285  if (&ww_timeout != timeout)
286  ww_timeout = *timeout;
288  } else
289  w_timeout = 0/*kInfiniteTimeout*/;
290  break;
291  case eIO_ReadWrite:
292  if ( timeout ) {
293  if (&rr_timeout != timeout)
294  rr_timeout = *timeout;
296  if (&ww_timeout != timeout)
297  ww_timeout = *timeout;
299  } else {
300  r_timeout = 0/*kInfiniteTimeout*/;
301  w_timeout = 0/*kInfiniteTimeout*/;
302  }
303  break;
304  case eIO_Close:
305  if ( timeout ) {
306  if (&cc_timeout != timeout)
307  cc_timeout = *timeout;
309  } else
310  c_timeout = 0/*kInfiniteTimeout*/;
311  break;
312  default:
313  return eIO_InvalidArg;
314  }
315  return m_Socket ? SOCK_SetTimeout(m_Socket, event, timeout) : eIO_Success;
316 }
317 
318 
320 {
321  switch (event) {
322  case eIO_Open:
323  return o_timeout;
324  case eIO_Read:
325  return r_timeout;
326  case eIO_Write:
327  return w_timeout;
328  case eIO_ReadWrite:
329  if ( !r_timeout )
330  return w_timeout;
331  if ( !w_timeout )
332  return r_timeout;
333  return ((unsigned long) r_timeout->sec * 1000000 + r_timeout->usec >
334  (unsigned long) w_timeout->sec * 1000000 + w_timeout->usec)
335  ? w_timeout : r_timeout;
336  case eIO_Close:
337  return c_timeout;
338  default:
339  break;
340  }
341  return kDefaultTimeout;
342 }
343 
344 
346  size_t size,
347  size_t* n_read,
348  EIO_ReadMethod how)
349 {
350  if ( m_Socket )
351  return SOCK_Read(m_Socket, buf, size, n_read, how);
352  if ( n_read )
353  *n_read = 0;
354  return eIO_Closed;
355 }
356 
357 
359 {
360  str.clear();
361  if ( !m_Socket )
362  return eIO_Closed;
363  EIO_Status status;
364  char buf[1024];
365  size_t size;
366  do {
367  status = SOCK_ReadLine(m_Socket, buf, sizeof(buf), &size);
368  if ( !size )
369  break;
370  str.append(buf, size);
371  } while (status == eIO_Success && size == sizeof(buf));
372  return status;
373 }
374 
375 
377  size_t size,
378  size_t* n_written,
379  EIO_WriteMethod how)
380 {
381  if ( m_Socket )
382  return SOCK_Write(m_Socket, buf, size, n_written, how);
383  if ( n_written )
384  *n_written = 0;
385  return eIO_Closed;
386 }
387 
388 
389 void CSocket::GetPeerAddress(unsigned int* host,
390  unsigned short* port,
391  ENH_ByteOrder byte_order) const
392 {
393  if ( !m_Socket ) {
394  if ( host )
395  *host = 0;
396  if ( port )
397  *port = 0;
398  } else
399  SOCK_GetPeerAddress(m_Socket, host, port, byte_order);
400 }
401 
402 
404 {
405  char buf[PATH_MAX + 1];
406  if (m_Socket &&
408  return string(buf);
409  }
410  return "";
411 }
412 
413 
414 void CSocket::Reset(SOCK sock, EOwnership if_to_own, ECopyTimeout whence)
415 {
416  if (m_Socket != sock) {
417  if (m_Socket && m_IsOwned != eNoOwnership)
419  m_Socket = sock;
420  }
421  m_IsOwned = if_to_own;
422  if (whence == eCopyTimeoutsFromSOCK) {
423  if ( sock ) {
424  const STimeout* timeout;
425  timeout = SOCK_GetTimeout(sock, eIO_Read);
426  if ( timeout ) {
427  rr_timeout = *timeout;
429  } else
430  r_timeout = 0/*kInfiniteTimeout*/;
431  timeout = SOCK_GetTimeout(sock, eIO_Write);
432  if ( timeout ) {
433  ww_timeout = *timeout;
435  } else
436  w_timeout = 0/*kInfiniteTimeout*/;
437  timeout = SOCK_GetTimeout(sock, eIO_Close);
438  if ( timeout ) {
439  cc_timeout = *timeout;
441  } else
442  c_timeout = 0/*kInfiniteTimeout*/;
443  } else
444  r_timeout = w_timeout = c_timeout = 0/*kInfiniteTimeout*/;
445  } else if ( sock ) {
449  }
450 }
451 
452 
453 
454 /////////////////////////////////////////////////////////////////////////////
455 // CDatagramSocket::
456 //
457 
458 
460  unsigned short port)
461 {
462  char addr[16/*sizeof("255.255.255.255")*/];
463  if (host && SOCK_ntoa(host, addr, sizeof(addr)) != 0)
464  return eIO_Unknown;
465  return m_Socket
466  ? DSOCK_Connect(m_Socket, host ? addr : 0, port)
467  : eIO_Closed;
468 }
469 
470 
471 EIO_Status CDatagramSocket::Connect(const string& hostport)
472 {
473  const char* end;
474  unsigned int x_host;
475  unsigned short port;
476  char host[16/*sizeof("255.255.255.255")*/];
477  if (!m_Socket)
478  return eIO_Closed;
479  if (!(end = SOCK_StringToHostPort(hostport.c_str(), &x_host, &port)) ||
480  *end || (x_host && SOCK_ntoa(x_host, host, sizeof(host)) != 0)) {
481  return eIO_Unknown;
482  }
483  return DSOCK_Connect(m_Socket, x_host ? host : 0, port);
484 }
485 
486 
488  size_t buflen,
489  size_t* msglen,
490  string* sender_host,
491  unsigned short* sender_port,
492  size_t maxmsglen)
493 {
494  if ( !m_Socket ) {
495  if ( msglen )
496  *msglen = 0;
497  if ( sender_host )
498  *sender_host = "";
499  if ( sender_port )
500  *sender_port = 0;
501  return eIO_Closed;
502  }
503 
504  unsigned int addr;
505  EIO_Status status = DSOCK_RecvMsg(m_Socket, buf, buflen, maxmsglen,
506  msglen, &addr, sender_port);
507  if ( sender_host )
508  *sender_host = CSocketAPI::ntoa(addr);
509 
510  return status;
511 }
512 
513 
514 
515 /////////////////////////////////////////////////////////////////////////////
516 // CListeningSocket::
517 //
518 
519 
521 {
522  Close();
523 }
524 
525 
527  const STimeout* timeout,
528  TSOCK_Flags flags) const
529 {
530  if ( !m_Socket ) {
531  sock = 0;
532  return eIO_Closed;
533  }
534 
535  SOCK x_sock;
536  EIO_Status status;
537  status = LSOCK_AcceptEx(m_Socket, timeout, &x_sock, flags);
538  assert(!x_sock ^ !(status != eIO_Success));
539  if (status == eIO_Success) {
540  try {
541  sock = new CSocket;
542  } catch (...) {
543  sock = 0;
544  SOCK_Abort(x_sock);
545  SOCK_Close(x_sock);
546  throw;
547  }
548  sock->Reset(x_sock, eTakeOwnership, eCopyTimeoutsToSOCK);
549  } else
550  sock = 0;
551  return status;
552 }
553 
554 
556  const STimeout* timeout,
557  TSOCK_Flags flags) const
558 {
559  SOCK x_sock;
560  EIO_Status status;
561  if ( !m_Socket ) {
562  x_sock = 0;
563  status = eIO_Closed;
564  } else
565  status = LSOCK_AcceptEx(m_Socket, timeout, &x_sock, flags);
566  assert(!x_sock ^ !(status != eIO_Success));
568  return status;
569 }
570 
571 
573 {
574  if ( !m_Socket )
575  return eIO_Closed;
576 
577  EIO_Status status = m_IsOwned != eNoOwnership
579  m_Socket = 0;
580  return status;
581 }
582 
583 
584 
585 /////////////////////////////////////////////////////////////////////////////
586 // CSocketAPI::
587 //
588 
589 
590 EIO_Status CSocketAPI::Poll(vector<SPoll>& polls,
591  const STimeout* timeout,
592  size_t* n_ready)
593 {
594  static const STimeout kZero = {0, 0};
595  size_t x_n = polls.size();
596  SPOLLABLE_Poll* x_polls = 0;
597  size_t x_ready = 0;
598 
599  if (x_n && !(x_polls = new SPOLLABLE_Poll[x_n]))
600  return eIO_Unknown;
601 
602  for (size_t i = 0; i < x_n; ++i) {
603  CPollable* p = polls[i].m_Pollable;
604  EIO_Event event = polls[i].m_Event;
605  if (p && event) {
606  CSocket* s = dynamic_cast<CSocket*>(p);
607  if (!s) {
608  CListeningSocket* ls = dynamic_cast<CListeningSocket*>(p);
609  if (!ls) {
610  CTrigger* tr = dynamic_cast<CTrigger*>(p);
611  x_polls[i].poll
612  = tr ? POLLABLE_FromTRIGGER(tr->GetTRIGGER()) : 0;
613  } else
614  x_polls[i].poll = POLLABLE_FromLSOCK(ls->GetLSOCK());
615  polls[i].m_REvent = eIO_Open;
616  } else {
617  EIO_Event revent;
618  if (s->GetStatus(eIO_Open) != eIO_Closed) {
619  x_polls[i].poll = POLLABLE_FromSOCK(s->GetSOCK());
620  revent = eIO_Open;
621  } else {
622  x_polls[i].poll = 0;
623  revent = eIO_Close;
624  ++x_ready;
625  }
626  polls[i].m_REvent = revent;
627  }
628  x_polls[i].event = event;
629  } else {
630  x_polls[i].poll = 0;
631  polls[i].m_REvent = eIO_Open;
632  }
633  }
634 
635  size_t xx_ready;
636  EIO_Status status = POLLABLE_Poll(x_n, x_polls,
637  x_ready ? &kZero : timeout, &xx_ready);
638 
639  for (size_t i = 0; i < x_n; ++i) {
640  if (x_polls[i].revent)
641  polls[i].m_REvent = x_polls[i].revent;
642  }
643 
644  if ( n_ready )
645  *n_ready = xx_ready + x_ready;
646 
647  delete[] x_polls;
648  return status;
649 }
650 
651 
652 string CSocketAPI::ntoa(unsigned int host)
653 {
654  char addr[16/*sizeof("255.255.255.255")*/];
655  if (SOCK_ntoa(host, addr, sizeof(addr)) != 0)
656  *addr = 0;
657  return string(addr);
658 }
659 
660 
662 {
663  char hostname[256];
664  if (SOCK_gethostnameEx(hostname, sizeof(hostname), log) != 0)
665  *hostname = 0;
666  return string(hostname);
667 }
668 
669 
670 string CSocketAPI::gethostbyaddr(unsigned int host, ESwitch log)
671 {
672  char hostname[256];
673  if (!SOCK_gethostbyaddrEx(host, hostname, sizeof(hostname), log))
674  *hostname = 0;
675  return string(hostname);
676 }
677 
678 
679 unsigned int CSocketAPI::gethostbyname(const string& host, ESwitch log)
680 {
681  return SOCK_gethostbynameEx(host == kEmptyStr ? 0 : host.c_str(), log);
682 }
683 
684 
685 string CSocketAPI::HostPortToString(unsigned int host,
686  unsigned short port)
687 {
688  char buf[80];
689  size_t len = SOCK_HostPortToString(host, port, buf, sizeof(buf));
690  return string(buf, len);
691 }
692 
693 
695  unsigned int* host,
696  unsigned short* port)
697 {
698  const char* s = str.c_str();
699  const char* e = SOCK_StringToHostPort(s, host, port);
700  return e ? (SIZE_TYPE)(e - s) : NPOS;
701 }
702 
703 
CListeningSocket::
CSocket::
CTrigger::
Definition: ncbi_socket.hpp:88
static uch flags
static const char * str(char *buf, int n)
Definition: stats.c:84
@ eTakeOwnership
An object can take ownership of another.
Definition: ncbi_types.h:136
@ eNoOwnership
No ownership is assumed.
Definition: ncbi_types.h:135
string
Definition: cgiapp.hpp:687
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
EIO_Status Connect(const string &filename, const STimeout *timeout=kDefaultTimeout, TSOCK_Flags flags=fSOCK_LogDefault)
EIO_Status Connect(const string &host, unsigned short port, const STimeout *timeout=kDefaultTimeout, TSOCK_Flags flags=fSOCK_LogDefault)
Connect to "host:port".
const char * SOCK_StringToHostPort(const char *str, unsigned int *host, unsigned short *port)
Read (skipping leading blanks) "[host][:port]" from a string stopping at either EOL or a blank charac...
Definition: ncbi_socket.c:8901
void SOCK_GetPeerAddress(SOCK sock, unsigned int *host, unsigned short *port, ENH_ByteOrder byte_order)
Get host and port of the socket's peer (remote end).
Definition: ncbi_socket.c:7612
char * SOCK_GetPeerAddressStringEx(SOCK sock, char *buf, size_t bufsize, ESOCK_AddressFormat format)
Definition: ncbi_socket.c:7652
static SIZE_TYPE StringToHostPort(const string &str, unsigned int *host, unsigned short *port)
Return position past the end of the parsed portion, NPOS on error.
EIO_Status SOCK_SetTimeout(SOCK sock, EIO_Event event, const STimeout *timeout)
Specify timeout for the connection I/O (see SOCK_[Read|Write|Close]()).
Definition: ncbi_socket.c:7194
static string HostPortToString(unsigned int host, unsigned short port)
See SOCK_HostPortToString()
EIO_Status Accept(CSocket *&sock, const STimeout *timeout=kInfiniteTimeout, TSOCK_Flags flags=fSOCK_LogDefault) const
EIO_Status SOCK_Close(SOCK sock)
Close the SOCK handle, and destroy all relevant internal data.
Definition: ncbi_socket.c:6851
size_t SOCK_HostPortToString(unsigned int host, unsigned short port, char *buf, size_t bufsize)
Print numeric string "host:port" into a buffer provided, not to exceed 'bufsize' bytes (including the...
Definition: ncbi_socket.c:8945
ESOCK_AddressFormat
Get textual representation of the socket's peer.
Definition: ncbi_socket.h:1389
EIO_Status DSOCK_RecvMsg(SOCK sock, void *buf, size_t bufsize, size_t maxmsglen, size_t *msglen, unsigned int *sender_addr, unsigned short *sender_port)
Receive a datagram from a datagram socket.
Definition: ncbi_socket.c:8219
EIO_Event revent
Definition: ncbi_socket.h:1981
SOCK GetSOCK(void) const
Access to the underlying "SOCK".
EIO_Status DSOCK_Connect(SOCK sock, const char *host, unsigned short port)
Associate a datagram socket with a destination address.
Definition: ncbi_socket.c:8073
TRIGGER GetTRIGGER(void) const
Access to the underlying "TRIGGER".
EIO_Status Recv(void *buf, size_t buflen, size_t *msglen=0, string *sender_host=0, unsigned short *sender_port=0, size_t maxmsglen=0)
EIO_Status ReadLine(string &str)
Read a line from socket (up to CR-LF, LF, or null character, discarding any of the EOLs).
EOwnership m_IsOwned
STimeout * c_timeout
eIO_Close
EIO_Status SOCK_Read(SOCK sock, void *buf, size_t size, size_t *n_read, EIO_ReadMethod how)
Read/peek up to "size" bytes from "sock" to a buffer pointed to by "buf".
Definition: ncbi_socket.c:7270
EIO_Status SOCK_Status(SOCK sock, EIO_Event direction)
Return low-level socket I/O status of *last* socket operation.
Definition: ncbi_socket.c:7459
const STimeout * GetTimeout(EIO_Event event) const
Get timeout for I/O in the specified direction.
unsigned int SOCK_gethostbynameEx(const char *hostname, ESwitch log)
Find and return IPv4 address of a named host.
Definition: ncbi_socket.c:8780
EIO_Status POLLABLE_Poll(size_t n, SPOLLABLE_Poll polls[], const STimeout *timeout, size_t *n_ready)
Poll for I/O readiness.
Definition: ncbi_socket.c:8597
static string gethostname(ESwitch log=eOff)
Return empty string on error.
STimeout oo_timeout
storage for o_timeout
static EIO_Status Poll(vector< SPoll > &polls, const STimeout *timeout, size_t *n_ready=0)
Poll a vector of CPollable objects for I/O readiness.
ECopyTimeout
Definition: ncbi_socket.hpp:51
POLLABLE POLLABLE_FromSOCK(SOCK)
Conversion utilities from handles to POLLABLEs, and back.
Definition: ncbi_socket.c:8627
static string ntoa(unsigned int host)
BSD-like API. NB: when int, "host" must be in network byte order.
EIO_Status Connect(const string &host, unsigned short port)
ENH_ByteOrder
Network and host byte order enumeration type.
Definition: ncbi_socket.h:192
static unsigned int gethostbyname(const string &host, ESwitch log=eOff)
Return 0 on error.
EIO_Status SetTimeout(EIO_Event event, const STimeout *timeout)
Set timeout for I/O in the specified direction.
EIO_Event event
Definition: ncbi_socket.h:1980
void Reset(SOCK sock, EOwnership if_to_own, ECopyTimeout whence)
Close the current underlying "SOCK" (if any, and if owned), and from now on use "sock" as the underly...
EIO_Status SOCK_CreateEx(const char *host, unsigned short port, const STimeout *timeout, SOCK *sock, const void *data, size_t size, TSOCK_Flags flags)
[CLIENT-side] Connect client to another(server-side, listening) socket (socket() + connect() [+ selec...
Definition: ncbi_socket.c:6648
EIO_Status SOCK_Abort(SOCK sock)
If there is outstanding connection or output data pending, cancel it.
Definition: ncbi_socket.c:7557
EIO_Status LSOCK_AcceptEx(LSOCK lsock, const STimeout *timeout, SOCK *sock, TSOCK_Flags flags)
[SERVER-side] Accept connection from a client.
Definition: ncbi_socket.c:6546
int SOCK_ntoa(unsigned int addr, char *buf, size_t bufsize)
Convert IP address to a string in dotted notation.
Definition: ncbi_socket.c:8661
EIO_Status Close(void)
STimeout cc_timeout
storage for c_timeout
TRIGGER m_Trigger
STimeout * r_timeout
eIO_Read
EIO_Status TRIGGER_Close(TRIGGER trigger)
Close an event trigger.
Definition: ncbi_socket.c:6341
EIO_Status SOCK_ReadLine(SOCK sock, char *line, size_t size, size_t *n_read)
Read a line from SOCK.
Definition: ncbi_socket.c:7329
EIO_Status GetStatus(EIO_Event direction) const
Return status of *last* I/O operation without making any actual I/O.
EIO_Status SOCK_Write(SOCK sock, const void *data, size_t size, size_t *n_written, EIO_WriteMethod how)
Write "size" bytes of "data" to "sock".
Definition: ncbi_socket.c:7486
EIO_Status Reconnect(const STimeout *timeout=kDefaultTimeout)
Reconnect to the same address.
EIO_Status SOCK_Reconnect(SOCK sock, const char *host, unsigned short port, const STimeout *timeout)
[CLIENT-side] Close the socket referred to by "sock" and then connect it to another "host:port"; fail...
Definition: ncbi_socket.c:6739
void GetPeerAddress(unsigned int *host, unsigned short *port, ENH_ByteOrder byte_order) const
Get peer address.
int SOCK_gethostnameEx(char *name, size_t namelen, ESwitch log)
Get the local host name.
Definition: ncbi_socket.c:8760
virtual ~CSocket(void)
Call Close(), then self-destruct.
CSocket(void)
CSocket::
static string gethostbyaddr(unsigned int host, ESwitch log=eOff)
Return empty string on error.
const char * SOCK_gethostbyaddrEx(unsigned int addr, char *name, size_t namelen, ESwitch log)
Take IPv4 host address (in network byte order) or 0 for current host, and fill out the provided buffe...
Definition: ncbi_socket.c:8796
STimeout * o_timeout
eIO_Open
EOwnership m_IsOwned
Timeouts.
POLLABLE POLLABLE_FromLSOCK(LSOCK)
Definition: ncbi_socket.c:8620
LSOCK GetLSOCK(void) const
Access to the underlying "LSOCK".
POLLABLE POLLABLE_FromTRIGGER(TRIGGER)
Definition: ncbi_socket.c:8613
STimeout ww_timeout
storage for w_timeout
EIO_Status LSOCK_Close(LSOCK lsock)
[SERVER-side] Close the listening socket, destroy relevant internal data.
Definition: ncbi_socket.c:6555
const STimeout * SOCK_GetTimeout(SOCK sock, EIO_Event event)
Get the connection's i/o timeout (or NULL, if the timeout is infinite).
Definition: ncbi_socket.c:7230
virtual ~CTrigger()
virtual ~CListeningSocket(void)
Call Close(), then self-destruct.
EIO_Status SOCK_CreateUNIX(const char *path, const STimeout *timeout, SOCK *sock, const void *data, size_t size, TSOCK_Flags flags)
Definition: ncbi_socket.c:6673
STimeout rr_timeout
storage for r_timeout
STimeout * w_timeout
eIO_Write
EIO_Status Write(const void *buf, size_t size, size_t *n_written=0, EIO_WriteMethod how=eIO_WritePersist)
Write to socket.
SOCK m_Socket
unsigned int TSOCK_Flags
bitwise "OR" of ESOCK_Flags
Definition: ncbi_socket.h:503
EIO_Status Read(void *buf, size_t size, size_t *n_read=0, EIO_ReadMethod how=eIO_ReadPlain)
Read from socket.
@ eCopyTimeoutsToSOCK
Definition: ncbi_socket.hpp:53
@ eCopyTimeoutsFromSOCK
Definition: ncbi_socket.hpp:52
NCBI_NS_STD::string::size_type SIZE_TYPE
Definition: ncbistr.hpp:132
#define kEmptyStr
Definition: ncbistr.hpp:123
#define NPOS
Definition: ncbistr.hpp:133
enum ENcbiSwitch ESwitch
Aux.
EIO_Status
I/O status.
Definition: ncbi_core.h:132
unsigned int usec
microseconds (modulo 1,000,000)
Definition: ncbi_types.h:78
EIO_ReadMethod
I/O read method.
Definition: ncbi_core.h:88
enum ENcbiOwnership EOwnership
Ownership relations between objects.
EIO_WriteMethod
I/O write method.
Definition: ncbi_core.h:99
unsigned int sec
seconds
Definition: ncbi_types.h:77
EIO_Event
I/O event (or direction).
Definition: ncbi_core.h:118
#define kDefaultTimeout
Definition: ncbi_types.h:81
@ eIO_Success
everything is fine, no error occurred
Definition: ncbi_core.h:133
@ eIO_Unknown
unknown I/O error (likely fatal but can retry)
Definition: ncbi_core.h:139
@ eIO_InvalidArg
bad argument / parameter value(s) supplied
Definition: ncbi_core.h:137
@ eIO_Write
write
Definition: ncbi_core.h:121
@ eIO_ReadWrite
eIO_Read | eIO_Write (also, eCONN_OnFlush)
Definition: ncbi_core.h:122
@ eIO_Open
also serves as no-event indicator in SOCK_Poll
Definition: ncbi_core.h:119
@ eIO_Close
also serves as an error indicator in SOCK_Poll
Definition: ncbi_core.h:123
@ eIO_Read
read
Definition: ncbi_core.h:120
char * buf
int i
int len
const struct ncbi::grid::netcache::search::fields::SIZE size
#define PATH_MAX
static Format format
Definition: njn_ioutil.cpp:53
#define assert(x)
Definition: srv_diag.hpp:58
Timeout structure.
Definition: ncbi_types.h:76
Modified on Fri May 24 14:57:26 2024 by modify_doxy.py rev. 669887