rllib  1
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Private Attributes | List of all members
rlSocket Class Reference

#include <rlsocket.h>

Inheritance diagram for rlSocket:
Inheritance graph
[legend]

Public Types

enum  SocketEnum {
  SOCKET_ERR = -1, SETSOCKOPT_ERR = -2, LISTEN_ERR = -3, ACCEPT_ERR = -4,
  INET_ADDR_ERR = -5, CONNECT_ERR = -6, PORT_ERR = -7
}
 

Public Member Functions

 rlSocket (const char *adr, int port, int active)
 
 rlSocket (int socket)
 
virtual ~rlSocket ()
 
void setAdr (const char *adr)
 
void setPort (int port)
 
int getPort ()
 
void setActive (int active)
 
int read (void *buf, int len, int timeout=0)
 
int readStr (char *buf, int len, int timeout=0)
 
int readHttpHeader (rlString *header, int timeout=0)
 
int write (const void *buf, int len)
 
int printf (const char *format,...)
 
int connect ()
 
int disconnect ()
 
int select (int timeout=0)
 
int isConnected ()
 
int setIPVersion (int version)
 
int getIPVersion ()
 
int sendProcessViewBrowserButtonEvent (int id)
 
int rlGetsockopt (int level, int optname)
 
int rlSetsockopt (int level, int optname)
 
int readHttpContentLength (int timeout)
 

Static Public Member Functions

static int rlGetsockopt (int sockfd, int level, int optname, void *optval, int *optlen)
 
static int rlSetsockopt (int sockfd, int level, int optname, const void *optval, int optlen)
 

Public Attributes

int s
 
unsigned char sockaddr [16+48]
 

Private Attributes

char adr [132]
 
int port
 
int active
 
int os
 
int first
 
int prefer_ipv6
 
int rl_ipversion
 

Detailed Description

class for encapsulating TCP/IP socket calls

Definition at line 46 of file rlsocket.h.

Member Enumeration Documentation

◆ SocketEnum

Enumerator
SOCKET_ERR 
SETSOCKOPT_ERR 
LISTEN_ERR 
ACCEPT_ERR 
INET_ADDR_ERR 
CONNECT_ERR 
PORT_ERR 

Definition at line 50 of file rlsocket.h.

Constructor & Destructor Documentation

◆ rlSocket() [1/2]

rlSocket::rlSocket ( const char *  adr,
int  port,
int  active 
)
    construct a new rlSocket but do not connect
    adr  = hostname | dotted address
    port = port number of socket
    active = 0 wait for connections with accept()
    active = 1 open the connection with connect()
    active = 2 neither accept() nor connect()

Definition at line 130 of file rlsocket.cpp.

131 {
132  rlwsa(); // init sockets on windows
133  setAdr(a);
134  port = p;
135  active = act;
136  s = -1;
137  os = -1;
138  first = 1;
139  rl_ipversion = 4;
140  memset(sockaddr,0,sizeof(sockaddr));
141 }
int port
Definition: rlsocket.h:262
int first
Definition: rlsocket.h:265
unsigned char sockaddr[16+48]
Definition: rlsocket.h:258
int os
Definition: rlsocket.h:264
int s
Definition: rlsocket.h:197
int active
Definition: rlsocket.h:263
void setAdr(const char *adr)
Definition: rlsocket.cpp:168
int rlwsa()
Definition: rlsocket.cpp:68
int rl_ipversion
Definition: rlsocket.h:267

◆ rlSocket() [2/2]

rlSocket::rlSocket ( int  socket)
    construct a new rlSocket
    use connection on open socket

Definition at line 143 of file rlsocket.cpp.

144 {
145  adr[0] = '\0';
146  port = -1;
147  active = 0;
148  s = socket;
149  os = -1;
150  first = 0;
151  rl_ipversion = 4;
152  memset(sockaddr,0,sizeof(sockaddr));
153 }
int port
Definition: rlsocket.h:262
int first
Definition: rlsocket.h:265
unsigned char sockaddr[16+48]
Definition: rlsocket.h:258
char adr[132]
Definition: rlsocket.h:261
int os
Definition: rlsocket.h:264
int s
Definition: rlsocket.h:197
int active
Definition: rlsocket.h:263
int rl_ipversion
Definition: rlsocket.h:267

