diff --git a/logging/hvu_threadname.h b/logging/hvu_threadname.h index 6d6ba8453..624155b07 100644 --- a/logging/hvu_threadname.h +++ b/logging/hvu_threadname.h @@ -32,9 +32,13 @@ written by // MINGW-W64(4.0.6) // If not available, this facility will try to get the thread ID -// using C++11 facilities. For C++03 you have to provide: -// - HVU_EXT_INCLUDE_THREAD (For C++11: ) -// - HVU_EXT_THIS_THREAD (For C++11: std::this_thread) +// using C++11 facilities. + +// If you want to compile it in C++03 mode, then you have to provide a +// replacement for `hvu_sync.h` file and cover all HVU_EXT_* macros, +// then supply the path to this file in HVU_EXT_INCLUDE_SYNC macro +// through the command line: +// $(CXX) ... -DHVU_EXT_INCLUDE_SYNC="your_sync_imp.h" #if defined(HAVE_PTHREAD_GETNAME_NP_IN_PTHREAD_NP_H) \ || defined(HAVE_PTHREAD_SETNAME_NP_IN_PTHREAD_NP_H) @@ -153,7 +157,7 @@ class ThreadName if (tid == pthread_self()) set(old_name); } - + private: ThreadNameImpl(ThreadNameImpl& other); ThreadNameImpl& operator=(const ThreadNameImpl& other); diff --git a/logging/logging.h b/logging/logging.h index 75d0f0876..fca239a95 100644 --- a/logging/logging.h +++ b/logging/logging.h @@ -64,8 +64,10 @@ written by #include #include +#include #include #include +#include #include #include @@ -427,6 +429,16 @@ class LogConfig return -1; } + std::map name_map() const + { + std::map output; + for (size_t i = 0; i < names.size(); ++i) + { + output[names[i]] = i; + } + return output; + } + // Setters void set_handler(void* opaque, HVU_LOG_HANDLER_FN* fn) { diff --git a/logging/windows_syslog.h b/logging/windows_syslog.h index 7d8312641..3788b0fb4 100644 --- a/logging/windows_syslog.h +++ b/logging/windows_syslog.h @@ -1,5 +1,5 @@ -#ifndef INC_SRT_WINDOWS_SYSLOG_DEFS_H -#define INC_SRT_WINDOWS_SYSLOG_DEFS_H +#ifndef INC_HVU_WINDOWS_SYSLOG_H +#define INC_HVU_WINDOWS_SYSLOG_H #define LOG_EMERG 0 #define LOG_ALERT 1 diff --git a/srtcore/common.h b/srtcore/common.h index 4ebb659d8..8fc1074ed 100644 --- a/srtcore/common.h +++ b/srtcore/common.h @@ -71,6 +71,8 @@ modified by #include "netinet_any.h" #include "packetfilter_api.h" +#include "common_debug.h" // reexports common_types + // System-independent errno #ifndef _WIN32 #define NET_ERROR errno @@ -99,13 +101,6 @@ namespace srt class CUDTUnited; class CUDTSocket; -enum ErrorHandling -{ - ERH_RETURN, - ERH_THROW, - ERH_ABORT -}; - #if HAVE_FULL_CXX11 #define SRT_STATIC_ASSERT(cond, msg) static_assert(cond, msg) #else @@ -239,11 +234,6 @@ struct SocketKeeper -std::string SockStatusStr(SRT_SOCKSTATUS s); -std::string MemberStatusStr(SRT_MEMBERSTATUS s); -std::string SrtCmdName(int cmd); - - // Class CUDTException exposed for C++ API. // This is actually useless, unless you'd use a DIRECT C++ API, // however there's no such API so far. The current C++ API for UDT/SRT @@ -333,115 +323,9 @@ class CUDTException: public std::exception }; - -enum UDTSockType -{ - UDT_UNDEFINED = 0, // initial trap representation - UDT_STREAM = 1, - UDT_DGRAM -}; - - -/// The message types used by UDT protocol. This is a part of UDT -/// protocol and should never be changed. -enum UDTMessageType -{ - UMSG_HANDSHAKE = 0, //< Connection Handshake. Control: see @a CHandShake. - UMSG_KEEPALIVE = 1, //< Keep-alive. - UMSG_ACK = 2, //< Acknowledgement. Control: past-the-end sequence number up to which packets have been received. - UMSG_LOSSREPORT = 3, //< Negative Acknowledgement (NAK). Control: Loss list. - UMSG_CGWARNING = 4, //< Congestion warning. - UMSG_SHUTDOWN = 5, //< Shutdown. - UMSG_ACKACK = 6, //< Acknowledgement of Acknowledgement. Add info: The ACK sequence number - UMSG_DROPREQ = 7, //< Message Drop Request. Add info: Message ID. Control Info: (first, last) number of the message. - UMSG_PEERERROR = 8, //< Signal from the Peer side. Add info: Error code. - // ... add extra code types here - UMSG_END_OF_TYPES, - UMSG_EXT = 0x7FFF //< For the use of user-defined control packets. -}; - -// This side's role is: INITIATOR prepares the environment first, and sends -// appropriate information to the peer. The peer must be RESPONDER and be ready -// to receive it. It's important for the encryption: the INITIATOR side generates -// the KM, and sends it to RESPONDER. RESPONDER awaits KM received from the -// INITIATOR. The INITIATOR creates both sending and receiving contexts (each one -// with its dedicated key), then sends the key to RESPONDER, which creates both -// sending and receiving contexts, using the key received from INITIATOR. -// -// The method of selection: -// -// 1. For caller-listener configuration: caller is INITIATOR, listener is -// RESPONDER. -// -// 2. For rendezvous the parties are equivalent, so the role is resolved by -// "cookie contest". Rendezvous sockets both know each other's cookie generated -// during the URQ_WAVEAHAND handshake phase. The cookies are simply compared -// as integer numbers; the party which's cookie is a greater number becomes an -// INITIATOR, and the other party becomes a RESPONDER. -// -// The case of a draw - that both occasionally have baked identical cookies - -// is treated as an extremely rare and virtually impossible case, so this -// results in connection rejected. -enum HandshakeSide -{ - HSD_DRAW, - HSD_INITIATOR, //< Side that initiates HSREQ/KMREQ. HSv4: data sender, HSv5: connecting socket or winner rendezvous socket - HSD_RESPONDER //< Side that expects HSREQ/KMREQ from the peer. HSv4: data receiver, HSv5: accepted socket or loser rendezvous socket -}; - -// For debug -std::string MessageTypeStr(UDTMessageType mt, uint32_t extt = 0); - -//////////////////////////////////////////////////////////////////////////////// - -// Commonly used by various reading facilities -enum EReadStatus -{ - RST_OK = 0, //< A new portion of data has been received - RST_AGAIN, //< Nothing has been received, try again - RST_ERROR = -1 //< Irrecoverable error, please close descriptor and stop reading. -}; - -enum EConnectStatus -{ - CONN_ACCEPT = 0, //< Received final handshake that confirms connection established - CONN_REJECT = -1, //< Error during processing handshake. - CONN_CONTINUE = 1, //< induction->conclusion phase - CONN_RENDEZVOUS = 2, //< pass to a separate rendezvous processing (HSv5 only) - CONN_CONFUSED = 3, //< listener thinks it's connected, but caller missed conclusion - CONN_RUNNING = 10, //< no connection in progress, already connected - CONN_AGAIN = -2 //< No data was read, don't change any state. -}; - -enum EConnectMethod -{ - COM_ASYNCHRO, - COM_SYNCHRO -}; - -std::string ConnectStatusStr(EConnectStatus est); - - const int64_t BW_INFINITE = 1000000000/8; //Infinite=> 1 Gbps -enum ETransmissionEvent -{ - TEV_INIT, // --> After creation, and after any parameters were updated. - TEV_ACK, // --> When handling UMSG_ACK - older CCC:onAck() - TEV_ACKACK, // --> UDT does only RTT sync, can be read from CUDT::avgRTT(). - TEV_LOSSREPORT, // --> When handling UMSG_LOSSREPORT - older CCC::onLoss() - TEV_CHECKTIMER, // --> See TEV_CHT_REXMIT - TEV_SEND, // --> When the packet is scheduled for sending - older CCC::onPktSent - TEV_RECEIVE, // --> When a data packet was received - older CCC::onPktReceived - TEV_CUSTOM, // --> probably dead call - older CCC::processCustomMsg - TEV_SYNC, // --> Backup group. When rate estimation is derived from an active member, and update is needed. - - TEV_E_SIZE -}; - -std::string TransmissionEventStr(ETransmissionEvent ev); - // Special parameter for TEV_CHECKTIMER enum ECheckTimerStage { diff --git a/srtcore/common_debug.h b/srtcore/common_debug.h new file mode 100644 index 000000000..b6bc82a63 --- /dev/null +++ b/srtcore/common_debug.h @@ -0,0 +1,69 @@ +/* + * SRT - Secure, Reliable, Transport + * Copyright (c) 2018 Haivision Systems Inc. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +/***************************************************************************** +Copyright (c) 2001 - 2009, The Board of Trustees of the University of Illinois. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above + copyright notice, this list of conditions and the + following disclaimer. + +* Redistributions in binary form must reproduce the + above copyright notice, this list of conditions + and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the University of Illinois + nor the names of its contributors may be used to + endorse or promote products derived from this + software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*****************************************************************************/ + +/***************************************************************************** +written by + Haivision Systems Inc. +*****************************************************************************/ + +#ifndef INC_SRT_COMMON_DEBUG_H +#define INC_SRT_COMMON_DEBUG_H + +#include "srt.h" +#include "common_types.h" + +namespace srt +{ + +std::string SockStatusStr(SRT_SOCKSTATUS s); +std::string MemberStatusStr(SRT_MEMBERSTATUS s); +std::string SrtCmdName(int cmd); +std::string MessageTypeStr(UDTMessageType mt, uint32_t extt = 0); +std::string ConnectStatusStr(EConnectStatus est); +std::string TransmissionEventStr(ETransmissionEvent ev); + + +} +#endif diff --git a/srtcore/common_types.h b/srtcore/common_types.h new file mode 100644 index 000000000..53e81d7b6 --- /dev/null +++ b/srtcore/common_types.h @@ -0,0 +1,162 @@ +/* + * SRT - Secure, Reliable, Transport + * Copyright (c) 2018 Haivision Systems Inc. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +/***************************************************************************** +Copyright (c) 2001 - 2009, The Board of Trustees of the University of Illinois. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above + copyright notice, this list of conditions and the + following disclaimer. + +* Redistributions in binary form must reproduce the + above copyright notice, this list of conditions + and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the University of Illinois + nor the names of its contributors may be used to + endorse or promote products derived from this + software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*****************************************************************************/ + +/***************************************************************************** +written by + Haivision Systems Inc. +*****************************************************************************/ + +#ifndef INC_SRT_COMMON_TYPES_H +#define INC_SRT_COMMON_TYPES_H + +namespace srt +{ + +enum ErrorHandling +{ + ERH_RETURN, + ERH_THROW, + ERH_ABORT +}; + +// This enum is deprecated, only used to recognize the old version +// of the handshake pre 1.3.0 version. +enum UDTSockType +{ + UDT_UNDEFINED = 0, // initial trap representation + UDT_STREAM = 1, + UDT_DGRAM = 2 +}; + + +/// The message types used by UDT protocol. This is a part of UDT +/// protocol and should never be changed. +enum UDTMessageType +{ + UMSG_HANDSHAKE = 0, //< Connection Handshake. Control: see @a CHandShake. + UMSG_KEEPALIVE = 1, //< Keep-alive. + UMSG_ACK = 2, //< Acknowledgement. Control: past-the-end sequence number up to which packets have been received. + UMSG_LOSSREPORT = 3, //< Negative Acknowledgement (NAK). Control: Loss list. + UMSG_CGWARNING = 4, //< Congestion warning. + UMSG_SHUTDOWN = 5, //< Shutdown. + UMSG_ACKACK = 6, //< Acknowledgement of Acknowledgement. Add info: The ACK sequence number + UMSG_DROPREQ = 7, //< Message Drop Request. Add info: Message ID. Control Info: (first, last) number of the message. + UMSG_PEERERROR = 8, //< Signal from the Peer side. Add info: Error code. + // ... add extra code types here + UMSG_END_OF_TYPES, + UMSG_EXT = 0x7FFF //< For the use of user-defined control packets. +}; + +// This side's role is: INITIATOR prepares the environment first, and sends +// appropriate information to the peer. The peer must be RESPONDER and be ready +// to receive it. It's important for the encryption: the INITIATOR side generates +// the KM, and sends it to RESPONDER. RESPONDER awaits KM received from the +// INITIATOR. The INITIATOR creates both sending and receiving contexts (each one +// with its dedicated key), then sends the key to RESPONDER, which creates both +// sending and receiving contexts, using the key received from INITIATOR. +// +// The method of selection: +// +// 1. For caller-listener configuration: caller is INITIATOR, listener is +// RESPONDER. +// +// 2. For rendezvous the parties are equivalent, so the role is resolved by +// "cookie contest". Rendezvous sockets both know each other's cookie generated +// during the URQ_WAVEAHAND handshake phase. The cookies are simply compared +// as integer numbers; the party which's cookie is a greater number becomes an +// INITIATOR, and the other party becomes a RESPONDER. +// +// The case of a draw - that both occasionally have baked identical cookies - +// is treated as an extremely rare and virtually impossible case, so this +// results in connection rejected. +enum HandshakeSide +{ + HSD_DRAW, + HSD_INITIATOR, //< Side that initiates HSREQ/KMREQ. HSv4: data sender, HSv5: connecting socket or winner rendezvous socket + HSD_RESPONDER //< Side that expects HSREQ/KMREQ from the peer. HSv4: data receiver, HSv5: accepted socket or loser rendezvous socket +}; + +// Commonly used by various reading facilities +enum EReadStatus +{ + RST_OK = 0, //< A new portion of data has been received + RST_AGAIN, //< Nothing has been received, try again + RST_ERROR = -1 //< Irrecoverable error, please close descriptor and stop reading. +}; + +enum EConnectStatus +{ + CONN_ACCEPT = 0, //< Received final handshake that confirms connection established + CONN_REJECT = -1, //< Error during processing handshake. + CONN_CONTINUE = 1, //< induction->conclusion phase + CONN_RENDEZVOUS = 2, //< pass to a separate rendezvous processing (HSv5 only) + CONN_CONFUSED = 3, //< listener thinks it's connected, but caller missed conclusion + CONN_RUNNING = 10, //< no connection in progress, already connected + CONN_AGAIN = -2 //< No data was read, don't change any state. +}; + +enum EConnectMethod +{ + COM_ASYNCHRO, + COM_SYNCHRO +}; + +enum ETransmissionEvent +{ + TEV_INIT, // --> After creation, and after any parameters were updated. + TEV_ACK, // --> When handling UMSG_ACK - older CCC:onAck() + TEV_ACKACK, // --> UDT does only RTT sync, can be read from CUDT::avgRTT(). + TEV_LOSSREPORT, // --> When handling UMSG_LOSSREPORT - older CCC::onLoss() + TEV_CHECKTIMER, // --> See TEV_CHT_REXMIT + TEV_SEND, // --> When the packet is scheduled for sending - older CCC::onPktSent + TEV_RECEIVE, // --> When a data packet was received - older CCC::onPktReceived + TEV_CUSTOM, // --> probably dead call - older CCC::processCustomMsg + TEV_SYNC, // --> Backup group. When rate estimation is derived from an active member, and update is needed. + + TEV_E_SIZE +}; + +} +#endif diff --git a/srtcore/sync.cpp b/srtcore/sync.cpp index 6a3c988bf..bb59b6132 100644 --- a/srtcore/sync.cpp +++ b/srtcore/sync.cpp @@ -94,6 +94,16 @@ std::string FormatTime(const steady_clock::time_point& timestamp) return out.str(); } +string FormatDurationValue(double value, const char* unit, bool plus) +{ + using namespace hvu; + + fmtc fixed_maybe_plus = plus ? + fmtc().fixed().showpos() : + fmtc().fixed(); + return ofcat(fmt(value, fixed_maybe_plus), unit); +} + SysClockReference::SysClockReference() { timeval tv; diff --git a/srtcore/sync.h b/srtcore/sync.h index e52cf4f70..6c49df8a3 100644 --- a/srtcore/sync.h +++ b/srtcore/sync.h @@ -1262,16 +1262,12 @@ struct DurationUnitName static double count(const steady_clock::duration& dur) { return static_cast(count_microseconds(dur))/1000000.0; } }; +std::string FormatDurationValue(double value, const char* unit, bool plus); + template inline std::string FormatDuration(const steady_clock::duration& dur, bool plus = false) { - using namespace hvu; - fmtc fixed_maybe_plus = plus ? - fmtc().fixed().showpos() : - fmtc().fixed(); - return ofcat( - fmt(DurationUnitName::count(dur), fixed_maybe_plus), - DurationUnitName::name()); + return FormatDurationValue(DurationUnitName::count(dur), DurationUnitName::name(), plus); } inline std::string FormatDuration(const steady_clock::duration& dur)