giuliomoro@217
|
1 /*
|
giuliomoro@217
|
2 * udpClient.cpp
|
giuliomoro@217
|
3 *
|
giuliomoro@217
|
4 * Created on: 19 May 2015
|
giuliomoro@217
|
5 * Author: giulio moro
|
giuliomoro@217
|
6 */
|
giuliomoro@217
|
7 #include "UdpClient.h"
|
giuliomoro@217
|
8
|
giuliomoro@217
|
9 UdpClient::UdpClient(){
|
giuliomoro@217
|
10 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
|
giuliomoro@267
|
11 int broadcastEnable = 1;
|
giuliomoro@267
|
12 int ret = setsockopt(outSocket, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));
|
giuliomoro@267
|
13 printf("setsockopt returned %d\n", ret);
|
giuliomoro@217
|
14 isSetPort=false;
|
giuliomoro@217
|
15 isSetServer=false;
|
giuliomoro@217
|
16 enabled=false;
|
giuliomoro@217
|
17 }
|
giuliomoro@217
|
18 UdpClient::UdpClient(int aPort, const char* aServerName){
|
giuliomoro@217
|
19 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
|
giuliomoro@217
|
20 if(outSocket<0){
|
giuliomoro@217
|
21 enabled=false;
|
giuliomoro@217
|
22 return;
|
giuliomoro@217
|
23 }
|
giuliomoro@217
|
24 setPort(aPort);
|
giuliomoro@217
|
25 setServer(aServerName);
|
giuliomoro@217
|
26 isSetPort=true;
|
giuliomoro@217
|
27 isSetServer=true;
|
giuliomoro@217
|
28 enabled=true;
|
giuliomoro@217
|
29 memset(&stTimeOut, 0, sizeof(struct timeval));
|
giuliomoro@217
|
30 }
|
giuliomoro@217
|
31 UdpClient::~UdpClient(){
|
giuliomoro@217
|
32 close(outSocket);
|
giuliomoro@217
|
33 }
|
giuliomoro@217
|
34 void UdpClient::setPort(int aPort){
|
giuliomoro@217
|
35 port=aPort;
|
giuliomoro@217
|
36 destinationServer.sin_port = htons(port);
|
giuliomoro@217
|
37 destinationServer.sin_family = AF_INET;
|
giuliomoro@217
|
38 isSetPort=true;
|
giuliomoro@217
|
39 if(isSetServer){
|
giuliomoro@217
|
40 enabled=true;
|
giuliomoro@217
|
41 }
|
giuliomoro@217
|
42 };
|
giuliomoro@217
|
43 void UdpClient::setServer(const char* aServerName){
|
giuliomoro@217
|
44 inet_pton(AF_INET,aServerName,&destinationServer.sin_addr);
|
giuliomoro@217
|
45 isSetServer=true;
|
giuliomoro@217
|
46 if(isSetPort){
|
giuliomoro@217
|
47 enabled=true;
|
giuliomoro@217
|
48 }
|
giuliomoro@217
|
49 };
|
giuliomoro@217
|
50 int UdpClient::send(void * message, int size){
|
giuliomoro@217
|
51 if(!enabled)
|
giuliomoro@217
|
52 return -1;
|
giuliomoro@217
|
53 unsigned int length;
|
giuliomoro@217
|
54 length=sizeof(struct sockaddr_in);
|
giuliomoro@217
|
55 int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length);
|
giuliomoro@217
|
56 if (n < 0){
|
giuliomoro@217
|
57 return n;
|
giuliomoro@217
|
58 }
|
giuliomoro@217
|
59 return 1;
|
giuliomoro@217
|
60 };
|
giuliomoro@217
|
61 int UdpClient::write(const char* remoteHostname, int remotePortNumber, void* sourceBuffer, int numBytesToWrite){
|
giuliomoro@217
|
62 setServer(remoteHostname);
|
giuliomoro@217
|
63 setPort(remotePortNumber);
|
giuliomoro@217
|
64 send(sourceBuffer, numBytesToWrite);
|
giuliomoro@217
|
65 }
|
giuliomoro@217
|
66 int UdpClient::waitUntilReady(bool readyForReading, int timeoutMsecs){
|
giuliomoro@217
|
67 // 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@217
|
68 if(enabled==false)
|
giuliomoro@217
|
69 return -1;
|
giuliomoro@217
|
70 if(timeoutMsecs<0)
|
giuliomoro@217
|
71 return select(outSocket+1, NULL, &stWriteFDS, NULL, NULL); //calling this with a NULL timeout will block indefinitely
|
giuliomoro@217
|
72 FD_ZERO(&stWriteFDS);
|
giuliomoro@217
|
73 FD_SET(outSocket, &stWriteFDS);
|
giuliomoro@217
|
74 float timeOutSecs=timeoutMsecs*0.001;
|
giuliomoro@217
|
75 stTimeOut.tv_sec=(int)timeOutSecs;
|
giuliomoro@217
|
76 timeOutSecs-=(int)timeOutSecs;
|
giuliomoro@217
|
77 stTimeOut.tv_usec=(int)(timeOutSecs*1000000);
|
giuliomoro@217
|
78 int descriptorReady= select(outSocket+1, NULL, &stWriteFDS, NULL, &stTimeOut);
|
giuliomoro@217
|
79 return descriptorReady>0? 1 : descriptorReady;
|
giuliomoro@267
|
80 }
|