annotate core/UdpClient.cpp @ 269:ac8eb07afcf5

Oxygen text added to each render.cpp file for the default projects. Text includes project explanation from Wiki, edited in places. Empty project added as a default project. Doxyfile updated. Each of the project locations added to INPUT configuration option. Consider just watching the whole project file so all new projects are automatically pulled through.
author Robert Jack <robert.h.jack@gmail.com>
date Tue, 17 May 2016 15:40:16 +0100
parents c42a6b4dc2d4
children 247a182adb6d
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@217 11 isSetPort=false;
giuliomoro@217 12 isSetServer=false;
giuliomoro@217 13 enabled=false;
giuliomoro@217 14 }
giuliomoro@217 15 UdpClient::UdpClient(int aPort, const char* aServerName){
giuliomoro@217 16 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
giuliomoro@217 17 if(outSocket<0){
giuliomoro@217 18 enabled=false;
giuliomoro@217 19 return;
giuliomoro@217 20 }
giuliomoro@217 21 setPort(aPort);
giuliomoro@217 22 setServer(aServerName);
giuliomoro@217 23 isSetPort=true;
giuliomoro@217 24 isSetServer=true;
giuliomoro@217 25 enabled=true;
giuliomoro@217 26 memset(&stTimeOut, 0, sizeof(struct timeval));
giuliomoro@217 27 }
giuliomoro@217 28 UdpClient::~UdpClient(){
giuliomoro@217 29 close(outSocket);
giuliomoro@217 30 }
giuliomoro@217 31 void UdpClient::setPort(int aPort){
giuliomoro@217 32 port=aPort;
giuliomoro@217 33 destinationServer.sin_port = htons(port);
giuliomoro@217 34 destinationServer.sin_family = AF_INET;
giuliomoro@217 35 isSetPort=true;
giuliomoro@217 36 if(isSetServer){
giuliomoro@217 37 enabled=true;
giuliomoro@217 38 }
giuliomoro@217 39 };
giuliomoro@217 40 void UdpClient::setServer(const char* aServerName){
giuliomoro@217 41 inet_pton(AF_INET,aServerName,&destinationServer.sin_addr);
giuliomoro@217 42 isSetServer=true;
giuliomoro@217 43 if(isSetPort){
giuliomoro@217 44 enabled=true;
giuliomoro@217 45 }
giuliomoro@217 46 };
giuliomoro@217 47 int UdpClient::send(void * message, int size){
giuliomoro@217 48 if(!enabled)
giuliomoro@217 49 return -1;
giuliomoro@217 50 unsigned int length;
giuliomoro@217 51 length=sizeof(struct sockaddr_in);
giuliomoro@217 52 int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length);
giuliomoro@217 53 if (n < 0){
giuliomoro@217 54 return n;
giuliomoro@217 55 }
giuliomoro@217 56 return 1;
giuliomoro@217 57 };
giuliomoro@217 58 int UdpClient::write(const char* remoteHostname, int remotePortNumber, void* sourceBuffer, int numBytesToWrite){
giuliomoro@217 59 setServer(remoteHostname);
giuliomoro@217 60 setPort(remotePortNumber);
giuliomoro@217 61 send(sourceBuffer, numBytesToWrite);
giuliomoro@217 62 }
giuliomoro@217 63 int UdpClient::waitUntilReady(bool readyForReading, int timeoutMsecs){
giuliomoro@217 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@217 65 if(enabled==false)
giuliomoro@217 66 return -1;
giuliomoro@217 67 if(timeoutMsecs<0)
giuliomoro@217 68 return select(outSocket+1, NULL, &stWriteFDS, NULL, NULL); //calling this with a NULL timeout will block indefinitely
giuliomoro@217 69 FD_ZERO(&stWriteFDS);
giuliomoro@217 70 FD_SET(outSocket, &stWriteFDS);
giuliomoro@217 71 float timeOutSecs=timeoutMsecs*0.001;
giuliomoro@217 72 stTimeOut.tv_sec=(int)timeOutSecs;
giuliomoro@217 73 timeOutSecs-=(int)timeOutSecs;
giuliomoro@217 74 stTimeOut.tv_usec=(int)(timeOutSecs*1000000);
giuliomoro@217 75 int descriptorReady= select(outSocket+1, NULL, &stWriteFDS, NULL, &stTimeOut);
giuliomoro@217 76 return descriptorReady>0? 1 : descriptorReady;
giuliomoro@217 77 }