Mercurial > hg > beaglert
comparison 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 |
comparison
equal
deleted
inserted
replaced
120:cdd441a304a9 | 121:2197435e8fb4 |
---|---|
15 UdpServer::~UdpServer(){ | 15 UdpServer::~UdpServer(){ |
16 //TODO: unbind from port. AFAIK, this involves closing the socket, therefore creating the socket should become part of bindToPort | 16 //TODO: unbind from port. AFAIK, this involves closing the socket, therefore creating the socket should become part of bindToPort |
17 }; | 17 }; |
18 bool UdpServer::init(int aPort){ | 18 bool UdpServer::init(int aPort){ |
19 enabled=true; | 19 enabled=true; |
20 stTimeOut.tv_sec = 0; //set timeout to 0 | 20 stZeroTimeOut.tv_sec = 0; //set timeout to 0 |
21 stTimeOut.tv_usec = 0; | 21 stZeroTimeOut.tv_usec = 0; |
22 inSocket=socket(AF_INET, SOCK_DGRAM, 0); | 22 inSocket=socket(AF_INET, SOCK_DGRAM, 0); |
23 if (inSocket < 0){ | 23 if (inSocket < 0){ |
24 enabled=false; | 24 enabled=false; |
25 } | 25 } |
26 length = sizeof(server); | 26 length = sizeof(server); |
44 return false; | 44 return false; |
45 } | 45 } |
46 enabled=true; | 46 enabled=true; |
47 return true; | 47 return true; |
48 }; | 48 }; |
49 int UdpServer::waitUntilReady(bool readyForReading, int timeoutMsecs){ | |
50 // 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. | |
51 if(enabled==false) | |
52 return -1; | |
53 if(timeoutMsecs<0) | |
54 return select(inSocket+1, &stReadFDS, NULL, NULL, NULL); //calling this with a NULL timeout will block indefinitely | |
55 FD_ZERO(&stReadFDS); | |
56 FD_SET(inSocket, &stReadFDS); | |
57 if(timeoutMsecs>=1000){ | |
58 float timeOutSecs=timeoutMsecs*0.001; | |
59 stTimeOut.tv_sec=(long int)timeOutSecs; | |
60 timeOutSecs-=(int)timeOutSecs; | |
61 stTimeOut.tv_usec=(long int)(timeOutSecs*1000000); | |
62 } else //faster! | |
63 stTimeOut.tv_usec=(long int)timeoutMsecs*1000; | |
64 int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stTimeOut); | |
65 return descriptorReady>0? 1 : descriptorReady; | |
66 } | |
67 | |
49 int UdpServer::read(//Returns the number of bytes read, or -1 if there was an error. | 68 int UdpServer::read(//Returns the number of bytes read, or -1 if there was an error. |
50 void *destBuffer, | 69 void *destBuffer, |
51 int maxBytesToRead){ | 70 int maxBytesToRead, |
71 bool blockUntilSpecifiedAmountHasArrived) | |
72 { | |
52 if(enabled==false) | 73 if(enabled==false) |
53 return -1; | 74 return -1; |
54 FD_ZERO(&stReadFDS); | 75 FD_ZERO(&stReadFDS); |
55 FD_SET(inSocket, &stReadFDS); | 76 FD_SET(inSocket, &stReadFDS); |
56 int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stTimeOut); | 77 int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stZeroTimeOut); |
57 if(descriptorReady<0){ //an error occurred | 78 if(descriptorReady<0){ //an error occurred |
58 return -1; | 79 return -1; |
59 } | 80 } |
60 int numberOfBytes=0; | 81 int numberOfBytes=0; |
61 if (FD_ISSET(inSocket, &stReadFDS)) { | 82 // do |
62 numberOfBytes=recvfrom(inSocket,destBuffer,maxBytesToRead,0,(struct sockaddr *)&from,&fromLength); | 83 { |
63 if(numberOfBytes<0) | 84 if (FD_ISSET(inSocket, &stReadFDS)) |
64 return -1; | 85 { |
86 // numberOfBytes=recvfrom(inSocket,destBuffer,maxBytesToRead,0,(struct sockaddr *)&from,&fromLength); | |
87 numberOfBytes+=recv(inSocket,destBuffer,maxBytesToRead-numberOfBytes,0); | |
88 if(numberOfBytes<0) | |
89 return -1; | |
90 } | |
65 } | 91 } |
92 // while (blockUntilSpecifiedAmountHasArrived && numberOfBytes==maxBytesToRead); | |
66 return numberOfBytes; | 93 return numberOfBytes; |
67 }; | 94 } |
68 int UdpServer::emptySocket(){ | 95 int UdpServer::emptySocket(){ |
69 return emptySocket(0); | 96 return emptySocket(0); |
70 } | 97 } |
71 int UdpServer::emptySocket(int maxBytes){//discards up to maxBytes from the socket. Returns the number of bytes discarded. | 98 int UdpServer::emptySocket(int maxBytes){//discards up to maxBytes from the socket. Returns the number of bytes discarded. |
72 if(wasteBuffer==NULL) | 99 if(wasteBuffer==NULL) |
73 return -1; | 100 return -1; |
74 int numberOfBytes=0; | 101 int numberOfBytes=0; |
75 while(int n=read(wasteBuffer, wasteBufferSize)){// calls the read function until it does not return any more bytes (i.e.: socket is empty) | 102 while(int n=read(wasteBuffer, wasteBufferSize, false)){// calls the read function until it does not return any more bytes (i.e.: socket is empty) |
76 if(n>0) | 103 if(n>0) |
77 numberOfBytes+=n; | 104 numberOfBytes+=n; |
78 if(n<0) | 105 if(n<0) |
79 return -1; | 106 return -1; |
80 if(maxBytes>0 && numberOfBytes>=maxBytes) | 107 if(maxBytes>0 && numberOfBytes>=maxBytes) |