giuliomoro@135: /* giuliomoro@135: * udpClient.cpp giuliomoro@135: * giuliomoro@135: * Created on: 19 May 2015 giuliomoro@135: * Author: giulio moro giuliomoro@135: */ giuliomoro@135: #include "UdpClient.h" giuliomoro@135: giuliomoro@135: UdpClient::UdpClient(){ giuliomoro@135: outSocket=socket(AF_INET, SOCK_DGRAM, 0); giuliomoro@135: isSetPort=false; giuliomoro@135: isSetServer=false; giuliomoro@135: enabled=false; giuliomoro@135: } giuliomoro@135: UdpClient::UdpClient(int aPort, const char* aServerName){ giuliomoro@135: outSocket=socket(AF_INET, SOCK_DGRAM, 0); giuliomoro@135: if(outSocket<0){ giuliomoro@135: enabled=false; giuliomoro@135: return; giuliomoro@135: } giuliomoro@135: setPort(aPort); giuliomoro@135: setServer(aServerName); giuliomoro@135: isSetPort=true; giuliomoro@135: isSetServer=true; giuliomoro@135: enabled=true; giuliomoro@135: memset(&stTimeOut, 0, sizeof(struct timeval)); giuliomoro@135: } giuliomoro@135: UdpClient::~UdpClient(){ giuliomoro@135: close(outSocket); giuliomoro@135: } giuliomoro@135: void UdpClient::setPort(int aPort){ giuliomoro@135: port=aPort; giuliomoro@135: destinationServer.sin_port = htons(port); giuliomoro@135: destinationServer.sin_family = AF_INET; giuliomoro@135: isSetPort=true; giuliomoro@135: if(isSetServer){ giuliomoro@135: enabled=true; giuliomoro@135: } giuliomoro@135: }; giuliomoro@135: void UdpClient::setServer(const char* aServerName){ giuliomoro@135: inet_pton(AF_INET,aServerName,&destinationServer.sin_addr); giuliomoro@135: isSetServer=true; giuliomoro@135: if(isSetPort){ giuliomoro@135: enabled=true; giuliomoro@135: } giuliomoro@135: }; giuliomoro@135: int UdpClient::send(void * message, int size){ giuliomoro@135: if(!enabled) giuliomoro@135: return -1; giuliomoro@135: unsigned int length; giuliomoro@135: length=sizeof(struct sockaddr_in); giuliomoro@135: int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length); giuliomoro@135: if (n < 0){ giuliomoro@135: return n; giuliomoro@135: } giuliomoro@135: return 1; giuliomoro@135: }; giuliomoro@135: int UdpClient::write(const char* remoteHostname, int remotePortNumber, void* sourceBuffer, int numBytesToWrite){ giuliomoro@135: setServer(remoteHostname); giuliomoro@135: setPort(remotePortNumber); giuliomoro@135: send(sourceBuffer, numBytesToWrite); giuliomoro@135: } giuliomoro@135: int UdpClient::waitUntilReady(bool readyForReading, int timeoutMsecs){ giuliomoro@135: // 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: if(enabled==false) giuliomoro@135: return -1; giuliomoro@135: if(timeoutMsecs<0) giuliomoro@135: return select(outSocket+1, NULL, &stWriteFDS, NULL, NULL); //calling this with a NULL timeout will block indefinitely giuliomoro@135: FD_ZERO(&stWriteFDS); giuliomoro@135: FD_SET(outSocket, &stWriteFDS); giuliomoro@135: float timeOutSecs=timeoutMsecs*0.001; giuliomoro@135: stTimeOut.tv_sec=(int)timeOutSecs; giuliomoro@135: timeOutSecs-=(int)timeOutSecs; giuliomoro@135: stTimeOut.tv_usec=(int)(timeOutSecs*1000000); giuliomoro@135: int descriptorReady= select(outSocket+1, NULL, &stWriteFDS, NULL, &stTimeOut); giuliomoro@135: return descriptorReady>0? 1 : descriptorReady; giuliomoro@135: }