annotate core/UdpClient.cpp @ 310:02c4ca0e3718 prerelease

Renamed pinModeFrame -> pinMode
author andrewm
date Fri, 27 May 2016 18:29:20 +0100
parents a14fe8b0f588
children f14dc4ac8955
rev   line source
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@217 13 isSetPort=false;
giuliomoro@217 14 isSetServer=false;
giuliomoro@217 15 enabled=false;
giuliomoro@217 16 }
giuliomoro@217 17 UdpClient::UdpClient(int aPort, const char* aServerName){
giuliomoro@217 18 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
giuliomoro@217 19 if(outSocket<0){
giuliomoro@217 20 enabled=false;
giuliomoro@217 21 return;
giuliomoro@217 22 }
giuliomoro@217 23 setPort(aPort);
giuliomoro@217 24 setServer(aServerName);
giuliomoro@217 25 isSetPort=true;
giuliomoro@217 26 isSetServer=true;
giuliomoro@217 27 enabled=true;
giuliomoro@217 28 memset(&stTimeOut, 0, sizeof(struct timeval));
giuliomoro@217 29 }
giuliomoro@217 30 UdpClient::~UdpClient(){
giuliomoro@217 31 close(outSocket);
giuliomoro@217 32 }
giuliomoro@217 33 void UdpClient::setPort(int aPort){
giuliomoro@217 34 port=aPort;
giuliomoro@217 35 destinationServer.sin_port = htons(port);
giuliomoro@217 36 destinationServer.sin_family = AF_INET;
giuliomoro@217 37 isSetPort=true;
giuliomoro@217 38 if(isSetServer){
giuliomoro@217 39 enabled=true;
giuliomoro@217 40 }
giuliomoro@217 41 };
giuliomoro@217 42 void UdpClient::setServer(const char* aServerName){
giuliomoro@217 43 inet_pton(AF_INET,aServerName,&destinationServer.sin_addr);
giuliomoro@217 44 isSetServer=true;
giuliomoro@217 45 if(isSetPort){
giuliomoro@217 46 enabled=true;
giuliomoro@217 47 }
giuliomoro@217 48 };
giuliomoro@217 49 int UdpClient::send(void * message, int size){
giuliomoro@217 50 if(!enabled)
giuliomoro@217 51 return -1;
giuliomoro@217 52 unsigned int length;
giuliomoro@217 53 length=sizeof(struct sockaddr_in);
giuliomoro@217 54 int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length);
giuliomoro@217 55 if (n < 0){
giuliomoro@217 56 return n;
giuliomoro@217 57 }
giuliomoro@217 58 return 1;
giuliomoro@217 59 };
giuliomoro@217 60 int UdpClient::write(const char* remoteHostname, int remotePortNumber, void* sourceBuffer, int numBytesToWrite){
giuliomoro@217 61 setServer(remoteHostname);
giuliomoro@217 62 setPort(remotePortNumber);
giuliomoro@217 63 send(sourceBuffer, numBytesToWrite);
giuliomoro@217 64 }
giuliomoro@217 65 int UdpClient::waitUntilReady(bool readyForReading, int timeoutMsecs){
giuliomoro@217 66 // 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 67 if(enabled==false)
giuliomoro@217 68 return -1;
giuliomoro@217 69 if(timeoutMsecs<0)
giuliomoro@217 70 return select(outSocket+1, NULL, &stWriteFDS, NULL, NULL); //calling this with a NULL timeout will block indefinitely
giuliomoro@217 71 FD_ZERO(&stWriteFDS);
giuliomoro@217 72 FD_SET(outSocket, &stWriteFDS);
giuliomoro@217 73 float timeOutSecs=timeoutMsecs*0.001;
giuliomoro@217 74 stTimeOut.tv_sec=(int)timeOutSecs;
giuliomoro@217 75 timeOutSecs-=(int)timeOutSecs;
giuliomoro@217 76 stTimeOut.tv_usec=(int)(timeOutSecs*1000000);
giuliomoro@217 77 int descriptorReady= select(outSocket+1, NULL, &stWriteFDS, NULL, &stTimeOut);
giuliomoro@217 78 return descriptorReady>0? 1 : descriptorReady;
giuliomoro@267 79 }