| OLD | NEW |
| (Empty) |
| 1 /***************************************************************************** | |
| 2 Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois. | |
| 3 All rights reserved. | |
| 4 | |
| 5 Redistribution and use in source and binary forms, with or without | |
| 6 modification, are permitted provided that the following conditions are | |
| 7 met: | |
| 8 | |
| 9 * Redistributions of source code must retain the above | |
| 10 copyright notice, this list of conditions and the | |
| 11 following disclaimer. | |
| 12 | |
| 13 * Redistributions in binary form must reproduce the | |
| 14 above copyright notice, this list of conditions | |
| 15 and the following disclaimer in the documentation | |
| 16 and/or other materials provided with the distribution. | |
| 17 | |
| 18 * Neither the name of the University of Illinois | |
| 19 nor the names of its contributors may be used to | |
| 20 endorse or promote products derived from this | |
| 21 software without specific prior written permission. | |
| 22 | |
| 23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | |
| 24 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 25 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 26 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 31 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 34 *****************************************************************************/ | |
| 35 | |
| 36 /***************************************************************************** | |
| 37 written by | |
| 38 Yunhong Gu, last updated 01/18/2011 | |
| 39 *****************************************************************************/ | |
| 40 | |
| 41 #ifndef __UDT_H__ | |
| 42 #define __UDT_H__ | |
| 43 | |
| 44 | |
| 45 #ifndef WIN32 | |
| 46 #include <sys/types.h> | |
| 47 #include <sys/socket.h> | |
| 48 #include <netinet/in.h> | |
| 49 #else | |
| 50 #ifdef __MINGW__ | |
| 51 #include <stdint.h> | |
| 52 #include <ws2tcpip.h> | |
| 53 #endif | |
| 54 #include <windows.h> | |
| 55 #endif | |
| 56 #include <fstream> | |
| 57 #include <set> | |
| 58 #include <string> | |
| 59 #include <vector> | |
| 60 | |
| 61 | |
| 62 //////////////////////////////////////////////////////////////////////////////// | |
| 63 | |
| 64 //if compiling on VC6.0 or pre-WindowsXP systems | |
| 65 //use -DLEGACY_WIN32 | |
| 66 | |
| 67 //if compiling with MinGW, it only works on XP or above | |
| 68 //use -D_WIN32_WINNT=0x0501 | |
| 69 | |
| 70 | |
| 71 #ifdef WIN32 | |
| 72 #ifndef __MINGW__ | |
| 73 // Explicitly define 32-bit and 64-bit numbers | |
| 74 typedef __int32 int32_t; | |
| 75 typedef __int64 int64_t; | |
| 76 typedef unsigned __int32 uint32_t; | |
| 77 #ifndef LEGACY_WIN32 | |
| 78 typedef unsigned __int64 uint64_t; | |
| 79 #else | |
| 80 // VC 6.0 does not support unsigned __int64: may cause potential proble
ms. | |
| 81 typedef __int64 uint64_t; | |
| 82 #endif | |
| 83 | |
| 84 #ifdef UDT_EXPORTS | |
| 85 #define UDT_API __declspec(dllexport) | |
| 86 #else | |
| 87 #define UDT_API __declspec(dllimport) | |
| 88 #endif | |
| 89 #else | |
| 90 #define UDT_API | |
| 91 #endif | |
| 92 #else | |
| 93 #define UDT_API | |
| 94 #endif | |
| 95 | |
| 96 #define NO_BUSY_WAITING | |
| 97 | |
| 98 #ifdef WIN32 | |
| 99 #ifndef __MINGW__ | |
| 100 typedef SOCKET UDPSOCKET; | |
| 101 typedef SOCKET SYSSOCKET; | |
| 102 #else | |
| 103 typedef int UDPSOCKET; | |
| 104 typedef int SYSSOCKET; | |
| 105 #endif | |
| 106 #else | |
| 107 typedef int UDPSOCKET; | |
| 108 typedef int SYSSOCKET; | |
| 109 #endif | |
| 110 | |
| 111 typedef int UDTSOCKET; | |
| 112 | |
| 113 //////////////////////////////////////////////////////////////////////////////// | |
| 114 | |
| 115 typedef std::set<UDTSOCKET> ud_set; | |
| 116 #define UD_CLR(u, uset) ((uset)->erase(u)) | |
| 117 #define UD_ISSET(u, uset) ((uset)->find(u) != (uset)->end()) | |
| 118 #define UD_SET(u, uset) ((uset)->insert(u)) | |
| 119 #define UD_ZERO(uset) ((uset)->clear()) | |
| 120 | |
| 121 enum EPOLLOpt | |
| 122 { | |
| 123 // this values are defined same as linux epoll.h | |
| 124 // so that if system values are used by mistake, they should have the same ef
fect | |
| 125 UDT_EPOLL_IN = 0x1, | |
| 126 UDT_EPOLL_OUT = 0x4, | |
| 127 UDT_EPOLL_ERR = 0x8 | |
| 128 }; | |
| 129 | |
| 130 enum UDTSTATUS {INIT = 1, OPENED, LISTENING, CONNECTED, BROKEN, CLOSED, NONEXIST
}; | |
| 131 | |
| 132 //////////////////////////////////////////////////////////////////////////////// | |
| 133 | |
| 134 enum UDTOpt | |
| 135 { | |
| 136 UDT_MSS, // the Maximum Transfer Unit | |
| 137 UDT_SNDSYN, // if sending is blocking | |
| 138 UDT_RCVSYN, // if receiving is blocking | |
| 139 UDT_CC, // custom congestion control algorithm | |
| 140 UDT_FC, // Flight flag size (window size) | |
| 141 UDT_SNDBUF, // maximum buffer in sending queue | |
| 142 UDT_RCVBUF, // UDT receiving buffer size | |
| 143 UDT_LINGER, // waiting for unsent data when closing | |
| 144 UDP_SNDBUF, // UDP sending buffer size | |
| 145 UDP_RCVBUF, // UDP receiving buffer size | |
| 146 UDT_MAXMSG, // maximum datagram message size | |
| 147 UDT_MSGTTL, // time-to-live of a datagram message | |
| 148 UDT_RENDEZVOUS, // rendezvous connection mode | |
| 149 UDT_SNDTIMEO, // send() timeout | |
| 150 UDT_RCVTIMEO, // recv() timeout | |
| 151 UDT_REUSEADDR, // reuse an existing port or create a new one | |
| 152 UDT_MAXBW // maximum bandwidth (bytes per second) that the connect
ion can use | |
| 153 }; | |
| 154 | |
| 155 //////////////////////////////////////////////////////////////////////////////// | |
| 156 | |
| 157 struct CPerfMon | |
| 158 { | |
| 159 // global measurements | |
| 160 int64_t msTimeStamp; // time since the UDT entity is started,
in milliseconds | |
| 161 int64_t pktSentTotal; // total number of sent data packets, in
cluding retransmissions | |
| 162 int64_t pktRecvTotal; // total number of received packets | |
| 163 int pktSndLossTotal; // total number of lost packets (sender
side) | |
| 164 int pktRcvLossTotal; // total number of lost packets (receive
r side) | |
| 165 int pktRetransTotal; // total number of retransmitted packets | |
| 166 int pktSentACKTotal; // total number of sent ACK packets | |
| 167 int pktRecvACKTotal; // total number of received ACK packets | |
| 168 int pktSentNAKTotal; // total number of sent NAK packets | |
| 169 int pktRecvNAKTotal; // total number of received NAK packets | |
| 170 int64_t usSndDurationTotal; // total time duration when UDT is sendi
ng data (idle time exclusive) | |
| 171 | |
| 172 // local measurements | |
| 173 int64_t pktSent; // number of sent data packets, includin
g retransmissions | |
| 174 int64_t pktRecv; // number of received packets | |
| 175 int pktSndLoss; // number of lost packets (sender side) | |
| 176 int pktRcvLoss; // number of lost packets (receiver side
) | |
| 177 int pktRetrans; // number of retransmitted packets | |
| 178 int pktSentACK; // number of sent ACK packets | |
| 179 int pktRecvACK; // number of received ACK packets | |
| 180 int pktSentNAK; // number of sent NAK packets | |
| 181 int pktRecvNAK; // number of received NAK packets | |
| 182 double mbpsSendRate; // sending rate in Mb/s | |
| 183 double mbpsRecvRate; // receiving rate in Mb/s | |
| 184 int64_t usSndDuration; // busy sending time (i.e., idle time ex
clusive) | |
| 185 | |
| 186 // instant measurements | |
| 187 double usPktSndPeriod; // packet sending period, in microsecond
s | |
| 188 int pktFlowWindow; // flow window size, in number of packet
s | |
| 189 int pktCongestionWindow; // congestion window size, in number of
packets | |
| 190 int pktFlightSize; // number of packets on flight | |
| 191 double msRTT; // RTT, in milliseconds | |
| 192 double mbpsBandwidth; // estimated bandwidth, in Mb/s | |
| 193 int byteAvailSndBuf; // available UDT sender buffer size | |
| 194 int byteAvailRcvBuf; // available UDT receiver buffer size | |
| 195 }; | |
| 196 | |
| 197 //////////////////////////////////////////////////////////////////////////////// | |
| 198 | |
| 199 class UDT_API CUDTException | |
| 200 { | |
| 201 public: | |
| 202 CUDTException(int major = 0, int minor = 0, int err = -1); | |
| 203 CUDTException(const CUDTException& e); | |
| 204 virtual ~CUDTException(); | |
| 205 | |
| 206 // Functionality: | |
| 207 // Get the description of the exception. | |
| 208 // Parameters: | |
| 209 // None. | |
| 210 // Returned value: | |
| 211 // Text message for the exception description. | |
| 212 | |
| 213 virtual const char* getErrorMessage(); | |
| 214 | |
| 215 // Functionality: | |
| 216 // Get the system errno for the exception. | |
| 217 // Parameters: | |
| 218 // None. | |
| 219 // Returned value: | |
| 220 // errno. | |
| 221 | |
| 222 virtual int getErrorCode() const; | |
| 223 | |
| 224 // Functionality: | |
| 225 // Clear the error code. | |
| 226 // Parameters: | |
| 227 // None. | |
| 228 // Returned value: | |
| 229 // None. | |
| 230 | |
| 231 virtual void clear(); | |
| 232 | |
| 233 private: | |
| 234 int m_iMajor; // major exception categories | |
| 235 | |
| 236 // 0: correct condition | |
| 237 // 1: network setup exception | |
| 238 // 2: network connection broken | |
| 239 // 3: memory exception | |
| 240 // 4: file exception | |
| 241 // 5: method not supported | |
| 242 // 6+: undefined error | |
| 243 | |
| 244 int m_iMinor; // for specific error reasons | |
| 245 int m_iErrno; // errno returned by the system if there is any | |
| 246 std::string m_strMsg; // text error message | |
| 247 | |
| 248 std::string m_strAPI; // the name of UDT function that returns the err
or | |
| 249 std::string m_strDebug; // debug information, set to the original place
that causes the error | |
| 250 | |
| 251 public: // Error Code | |
| 252 static const int SUCCESS; | |
| 253 static const int ECONNSETUP; | |
| 254 static const int ENOSERVER; | |
| 255 static const int ECONNREJ; | |
| 256 static const int ESOCKFAIL; | |
| 257 static const int ESECFAIL; | |
| 258 static const int ECONNFAIL; | |
| 259 static const int ECONNLOST; | |
| 260 static const int ENOCONN; | |
| 261 static const int ERESOURCE; | |
| 262 static const int ETHREAD; | |
| 263 static const int ENOBUF; | |
| 264 static const int EFILE; | |
| 265 static const int EINVRDOFF; | |
| 266 static const int ERDPERM; | |
| 267 static const int EINVWROFF; | |
| 268 static const int EWRPERM; | |
| 269 static const int EINVOP; | |
| 270 static const int EBOUNDSOCK; | |
| 271 static const int ECONNSOCK; | |
| 272 static const int EINVPARAM; | |
| 273 static const int EINVSOCK; | |
| 274 static const int EUNBOUNDSOCK; | |
| 275 static const int ENOLISTEN; | |
| 276 static const int ERDVNOSERV; | |
| 277 static const int ERDVUNBOUND; | |
| 278 static const int ESTREAMILL; | |
| 279 static const int EDGRAMILL; | |
| 280 static const int EDUPLISTEN; | |
| 281 static const int ELARGEMSG; | |
| 282 static const int EINVPOLLID; | |
| 283 static const int EASYNCFAIL; | |
| 284 static const int EASYNCSND; | |
| 285 static const int EASYNCRCV; | |
| 286 static const int EPEERERR; | |
| 287 static const int EUNKNOWN; | |
| 288 }; | |
| 289 | |
| 290 //////////////////////////////////////////////////////////////////////////////// | |
| 291 | |
| 292 namespace UDT | |
| 293 { | |
| 294 typedef CUDTException ERRORINFO; | |
| 295 typedef UDTOpt SOCKOPT; | |
| 296 typedef CPerfMon TRACEINFO; | |
| 297 typedef ud_set UDSET; | |
| 298 | |
| 299 UDT_API extern const UDTSOCKET INVALID_SOCK; | |
| 300 #undef ERROR | |
| 301 UDT_API extern const int ERROR; | |
| 302 | |
| 303 UDT_API int startup(); | |
| 304 UDT_API int cleanup(); | |
| 305 UDT_API UDTSOCKET socket(int af, int type, int protocol); | |
| 306 UDT_API int bind(UDTSOCKET u, const struct sockaddr* name, int namelen); | |
| 307 UDT_API int bind(UDTSOCKET u, UDPSOCKET udpsock); | |
| 308 UDT_API int listen(UDTSOCKET u, int backlog); | |
| 309 UDT_API UDTSOCKET accept(UDTSOCKET u, struct sockaddr* addr, int* addrlen); | |
| 310 UDT_API int connect(UDTSOCKET u, const struct sockaddr* name, int namelen); | |
| 311 UDT_API int close(UDTSOCKET u); | |
| 312 UDT_API int getpeername(UDTSOCKET u, struct sockaddr* name, int* namelen); | |
| 313 UDT_API int getsockname(UDTSOCKET u, struct sockaddr* name, int* namelen); | |
| 314 UDT_API int getsockopt(UDTSOCKET u, int level, SOCKOPT optname, void* optval, in
t* optlen); | |
| 315 UDT_API int setsockopt(UDTSOCKET u, int level, SOCKOPT optname, const void* optv
al, int optlen); | |
| 316 UDT_API int send(UDTSOCKET u, const char* buf, int len, int flags); | |
| 317 UDT_API int recv(UDTSOCKET u, char* buf, int len, int flags); | |
| 318 UDT_API int sendmsg(UDTSOCKET u, const char* buf, int len, int ttl = -1, bool in
order = false); | |
| 319 UDT_API int recvmsg(UDTSOCKET u, char* buf, int len); | |
| 320 UDT_API int64_t sendfile(UDTSOCKET u, std::fstream& ifs, int64_t& offset, int64_
t size, int block = 364000); | |
| 321 UDT_API int64_t recvfile(UDTSOCKET u, std::fstream& ofs, int64_t& offset, int64_
t size, int block = 7280000); | |
| 322 UDT_API int select(int nfds, UDSET* readfds, UDSET* writefds, UDSET* exceptfds,
const struct timeval* timeout); | |
| 323 UDT_API int selectEx(const std::vector<UDTSOCKET>& fds, std::vector<UDTSOCKET>*
readfds, std::vector<UDTSOCKET>* writefds, std::vector<UDTSOCKET>* exceptfds, in
t64_t msTimeOut); | |
| 324 UDT_API int epoll_create(); | |
| 325 UDT_API int epoll_add_usock(const int eid, const UDTSOCKET u, const int* events
= NULL); | |
| 326 UDT_API int epoll_add_ssock(const int eid, const SYSSOCKET s, const int* events
= NULL); | |
| 327 UDT_API int epoll_remove_usock(const int eid, const UDTSOCKET u, const int* even
ts = NULL); | |
| 328 UDT_API int epoll_remove_ssock(const int eid, const SYSSOCKET s, const int* even
ts = NULL); | |
| 329 UDT_API int epoll_wait(const int eid, std::set<UDTSOCKET>* readfds, std::set<UDT
SOCKET>* writefds, int64_t msTimeOut, std::set<SYSSOCKET>* lrfds = NULL, std::se
t<SYSSOCKET>* wrfds = NULL); | |
| 330 UDT_API int epoll_release(const int eid); | |
| 331 UDT_API ERRORINFO& getlasterror(); | |
| 332 UDT_API int perfmon(UDTSOCKET u, TRACEINFO* perf, bool clear = true); | |
| 333 UDT_API UDTSTATUS getsockstate(UDTSOCKET u); | |
| 334 } | |
| 335 | |
| 336 #endif | |
| OLD | NEW |