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