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