◆ ~rlSocket()

rlSocket::~rlSocket ( )
virtual
    destruct the socket
    attention if active = 0 the socket will still be bound to port

Definition at line 155 of file rlsocket.cpp.

156 {
157  disconnect();
158  if(os != -1 && active == 0)
159  {
160 #ifdef RLWIN32
161  closesocket(os);
162 #else
163  close(os);
164 #endif
165  }
166 }
int os
Definition: rlsocket.h:264
int disconnect()
Definition: rlsocket.cpp:545
int active
Definition: rlsocket.h:263

Member Function Documentation

◆ connect()

int rlSocket::connect ( )
    connect
    return >= 0 socket used
    return < 0  error (see: enum SocketEnum)

Definition at line 321 of file rlsocket.cpp.

322 {
323  int option;
324  int ret;
325 #ifdef __VMS
326  size_t socklen = sizeof(struct sockaddr);
327 #else
328  socklen_t socklen = sizeof(struct sockaddr);
329 #endif
330  struct sockaddr_in localAddr;
331  struct sockaddr_in remoteAddr;
332  struct hostent *host;
333  struct in_addr RemoteIpAddress;
334 #ifdef AF_INET6_IS_AVAILABLE
335  struct addrinfo hints0, hints1;
336  struct addrinfo *res, *ressave;
337  int n;
338  char portstr[32];
339 #endif
340 
341  if(port <= 0) return PORT_ERR;
342  if(port >= 256*256) return PORT_ERR;
343  if(active == 0)
344  { // accept calls
345  s = -1;
346  if(rl_ipversion == 4)
347  {
348  if(first == 1)
349  {
350  // create a socket
351  os = socket(AF_INET,SOCK_STREAM,0);
352  if(os == -1) return SOCKET_ERR;
353  // set socket options
354 #ifdef __VMS
355  option = 1;
356  if(setsockopt(os,SOL_SOCKET,SO_KEEPALIVE,&option,sizeof(option)) < 0)
357  {
358  return SETSOCKOPT_ERR;
359  }
360 #endif
361  option = 1;
362 #ifdef RLWIN32
363  setsockopt(os,SOL_SOCKET,SO_REUSEADDR,(const char *) &option,sizeof(option));
364 #else
365  setsockopt(os,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option));
366 #endif
367  // Bind our server to the agreed upon port number.
368  memset(&localAddr,0,sizeof(localAddr));
369  localAddr.sin_port = htons((short) port);
370  localAddr.sin_family = AF_INET;
371 bind:
372  ret = bind(os, (struct sockaddr *) &localAddr, sizeof(localAddr));
373  if(ret == -1)
374  {
375  ::printf("warning: could not bind to port=%d\n", port);
376  rlwthread_sleep(1000);
377  goto bind;
378  }
379  // Prepare to accept client connections. Allow up to 5 pending
380  // connections.
381  ret = listen(os, 5);
382  if(ret == -1) return LISTEN_ERR;
383  }
384  first = 0;
385 
386  // accept connections
387  if(os < 0) return ACCEPT_ERR;
388  s = accept(os, (struct sockaddr *) &sockaddr[0], &socklen);
389  if(s == -1) return ACCEPT_ERR;
390  }
391  else if(rl_ipversion == 6)
392  {
393 #ifdef AF_INET6_IS_AVAILABLE
394  if(first == 1)
395  {
396  memset(&hints0,0,sizeof(hints0));
397  hints0.ai_flags = AI_PASSIVE;
398  //hints0.ai_family = AF_UNSPEC;
399  hints0.ai_family = AF_INET6;
400  hints0.ai_socktype = SOCK_STREAM;
401  sprintf(portstr,"%d",port);
402  //::printf("server getaddrinfo(%s,%s)\n", adr, portstr);
403  n = getaddrinfo(adr, portstr, &hints0, &res);
404  if(n != 0)
405  {
406 #ifndef RLWIN32
407  ::printf("rlSocket:tcp_listen error for %s port=%s : %s\n", adr, portstr, gai_strerror(n));
408 #endif
409  return -1;
410  }
411  //::printf("done\n");
412  ressave = res;
413 bindv6:
414  do
415  {
416  os = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
417  if(os < 0) continue; // error, try next one
418 #ifdef __VMS
419  option = 1;
420  if(setsockopt(os,SOL_SOCKET,SO_KEEPALIVE,&option,sizeof(option)) < 0)
421  {
422  return SETSOCKOPT_ERR;
423  }
424 #endif
425  option = 1;
426 #ifdef RLWIN32
427  setsockopt(os,SOL_SOCKET,SO_REUSEADDR,(const char *) &option,sizeof(option));
428 #else
429  setsockopt(os,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option));
430 #endif
431  if(bind(os, res->ai_addr, res->ai_addrlen) == 0) break; // success
432  s = os;
433  disconnect(); // bind error, close and try next one
434  }
435  while((res = res->ai_next) != NULL);
436  if(res == NULL) // errno from final socket() or bind()
437  {
438  ::printf("warning: could not bind to port=%d\n", port);
439  rlwthread_sleep(1000);
440  goto bindv6;
441  }
442  // Prepare to accept client connections. Allow up to 5 pending
443  // connections
444  ret = listen(os, 5);
445  freeaddrinfo(ressave);
446  if(ret == -1) return LISTEN_ERR;
447  }
448  first = 0;
449  // accept connections
450  if(os < 0) return ACCEPT_ERR;
451  s = accept(os, (struct sockaddr *) &sockaddr[0], &socklen);
452  if(s == -1) return ACCEPT_ERR;
453 #else
454  ::printf("rlSocket:ERROR IPV6 not available on this platform\n");
455 #endif
456  }
457  else
458  {
459  ::printf("rlSocket:ERROR: rl_ipversion=%d not known\n", rl_ipversion);
460  }
461  } // end active == 0
462  else if(active == 1)
463  {
464  disconnect();
465  //::printf("debug: adr=%s port=%d\n",adr,port);
466  s = -1;
467  if(rl_ipversion == 4)
468  {
469  os = socket(AF_INET,SOCK_STREAM,0);
470  if(os == -1) return SOCKET_ERR;
471  s = os;
472 
473  //::printf("debug: gethostbyname\n");
474  // fill destblk structure
475  host = gethostbyname(adr);
476  if(host == NULL)
477  {
478  // See if the host is specified in "dot address" form
479  RemoteIpAddress.s_addr = inet_addr(adr);
480  if(RemoteIpAddress.s_addr == INADDR_NONE)
481  {
482  // mur was here: without disconnect the sockets will be created over and over
483  disconnect();
484  s = -1;
485  return INET_ADDR_ERR; // -1
486  }
487  }
488  else
489  {
490  memcpy(&RemoteIpAddress,host->h_addr,host->h_length);
491  }
492 
493  memset(&remoteAddr,0,sizeof(remoteAddr));
494  remoteAddr.sin_family = AF_INET;
495  remoteAddr.sin_port = htons((short) port);
496  remoteAddr.sin_addr = RemoteIpAddress;
497 
498  //::printf("debug: connect\n");
499  ret = ::connect(s, (struct sockaddr *) &remoteAddr, sizeof(remoteAddr));
500  //::printf("debug: connect ret=%d\n",ret);
501  if(ret == -1)
502  {
503  disconnect(); // close s = os
504  return CONNECT_ERR;
505  }
506  }
507  else if(rl_ipversion == 6)
508  {
509 #ifdef AF_INET6_IS_AVAILABLE
510  sprintf(portstr,"%d",port);
511  memset(&hints1, 0, sizeof(hints1));
512  hints1.ai_family = AF_UNSPEC;
513  hints1.ai_socktype = SOCK_STREAM;
514  n = getaddrinfo(adr, portstr, &hints1, &res);
515  if(n != 0)
516  {
517 #ifndef RLWIN32
518  ::printf("rlSocket:tcp_connect error for %s port=%s : %s\n", adr, portstr, gai_strerror(n));
519 #endif
520  return -1;
521  }
522  ressave = res;
523  do
524  {
525  s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
526  if(s < 0) continue; // ignore this one
527  if(::connect(s, res->ai_addr, res->ai_addrlen) == 0) break; // success
528  disconnect(); // ignore this one
529  }
530  while((res = res->ai_next) != NULL);
531  if(res == NULL) ::printf("rlSocket:tcp_connect error for %s port=%s\n", adr, portstr);
532  freeaddrinfo(ressave);
533 #else
534  ::printf("rlSocket:ERROR IPV6 not available on this platform\n");
535 #endif
536  }
537  else
538  {
539  ::printf("rlSocket:ERROR: rl_ipversion=%d not known\n", rl_ipversion);
540  }
541  }
542  return s;
543 }
int port
Definition: rlsocket.h:262
int first
Definition: rlsocket.h:265
int WSAAPI getaddrinfo(const char *, const char *, const struct addrinfo *, struct addrinfo **)
unsigned char sockaddr[16+48]
Definition: rlsocket.h:258
char adr[132]
Definition: rlsocket.h:261
int os
Definition: rlsocket.h:264
void WSAAPI freeaddrinfo(struct addrinfo *)
int connect()
Definition: rlsocket.cpp:321
int s
Definition: rlsocket.h:197
int disconnect()
Definition: rlsocket.cpp:545
int active
Definition: rlsocket.h:263
int rlwthread_sleep(long msec)
Definition: rlwthread.cpp:366
int printf(const char *format,...)
Definition: rlsocket.cpp:586
int rl_ipversion
Definition: rlsocket.h:267

