Mercurial > hg > beaglert
comparison core/UdpClient.cpp @ 336:6599a9978ac4 prerelease
Refactored UdpClient, changed line endings to Unix
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Sun, 05 Jun 2016 20:22:55 +0100 |
parents | f14dc4ac8955 |
children |
comparison
equal
deleted
inserted
replaced
335:f14dc4ac8955 | 336:6599a9978ac4 |
---|---|
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 "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 int broadcastEnable = 1; | 11 setSocketBroadcast(1); |
12 int ret = setsockopt(outSocket, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable)); | |
13 isSetPort=false; | 12 isSetPort=false; |
14 isSetServer=false; | 13 isSetServer=false; |
15 enabled=false; | 14 enabled=false; |
16 } | 15 } |
17 UdpClient::UdpClient(int aPort, const char* aServerName){ | 16 UdpClient::UdpClient(int aPort, const char* aServerName){ |
18 outSocket=socket(AF_INET, SOCK_DGRAM, 0); | 17 outSocket=socket(AF_INET, SOCK_DGRAM, 0); |
19 if(outSocket<0){ | 18 if(outSocket<0){ |
20 enabled=false; | 19 enabled=false; |
21 return; | 20 return; |
22 } | 21 } |
22 setSocketBroadcast(1); | |
23 setPort(aPort); | 23 setPort(aPort); |
24 setServer(aServerName); | 24 setServer(aServerName); |
25 isSetPort=true; | 25 isSetPort=true; |
26 isSetServer=true; | 26 isSetServer=true; |
27 enabled=true; | 27 enabled=true; |
28 memset(&stTimeOut, 0, sizeof(struct timeval)); | 28 memset(&stTimeOut, 0, sizeof(struct timeval)); |
29 } | 29 } |
30 UdpClient::~UdpClient(){ | 30 UdpClient::~UdpClient(){ |
31 close(outSocket); | 31 close(outSocket); |
32 } | 32 } |
33 void UdpClient::setPort(int aPort){ | 33 void UdpClient::setPort(int aPort){ |
75 timeOutSecs-=(int)timeOutSecs; | 75 timeOutSecs-=(int)timeOutSecs; |
76 stTimeOut.tv_usec=(int)(timeOutSecs*1000000); | 76 stTimeOut.tv_usec=(int)(timeOutSecs*1000000); |
77 int descriptorReady= select(outSocket+1, NULL, &stWriteFDS, NULL, &stTimeOut); | 77 int descriptorReady= select(outSocket+1, NULL, &stWriteFDS, NULL, &stTimeOut); |
78 return descriptorReady>0? 1 : descriptorReady; | 78 return descriptorReady>0? 1 : descriptorReady; |
79 } | 79 } |
80 int UdpClient::setSocketBroadcast(int broadcastEnable){ | |
81 int ret = setsockopt(outSocket, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable)); | |
82 if(ret < 0){ | |
83 printf("Impossible to set the socket to Broadcast\n"); | |
84 } | |
85 return ret; | |
86 } |