comparison core/UdpClient.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 da1c61aa97ea
children
comparison
equal deleted inserted replaced
133:04b1678614c9 135:e77e2e712fbc
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){
57 int UdpClient::write(const char* remoteHostname, int remotePortNumber, void* sourceBuffer, int numBytesToWrite){ 58 int UdpClient::write(const char* remoteHostname, int remotePortNumber, void* sourceBuffer, int numBytesToWrite){
58 setServer(remoteHostname); 59 setServer(remoteHostname);
59 setPort(remotePortNumber); 60 setPort(remotePortNumber);
60 send(sourceBuffer, numBytesToWrite); 61 send(sourceBuffer, numBytesToWrite);
61 } 62 }
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 }