◆ disconnect()

int rlSocket::disconnect ( )
    disconnect
    return = 0

Definition at line 545 of file rlsocket.cpp.

546 {
547  if(s != -1)
548  {
549 #ifdef RLWIN32
550  closesocket(s);
551 #else
552  close(s);
553 #endif
554  }
555  s = -1;
556  return 0;
557 }
int s
Definition: rlsocket.h:197

◆ getIPVersion()

int rlSocket::getIPVersion ( )
    return == 4 IPV4
    return == 6 IPV6

Definition at line 611 of file rlsocket.cpp.

612 {
613  return rl_ipversion;
614 }
int rl_ipversion
Definition: rlsocket.h:267

◆ getPort()

int rlSocket::getPort ( )
    get port

Definition at line 181 of file rlsocket.cpp.

182 {
183  return port;
184 }
int port
Definition: rlsocket.h:262

◆ isConnected()

int rlSocket::isConnected ( )
    return == 1 socket is connected
    return == 0 socket is not connected

Definition at line 559 of file rlsocket.cpp.

560 {
561  if(s == -1) return 0;
562  return 1;
563 }
int s
Definition: rlsocket.h:197

◆ printf()

int rlSocket::printf ( const char *  format,
  ... 
)
    similar to printf
    return > 0 length of data written
    return < 0 error

