annotate core/UdpServer.cpp @ 131:ff28e56e5b7e scope-refactoring

Updated Network files to match Udpioplugin 18:fb5a61b10223
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 26 Aug 2015 02:02:10 +0100
parents 719119fb2905
children e77e2e712fbc
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@44 7 #include "../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@44 32 return enabled;
giuliomoro@44 33 }
giuliomoro@44 34
giuliomoro@44 35 bool UdpServer::bindToPort(int aPort){
giuliomoro@44 36 port=aPort;
giuliomoro@53 37 if(port<1){
giuliomoro@53 38 enabled=false;
giuliomoro@53 39 return false;
giuliomoro@53 40 }
giuliomoro@44 41 server.sin_port=htons(port);
giuliomoro@44 42 if (bind(inSocket,(struct sockaddr *)&server,length)<0){
giuliomoro@44 43 enabled=false;
giuliomoro@44 44 return false;
giuliomoro@44 45 }
giuliomoro@53 46 enabled=true;
giuliomoro@44 47 return true;
giuliomoro@126 48 }
giuliomoro@126 49
giuliomoro@126 50 void UdpServer::close(){
giuliomoro@126 51 int ret=::close(inSocket);
giuliomoro@126 52 if(ret != 0)
giuliomoro@126 53 printf("Error while closing socket, errno: %d\n", errno);//Stop receiving data for this socket. If further data arrives, reject it.
giuliomoro@126 54 inSocket=0;
giuliomoro@126 55 }
giuliomoro@126 56
giuliomoro@121 57 int UdpServer::waitUntilReady(bool readyForReading, int timeoutMsecs){
giuliomoro@121 58 // 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 59 if(enabled==false)
giuliomoro@121 60 return -1;
giuliomoro@121 61 if(timeoutMsecs<0)
giuliomoro@121 62 return select(inSocket+1, &stReadFDS, NULL, NULL, NULL); //calling this with a NULL timeout will block indefinitely
giuliomoro@121 63 FD_ZERO(&stReadFDS);
giuliomoro@121 64 FD_SET(inSocket, &stReadFDS);
giuliomoro@121 65 if(timeoutMsecs>=1000){
giuliomoro@121 66 float timeOutSecs=timeoutMsecs*0.001;
giuliomoro@121 67 stTimeOut.tv_sec=(long int)timeOutSecs;
giuliomoro@121 68 timeOutSecs-=(int)timeOutSecs;
giuliomoro@121 69 stTimeOut.tv_usec=(long int)(timeOutSecs*1000000);
giuliomoro@121 70 } else //faster!
giuliomoro@121 71 stTimeOut.tv_usec=(long int)timeoutMsecs*1000;
giuliomoro@121 72 int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stTimeOut);
giuliomoro@121 73 return descriptorReady>0? 1 : descriptorReady;
giuliomoro@121 74 }
giuliomoro@121 75
giuliomoro@44 76 int UdpServer::read(//Returns the number of bytes read, or -1 if there was an error.
giuliomoro@44 77 void *destBuffer,
giuliomoro@121 78 int maxBytesToRead,
giuliomoro@121 79 bool blockUntilSpecifiedAmountHasArrived)
giuliomoro@121 80 {
giuliomoro@44 81 if(enabled==false)
giuliomoro@44 82 return -1;
giuliomoro@44 83 FD_ZERO(&stReadFDS);
giuliomoro@44 84 FD_SET(inSocket, &stReadFDS);
giuliomoro@131 85 int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stZeroTimeOut); //TODO: this is not JUCE-compliant
giuliomoro@44 86 if(descriptorReady<0){ //an error occurred
giuliomoro@44 87 return -1;
giuliomoro@44 88 }
giuliomoro@44 89 int numberOfBytes=0;
giuliomoro@121 90 // do
giuliomoro@121 91 {
giuliomoro@121 92 if (FD_ISSET(inSocket, &stReadFDS))
giuliomoro@121 93 {
giuliomoro@121 94 // numberOfBytes=recvfrom(inSocket,destBuffer,maxBytesToRead,0,(struct sockaddr *)&from,&fromLength);
giuliomoro@121 95 numberOfBytes+=recv(inSocket,destBuffer,maxBytesToRead-numberOfBytes,0);
giuliomoro@121 96 if(numberOfBytes<0)
giuliomoro@121 97 return -1;
giuliomoro@121 98 }
giuliomoro@44 99 }
giuliomoro@121 100 // while (blockUntilSpecifiedAmountHasArrived && numberOfBytes==maxBytesToRead);
giuliomoro@44 101 return numberOfBytes;
giuliomoro@121 102 }
giuliomoro@131 103 int UdpServer::empty(){
giuliomoro@131 104 return empty(0);
giuliomoro@44 105 }
giuliomoro@131 106 int UdpServer::empty(int maxCount){
giuliomoro@131 107 int count=0;
giuliomoro@131 108 int n;
giuliomoro@131 109 do {
giuliomoro@131 110 if(waitUntilReady(true, 0)==0)
giuliomoro@131 111 return 0;
giuliomoro@131 112 float waste;
giuliomoro@131 113 n=read(&waste, sizeof(float), false);
giuliomoro@131 114 count++;
giuliomoro@131 115 } while (n>0 && (maxCount<=0 || maxCount<count));
giuliomoro@131 116 printf("socket emptied with %d reads\n", count);
giuliomoro@131 117 return count;
giuliomoro@44 118 }