rllib  1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
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 ( int  debug = 0)

Definition at line 72 of file rludpsocket.cpp.

{
debug = _debug;
if(debug) ::printf("rlUdpSocket() constructor\n");
rlwsa(); // init sockets on windows
s = socket(AF_INET,SOCK_DGRAM,0);
if(s < 0)
{
s = -1;
::printf("rlUdpSocket::rlUdpSocket could not get socket\n");
}
}
rlUdpSocket::~rlUdpSocket ( )
virtual

Definition at line 86 of file rludpsocket.cpp.

{
if(s > 0)
{
#ifdef RLWIN32
closesocket(s);
#else
close(s);
#endif
}
}

Member Function Documentation

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

Definition at line 122 of file rludpsocket.cpp.

{
if(port < 0 || port >= 256*256) return -1;
memset(&address,0,sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons((short) port);
address.sin_addr.s_addr = htonl(INADDR_ANY);
// bind socket
if(::bind(s, (sockaddr *) &address, sizeof(address)) < 0)
{
::printf("rlUdpSocket::setAdr() bind() failed port=%d\n", port);
return -1;
}
return 0;
}
int rlUdpSocket::printf ( rlIpAdr dest,
const char *  format,
  ... 
)
 return >= 0 -> number of bytes written else error
 

Definition at line 218 of file rludpsocket.cpp.

{
int ret;
char message[rl_PRINTF_LENGTH]; // should be big enough
va_list ap;
va_start(ap,format);
ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
va_end(ap);
if(ret < 0) return ret;
return sendto(message,strlen(message)+1,dest);
}
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.

{
int ret, len;
if(timeout >= 0)
{
ret = select(timeout);
if(ret != 1) return -1; // timeout
}
if(debug) ::printf("rlUdpSocket()::recvfrom() ...\n");
len = sizeof(source->address);
#ifdef RLWIN32
ret = ::recvfrom(s, (char *) buf, maxlen, readflag,
(struct sockaddr *) &source->address, (int FAR *) &len);
#endif
#ifdef RLUNIX
ret = ::recvfrom(s, buf, maxlen, readflag,
(struct sockaddr *) &source->address, (socklen_t *) &len);
#endif
#ifdef __VMS
ret = ::recvfrom(s, buf, maxlen, readflag,
(struct sockaddr *) &source->address, (size_t *) &len);
#endif
if(ret < 0)
{
::printf("ERROR: rlUdpSocket::read()\n");
return -2;
}
if(debug)
{
unsigned char *cbuf = (unsigned char *) buf;
::printf("rlUdpSocket()::recvfrom() ret=%d data=[0x%x",ret,cbuf[0]);
for(int i=1; i<ret; i++) ::printf(",0x%x",cbuf[i]);
::printf("]\n");
}
return ret;
}
int rlUdpSocket::select ( int  timeout)
 return == 0 -> timeout
 

Definition at line 139 of file rludpsocket.cpp.

{
if(timeout < 0) return -1;
struct timeval timout;
fd_set wset,rset,eset;
int ret,maxfdp1;
/* setup sockets to read */
maxfdp1 = s+1;
FD_ZERO(&rset);
FD_SET (s,&rset);
FD_ZERO(&wset);
FD_ZERO(&eset);
timout.tv_sec = timeout / 1000;
timout.tv_usec = (timeout % 1000) * 1000;
ret = ::select(maxfdp1,&rset,&wset,&eset,&timout);
if(ret == 0) return 0; /* timeout */
return 1;
}
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.

{
#ifdef RLWIN32
int ret = ::sendto(s, (const char *) buf, len, writeflag,
(struct sockaddr *) &dest->address, sizeof(struct sockaddr_in));
#else
int ret = ::sendto(s, buf, len, writeflag,
(struct sockaddr *) &dest->address, sizeof(struct sockaddr_in));
#endif
if(ret < 0) ::printf("ERROR: rlUdpSocket::sendto()\n");
if(debug)
{
unsigned char *cbuf = (unsigned char *) buf;
::printf("rlUdpSocket()::sendto() ret=%d data=[0x%x",ret,cbuf[0]);
for(int i=1; i<ret; i++) ::printf(",0x%x",cbuf[i]);
::printf("]\n");
}
return ret;
}
int rlUdpSocket::setSockopt ( int  opt)
 setsocketopt for SOL_SOCKET level
 

Definition at line 98 of file rludpsocket.cpp.

{
const int on = 1;
if(s == -1) return -1;
// set socket options
#ifdef RLWIN32
return setsockopt(s,SOL_SOCKET,opt,(const char *) &on,sizeof(on));
#else
return setsockopt(s,SOL_SOCKET,opt,&on,sizeof(on));
#endif
}
int rlUdpSocket::setSockopt ( int  level,
int  optname,
void *  optval,
int  optlen 
)
 setsocketopt with full control
 

Definition at line 110 of file rludpsocket.cpp.

{
if(s == -1) return -1;
if(optlen <= 0) return -1;
// set socket options
#ifdef RLWIN32
return setsockopt(s,level,optname,(const char *) &optval,optlen);
#else
return setsockopt(s,level,optname,optval,optlen);
#endif
}

Member Data Documentation

struct sockaddr_in rlUdpSocket::address
private

Definition at line 97 of file rludpsocket.h.

int rlUdpSocket::debug

Definition at line 94 of file rludpsocket.h.

int rlUdpSocket::readflag

Definition at line 94 of file rludpsocket.h.

int rlUdpSocket::s
private

Definition at line 98 of file rludpsocket.h.

int rlUdpSocket::writeflag

Definition at line 94 of file rludpsocket.h.


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