udt.h

00001 /*****************************************************************************
00002 Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are
00007 met:
00008 
00009 * Redistributions of source code must retain the above
00010   copyright notice, this list of conditions and the
00011   following disclaimer.
00012 
00013 * Redistributions in binary form must reproduce the
00014   above copyright notice, this list of conditions
00015   and the following disclaimer in the documentation
00016   and/or other materials provided with the distribution.
00017 
00018 * Neither the name of the University of Illinois
00019   nor the names of its contributors may be used to
00020   endorse or promote products derived from this
00021   software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00024 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00025 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00026 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00027 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00028 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00029 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00030 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00031 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00032 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 *****************************************************************************/
00035 
00036 /*****************************************************************************
00037 written by
00038    Yunhong Gu, last updated 01/18/2011
00039 *****************************************************************************/
00040 
00041 #ifndef __UDT_H__
00042 #define __UDT_H__
00043 
00044 
00045 #ifndef WIN32
00046    #include <sys/types.h>
00047    #include <sys/socket.h>
00048    #include <netinet/in.h>
00049 #else
00050    #ifdef __MINGW__
00051       #include <stdint.h>
00052       #include <ws2tcpip.h>
00053    #endif
00054    #include <windows.h>
00055 #endif
00056 #include <fstream>
00057 #include <set>
00058 #include <string>
00059 #include <vector>
00060 
00061 
00063 
00064 //if compiling on VC6.0 or pre-WindowsXP systems
00065 //use -DLEGACY_WIN32
00066 
00067 //if compiling with MinGW, it only works on XP or above
00068 //use -D_WIN32_WINNT=0x0501
00069 
00070 
00071 #ifdef WIN32
00072    #ifndef __MINGW__
00073       // Explicitly define 32-bit and 64-bit numbers
00074       typedef __int32 int32_t;
00075       typedef __int64 int64_t;
00076       typedef unsigned __int32 uint32_t;
00077       #ifndef LEGACY_WIN32
00078          typedef unsigned __int64 uint64_t;
00079       #else
00080          // VC 6.0 does not support unsigned __int64: may cause potential problems.
00081          typedef __int64 uint64_t;
00082       #endif
00083 
00084       #ifdef UDT_EXPORTS
00085          #define UDT_API __declspec(dllexport)
00086       #else
00087          #define UDT_API __declspec(dllimport)
00088       #endif
00089    #else
00090       #define UDT_API
00091    #endif
00092 #else
00093    #define UDT_API __attribute__ ((visibility("default")))
00094 #endif
00095 
00096 #define NO_BUSY_WAITING
00097 
00098 #ifdef WIN32
00099    #ifndef __MINGW__
00100       typedef SOCKET SYSSOCKET;
00101    #else
00102       typedef int SYSSOCKET;
00103    #endif
00104 #else
00105    typedef int SYSSOCKET;
00106 #endif
00107 
00108 typedef SYSSOCKET UDPSOCKET;
00109 typedef int UDTSOCKET;
00110 
00112 
00113 typedef std::set<UDTSOCKET> ud_set;
00114 #define UD_CLR(u, uset) ((uset)->erase(u))
00115 #define UD_ISSET(u, uset) ((uset)->find(u) != (uset)->end())
00116 #define UD_SET(u, uset) ((uset)->insert(u))
00117 #define UD_ZERO(uset) ((uset)->clear())
00118 
00119 enum EPOLLOpt
00120 {
00121    // this values are defined same as linux epoll.h
00122    // so that if system values are used by mistake, they should have the same effect
00123    UDT_EPOLL_IN = 0x1,
00124    UDT_EPOLL_OUT = 0x4,
00125    UDT_EPOLL_ERR = 0x8
00126 };
00127 
00128 enum UDTSTATUS {INIT = 1, OPENED, LISTENING, CONNECTING, CONNECTED, BROKEN, CLOSING, CLOSED, NONEXIST};
00129 
00131 
00132 enum UDTOpt
00133 {
00134    UDT_MSS,             // the Maximum Transfer Unit
00135    UDT_SNDSYN,          // if sending is blocking
00136    UDT_RCVSYN,          // if receiving is blocking
00137    UDT_CC,              // custom congestion control algorithm
00138    UDT_FC,              // Flight flag size (window size)
00139    UDT_SNDBUF,          // maximum buffer in sending queue
00140    UDT_RCVBUF,          // UDT receiving buffer size
00141    UDT_LINGER,          // waiting for unsent data when closing
00142    UDP_SNDBUF,          // UDP sending buffer size
00143    UDP_RCVBUF,          // UDP receiving buffer size
00144    UDT_MAXMSG,          // maximum datagram message size
00145    UDT_MSGTTL,          // time-to-live of a datagram message
00146    UDT_RENDEZVOUS,      // rendezvous connection mode
00147    UDT_SNDTIMEO,        // send() timeout
00148    UDT_RCVTIMEO,        // recv() timeout
00149    UDT_REUSEADDR,       // reuse an existing port or create a new one
00150    UDT_MAXBW,           // maximum bandwidth (bytes per second) that the connection can use
00151    UDT_STATE,           // current socket state, see UDTSTATUS, read only
00152    UDT_EVENT,           // current avalable events associated with the socket
00153    UDT_SNDDATA,         // size of data in the sending buffer
00154    UDT_RCVDATA          // size of data available for recv
00155 };
00156 
00158 
00159 struct CPerfMon
00160 {
00161    // global measurements
00162    int64_t msTimeStamp;                 // time since the UDT entity is started, in milliseconds
00163    int64_t pktSentTotal;                // total number of sent data packets, including retransmissions
00164    int64_t pktRecvTotal;                // total number of received packets
00165    int pktSndLossTotal;                 // total number of lost packets (sender side)
00166    int pktRcvLossTotal;                 // total number of lost packets (receiver side)
00167    int pktRetransTotal;                 // total number of retransmitted packets
00168    int pktSentACKTotal;                 // total number of sent ACK packets
00169    int pktRecvACKTotal;                 // total number of received ACK packets
00170    int pktSentNAKTotal;                 // total number of sent NAK packets
00171    int pktRecvNAKTotal;                 // total number of received NAK packets
00172    int64_t usSndDurationTotal;          // total time duration when UDT is sending data (idle time exclusive)
00173 
00174    // local measurements
00175    int64_t pktSent;                     // number of sent data packets, including retransmissions
00176    int64_t pktRecv;                     // number of received packets
00177    int pktSndLoss;                      // number of lost packets (sender side)
00178    int pktRcvLoss;                      // number of lost packets (receiver side)
00179    int pktRetrans;                      // number of retransmitted packets
00180    int pktSentACK;                      // number of sent ACK packets
00181    int pktRecvACK;                      // number of received ACK packets
00182    int pktSentNAK;                      // number of sent NAK packets
00183    int pktRecvNAK;                      // number of received NAK packets
00184    double mbpsSendRate;                 // sending rate in Mb/s
00185    double mbpsRecvRate;                 // receiving rate in Mb/s
00186    int64_t usSndDuration;               // busy sending time (i.e., idle time exclusive)
00187 
00188    // instant measurements
00189    double usPktSndPeriod;               // packet sending period, in microseconds
00190    int pktFlowWindow;                   // flow window size, in number of packets
00191    int pktCongestionWindow;             // congestion window size, in number of packets
00192    int pktFlightSize;                   // number of packets on flight
00193    double msRTT;                        // RTT, in milliseconds
00194    double mbpsBandwidth;                // estimated bandwidth, in Mb/s
00195    int byteAvailSndBuf;                 // available UDT sender buffer size
00196    int byteAvailRcvBuf;                 // available UDT receiver buffer size
00197 };
00198 
00200 
00201 class UDT_API CUDTException
00202 {
00203 public:
00204    CUDTException(int major = 0, int minor = 0, int err = -1);
00205    CUDTException(const CUDTException& e);
00206    virtual ~CUDTException();
00207 
00208       // Functionality:
00209       //    Get the description of the exception.
00210       // Parameters:
00211       //    None.
00212       // Returned value:
00213       //    Text message for the exception description.
00214 
00215    virtual const char* getErrorMessage();
00216 
00217       // Functionality:
00218       //    Get the system errno for the exception.
00219       // Parameters:
00220       //    None.
00221       // Returned value:
00222       //    errno.
00223 
00224    virtual int getErrorCode() const;
00225 
00226       // Functionality:
00227       //    Clear the error code.
00228       // Parameters:
00229       //    None.
00230       // Returned value:
00231       //    None.
00232 
00233    virtual void clear();
00234 
00235 private:
00236    int m_iMajor;        // major exception categories
00237 
00238 // 0: correct condition
00239 // 1: network setup exception
00240 // 2: network connection broken
00241 // 3: memory exception
00242 // 4: file exception
00243 // 5: method not supported
00244 // 6+: undefined error
00245 
00246    int m_iMinor;                // for specific error reasons
00247    int m_iErrno;                // errno returned by the system if there is any
00248    std::string m_strMsg;        // text error message
00249 
00250    std::string m_strAPI;        // the name of UDT function that returns the error
00251    std::string m_strDebug;      // debug information, set to the original place that causes the error
00252 
00253 public: // Error Code
00254    static const int SUCCESS;
00255    static const int ECONNSETUP;
00256    static const int ENOSERVER;
00257    static const int ECONNREJ;
00258    static const int ESOCKFAIL;
00259    static const int ESECFAIL;
00260    static const int ECONNFAIL;
00261    static const int ECONNLOST;
00262    static const int ENOCONN;
00263    static const int ERESOURCE;
00264    static const int ETHREAD;
00265    static const int ENOBUF;
00266    static const int EFILE;
00267    static const int EINVRDOFF;
00268    static const int ERDPERM;
00269    static const int EINVWROFF;
00270    static const int EWRPERM;
00271    static const int EINVOP;
00272    static const int EBOUNDSOCK;
00273    static const int ECONNSOCK;
00274    static const int EINVPARAM;
00275    static const int EINVSOCK;
00276    static const int EUNBOUNDSOCK;
00277    static const int ENOLISTEN;
00278    static const int ERDVNOSERV;
00279    static const int ERDVUNBOUND;
00280    static const int ESTREAMILL;
00281    static const int EDGRAMILL;
00282    static const int EDUPLISTEN;
00283    static const int ELARGEMSG;
00284    static const int EINVPOLLID;
00285    static const int EASYNCFAIL;
00286    static const int EASYNCSND;
00287    static const int EASYNCRCV;
00288    static const int ETIMEOUT;
00289    static const int EPEERERR;
00290    static const int EUNKNOWN;
00291 };
00292 
00294 
00295 // If you need to export these APIs to be used by a different language,
00296 // declare extern "C" for them, and add a "udt_" prefix to each API.
00297 // The following APIs: sendfile(), recvfile(), epoll_wait(), geterrormsg(),
00298 // include C++ specific feature, please use the corresponding sendfile2(), etc.
00299 
00300 namespace UDT
00301 {
00302 
00303 typedef CUDTException ERRORINFO;
00304 typedef UDTOpt SOCKOPT;
00305 typedef CPerfMon TRACEINFO;
00306 typedef ud_set UDSET;
00307 
00308 UDT_API extern const UDTSOCKET INVALID_SOCK;
00309 #undef ERROR
00310 UDT_API extern const int ERROR;
00311 
00312 UDT_API int startup();
00313 UDT_API int cleanup();
00314 UDT_API UDTSOCKET socket(int af, int type, int protocol);
00315 UDT_API int bind(UDTSOCKET u, const struct sockaddr* name, int namelen);
00316 UDT_API int bind2(UDTSOCKET u, UDPSOCKET udpsock);
00317 UDT_API int listen(UDTSOCKET u, int backlog);
00318 UDT_API UDTSOCKET accept(UDTSOCKET u, struct sockaddr* addr, int* addrlen);
00319 UDT_API int connect(UDTSOCKET u, const struct sockaddr* name, int namelen);
00320 UDT_API int close(UDTSOCKET u);
00321 UDT_API int getpeername(UDTSOCKET u, struct sockaddr* name, int* namelen);
00322 UDT_API int getsockname(UDTSOCKET u, struct sockaddr* name, int* namelen);
00323 UDT_API int getsockopt(UDTSOCKET u, int level, SOCKOPT optname, void* optval, int* optlen);
00324 UDT_API int setsockopt(UDTSOCKET u, int level, SOCKOPT optname, const void* optval, int optlen);
00325 UDT_API int send(UDTSOCKET u, const char* buf, int len, int flags);
00326 UDT_API int recv(UDTSOCKET u, char* buf, int len, int flags);
00327 UDT_API int sendmsg(UDTSOCKET u, const char* buf, int len, int ttl = -1, bool inorder = false);
00328 UDT_API int recvmsg(UDTSOCKET u, char* buf, int len);
00329 UDT_API int64_t sendfile(UDTSOCKET u, std::fstream& ifs, int64_t& offset, int64_t size, int block = 364000);
00330 UDT_API int64_t recvfile(UDTSOCKET u, std::fstream& ofs, int64_t& offset, int64_t size, int block = 7280000);
00331 UDT_API int64_t sendfile2(UDTSOCKET u, const char* path, int64_t* offset, int64_t size, int block = 364000);
00332 UDT_API int64_t recvfile2(UDTSOCKET u, const char* path, int64_t* offset, int64_t size, int block = 7280000);
00333 
00334 // select and selectEX are DEPRECATED; please use epoll. 
00335 UDT_API int select(int nfds, UDSET* readfds, UDSET* writefds, UDSET* exceptfds, const struct timeval* timeout);
00336 UDT_API int selectEx(const std::vector<UDTSOCKET>& fds, std::vector<UDTSOCKET>* readfds,
00337                      std::vector<UDTSOCKET>* writefds, std::vector<UDTSOCKET>* exceptfds, int64_t msTimeOut);
00338 
00339 // BARCHART
00340 UDT_API int epoll_update_usock(int eid, UDTSOCKET u, const int* events = NULL);
00341 // BARCHART
00342 UDT_API int epoll_verify_usock(int eid, UDTSOCKET u, int* events);
00343 
00344 UDT_API int epoll_create();
00345 UDT_API int epoll_add_usock(int eid, UDTSOCKET u, const int* events = NULL);
00346 UDT_API int epoll_add_ssock(int eid, SYSSOCKET s, const int* events = NULL);
00347 UDT_API int epoll_remove_usock(int eid, UDTSOCKET u);
00348 UDT_API int epoll_remove_ssock(int eid, SYSSOCKET s);
00349 UDT_API int epoll_wait(int eid, std::set<UDTSOCKET>* readfds, std::set<UDTSOCKET>* writefds, int64_t msTimeOut,
00350                        std::set<SYSSOCKET>* lrfds = NULL, std::set<SYSSOCKET>* wrfds = NULL);
00351 UDT_API int epoll_wait2(int eid, UDTSOCKET* readfds, int* rnum, UDTSOCKET* writefds, int* wnum, int64_t msTimeOut,
00352                         SYSSOCKET* lrfds = NULL, int* lrnum = NULL, SYSSOCKET* lwfds = NULL, int* lwnum = NULL);
00353 UDT_API int epoll_release(int eid);
00354 UDT_API ERRORINFO& getlasterror();
00355 UDT_API int getlasterror_code();
00356 UDT_API const char* getlasterror_desc();
00357 UDT_API int perfmon(UDTSOCKET u, TRACEINFO* perf, bool clear = true);
00358 UDT_API UDTSTATUS getsockstate(UDTSOCKET u);
00359 
00360 }  // namespace UDT
00361 
00362 #endif

Generated on 9 Feb 2013 for barchart-udt-core-2.2.2 by  doxygen 1.6.1