comparison core/UdpClient.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 f3251851c718
children 247a182adb6d
comparison
equal deleted inserted replaced
216:869f5e703844 217:c42a6b4dc2d4
2 * udpClient.cpp 2 * udpClient.cpp
3 * 3 *
4 * Created on: 19 May 2015 4 * Created on: 19 May 2015
5 * Author: giulio moro 5 * Author: giulio moro
6 */ 6 */
7 #include "../include/UdpClient.h" 7 #include "UdpClient.h"
8 8
9 UdpClient::UdpClient(){ 9 UdpClient::UdpClient(){
10 outSocket=socket(AF_INET, SOCK_DGRAM, 0); 10 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
11 isSetPort=false; 11 isSetPort=false;
12 isSetServer=false; 12 isSetServer=false;
21 setPort(aPort); 21 setPort(aPort);
22 setServer(aServerName); 22 setServer(aServerName);
23 isSetPort=true; 23 isSetPort=true;
24 isSetServer=true; 24 isSetServer=true;
25 enabled=true; 25 enabled=true;
26 memset(&stTimeOut, 0, sizeof(struct timeval));
26 } 27 }
27 UdpClient::~UdpClient(){ 28 UdpClient::~UdpClient(){
28 close(outSocket); 29 close(outSocket);
29 } 30 }
30 void UdpClient::setPort(int aPort){ 31 void UdpClient::setPort(int aPort){
52 if (n < 0){ 53 if (n < 0){
53 return n; 54 return n;
54 } 55 }
55 return 1; 56 return 1;
56 }; 57 };
57 58 int UdpClient::write(const char* remoteHostname, int remotePortNumber, void* sourceBuffer, int numBytesToWrite){
59 setServer(remoteHostname);
60 setPort(remotePortNumber);
61 send(sourceBuffer, numBytesToWrite);
62 }
63 int UdpClient::waitUntilReady(bool readyForReading, int timeoutMsecs){
64 // 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.
65 if(enabled==false)
66 return -1;
67 if(timeoutMsecs<0)
68 return select(outSocket+1, NULL, &stWriteFDS, NULL, NULL); //calling this with a NULL timeout will block indefinitely
69 FD_ZERO(&stWriteFDS);
70 FD_SET(outSocket, &stWriteFDS);
71 float timeOutSecs=timeoutMsecs*0.001;
72 stTimeOut.tv_sec=(int)timeOutSecs;
73 timeOutSecs-=(int)timeOutSecs;
74 stTimeOut.tv_usec=(int)(timeOutSecs*1000000);
75 int descriptorReady= select(outSocket+1, NULL, &stWriteFDS, NULL, &stTimeOut);
76 return descriptorReady>0? 1 : descriptorReady;
77 }