Definition at line 586 of file rlsocket.cpp.

587 {
588  int ret;
589  char message[rl_PRINTF_LENGTH]; // should be big enough
590 
591  va_list ap;
592  va_start(ap,format);
593  ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
594  va_end(ap);
595  if(ret < 0) return ret;
596  return write(message,strlen(message));
597 }
int write(const void *buf, int len)
Definition: rlsocket.cpp:292
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
int rlvsnprintf(char *text, int len, const char *format, va_list ap)
Definition: rlcutil.cpp:197

◆ read()

int rlSocket::read ( void *  buf,
int  len,
int  timeout = 0 
)
    read a block of data
    len = length of data to be read
    timeout = 0 wait indefinite
    timeout > 0 wait at maximum for timeout milliseconds
    return > 0 length of message read
    return == 0 timeout
    return < 0 error

Definition at line 191 of file rlsocket.cpp.

192 {
193  int i,ret;
194  char *cbuf;
195 
196  if(s == -1) return -1;
197  if(select(timeout) == 0) return 0; // timeout
198 
199  cbuf = (char *) buf;
200  i = 0;
201  while(i < len)
202  {
203  ret = recv(s,&cbuf[i],len-i,0);
204  if(ret <= 0)
205  {
206  disconnect();
207  return -1;
208  }
209 
210  //::printf("debug: read len=%d buf=", len);
211  //for(int j=0; j<len; j++) ::printf(" %d", cbuf[j]);
212  //::printf("\n");
213 
214  i += ret;
215  if(i < len)
216  {
217  if(select(timeout) == 0) return 0; // timeout
218  }
219  }
220 
221  return i;
222 }
int select(int timeout=0)
Definition: rlsocket.cpp:565
int s
Definition: rlsocket.h:197
int disconnect()
Definition: rlsocket.cpp:545

