annotate core/UdpServer.cpp @ 44:f5b5c648cd5d ultra-staging

- added (unused) simple c++ classes for udp datagrams\n- added tests for the new classes
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 20 May 2015 18:07:16 +0100
parents
children 6907e2177eb8
rev   line source
giuliomoro@44 1 /*
giuliomoro@44 2 * udpServer.cpp
giuliomoro@44 3 *
giuliomoro@44 4 * Created on: 19 May 2015
giuliomoro@44 5 * Author: giulio moro
giuliomoro@44 6 */
giuliomoro@44 7 #include "../include/UdpServer.h"
giuliomoro@44 8
giuliomoro@44 9 UdpServer::UdpServer(int aPort){
giuliomoro@44 10 init(aPort);
giuliomoro@44 11 };
giuliomoro@44 12 UdpServer::UdpServer(){
giuliomoro@44 13 init(0);
giuliomoro@44 14 }
giuliomoro@44 15 UdpServer::~UdpServer(){
giuliomoro@44 16
giuliomoro@44 17 };
giuliomoro@44 18 bool UdpServer::init(int aPort){
giuliomoro@44 19 enabled=true;
giuliomoro@44 20 stTimeOut.tv_sec = 0; //set timeout to 0
giuliomoro@44 21 stTimeOut.tv_usec = 0;
giuliomoro@44 22 inSocket=socket(AF_INET, SOCK_DGRAM, 0);
giuliomoro@44 23 if (inSocket < 0){
giuliomoro@44 24 enabled=false;
giuliomoro@44 25 }
giuliomoro@44 26 length = sizeof(server);
giuliomoro@44 27 server.sin_family=AF_INET;
giuliomoro@44 28 server.sin_addr.s_addr=INADDR_ANY;
giuliomoro@44 29 enabled=bindToPort(aPort);
giuliomoro@44 30 wasteBufferSize=2048;
giuliomoro@44 31 wasteBuffer=malloc(wasteBufferSize);
giuliomoro@44 32 return enabled;
giuliomoro@44 33 }
giuliomoro@44 34
giuliomoro@44 35 bool UdpServer::bindToPort(int aPort){
giuliomoro@44 36 port=aPort;
giuliomoro@44 37 server.sin_port=htons(port);
giuliomoro@44 38 if (bind(inSocket,(struct sockaddr *)&server,length)<0){
giuliomoro@44 39 enabled=false;
giuliomoro@44 40 return false;
giuliomoro@44 41 }
giuliomoro@44 42 return true;
giuliomoro@44 43 };
giuliomoro@44 44 int UdpServer::read(//Returns the number of bytes read, or -1 if there was an error.
giuliomoro@44 45 void *destBuffer,
giuliomoro@44 46 int maxBytesToRead){
giuliomoro@44 47 if(enabled==false)
giuliomoro@44 48 return -1;
giuliomoro@44 49 FD_ZERO(&stReadFDS);
giuliomoro@44 50 FD_SET(inSocket, &stReadFDS);
giuliomoro@44 51 int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stTimeOut);
giuliomoro@44 52 if(descriptorReady<0){ //an error occurred
giuliomoro@44 53 return -1;
giuliomoro@44 54 }
giuliomoro@44 55 int numberOfBytes=0;
giuliomoro@44 56 if (FD_ISSET(inSocket, &stReadFDS)) {
giuliomoro@44 57 numberOfBytes=recvfrom(inSocket,destBuffer,maxBytesToRead,0,(struct sockaddr *)&from,&fromLength);
giuliomoro@44 58 if(numberOfBytes<0)
giuliomoro@44 59 return -1;
giuliomoro@44 60 }
giuliomoro@44 61 return numberOfBytes;
giuliomoro@44 62 };
giuliomoro@44 63 int UdpServer::emptySocket(){
giuliomoro@44 64 return emptySocket(0);
giuliomoro@44 65 }
giuliomoro@44 66 int UdpServer::emptySocket(int maxBytes){//discards up to maxBytes from the socket. Returns the number of bytes discarded.
giuliomoro@44 67 if(wasteBuffer==NULL)
giuliomoro@44 68 return -1;
giuliomoro@44 69 int numberOfBytes=0;
giuliomoro@44 70 while(int n=read(wasteBuffer, wasteBufferSize)){// calls the read function until it does not return any more bytes (i.e.: socket is empty)
giuliomoro@44 71 if(n>0)
giuliomoro@44 72 numberOfBytes+=n;
giuliomoro@44 73 if(n<0)
giuliomoro@44 74 return -1;
giuliomoro@44 75 if(maxBytes>0 && numberOfBytes>=maxBytes)
giuliomoro@44 76 break;
giuliomoro@44 77 };
giuliomoro@44 78 return numberOfBytes;
giuliomoro@44 79 }
giuliomoro@44 80 void* UdpServer::getWaste(){ //returns the last datagram retrieved by emptySocket()
giuliomoro@44 81 return wasteBuffer;
giuliomoro@44 82 }
giuliomoro@44 83