Skip to content

Commit

Permalink
Bug fix: UDP output threads may not send queued packets during a peri…
Browse files Browse the repository at this point in the history
…od of time when the thread is starting up

- Added dark mode title bar on Windows
- Print incoming/outgoing non-OSC packets
  • Loading branch information
MizPlusPlus committed Jun 3, 2022
1 parent 3fcda36 commit 9e19f6b
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 11 deletions.
2 changes: 1 addition & 1 deletion OSCRouter/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

////////////////////////////////////////////////////////////////////////////////

#define APP_VERSION "0.13"
#define APP_VERSION "0.14"
#define SETTING_LOG_DEPTH "LogDepth"
#define SETTING_FILE_DEPTH "FileDepth"
#define SETTING_LAST_FILE "LastFile"
Expand Down
18 changes: 18 additions & 0 deletions OSCRouter/OSCRouter.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<AdditionalIncludeDirectories>..\..\EosSyncLib\EosSyncLib;$(QTOSCROUTER)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4127;4996;4512</DisableSpecificWarnings>
<PreprocessorDefinitions>WIN32;NOMINMAX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatSpecificWarningsAsErrors>4715</TreatSpecificWarningsAsErrors>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand All @@ -74,6 +75,7 @@
<AdditionalIncludeDirectories>..\..\EosSyncLib\EosSyncLib;$(QTOSCROUTER)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4127;4996;4512</DisableSpecificWarnings>
<PreprocessorDefinitions>WIN32;NOMINMAX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatSpecificWarningsAsErrors>4715</TreatSpecificWarningsAsErrors>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down Expand Up @@ -139,6 +141,22 @@
</ItemGroup>
<ItemGroup>
<None Include="icon1.ico" />
<None Include="qt.conf" />
<CustomBuild Include="qt.qrc">
<FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(QTOSCROUTER)\bin\rcc.exe -no-compress %(Filename)%(Extension) -o %(Filename)%(Extension).cpp</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(QTOSCROUTER)\bin\rcc.exe -no-compress %(Filename)%(Extension) -o %(Filename)%(Extension).cpp</Command>
<Message Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">rcc'ing %(Filename)%(Extension)</Message>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename)%(Extension).cpp</Outputs>
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(QTOSCROUTER)\bin\rcc.exe</AdditionalInputs>
<Message Condition="'$(Configuration)|$(Platform)'=='Release|x64'">rcc'ing %(Filename)%(Extension)</Message>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename)%(Extension).cpp</Outputs>
<AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(QTOSCROUTER)\bin\rcc.exe</AdditionalInputs>
<OutputItemType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">ClCompile</OutputItemType>
<OutputItemType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">ClCompile</OutputItemType>
<BuildInParallel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</BuildInParallel>
<BuildInParallel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</BuildInParallel>
</CustomBuild>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
6 changes: 6 additions & 0 deletions OSCRouter/OSCRouter.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
<CustomBuild Include="MainWindow.h">
<Filter>OSCRouter\Header Files</Filter>
</CustomBuild>
<CustomBuild Include="qt.qrc">
<Filter>Resource Files</Filter>
</CustomBuild>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="OSCRouter.rc">
Expand All @@ -140,5 +143,8 @@
<None Include="icon1.ico">
<Filter>Resource Files</Filter>
</None>
<None Include="qt.conf">
<Filter>Resource Files</Filter>
</None>
</ItemGroup>
</Project>
65 changes: 55 additions & 10 deletions OSCRouter/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include <arpa/inet.h>
#endif

#include <sstream>
#include <iomanip>

// must be last include
#include "LeakWatcher.h"

Expand All @@ -49,6 +52,36 @@ void PacketLogger::OSCParserClient_Log(const std::string &message)

////////////////////////////////////////////////////////////////////////////////

void PacketLogger::PrintPacket(OSCParser& oscParser, const char* packet, size_t size)
{
if (packet == nullptr || size == 0)
return;

if (OSCParser::IsOSCPacket(packet, size) && oscParser.PrintPacket(*this, packet, size))
return;

// not printed as an OSC packet, so print the raw hex contents
const size_t MaxPrintSize = 32;
size_t printSize = qMin(size, MaxPrintSize);

std::stringstream ss;
ss << std::setfill('0') << std::setw(2) << std::hex;
for (size_t i = 0; i < printSize; ++i)
{
if (i != 0)
ss << ' ';
ss << static_cast<int>(packet[i]);
}

if (size > printSize)
ss << "...";

if (!ss.str().empty())
OSCParserClient_Log(ss.str());
}

////////////////////////////////////////////////////////////////////////////////

EosUdpInThread::EosUdpInThread()
: m_Run(false)
, m_Mutex(QMutex::Recursive)
Expand Down Expand Up @@ -155,7 +188,7 @@ void EosUdpInThread::run()
QHostAddress host( reinterpret_cast<const sockaddr*>(&addr) );
logPrefix = QString("IN [%1:%2] ").arg( host.toString() ).arg(m_Addr.port).toUtf8().constData();
packetLogger.SetPrefix(logPrefix);
logParser.PrintPacket(packetLogger, data, static_cast<size_t>(len));
packetLogger.PrintPacket(logParser, data, static_cast<size_t>(len));
unsigned int ip = static_cast<unsigned int>( host.toIPv4Address() );
m_Mutex.lock();
m_Q.push_back( sRecvPacket(data,len,ip) );
Expand Down Expand Up @@ -208,6 +241,7 @@ EosUdpOutThread::EosUdpOutThread()
, m_ItemStateTableId(ItemStateTable::sm_Invalid_Id)
, m_State(ItemState::STATE_UNINITIALIZED)
, m_ReconnectDelay(0)
, m_QEnabled(false)
{
}

