Skip to content

Commit

Permalink
added KeepAlive for tCP stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalkbrenner committed Jan 21, 2025
1 parent 8b2b898 commit 0c25b8a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/ZeDMDComm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ void ZeDMDComm::Run()
}
m_delayedFrameMutex.unlock();
m_frameQueueMutex.unlock();

KeepAlive();
std::this_thread::sleep_for(std::chrono::milliseconds(1));

continue;
Expand Down
3 changes: 3 additions & 0 deletions src/ZeDMDComm.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ typedef enum

ClearScreen = 0x0a,

KeepAlive = 0x0b,

DisableDebug = 0x62,
EnableDebug = 0x63,
} ZEDMD_COMM_COMMAND;
Expand Down Expand Up @@ -206,6 +208,7 @@ class ZeDMDComm

protected:
virtual bool SendChunks(uint8_t* pData, uint16_t size);
virtual bool KeepAlive() { return false; }
virtual void Reset();
void Log(const char* format, ...);
void ClearFrames();
Expand Down
31 changes: 30 additions & 1 deletion src/ZeDMDWiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <netdb.h>
#include <unistd.h>
#endif
#include <netinet/tcp.h>

#include "komihash/komihash.h"
#include "miniz/miniz.h"
Expand Down Expand Up @@ -120,6 +121,9 @@ bool ZeDMDWiFi::DoConnect(const char* ip)
return false;
}

int flag = 1; // Disable Nagle's algorithm
m_tcpConnector->set_option(IPPROTO_TCP, TCP_NODELAY, flag);

m_connected = true;

return true;
Expand Down Expand Up @@ -301,14 +305,39 @@ bool ZeDMDWiFi::IsConnected() { return m_connected; }

void ZeDMDWiFi::Reset() {}

bool ZeDMDWiFi::KeepAlive()
{
static auto lastRun = std::chrono::steady_clock::now();
const auto minInterval = std::chrono::milliseconds(100);

auto now = std::chrono::steady_clock::now();
if (now - lastRun < minInterval)
{
// Skip this call
return true;
}

lastRun = now;

uint16_t size = CTRL_CHARS_HEADER_SIZE + 3;
uint8_t* pData = (uint8_t*)malloc(size);
memcpy(pData, CTRL_CHARS_HEADER, CTRL_CHARS_HEADER_SIZE);
pData[CTRL_CHARS_HEADER_SIZE] = ZEDMD_COMM_COMMAND::KeepAlive;
pData[CTRL_CHARS_HEADER_SIZE + 1] = 0;
pData[CTRL_CHARS_HEADER_SIZE + 2] = 0;
SendChunks(pData, size);
free(pData);

return true;
}

bool ZeDMDWiFi::SendChunks(uint8_t* pData, uint16_t size)
{
if (m_tcpConnector->write_n(pData, size) < 0)
{
Log("TCP stream error: %s", m_tcpConnector->last_error_str().c_str());
return false;
}
// std::this_thread::sleep_for(std::chrono::microseconds(500));

return true;
}
1 change: 1 addition & 0 deletions src/ZeDMDWiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ZeDMDWiFi : public ZeDMDComm
protected:
bool DoConnect(const char* ip);
virtual bool SendChunks(uint8_t* pData, uint16_t size);
virtual bool KeepAlive();
virtual void Reset();
bool openTcpConnection(int sock, sockaddr_in server, int16_t timeout);
bool SendGetRequest(const std::string& path);
Expand Down
6 changes: 3 additions & 3 deletions src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ int main(int argc, const char* argv[])

if (pZeDMD->Open() || pZeDMD->OpenDefaultWiFi())
{
pZeDMD->EnableDebug();
pZeDMD->DisableDebug();
uint16_t width = pZeDMD->GetWidth();
uint16_t height = pZeDMD->GetHeight();

pZeDMD->SetFrameSize(128, 32);

uint8_t* pImage24 = CreateImageRGB24();
uint16_t sleep = 16;
uint16_t sleep = 8;

pZeDMD->EnableUpscaling();

Expand Down Expand Up @@ -158,7 +158,7 @@ int main(int argc, const char* argv[])

free(rgb888);

std::this_thread::sleep_for(std::chrono::milliseconds(2000));
std::this_thread::sleep_for(std::chrono::milliseconds(1000));

pZeDMD->LedTest();

Expand Down

0 comments on commit 0c25b8a

Please sign in to comment.