comparison core/UdpServer.cpp @ 217:c42a6b4dc2d4 mergingClockSync

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