This commit is contained in:
Michael Iedema 2016-07-06 12:19:56 -07:00
parent cbb5d790e5
commit 8f57dd84b3
3 changed files with 17 additions and 13 deletions

View File

@ -199,21 +199,25 @@ int DatagramSocket::send(const struct sockaddr* dest, const char * message)
return send(dest,message,length); return send(dest,message,length);
} }
int DatagramSocket::read(char* buffer, size_t length)
{
socklen_t addr_len = sizeof(mSource);
int rd_length = recvfrom(mSocketFD, (void *) buffer, length, 0,
(struct sockaddr*) &mSource, &addr_len);
if ((rd_length==-1) && (errno!=EAGAIN)) {
int DatagramSocket::read(char* buffer)
{
socklen_t temp_len = sizeof(mSource);
int length = recvfrom(mSocketFD, (void*)buffer, MAX_UDP_LENGTH, 0,
(struct sockaddr*)&mSource,&temp_len);
if ((length==-1) && (errno!=EAGAIN)) {
perror("DatagramSocket::read() failed"); perror("DatagramSocket::read() failed");
devassert(0); devassert(0);
throw SocketError(); throw SocketError();
} }
return rd_length; return length;
} }
int DatagramSocket::read(char* buffer, size_t length, unsigned timeout)
int DatagramSocket::read(char* buffer, unsigned timeout)
{ {
fd_set fds; fd_set fds;
FD_ZERO(&fds); FD_ZERO(&fds);
@ -228,7 +232,7 @@ int DatagramSocket::read(char* buffer, size_t length, unsigned timeout)
throw SocketError(); throw SocketError();
} }
if (sel==0) return -1; if (sel==0) return -1;
if (FD_ISSET(mSocketFD,&fds)) return read(buffer, length); if (FD_ISSET(mSocketFD,&fds)) return read(buffer);
return -1; return -1;
} }

View File

@ -110,7 +110,7 @@ public:
@param buffer A char[MAX_UDP_LENGTH] procured by the caller. @param buffer A char[MAX_UDP_LENGTH] procured by the caller.
@return The number of bytes received or -1 on non-blocking pass. @return The number of bytes received or -1 on non-blocking pass.
*/ */
int read(char* buffer, size_t length); int read(char* buffer);
/** /**
Receive a packet with a timeout. Receive a packet with a timeout.
@ -118,7 +118,7 @@ public:
@param maximum wait time in milliseconds @param maximum wait time in milliseconds
@return The number of bytes received or -1 on timeout. @return The number of bytes received or -1 on timeout.
*/ */
int read(char* buffer, size_t length, unsigned timeout); int read(char* buffer, unsigned timeout);
/** Send a packet to a given destination, other than the default. */ /** Send a packet to a given destination, other than the default. */

View File

@ -45,7 +45,7 @@ void *testReaderIP(void *)
int rc = 0; int rc = 0;
while (rc<gNumToSend) { while (rc<gNumToSend) {
char buf[MAX_UDP_LENGTH]; char buf[MAX_UDP_LENGTH];
int count = readSocket.read(buf, MAX_UDP_LENGTH); int count = readSocket.read(buf);
if (count>0) { if (count>0) {
COUT("read: " << buf); COUT("read: " << buf);
rc++; rc++;
@ -65,7 +65,7 @@ void *testReaderUnix(void *)
int rc = 0; int rc = 0;
while (rc<gNumToSend) { while (rc<gNumToSend) {
char buf[MAX_UDP_LENGTH]; char buf[MAX_UDP_LENGTH];
int count = readSocket.read(buf, MAX_UDP_LENGTH); int count = readSocket.read(buf);
if (count>0) { if (count>0) {
COUT("read: " << buf); COUT("read: " << buf);
rc++; rc++;