|
rllib
1
|
#include <rludpsocket.h>

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 |
class for encapsulating UDP socket calls
Definition at line 53 of file rludpsocket.h.
| rlUdpSocket::rlUdpSocket | ( | int | debug = 0 | ) |
| rlUdpSocket::~rlUdpSocket | ( | ) | [virtual] |
Definition at line 86 of file rludpsocket.cpp.
| 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.
| int rlUdpSocket::setSockopt | ( | int | level, |
| int | optname, | ||
| void * | optval, | ||
| int | optlen | ||
| ) |
setsocketopt with full control
Definition at line 110 of file rludpsocket.cpp.
struct sockaddr_in rlUdpSocket::address [private] |
Definition at line 97 of file rludpsocket.h.
Reimplemented in rlEIBnetIP.
Definition at line 94 of file rludpsocket.h.
Definition at line 94 of file rludpsocket.h.
int rlUdpSocket::s [private] |
Definition at line 98 of file rludpsocket.h.
Definition at line 94 of file rludpsocket.h.
1.7.5.1