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