annotate core/UdpServer.cpp @ 135:e77e2e712fbc ClockSync

To work with the ClockSync plugin
author Giulio Moro <giuliomoro@yahoo.it>
date Sat, 12 Sep 2015 20:05:55 +0100
parents ff28e56e5b7e
children 134bff10e561
rev   line source
giuliomoro@44 1 /*
giuliomoro@44 2 * udpServer.cpp
giuliomoro@44 3 *
giuliomoro@44 4 * Created on: 19 May 2015
giuliomoro@44 5 * Author: giulio moro
giuliomoro@44 6 */
giuliomoro@135 7 #include "UdpServer.h"
giuliomoro@44 8
giuliomoro@44 9 UdpServer::UdpServer(int aPort){
giuliomoro@44 10 init(aPort);
giuliomoro@44 11 };
giuliomoro@44 12 UdpServer::UdpServer(){
giuliomoro@44 13 init(0);
giuliomoro@44 14 }
giuliomoro@44 15 UdpServer::~UdpServer(){
giuliomoro@126 16 close();
giuliomoro@44 17 };
giuliomoro@44 18 bool UdpServer::init(int aPort){
giuliomoro@44 19 enabled=true;
giuliomoro@121 20 stZeroTimeOut.tv_sec = 0; //set timeout to 0
giuliomoro@121 21 stZeroTimeOut.tv_usec = 0;
giuliomoro@44 22 inSocket=socket(AF_INET, SOCK_DGRAM, 0);
giuliomoro@44 23 if (inSocket < 0){
giuliomoro@44 24 enabled=false;
giuliomoro@44 25 }
giuliomoro@44 26 length = sizeof(server);
giuliomoro@44 27 server.sin_family=AF_INET;
giuliomoro@44 28 server.sin_addr.s_addr=INADDR_ANY;
giuliomoro@44 29 enabled=bindToPort(aPort);
giuliomoro@44 30 wasteBufferSize=2048;
giuliomoro@44 31 wasteBuffer=malloc(wasteBufferSize);
giuliomoro@135 32 memset(&stTimeOut,0,sizeof(struct timeval));
giuliomoro@44 33 return enabled;
giuliomoro@44 34 }
giuliomoro@44 35
giuliomoro@44 36 bool UdpServer::bindToPort(int aPort){
giuliomoro@44 37 port=aPort;
giuliomoro@53 38 if(port<1){
giuliomoro@53 39 enabled=false;
giuliomoro@53 40 return false;
giuliomoro@53 41 }
giuliomoro@44 42 server.sin_port=htons(port);
giuliomoro@44 43 if (bind(inSocket,(struct sockaddr *)&server,length)<0){
giuliomoro@44 44 enabled=false;
giuliomoro@44 45 return false;
giuliomoro@44 46 }
giuliomoro@53 47 enabled=true;
giuliomoro@44 48 return true;
giuliomoro@126 49 }
giuliomoro@126 50
giuliomoro@126 51 void UdpServer::close(){
giuliomoro@126 52 int ret=::close(inSocket);
giuliomoro@126 53 if(ret != 0)
giuliomoro@126 54 printf("Error while closing socket, errno: %d\n", errno);//Stop receiving data for this socket. If further data arrives, reject it.
giuliomoro@126 55 inSocket=0;
giuliomoro@126 56 }
giuliomoro@126 57
giuliomoro@121 58 int UdpServer::waitUntilReady(bool readyForReading, int timeoutMsecs){
giuliomoro@121 59 // 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.
giuliomoro@121 60 if(enabled==false)
giuliomoro@121 61 return -1;
giuliomoro@121 62 if(timeoutMsecs<0)
giuliomoro@121 63 return select(inSocket+1, &stReadFDS, NULL, NULL, NULL); //calling this with a NULL timeout will block indefinitely
giuliomoro@121 64 FD_ZERO(&stReadFDS);
giuliomoro@121 65 FD_SET(inSocket, &stReadFDS);
giuliomoro@121 66 if(timeoutMsecs>=1000){
giuliomoro@121 67 float timeOutSecs=timeoutMsecs*0.001;
giuliomoro@121 68 stTimeOut.tv_sec=(long int)timeOutSecs;
giuliomoro@121 69 timeOutSecs-=(int)timeOutSecs;
giuliomoro@121 70 stTimeOut.tv_usec=(long int)(timeOutSecs*1000000);
giuliomoro@121 71 } else //faster!
giuliomoro@121 72 stTimeOut.tv_usec=(long int)timeoutMsecs*1000;
giuliomoro@121 73 int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stTimeOut);
giuliomoro@121 74 return descriptorReady>0? 1 : descriptorReady;
giuliomoro@121 75 }
giuliomoro@121 76
giuliomoro@44 77 int UdpServer::read(//Returns the number of bytes read, or -1 if there was an error.
giuliomoro@44 78 void *destBuffer,
giuliomoro@121 79 int maxBytesToRead,
giuliomoro@121 80 bool blockUntilSpecifiedAmountHasArrived)
giuliomoro@121 81 {
giuliomoro@44 82 if(enabled==false)
giuliomoro@44 83 return -1;
giuliomoro@44 84 FD_ZERO(&stReadFDS);
giuliomoro@44 85 FD_SET(inSocket, &stReadFDS);
giuliomoro@131 86 int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stZeroTimeOut); //TODO: this is not JUCE-compliant
giuliomoro@44 87 if(descriptorReady<0){ //an error occurred
giuliomoro@44 88 return -1;
giuliomoro@44 89 }
giuliomoro@44 90 int numberOfBytes=0;
giuliomoro@121 91 // do
giuliomoro@121 92 {
giuliomoro@121 93 if (FD_ISSET(inSocket, &stReadFDS))
giuliomoro@121 94 {
giuliomoro@121 95 // numberOfBytes=recvfrom(inSocket,destBuffer,maxBytesToRead,0,(struct sockaddr *)&from,&fromLength);
giuliomoro@121 96 numberOfBytes+=recv(inSocket,destBuffer,maxBytesToRead-numberOfBytes,0);
giuliomoro@121 97 if(numberOfBytes<0)
giuliomoro@121 98 return -1;
giuliomoro@121 99 }
giuliomoro@44 100 }
giuliomoro@121 101 // while (blockUntilSpecifiedAmountHasArrived && numberOfBytes==maxBytesToRead);
giuliomoro@44 102 return numberOfBytes;
giuliomoro@121 103 }
giuliomoro@131 104 int UdpServer::empty(){
giuliomoro@131 105 return empty(0);
giuliomoro@44 106 }
giuliomoro@131 107 int UdpServer::empty(int maxCount){
giuliomoro@131 108 int count=0;
giuliomoro@131 109 int n;
giuliomoro@131 110 do {
giuliomoro@131 111 if(waitUntilReady(true, 0)==0)
giuliomoro@131 112 return 0;
giuliomoro@131 113 float waste;
giuliomoro@131 114 n=read(&waste, sizeof(float), false);
giuliomoro@131 115 count++;
giuliomoro@131 116 } while (n>0 && (maxCount<=0 || maxCount<count));
giuliomoro@131 117 printf("socket emptied with %d reads\n", count);
giuliomoro@131 118 return count;
giuliomoro@44 119 }