diff core/UdpServer.cpp @ 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 d3f869b98147
children 23137a333c93
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)