00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00065
00066
00067
00068
00069
00070
00071 #ifdef WIN32
00072 #ifndef __MINGW__
00073
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
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
00122
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,
00135 UDT_SNDSYN,
00136 UDT_RCVSYN,
00137 UDT_CC,
00138 UDT_FC,
00139 UDT_SNDBUF,
00140 UDT_RCVBUF,
00141 UDT_LINGER,
00142 UDP_SNDBUF,
00143 UDP_RCVBUF,
00144 UDT_MAXMSG,
00145 UDT_MSGTTL,
00146 UDT_RENDEZVOUS,
00147 UDT_SNDTIMEO,
00148 UDT_RCVTIMEO,
00149 UDT_REUSEADDR,
00150 UDT_MAXBW,
00151 UDT_STATE,
00152 UDT_EVENT,
00153 UDT_SNDDATA,
00154 UDT_RCVDATA
00155 };
00156
00158
00159 struct CPerfMon
00160 {
00161
00162 int64_t msTimeStamp;
00163 int64_t pktSentTotal;
00164 int64_t pktRecvTotal;
00165 int pktSndLossTotal;
00166 int pktRcvLossTotal;
00167 int pktRetransTotal;
00168 int pktSentACKTotal;
00169 int pktRecvACKTotal;
00170 int pktSentNAKTotal;
00171 int pktRecvNAKTotal;
00172 int64_t usSndDurationTotal;
00173
00174
00175 int64_t pktSent;
00176 int64_t pktRecv;
00177 int pktSndLoss;
00178 int pktRcvLoss;
00179 int pktRetrans;
00180 int pktSentACK;
00181 int pktRecvACK;
00182 int pktSentNAK;
00183 int pktRecvNAK;
00184 double mbpsSendRate;
00185 double mbpsRecvRate;
00186 int64_t usSndDuration;
00187
00188
00189 double usPktSndPeriod;
00190 int pktFlowWindow;
00191 int pktCongestionWindow;
00192 int pktFlightSize;
00193 double msRTT;
00194 double mbpsBandwidth;
00195 int byteAvailSndBuf;
00196 int byteAvailRcvBuf;
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
00209
00210
00211
00212
00213
00214
00215 virtual const char* getErrorMessage();
00216
00217
00218
00219
00220
00221
00222
00223
00224 virtual int getErrorCode() const;
00225
00226
00227
00228
00229
00230
00231
00232
00233 virtual void clear();
00234
00235 private:
00236 int m_iMajor;
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 int m_iMinor;
00247 int m_iErrno;
00248 std::string m_strMsg;
00249
00250 std::string m_strAPI;
00251 std::string m_strDebug;
00252
00253 public:
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
00296
00297
00298
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
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
00340 UDT_API int epoll_update_usock(int eid, UDTSOCKET u, const int* events = NULL);
00341
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 }
00361
00362 #endif