rllib  1
Public Member Functions | Public Attributes | Private Attributes | List of all members
rlUdpSocket Class Reference

#include <rludpsocket.h>

Inheritance diagram for rlUdpSocket:
Inheritance graph
[legend]

Public Member Functions

 rlUdpSocket (int debug=0)
 
virtual ~rlUdpSocket ()
 
int setSockopt (int opt)
 
int setSockopt (int level, int optname, void *optval, int optlen)
 
int bind (int port)
 
int select (int timeout)
 
int recvfrom (void *buf, int maxlen, rlIpAdr *source, int timeout=-1)
 
int sendto (const void *buf, int len, rlIpAdr *dest)
 
int printf (rlIpAdr *dest, const char *format,...)
 

Public Attributes

int debug
 
int readflag
 
int writeflag
 

Private Attributes

struct sockaddr_in address
 
int s
 

Detailed Description

class for encapsulating UDP socket calls

Definition at line 53 of file rludpsocket.h.

Constructor & Destructor Documentation

◆ rlUdpSocket()

rlUdpSocket::rlUdpSocket ( int  debug = 0)

Definition at line 72 of file rludpsocket.cpp.

73 {
74  debug = _debug;
75  if(debug) ::printf("rlUdpSocket() constructor\n");
76  rlwsa(); // init sockets on windows
77  readflag = writeflag = 0;
78  s = socket(AF_INET,SOCK_DGRAM,0);
79  if(s < 0)
80  {
81  s = -1;
82  ::printf("rlUdpSocket::rlUdpSocket could not get socket\n");
83  }
84 }
int printf(rlIpAdr *dest, const char *format,...)
int rlwsa()
Definition: rlsocket.cpp:68

◆ ~rlUdpSocket()

rlUdpSocket::~rlUdpSocket ( )
virtual

Definition at line 86 of file rludpsocket.cpp.

87 {
88  if(s > 0)
89  {
90 #ifdef RLWIN32
91  closesocket(s);
92 #else
93  close(s);
94 #endif
95  }
96 }

Member Function Documentation

◆ bind()

int rlUdpSocket::bind ( int  port)
return > 0 -> socket else error

Definition at line 122 of file rludpsocket.cpp.

123 {
124  if(port < 0 || port >= 256*256) return -1;
125 
126  memset(&address,0,sizeof(address));
127  address.sin_family = AF_INET;
128  address.sin_port = htons((short) port);
129  address.sin_addr.s_addr = htonl(INADDR_ANY);
130  // bind socket
131  if(::bind(s, (sockaddr *) &address, sizeof(address)) < 0)
132  {
133  ::printf("rlUdpSocket::setAdr() bind() failed port=%d\n", port);
134  return -1;
135  }
136  return 0;
137 }
int printf(rlIpAdr *dest, const char *format,...)
int bind(int port)
struct sockaddr_in address
Definition: rludpsocket.h:97

◆ printf()

int rlUdpSocket::printf ( rlIpAdr dest,
const char *  format,
  ... 
)
return >= 0 -> number of bytes written else error

Definition at line 218 of file rludpsocket.cpp.

219 {
220  int ret;
221  char message[rl_PRINTF_LENGTH]; // should be big enough
222 
223  va_list ap;
224  va_start(ap,format);
225  ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
226  va_end(ap);
227  if(ret < 0) return ret;
228  return sendto(message,strlen(message)+1,dest);
229 }
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
int rlvsnprintf(char *text, int len, const char *format, va_list ap)
Definition: rlcutil.cpp:197
int sendto(const void *buf, int len, rlIpAdr *dest)

◆ recvfrom()

int rlUdpSocket::recvfrom ( void *  buf,
int  maxlen,
rlIpAdr source,
int  timeout = -1 
)
return > 0 -> number of bytes read return == 0 -> timeout else error

Definition at line 160 of file rludpsocket.cpp.

