udt.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * SRT - Secure, Reliable, Transport
  3. * Copyright (c) 2018 Haivision Systems Inc.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. */
  10. /*****************************************************************************
  11. Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
  12. All rights reserved.
  13. Redistribution and use in source and binary forms, with or without
  14. modification, are permitted provided that the following conditions are
  15. met:
  16. * Redistributions of source code must retain the above
  17. copyright notice, this list of conditions and the
  18. following disclaimer.
  19. * Redistributions in binary form must reproduce the
  20. above copyright notice, this list of conditions
  21. and the following disclaimer in the documentation
  22. and/or other materials provided with the distribution.
  23. * Neither the name of the University of Illinois
  24. nor the names of its contributors may be used to
  25. endorse or promote products derived from this
  26. software without specific prior written permission.
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *****************************************************************************/
  39. /*****************************************************************************
  40. written by
  41. Yunhong Gu, last updated 01/18/2011
  42. modified by
  43. Haivision Systems Inc.
  44. *****************************************************************************/
  45. /* WARNING!!!
  46. * Since now this file is a "C and C++ header".
  47. * It should be then able to be interpreted by C compiler, so
  48. * all C++-oriented things must be ifdef'd-out by __cplusplus.
  49. *
  50. * Mind also comments - to prevent any portability problems,
  51. * B/C++ comments (// -> EOL) should not be used unless the
  52. * area is under __cplusplus condition already.
  53. *
  54. * NOTE: this file contains _STRUCTURES_ that are common to C and C++,
  55. * plus some functions and other functionalities ONLY FOR C++. This
  56. * file doesn't contain _FUNCTIONS_ predicted to be used in C - see udtc.h
  57. */
  58. #ifndef INC_SRT_UDT_H
  59. #define INC_SRT_UDT_H
  60. #include "srt.h"
  61. /*
  62. * SRT_ENABLE_THREADCHECK IS SET IN MAKEFILE, NOT HERE
  63. */
  64. #if defined(SRT_ENABLE_THREADCHECK)
  65. #include "threadcheck.h"
  66. #else
  67. #define THREAD_STATE_INIT(name)
  68. #define THREAD_EXIT()
  69. #define THREAD_PAUSED()
  70. #define THREAD_RESUMED()
  71. #define INCREMENT_THREAD_ITERATIONS()
  72. #endif
  73. #ifdef __cplusplus
  74. #include <fstream>
  75. #include <set>
  76. #include <string>
  77. #include <vector>
  78. #endif
  79. ////////////////////////////////////////////////////////////////////////////////
  80. //if compiling on VC6.0 or pre-WindowsXP systems
  81. //use -DLEGACY_WIN32
  82. //if compiling with MinGW, it only works on XP or above
  83. //use -D_WIN32_WINNT=0x0501
  84. ////////////////////////////////////////////////////////////////////////////////
  85. struct CPerfMon
  86. {
  87. // global measurements
  88. int64_t msTimeStamp; // time since the UDT entity is started, in milliseconds
  89. int64_t pktSentTotal; // total number of sent data packets, including retransmissions
  90. int64_t pktRecvTotal; // total number of received packets
  91. int pktSndLossTotal; // total number of lost packets (sender side)
  92. int pktRcvLossTotal; // total number of lost packets (receiver side)
  93. int pktRetransTotal; // total number of retransmitted packets
  94. int pktRcvRetransTotal; // total number of retransmitted packets received
  95. int pktSentACKTotal; // total number of sent ACK packets
  96. int pktRecvACKTotal; // total number of received ACK packets
  97. int pktSentNAKTotal; // total number of sent NAK packets
  98. int pktRecvNAKTotal; // total number of received NAK packets
  99. int64_t usSndDurationTotal; // total time duration when UDT is sending data (idle time exclusive)
  100. // local measurements
  101. int64_t pktSent; // number of sent data packets, including retransmissions
  102. int64_t pktRecv; // number of received packets
  103. int pktSndLoss; // number of lost packets (sender side)
  104. int pktRcvLoss; // number of lost packets (receiver side)
  105. int pktRetrans; // number of retransmitted packets
  106. int pktRcvRetrans; // number of retransmitted packets received
  107. int pktSentACK; // number of sent ACK packets
  108. int pktRecvACK; // number of received ACK packets
  109. int pktSentNAK; // number of sent NAK packets
  110. int pktRecvNAK; // number of received NAK packets
  111. double mbpsSendRate; // sending rate in Mb/s
  112. double mbpsRecvRate; // receiving rate in Mb/s
  113. int64_t usSndDuration; // busy sending time (i.e., idle time exclusive)
  114. int pktReorderDistance; // size of order discrepancy in received sequences
  115. double pktRcvAvgBelatedTime; // average time of packet delay for belated packets (packets with sequence past the ACK)
  116. int64_t pktRcvBelated; // number of received AND IGNORED packets due to having come too late
  117. // instant measurements
  118. double usPktSndPeriod; // packet sending period, in microseconds
  119. int pktFlowWindow; // flow window size, in number of packets
  120. int pktCongestionWindow; // congestion window size, in number of packets
  121. int pktFlightSize; // number of packets on flight
  122. double msRTT; // RTT, in milliseconds
  123. double mbpsBandwidth; // estimated bandwidth, in Mb/s
  124. int byteAvailSndBuf; // available UDT sender buffer size
  125. int byteAvailRcvBuf; // available UDT receiver buffer size
  126. };
  127. typedef SRTSOCKET UDTSOCKET; //legacy alias
  128. #ifdef __cplusplus
  129. namespace srt { class CUDTException; }
  130. namespace UDT
  131. {
  132. typedef srt::CUDTException ERRORINFO;
  133. typedef CPerfMon TRACEINFO;
  134. // This facility is used only for select() function.
  135. // This is considered obsolete and the epoll() functionality rather should be used.
  136. typedef std::set<SRTSOCKET> UDSET;
  137. #define UD_CLR(u, uset) ((uset)->erase(u))
  138. #define UD_ISSET(u, uset) ((uset)->find(u) != (uset)->end())
  139. #define UD_SET(u, uset) ((uset)->insert(u))
  140. #define UD_ZERO(uset) ((uset)->clear())
  141. SRT_API extern const SRTSOCKET INVALID_SOCK;
  142. #undef ERROR
  143. SRT_API extern const int ERROR;
  144. SRT_API int startup();
  145. SRT_API int cleanup();
  146. SRT_API SRTSOCKET socket();
  147. inline SRTSOCKET socket(int , int , int ) { return socket(); }
  148. SRT_API int bind(SRTSOCKET u, const struct sockaddr* name, int namelen);
  149. SRT_API int bind2(SRTSOCKET u, UDPSOCKET udpsock);
  150. SRT_API int listen(SRTSOCKET u, int backlog);
  151. SRT_API SRTSOCKET accept(SRTSOCKET u, struct sockaddr* addr, int* addrlen);
  152. SRT_API int connect(SRTSOCKET u, const struct sockaddr* name, int namelen);
  153. SRT_API int close(SRTSOCKET u);
  154. SRT_API int getpeername(SRTSOCKET u, struct sockaddr* name, int* namelen);
  155. SRT_API int getsockname(SRTSOCKET u, struct sockaddr* name, int* namelen);
  156. SRT_API int getsockopt(SRTSOCKET u, int level, SRT_SOCKOPT optname, void* optval, int* optlen);
  157. SRT_API int setsockopt(SRTSOCKET u, int level, SRT_SOCKOPT optname, const void* optval, int optlen);
  158. SRT_API int send(SRTSOCKET u, const char* buf, int len, int flags);
  159. SRT_API int recv(SRTSOCKET u, char* buf, int len, int flags);
  160. SRT_API int sendmsg(SRTSOCKET u, const char* buf, int len, int ttl = -1, bool inorder = false, int64_t srctime = 0);
  161. SRT_API int recvmsg(SRTSOCKET u, char* buf, int len, uint64_t& srctime);
  162. SRT_API int recvmsg(SRTSOCKET u, char* buf, int len);
  163. SRT_API int64_t sendfile(SRTSOCKET u, std::fstream& ifs, int64_t& offset, int64_t size, int block = 364000);
  164. SRT_API int64_t recvfile(SRTSOCKET u, std::fstream& ofs, int64_t& offset, int64_t size, int block = 7280000);
  165. SRT_API int64_t sendfile2(SRTSOCKET u, const char* path, int64_t* offset, int64_t size, int block = 364000);
  166. SRT_API int64_t recvfile2(SRTSOCKET u, const char* path, int64_t* offset, int64_t size, int block = 7280000);
  167. // select and selectEX are DEPRECATED; please use epoll.
  168. SRT_API int select(int nfds, UDSET* readfds, UDSET* writefds, UDSET* exceptfds, const struct timeval* timeout);
  169. SRT_API int selectEx(const std::vector<SRTSOCKET>& fds, std::vector<SRTSOCKET>* readfds,
  170. std::vector<SRTSOCKET>* writefds, std::vector<SRTSOCKET>* exceptfds, int64_t msTimeOut);
  171. SRT_API int epoll_create();
  172. SRT_API int epoll_add_usock(int eid, SRTSOCKET u, const int* events = NULL);
  173. SRT_API int epoll_add_ssock(int eid, SYSSOCKET s, const int* events = NULL);
  174. SRT_API int epoll_remove_usock(int eid, SRTSOCKET u);
  175. SRT_API int epoll_remove_ssock(int eid, SYSSOCKET s);
  176. SRT_API int epoll_update_usock(int eid, SRTSOCKET u, const int* events = NULL);
  177. SRT_API int epoll_update_ssock(int eid, SYSSOCKET s, const int* events = NULL);
  178. SRT_API int epoll_wait(int eid, std::set<SRTSOCKET>* readfds, std::set<SRTSOCKET>* writefds, int64_t msTimeOut,
  179. std::set<SYSSOCKET>* lrfds = NULL, std::set<SYSSOCKET>* wrfds = NULL);
  180. SRT_API int epoll_wait2(int eid, SRTSOCKET* readfds, int* rnum, SRTSOCKET* writefds, int* wnum, int64_t msTimeOut,
  181. SYSSOCKET* lrfds = NULL, int* lrnum = NULL, SYSSOCKET* lwfds = NULL, int* lwnum = NULL);
  182. SRT_API int epoll_uwait(const int eid, SRT_EPOLL_EVENT* fdsSet, int fdsSize, int64_t msTimeOut);
  183. SRT_API int epoll_release(int eid);
  184. SRT_API ERRORINFO& getlasterror();
  185. SRT_API int getlasterror_code();
  186. SRT_API const char* getlasterror_desc();
  187. SRT_API int bstats(SRTSOCKET u, SRT_TRACEBSTATS* perf, bool clear = true);
  188. SRT_API SRT_SOCKSTATUS getsockstate(SRTSOCKET u);
  189. } // namespace UDT
  190. // This is a log configuration used inside SRT.
  191. // Applications using SRT, if they want to use the logging mechanism
  192. // are free to create their own logger configuration objects for their
  193. // own logger FA objects, or create their own. The object of this type
  194. // is required to initialize the logger FA object.
  195. namespace srt_logging { struct LogConfig; }
  196. SRT_API extern srt_logging::LogConfig srt_logger_config;
  197. namespace srt
  198. {
  199. // This is a C++ SRT API extension. This is not a part of legacy UDT API.
  200. SRT_API void setloglevel(srt_logging::LogLevel::type ll);
  201. SRT_API void addlogfa(srt_logging::LogFA fa);
  202. SRT_API void dellogfa(srt_logging::LogFA fa);
  203. SRT_API void resetlogfa(std::set<srt_logging::LogFA> fas);
  204. SRT_API void resetlogfa(const int* fara, size_t fara_size);
  205. SRT_API void setlogstream(std::ostream& stream);
  206. SRT_API void setloghandler(void* opaque, SRT_LOG_HANDLER_FN* handler);
  207. SRT_API void setlogflags(int flags);
  208. SRT_API bool setstreamid(SRTSOCKET u, const std::string& sid);
  209. SRT_API std::string getstreamid(SRTSOCKET u);
  210. // Namespace alias
  211. namespace logging {
  212. using namespace srt_logging;
  213. }
  214. } // namespace srt
  215. // Planned deprecated removal: rel1.6.0
  216. // There's also no portable way possible to enforce a deprecation
  217. // compiler warning, so leaving as is.
  218. namespace UDT
  219. {
  220. // Backward-compatible aliases, just for a case someone was using it.
  221. using srt::setloglevel;
  222. using srt::addlogfa;
  223. using srt::dellogfa;
  224. using srt::resetlogfa;
  225. using srt::setlogstream;
  226. using srt::setloghandler;
  227. using srt::setlogflags;
  228. using srt::setstreamid;
  229. using srt::getstreamid;
  230. }
  231. #endif /* __cplusplus */
  232. #endif