◆ readHttpContentLength()

int rlSocket::readHttpContentLength ( int  timeout)
    read the response to a http get request until Content-Length is received
    Tip: char line[256];

Definition at line 655 of file rlsocket.cpp.

656 {
657  char line[256];
658  while(1)
659  {
660  if(readStr(line, (int)sizeof(line) - 1, timeout) < 1) return -1;
661  if(strncmp(line,"Content-Length:",15) == 0) break;
662  }
663  int len = 0;
664  sscanf(line,"Content-Length: %d", &len);
665  if(readStr(line, (int)sizeof(line) - 1, timeout) < 1) return -1; // read CR LF
666  return len;
667 }
int readStr(char *buf, int len, int timeout=0)
Definition: rlsocket.cpp:224

◆ readHttpHeader()

int rlSocket::readHttpHeader ( rlString header,
int  timeout = 0 
)
    read a http header and return Content-Length
    return < 0 error

Definition at line 266 of file rlsocket.cpp.

267 {
268  char line[4096];
269  int contentLength = 0;
270 
271  *header = "";
272  while(1)
273  {
274  int ret = readStr(line, (int) sizeof(line)-1, timeout);
275  if(ret < 0)
276  {
277  printf("ERROR in rlSocket::readHttpHeader() ret=%d\n", ret);
278  return -1;
279  }
280  *header += line;
281  if(strstr(line, "Content-Length:") != NULL)
282  {
283  sscanf(line,"Content-Length: %d", &contentLength);
284  }
285  if(strlen(line) <= 2)
286  {
287  return contentLength;
288  }
289  }
290 }
int readStr(char *buf, int len, int timeout=0)
Definition: rlsocket.cpp:224
int printf(const char *format,...)
Definition: rlsocket.cpp:586

◆ readStr()

int rlSocket::readStr ( char *  buf,
int  len,
int  timeout = 0 
)
    read a '
' terminated string len = max length of data to be read timeout = 0 wait indefinite timeout > 0 wait at maximum for timeout milliseconds return > 0 length of message read return == 0 timeout return < 0 error

Definition at line 224 of file rlsocket.cpp.

225 {
226  int ret,i;
227 
228  if(s == -1) return -1;
229  if(select(timeout) == 0) return 0; // timeout
230 
231  // ::printf("debug: readStr len=%d", len);
232 
233  i = 0;
234  while(1)
235  {
236 #ifdef RLWIN32
237 tryagain:
238 #endif
239  ret = recv(s,&buf[i],1,0);
240  if(ret <= 0)
241  {
242 #ifdef RLWIN32
243  if(WSAEWOULDBLOCK == WSAGetLastError()) goto tryagain;
244 #endif
245  disconnect();
246  buf[i] = '\0';
247  return -1;
248  }
249 
250  //::printf(" %d\n", buf[i]);
251 
252  if(buf[i] == '\n')
253  {
254  buf[i+1] = '\0';
255  return i+1;
256  }
257  if(i >= len-1)
258  {
259  buf[i+1] = '\0';
260  return i+1;
261  }
262  i++;
263  }
264 }
int select(int timeout=0)
Definition: rlsocket.cpp:565
int s
Definition: rlsocket.h:197
int disconnect()
Definition: rlsocket.cpp:545

◆ rlGetsockopt() [1/2]

int rlSocket::rlGetsockopt ( int  sockfd,
int  level,
int  optname,
void *  optval,
int *  optlen 
)
static
    portable version of getsockopt