161 {
162  int ret, len;
163 
164  if(timeout >= 0)
165  {
166  ret = select(timeout);
167  if(ret != 1) return -1; // timeout
168  }
169  if(debug) ::printf("rlUdpSocket()::recvfrom() ...\n");
170  len = sizeof(source->address);
171 #ifdef RLWIN32
172  ret = ::recvfrom(s, (char *) buf, maxlen, readflag,
173  (struct sockaddr *) &source->address, (int FAR *) &len);
174 #endif
175 #ifdef RLUNIX
176  ret = ::recvfrom(s, buf, maxlen, readflag,
177  (struct sockaddr *) &source->address, (socklen_t *) &len);
178 #endif
179 #ifdef __VMS
180  ret = ::recvfrom(s, buf, maxlen, readflag,
181  (struct sockaddr *) &source->address, (size_t *) &len);
182 #endif
183  if(ret < 0)
184  {
185  ::printf("ERROR: rlUdpSocket::read()\n");
186  return -2;
187  }
188  if(debug)
189  {
190  unsigned char *cbuf = (unsigned char *) buf;
191  ::printf("rlUdpSocket()::recvfrom() ret=%d data=[0x%x",ret,cbuf[0]);
192  for(int i=1; i<ret; i++) ::printf(",0x%x",cbuf[i]);
193  ::printf("]\n");
194  }
195  return ret;
196 }
int recvfrom(void *buf, int maxlen, rlIpAdr *source, int timeout=-1)
int printf(rlIpAdr *dest, const char *format,...)
int select(int timeout)
struct sockaddr_in address
Definition: rludpsocket.h:47

◆ select()

int rlUdpSocket::select ( int  timeout)
return == 0 -> timeout

Definition at line 139 of file rludpsocket.cpp.

140 {
141  if(timeout < 0) return -1;
142  struct timeval timout;
143  fd_set wset,rset,eset;
144  int ret,maxfdp1;
145 
146  /* setup sockets to read */
147  maxfdp1 = s+1;
148  FD_ZERO(&rset);
149  FD_SET (s,&rset);
150  FD_ZERO(&wset);
151  FD_ZERO(&eset);
152  timout.tv_sec = timeout / 1000;
153  timout.tv_usec = (timeout % 1000) * 1000;
154 
155  ret = ::select(maxfdp1,&rset,&wset,&eset,&timout);
156  if(ret == 0) return 0; /* timeout */
157  return 1;
158 }
int select(int timeout)

◆ sendto()

int rlUdpSocket::sendto ( const void *  buf,
int  len,
rlIpAdr dest 
)
return >= 0 -> number of bytes written else error

Definition at line 198 of file rludpsocket.cpp.

199 {
200 #ifdef RLWIN32
201  int ret = ::sendto(s, (const char *) buf, len, writeflag,
202  (struct sockaddr *) &dest->address, sizeof(struct sockaddr_in));
203 #else
204  int ret = ::sendto(s, buf, len, writeflag,
205  (struct sockaddr *) &dest->address, sizeof(struct sockaddr_in));
206 #endif
207  if(ret < 0) ::printf("ERROR: rlUdpSocket::sendto()\n");
208  if(debug)
209  {
210  unsigned char *cbuf = (unsigned char *) buf;
211  ::printf("rlUdpSocket()::sendto() ret=%d data=[0x%x",ret,cbuf[0]);
212  for(int i=1; i<ret; i++) ::printf(",0x%x",cbuf[i]);
213  ::printf("]\n");
214  }
215  return ret;
216 }
int printf(rlIpAdr *dest, const char *format,...)
int sendto(const void *buf, int len, rlIpAdr *dest)
struct sockaddr_in address
Definition: rludpsocket.h:47

◆ setSockopt() [1/2]

int rlUdpSocket::setSockopt ( int  opt)
setsocketopt for SOL_SOCKET level

Definition at line 98 of file rludpsocket.cpp.

99 {
100  const int on = 1;
101  if(s == -1) return -1;
102  // set socket options
103 #ifdef RLWIN32
104  return setsockopt(s,SOL_SOCKET,opt,(const char *) &on,sizeof(on));
105 #else
106  return setsockopt(s,SOL_SOCKET,opt,&on,sizeof(on));
107 #endif
108 }

◆ setSockopt() [2/2]

int rlUdpSocket::setSockopt ( int  level,
int  optname,
void *  optval,
int  optlen 
)
setsocketopt with full control

Definition at line 110 of file rludpsocket.cpp.

111 {
112  if(s == -1) return -1;
113  if(optlen <= 0) return -1;
114  // set socket options
115 #ifdef RLWIN32
116  return setsockopt(s,level,optname,(const char *) &optval,optlen);
117 #else
118  return setsockopt(s,level,optname,optval,optlen);
119 #endif
120 }

Member Data Documentation

◆ address

struct sockaddr_in rlUdpSocket::address
private

Definition at line 97 of file rludpsocket.h.

◆ debug

int rlUdpSocket::debug

Definition at line 94 of file rludpsocket.h.

◆ readflag

int rlUdpSocket::readflag

Definition at line 94 of file rludpsocket.h.

◆ s

int rlUdpSocket::s
private

Definition at line 98 of file rludpsocket.h.

◆ writeflag

int rlUdpSocket::writeflag

Definition at line 94 of file rludpsocket.h.


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