Mercurial > hg > beaglert
changeset 121:2197435e8fb4 scope-refactoring
UdpServer : read and waitUntilReady are now somehow Juce-compliant
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Sat, 22 Aug 2015 01:20:35 +0100 |
parents | cdd441a304a9 |
children | bc514f29c3aa |
files | core/UdpServer.cpp include/UdpServer.h |
diffstat | 2 files changed, 57 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/core/UdpServer.cpp Fri Aug 21 15:52:37 2015 +0100 +++ b/core/UdpServer.cpp Sat Aug 22 01:20:35 2015 +0100 @@ -17,8 +17,8 @@ }; bool UdpServer::init(int aPort){ enabled=true; - stTimeOut.tv_sec = 0; //set timeout to 0 - stTimeOut.tv_usec = 0; + stZeroTimeOut.tv_sec = 0; //set timeout to 0 + stZeroTimeOut.tv_usec = 0; inSocket=socket(AF_INET, SOCK_DGRAM, 0); if (inSocket < 0){ enabled=false; @@ -46,25 +46,52 @@ enabled=true; return true; }; +int UdpServer::waitUntilReady(bool readyForReading, int timeoutMsecs){ +// If the socket is ready on return, this returns 1. If it times-out before the socket becomes ready, it returns 0. If an error occurs, it returns -1. + if(enabled==false) + return -1; + if(timeoutMsecs<0) + return select(inSocket+1, &stReadFDS, NULL, NULL, NULL); //calling this with a NULL timeout will block indefinitely + FD_ZERO(&stReadFDS); + FD_SET(inSocket, &stReadFDS); + if(timeoutMsecs>=1000){ + float timeOutSecs=timeoutMsecs*0.001; + stTimeOut.tv_sec=(long int)timeOutSecs; + timeOutSecs-=(int)timeOutSecs; + stTimeOut.tv_usec=(long int)(timeOutSecs*1000000); + } else //faster! + stTimeOut.tv_usec=(long int)timeoutMsecs*1000; + int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stTimeOut); + return descriptorReady>0? 1 : descriptorReady; +} + int UdpServer::read(//Returns the number of bytes read, or -1 if there was an error. void *destBuffer, - int maxBytesToRead){ + int maxBytesToRead, + bool blockUntilSpecifiedAmountHasArrived) +{ if(enabled==false) return -1; FD_ZERO(&stReadFDS); FD_SET(inSocket, &stReadFDS); - int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stTimeOut); + int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stZeroTimeOut); if(descriptorReady<0){ //an error occurred return -1; } int numberOfBytes=0; - if (FD_ISSET(inSocket, &stReadFDS)) { - numberOfBytes=recvfrom(inSocket,destBuffer,maxBytesToRead,0,(struct sockaddr *)&from,&fromLength); - if(numberOfBytes<0) - return -1; +// do + { + if (FD_ISSET(inSocket, &stReadFDS)) + { + // numberOfBytes=recvfrom(inSocket,destBuffer,maxBytesToRead,0,(struct sockaddr *)&from,&fromLength); + numberOfBytes+=recv(inSocket,destBuffer,maxBytesToRead-numberOfBytes,0); + if(numberOfBytes<0) + return -1; + } } +// while (blockUntilSpecifiedAmountHasArrived && numberOfBytes==maxBytesToRead); return numberOfBytes; -}; +} int UdpServer::emptySocket(){ return emptySocket(0); } @@ -72,7 +99,7 @@ if(wasteBuffer==NULL) return -1; int numberOfBytes=0; - while(int n=read(wasteBuffer, wasteBufferSize)){// calls the read function until it does not return any more bytes (i.e.: socket is empty) + while(int n=read(wasteBuffer, wasteBufferSize, false)){// calls the read function until it does not return any more bytes (i.e.: socket is empty) if(n>0) numberOfBytes+=n; if(n<0)
--- a/include/UdpServer.h Fri Aug 21 15:52:37 2015 +0100 +++ b/include/UdpServer.h Sat Aug 22 01:20:35 2015 +0100 @@ -25,6 +25,7 @@ int inSocket; struct sockaddr_in server; struct timeval stTimeOut; + struct timeval stZeroTimeOut; fd_set stReadFDS; int size; void *wasteBuffer; @@ -39,11 +40,28 @@ bool init(int aPort); bool bindToPort(int aPort); int getBoundPort() const; - int read(void *destBuffer, - int maxBytesToRead); + /* + * Reads bytes from the socket. + * + * Drop-in replacement for JUCE DatagramSocket::read() + * + If blockUntilSpecifiedAmountHasArrived is true, the method will block until maxBytesToRead + bytes have been read, (or until an error occurs). If this flag is false, the method will + return as much data as is currently available without blocking. + */ + int read(void* destBuffer, int maxBytesToRead, bool blockUntilSpecifiedAmountHasArrived); int emptySocket(); int emptySocket(int maxBytes); void *getWaste(); + /* + * Waits until the socket is ready for reading or writing. + * + Drop-in replacement for JUCE DatagramSocket::waitUntilReady. + If readyForReading is true, it will wait until the socket is ready for reading; if false, it will wait until it's ready for writing. + If the timeout is < 0, it will wait forever, or else will give up after the specified time. + If the socket is ready on return, this returns 1. If it times-out before the socket becomes ready, it returns 0. If an error occurs, it returns -1. + */ + int waitUntilReady(bool readyForReading, int timeoutMsecs); };