Definition at line 616 of file rlsocket.cpp.

617 {
618 #ifdef RLWIN32
619  return getsockopt(sockfd, level, optname, (char *) optval, optlen);
620 #elif defined(__VMS)
621  size_t len = *optlen;
622  int ret = getsockopt(sockfd, level, optname, optval, &len);
623  *optlen = len;
624  return ret;
625 #else
626  socklen_t len = *optlen;
627  int ret = getsockopt(sockfd, level, optname, optval, &len);
628  *optlen = len;
629  return ret;
630 #endif
631 }

◆ rlGetsockopt() [2/2]

int rlSocket::rlGetsockopt ( int  level,
int  optname 
)
    get an option from this->s

Definition at line 642 of file rlsocket.cpp.

643 {
644  int option = 1;
645  int len = sizeof(option);
646  return rlGetsockopt(s,level,optname,&option,&len);
647 }
int s
Definition: rlsocket.h:197
static int rlGetsockopt(int sockfd, int level, int optname, void *optval, int *optlen)
Definition: rlsocket.cpp:616

◆ rlSetsockopt() [1/2]

int rlSocket::rlSetsockopt ( int  sockfd,
int  level,
int  optname,
const void *  optval,
int  optlen 
)
static
    portable version of setsockopt

Definition at line 633 of file rlsocket.cpp.

634 {
635 #ifdef RLWIN32
636  return setsockopt(sockfd, level, optname, (const char *) optval, optlen);
637 #else
638  return setsockopt(sockfd, level, optname, optval, optlen);
639 #endif
640 }

◆ rlSetsockopt() [2/2]

int rlSocket::rlSetsockopt ( int  level,
int  optname 
)
    set an option on this->s

Definition at line 649 of file rlsocket.cpp.

650 {
651  int option = 1;
652  return rlSetsockopt(s,level,optname,&option,sizeof(option));
653 }
static int rlSetsockopt(int sockfd, int level, int optname, const void *optval, int optlen)
Definition: rlsocket.cpp:633
int s
Definition: rlsocket.h:197

◆ select()

int rlSocket::select ( int  timeout = 0)
    wait for data arriving on socket
    timeout > 0 timeout in milliseconds
    timeout == 0 indefinite timeout
    return = 1 DATA_AVAILABLE
    return = 0 TIMEOUT

Definition at line 565 of file rlsocket.cpp.

566 {
567  struct timeval timout;
568  fd_set wset,rset,eset;
569  int ret,maxfdp1;
570 
571  if(timeout == 0) return 1;
572  /* setup sockets to read */
573  maxfdp1 = s+1;
574  FD_ZERO(&rset);
575  FD_SET (s,&rset);
576  FD_ZERO(&wset);
577  FD_ZERO(&eset);
578  timout.tv_sec = timeout / 1000;
579  timout.tv_usec = (timeout % 1000) * 1000;
580 
581  ret = ::select(maxfdp1,&rset,&wset,&eset,&timout);
582  if(ret == 0) return 0; /* timeout */
583  return 1;
584 }
int select(int timeout=0)
Definition: rlsocket.cpp:565
int s
Definition: rlsocket.h:197

◆ sendProcessViewBrowserButtonEvent()

int rlSocket::sendProcessViewBrowserButtonEvent ( int  id)
    This method is intendet for data providers implemented as ProcessViewServer

Definition at line 599 of file rlsocket.cpp.

600 {
601  return printf("QPushButton(%d)\n",id);
602 }
int printf(const char *format,...)
Definition: rlsocket.cpp:586

◆ setActive()

void rlSocket::setActive ( int  active)
    set port active = 0|1

Definition at line 186 of file rlsocket.cpp.

187 {
188  active = act;
189 }
int active
Definition: rlsocket.h:263

◆ setAdr()

void rlSocket::setAdr ( const char *  adr)
    set adr to a different adr than in the constructor

Definition at line 168 of file rlsocket.cpp.

169 {
170  adr[0] = '\0';
171  if(a == NULL) return;
172  if((strlen(a)+1) > sizeof(adr)) return;
173  strcpy(adr,a);
174 }
char adr[132]
Definition: rlsocket.h:261

