udpraw.h
Go to the documentation of this file.
00001 /* This file is part of Udpraw - A library for UDP transmission.
00002  *
00003  * Copyright (C) 2011 Trevor Dodds <@gmail.com trev.dodds>
00004  *
00005  * Udpraw is free software: you can redistribute it and/or modify it under the
00006  * terms of the GNU General Public License as published by the Free Software
00007  * Foundation, either version 3 of the License, or (at your option) any later
00008  * version.
00009  * 
00010  * Udpraw is distributed in the hope that it will be useful, but WITHOUT ANY
00011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013  * details.
00014  * 
00015  * You should have received a copy of the GNU General Public License along with
00016  * Udpraw.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #ifndef UDPRAW_H
00020 #define UDPRAW_H
00021 
00022 #include "udpraw_api.h"
00023 #include <string>
00024 #include <vector>
00025 #ifdef _MSC_VER
00026   // link to ws2_32.lib, ws2_32.dll, see WSAStartup docs.
00027   #include <winsock2.h>
00028 #else
00029   #include <netinet/in.h>
00030 #endif
00031 
00039 class UDPRAW_API Udpraw {
00040   private:
00041 
00042   protected:
00043 
00044 #ifdef _MSC_VER
00045     WSADATA wsaData;          
00046     SOCKET recvSocket,        
00047            sendSocket;        
00048     bool initWSA;             
00049 #else
00050     int recvSocket, 
00051         sendSocket; 
00052 #endif
00053 
00054     bool createdRecv, 
00055          createdSend; 
00057     fd_set readSocks;                   
00058     fd_set writeSocks;                  
00059     fd_set exceptionSocks;              
00060     int numSocksReadable;               
00062     sockaddr_in recvAddr, 
00063                 sendAddr, 
00064                 fromAddr; 
00066     std::vector <std::string> sendIp;                 
00067     std::vector<int>::iterator recv_port_iterator;    
00068     std::vector <int> sendPort;                       
00070     // generate some basic stats
00071     int statSent;   
00072     int statRecvd;  
00074     int lastError;  
00075     std::string errorPrepend; 
00077     struct {
00078       float send;
00079       float recv;
00080     } simulateLoss; 
00082     void init(); 
00083     bool simulatePacketLoss(float prob); 
00086   public:
00087 
00089     enum UdprawError {
00090       UDPRAW_NO_ERROR,      
00091       UDPRAW_BIND,          
00092       UDPRAW_MSGSIZE,       
00093       UDPRAW_NOINIT,        
00094       UDPRAW_NOIP,          
00095       UDPRAW_NOPORT,        
00096       UDPRAW_RECV,          
00097       UDPRAW_SELECT,        
00098       UDPRAW_SEND,          
00099       UDPRAW_SOCKEXCEPTION, 
00100       UDPRAW_UNMATCHED      
00101     };
00102 
00103 
00104     Udpraw(); 
00105     ~Udpraw(); 
00108     int getError(); 
00109     std::string getErrorStr(); 
00110     std::string getErrorStr(int error); 
00111     void writeError(); 
00112     void writeError(std::string prependMsg); 
00113     void writeError(int error); 
00116     // Call either initReceiver or initSender.
00117     // For the receiver, you must specify a port, because it binds in the init.
00118     // For the sender, you can specify a port/ip straight away, or later with the
00119     // methods below, because it's only used when the data is actually sent.
00120     // The ip/ports for sending can be changed at any time, see below for details.
00121 
00122     int initReceiver(int port); 
00123     void initSender(int port = 0); 
00124     void initSender(const char* ip, int port = 0); 
00125     void initSender(std::vector <std::string> ip_list, int port = 0); 
00126     void initSender(std::vector <std::string> ip_list, std::vector <int> port_list); 
00129     // set/change IPs and ports
00130     // Can be either one IP and list of ports,
00131     // one port and list of IPs,
00132     // or equal size list of IPs and corresponding ports.
00133     // If they are not equal size then sendRaw will return an error.
00134 
00135     void setSendIp(const char* ip); 
00136     void setSendIp(std::vector <std::string> ip_list); 
00137     void setSendPort(int port); 
00138     void setSendPort(std::vector <int> port_list); 
00151     int doSelect();
00152 
00169     int recvRaw(char* buf, int len, bool select = true);
00170 
00185     int sendRaw(const char* buf, int len, bool select = true);
00186 
00187 
00188     int getStatSent(); 
00189     int getStatRecvd(); 
00190     void setStatSent(int bytes); 
00191     void setStatRecvd(int bytes); 
00194     void setSimulateLossSend(float probabilityofloss); 
00195     void setSimulateLossRecv(float probabilityofloss); 
00196     float getSimulateLossSend(); 
00197     float getSimulateLossRecv(); 
00200     int closeAndCleanup(); 
00202 };
00203 
00204 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


tk_vrqc2011
Author(s): Martin Riedel
autogenerated on Wed Apr 24 2013 11:26:17