udt.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 (THIS 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. /* Obsolete way to define MINGW */
  74. #ifndef __MINGW__
  75. #if defined(__MINGW32__) || defined(__MINGW64__)
  76. #define __MINGW__ 1
  77. #endif
  78. #endif
  79. #ifdef __cplusplus
  80. #include <fstream>
  81. #include <set>
  82. #include <string>
  83. #include <vector>
  84. #endif
  85. ////////////////////////////////////////////////////////////////////////////////
  86. //if compiling on VC6.0 or pre-WindowsXP systems
  87. //use -DLEGACY_WIN32
  88. //if compiling with MinGW, it only works on XP or above
  89. //use -D_WIN32_WINNT=0x0501
  90. ////////////////////////////////////////////////////////////////////////////////
  91. struct CPerfMon
  92. {
  93. // global measurements
  94. int64_t msTimeStamp; // time since the UDT entity is started, in milliseconds
  95. int64_t pktSentTotal; // total number of sent data packets, including retransmissions
  96. int64_t pktRecvTotal; // total number of received packets
  97. int pktSndLossTotal; // total number of lost packets (sender side)
  98. int pktRcvLossTotal; // total number of lost packets (receiver side)
  99. int pktRetransTotal; // total number of retransmitted packets
  100. int pktRcvRetransTotal; // total number of retransmitted packets received
  101. int pktSentACKTotal; // total number of sent ACK packets
  102. int pktRecvACKTotal; // total number of received ACK packets
  103. int pktSentNAKTotal; // total number of sent NAK packets
  104. int pktRecvNAKTotal; // total number of received NAK packets
  105. int64_t usSndDurationTotal; // total time duration when UDT is sending data (idle time exclusive)
  106. // local measurements
  107. int64_t pktSent; // number of sent data packets, including retransmissions
  108. int64_t pktRecv; // number of received packets
  109. int pktSndLoss; // number of lost packets (sender side)
  110. int pktRcvLoss; // number of lost packets (receiver side)
  111. int pktRetrans; // number of retransmitted packets
  112. int pktRcvRetrans; // number of retransmitted packets received
  113. int pktSentACK; // number of sent ACK packets
  114. int pktRecvACK; // number of received ACK packets
  115. int pktSentNAK; // number of sent NAK packets
  116. int pktRecvNAK; // number of received NAK packets
  117. double mbpsSendRate; // sending rate in Mb/s
  118. double mbpsRecvRate; // receiving rate in Mb/s
  119. int64_t usSndDuration; // busy sending time (i.e., idle time exclusive)
  120. int pktReorderDistance; // size of order discrepancy in received sequences
  121. double pktRcvAvgBelatedTime; // average time of packet delay for belated packets (packets with sequence past the ACK)
  122. int64_t pktRcvBelated; // number of received AND IGNORED packets due to having come too late
  123. // instant measurements
  124. double usPktSndPeriod; // packet sending period, in microseconds
  125. int pktFlowWindow; // flow window size, in number of packets
  126. int pktCongestionWindow; // congestion window size, in number of packets
  127. int pktFlightSize; // number of packets on flight
  128. double msRTT; // RTT, in milliseconds
  129. double mbpsBandwidth; // estimated bandwidth, in Mb/s
  130. int byteAvailSndBuf; // available UDT sender buffer size
  131. int byteAvailRcvBuf; // available UDT receiver buffer size
  132. };
  133. typedef SRTSOCKET UDTSOCKET; //legacy alias
  134. #ifdef __cplusplus
  135. class CUDTException;
  136. namespace UDT
  137. {
  138. typedef CUDTException ERRORINFO;
  139. typedef CPerfMon TRACEINFO;
  140. // This facility is used only for select() function.
  141. // This is considered obsolete and the epoll() functionality rather should be used.
  142. typedef std::set<SRTSOCKET> UDSET;
  143. #define UD_CLR(u, uset) ((uset)->erase(u))
  144. #define UD_ISSET(u, uset) ((uset)->find(u) != (uset)->end())
  145. #define UD_SET(u, uset) ((uset)->insert(u))
  146. #define UD_ZERO(uset) ((uset)->clear())
  147. SRT_API extern const SRTSOCKET INVALID_SOCK;
  148. #undef ERROR
  149. SRT_API extern const int ERROR;
  150. SRT_API int startup();
  151. SRT_API int cleanup();
  152. SRT_API SRTSOCKET socket();
  153. inline SRTSOCKET socket(int , int , int ) { return socket(); }
  154. SRT_API int bind(SRTSOCKET u, const struct sockaddr* name, int namelen);
  155. SRT_API int bind2(SRTSOCKET u, UDPSOCKET udpsock);
  156. SRT_API int listen(SRTSOCKET u, int backlog);
  157. SRT_API SRTSOCKET accept(SRTSOCKET u, struct sockaddr* addr, int* addrlen);
  158. SRT_API int connect(SRTSOCKET u, const struct sockaddr* name, int namelen);
  159. SRT_API int close(SRTSOCKET u);
  160. SRT_API int getpeername(SRTSOCKET u, struct sockaddr* name, int* namelen);
  161. SRT_API int getsockname(SRTSOCKET u, struct sockaddr* name, int* namelen);
  162. SRT_API int getsockopt(SRTSOCKET u, int level, SRT_SOCKOPT optname, void* optval, int* optlen);
  163. SRT_API int setsockopt(SRTSOCKET u, int level, SRT_SOCKOPT optname, const void* optval, int optlen);
  164. SRT_API int send(SRTSOCKET u, const char* buf, int len, int flags);
  165. SRT_API int recv(SRTSOCKET u, char* buf, int len, int flags);
  166. SRT_API int sendmsg(SRTSOCKET u, const char* buf, int len, int ttl = -1, bool inorder = false, int64_t srctime = 0);
  167. SRT_API int recvmsg(SRTSOCKET u, char* buf, int len, uint64_t& srctime);
  168. SRT_API int recvmsg(SRTSOCKET u, char* buf, int len);
  169. SRT_API int64_t sendfile(SRTSOCKET u, std::fstream& ifs, int64_t& offset, int64_t size, int block = 364000);
  170. SRT_API int64_t recvfile(SRTSOCKET u, std::fstream& ofs, int64_t& offset, int64_t size, int block = 7280000);
  171. SRT_API int64_t sendfile2(SRTSOCKET u, const char* path, int64_t* offset, int64_t size, int block = 364000);
  172. SRT_API int64_t recvfile2(SRTSOCKET u, const char* path, int64_t* offset, int64_t size, int block = 7280000);
  173. // select and selectEX are DEPRECATED; please use epoll.
  174. SRT_API int select(int nfds, UDSET* readfds, UDSET* writefds, UDSET* exceptfds, const struct timeval* timeout);
  175. SRT_API int selectEx(const std::vector<SRTSOCKET>& fds, std::vector<SRTSOCKET>* readfds,
  176. std::vector<SRTSOCKET>* writefds, std::vector<SRTSOCKET>* exceptfds, int64_t msTimeOut);
  177. SRT_API int epoll_create();
  178. SRT_API int epoll_add_usock(int eid, SRTSOCKET u, const int* events = NULL);
  179. SRT_API int epoll_add_ssock(int eid, SYSSOCKET s, const int* events = NULL);
  180. SRT_API int epoll_remove_usock(int eid, SRTSOCKET u);
  181. SRT_API int epoll_remove_ssock(int eid, SYSSOCKET s);
  182. SRT_API int epoll_update_usock(int eid, SRTSOCKET u, const int* events = NULL);
  183. SRT_API int epoll_update_ssock(int eid, SYSSOCKET s, const int* events = NULL);
  184. SRT_API int epoll_wait(int eid, std::set<SRTSOCKET>* readfds, std::set<SRTSOCKET>* writefds, int64_t msTimeOut,
  185. std::set<SYSSOCKET>* lrfds = NULL, std::set<SYSSOCKET>* wrfds = NULL);
  186. SRT_API int epoll_wait2(int eid, SRTSOCKET* readfds, int* rnum, SRTSOCKET* writefds, int* wnum, int64_t msTimeOut,
  187. SYSSOCKET* lrfds = NULL, int* lrnum = NULL, SYSSOCKET* lwfds = NULL, int* lwnum = NULL);
  188. SRT_API int epoll_uwait(const int eid, SRT_EPOLL_EVENT* fdsSet, int fdsSize, int64_t msTimeOut);
  189. SRT_API int epoll_release(int eid);
  190. SRT_API ERRORINFO& getlasterror();
  191. SRT_API int getlasterror_code();
  192. SRT_API const char* getlasterror_desc();
  193. SRT_API int bstats(SRTSOCKET u, SRT_TRACEBSTATS* perf, bool clear = true);
  194. SRT_API SRT_SOCKSTATUS getsockstate(SRTSOCKET u);
  195. } // namespace UDT
  196. // This is a log configuration used inside SRT.
  197. // Applications using SRT, if they want to use the logging mechanism
  198. // are free to create their own logger configuration objects for their
  199. // own logger FA objects, or create their own. The object of this type
  200. // is required to initialize the logger FA object.
  201. namespace srt_logging { struct LogConfig; }
  202. SRT_API extern srt_logging::LogConfig srt_logger_config;
  203. namespace srt
  204. {
  205. // This is a C++ SRT API extension. This is not a part of legacy UDT API.
  206. SRT_API void setloglevel(srt_logging::LogLevel::type ll);
  207. SRT_API void addlogfa(srt_logging::LogFA fa);
  208. SRT_API void dellogfa(srt_logging::LogFA fa);
  209. SRT_API void resetlogfa(std::set<srt_logging::LogFA> fas);
  210. SRT_API void resetlogfa(const int* fara, size_t fara_size);
  211. SRT_API void setlogstream(std::ostream& stream);
  212. SRT_API void setloghandler(void* opaque, SRT_LOG_HANDLER_FN* handler);
  213. SRT_API void setlogflags(int flags);
  214. SRT_API bool setstreamid(SRTSOCKET u, const std::string& sid);
  215. SRT_API std::string getstreamid(SRTSOCKET u);
  216. // Namespace alias
  217. namespace logging {
  218. using namespace srt_logging;
  219. }
  220. }
  221. // Planned deprecated removal: rel1.6.0
  222. // There's also no portable way possible to enforce a deprecation
  223. // compiler warning, so leaving as is.
  224. namespace UDT
  225. {
  226. // Backward-compatible aliases, just for a case someone was using it.
  227. using srt::setloglevel;
  228. using srt::addlogfa;
  229. using srt::dellogfa;
  230. using srt::resetlogfa;
  231. using srt::setlogstream;
  232. using srt::setloghandler;
  233. using srt::setlogflags;
  234. using srt::setstreamid;
  235. using srt::getstreamid;
  236. }
  237. #endif /* __cplusplus */
  238. #endif