◆ setIPVersion()

int rlSocket::setIPVersion ( int  version)
    default: prefer IPV4
    if(ip==6) prefer IPV6
    else      prefer IPV4

Definition at line 604 of file rlsocket.cpp.

605 {
606  if(version == 6) rl_ipversion = 6;
607  else rl_ipversion = 4;
608  return rl_ipversion;
609 }
int rl_ipversion
Definition: rlsocket.h:267

◆ setPort()

void rlSocket::setPort ( int  port)
    set port to a different port than in the constructor

Definition at line 176 of file rlsocket.cpp.

177 {
178  port = p;
179 }
int port
Definition: rlsocket.h:262

◆ write()

int rlSocket::write ( const void *  buf,
int  len 
)
    write a block of data
    return > 0 length of data written
    return < 0 error

Definition at line 292 of file rlsocket.cpp.

293 {
294  int ret,bytes_left,first_byte;
295  const char *cbuf;
296 
297  if(s == -1) return -1;
298  cbuf = (char *) buf;
299  bytes_left = len;
300  first_byte = 0;
301 
302  //::printf("debug: write len=%d buf=", len);
303  //for(int i=0; i<len; i++) ::printf(" %d", cbuf[i]);
304  //::printf("\n");
305 
306  while(bytes_left > 0)
307  {
308  ret = send(s,&cbuf[first_byte],bytes_left,MSG_NOSIGNAL);
309  if(ret <= 0)
310  {
311  disconnect();
312  return -1;
313  }
314  bytes_left -= ret;
315  first_byte += ret;
316  }
317 
318  return first_byte;
319 }
#define MSG_NOSIGNAL
Definition: rlsocket.cpp:53
int s
Definition: rlsocket.h:197
int disconnect()
Definition: rlsocket.cpp:545

Member Data Documentation

◆ active

int rlSocket::active
private

Definition at line 263 of file rlsocket.h.

◆ adr

char rlSocket::adr[132]
private

Definition at line 261 of file rlsocket.h.

◆ first

int rlSocket::first
private

Definition at line 265 of file rlsocket.h.

◆ os

int rlSocket::os
private

Definition at line 264 of file rlsocket.h.

◆ port

int rlSocket::port
private

Definition at line 262 of file rlsocket.h.

◆ prefer_ipv6

int rlSocket::prefer_ipv6
private

Definition at line 266 of file rlsocket.h.

◆ rl_ipversion

int rlSocket::rl_ipversion
private

Definition at line 267 of file rlsocket.h.

◆ s

int rlSocket::s
    this is the real socket used for communication
    s >= 0  connected
    s == -1 disconnected

Definition at line 197 of file rlsocket.h.

◆ sockaddr

unsigned char rlSocket::sockaddr[16+48]
    this array can be casted to (struct sockaddr *) &sockaddr[0];
    in case of active==0 it will store sockaddr of the last client
    (48 bytes spare)
    struct sockaddr {
      unsigned short    sa_family;    // address family, AF_xxx
      char              sa_data[14];  // 14 bytes of protocol address
    };
    // IPv4 AF_INET sockets:
    struct sockaddr_in {
      short            sin_family;   // e.g. AF_INET, AF_INET6
      unsigned short   sin_port;     // e.g. htons(3490)
      struct in_addr   sin_addr;     // see struct in_addr, below
      char             sin_zero[8];  // zero this if you want to
    };
    struct in_addr {
      unsigned long s_addr;          // load with inet_pton()
    };
    // IPv6 AF_INET6 sockets:
    struct sockaddr_in6 {
      u_int16_t       sin6_family;   // address family, AF_INET6
      u_int16_t       sin6_port;     // port number, Network Byte Order
      u_int32_t       sin6_flowinfo; // IPv6 flow information
      struct in6_addr sin6_addr;     // IPv6 address
      u_int32_t       sin6_scope_id; // Scope ID
    };
    struct in6_addr {
        unsigned char   s6_addr[16];   // load with inet_pton()
    };

Definition at line 258 of file rlsocket.h.


The documentation for this class was generated from the following files: