annotate core/UdpClient.cpp @ 519:5406a719910b prerelease

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