Expand All @@ -228,6 +262,7 @@ void EosUdpOutThread::Start(const EosAddr &addr, ItemStateTable::ID itemStateTab
m_ItemStateTableId = itemStateTableId;
m_ReconnectDelay = reconnectDelayMS;
m_Run = true;
m_QEnabled = true; // q commands while on-demand thread is first starting
start();
}

Expand All @@ -244,7 +279,7 @@ void EosUdpOutThread::Stop()
bool EosUdpOutThread::Send(const EosPacket &packet)
{
m_Mutex.lock();
if(GetState() == ItemState::STATE_CONNECTED)
if(m_QEnabled)
{
m_Q.push_back(packet);
m_Mutex.unlock();
Expand Down Expand Up @@ -279,7 +314,16 @@ ItemState::EnumState EosUdpOutThread::GetState()
void EosUdpOutThread::SetState(ItemState::EnumState state)
{
m_Mutex.lock();
m_State = state;
if (m_State != state)
{
m_State = state;

switch (m_State)
{
case ItemState::STATE_CONNECTED: m_QEnabled = true; break;
case ItemState::STATE_NOT_CONNECTED: m_QEnabled = false; break;
}
}
m_Mutex.unlock();
}

Expand All @@ -294,7 +338,7 @@ void EosUdpOutThread::run()
EosTimer reconnectTimer;

// outer loop for auto-reconnect
while( m_Run )
do
{
SetState(ItemState::STATE_CONNECTING);

Expand All @@ -320,8 +364,8 @@ void EosUdpOutThread::run()
{
const char *buf = i->GetData();
int len = i->GetSize();
if( udpOut->SendPacket(m_PrivateLog,buf,len) )
logParser.PrintPacket(packetLogger,buf,static_cast<size_t>(len));
if (udpOut->SendPacket(m_PrivateLog, buf, len))
packetLogger.PrintPacket(logParser, buf, static_cast<size_t>(len));
}
q.clear();

Expand All @@ -345,7 +389,7 @@ void EosUdpOutThread::run()
reconnectTimer.Start();
while(m_Run && !reconnectTimer.GetExpired(m_ReconnectDelay))
msleep(10);
}
} while (m_Run);

msg = QString("udp output %1:%2 thread ended").arg(m_Addr.ip).arg(m_Addr.port);
m_PrivateLog.AddInfo( msg.toUtf8().constData() );
Expand Down Expand Up @@ -546,7 +590,7 @@ void EosTcpClientThread::run()
{
if(frameSize != 0)
{
logParser.PrintPacket(inPacketLogger, frame, frameSize);
inPacketLogger.PrintPacket(logParser, frame, frameSize);
m_Mutex.lock();
m_RecvQ.push_back( EosUdpInThread::sRecvPacket(frame,static_cast<int>(frameSize),ip) );
m_Mutex.unlock();
Expand Down Expand Up @@ -578,8 +622,8 @@ void EosTcpClientThread::run()
char *frame = recvStream.GetNextFrame(frameSize);
if( frame )
{
if(frameSize != 0)
logParser.PrintPacket(outPacketLogger, frame, frameSize);
if (frameSize != 0)
outPacketLogger.PrintPacket(logParser, frame, frameSize);
delete[] frame;
}
else
Expand Down Expand Up @@ -956,6 +1000,7 @@ EosUdpOutThread* RouterThread::CreateUdpOutThread(const EosAddr &addr, ItemState
EosUdpOutThread *thread = new EosUdpOutThread();
udpOutThreads[addr] = thread;
thread->Start(addr, itemStateTableId, m_ReconnectDelay);
return thread;
}
else
return i->second;
Expand Down
2 changes: 2 additions & 0 deletions OSCRouter/Router.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class PacketLogger
virtual void SetPrefix(const std::string &prefix) {m_Prefix = prefix;}
virtual void OSCParserClient_Log(const std::string &message);
virtual void OSCParserClient_Send(const char *, size_t) {}
virtual void PrintPacket(OSCParser& oscParser, const char* packet, size_t size);

protected:
EosLog::EnumLogMsgType m_LogType;
Expand Down Expand Up @@ -164,6 +165,7 @@ class EosUdpOutThread
EosLog m_Log;
EosLog m_PrivateLog;
EosPacket::Q m_Q;
bool m_QEnabled;
QMutex m_Mutex;

virtual void run();
Expand Down
2 changes: 2 additions & 0 deletions OSCRouter/qt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Platforms]
WindowsArguments = darkmode=1
5 changes: 5 additions & 0 deletions OSCRouter/qt.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/qt/etc">
<file>qt.conf</file>
</qresource>
</RCC>

0 comments on commit 9e19f6b

Please sign in to comment.