Skip to content

Commit

Permalink
Improved topology logic
Browse files Browse the repository at this point in the history
Improved logging
  • Loading branch information
JPersson77 committed Feb 15, 2023
1 parent b025a2c commit fa931e3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// common general application definitions
#define APPNAME L"LGTV Companion"
#define APP_VERSION L"2.1.2"
#define APP_VERSION L"2.1.5"
#define CONFIG_FILE L"config.json"
#define LOG_FILE L"Log.txt"
#define WINDOW_CLASS_UNIQUE L"YOLOx0x0x0181818"
Expand Down
69 changes: 40 additions & 29 deletions LGTV Companion Service/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
using json = nlohmann::json;

mutex mMutex;
mutex mMutex;

namespace {
class NetEntryDeleter final {
Expand All @@ -39,33 +39,34 @@ namespace {
const SOCKADDR_INET address;
};

boost::optional<NET_LUID> GetLocalInterface(SOCKADDR_INET destination, string sDev) {
boost::optional<NET_LUID> GetLocalInterface(SOCKADDR_INET destination, string sDev, string * log_msg) {
MIB_IPFORWARD_ROW2 row;
SOCKADDR_INET bestSourceAddress;
const auto result = GetBestRoute2(NULL, 0, NULL, &destination, 0, &row, &bestSourceAddress);

std::stringstream log;
log << sDev << ", GetBestRoute2()";
log << sDev << ", Best route to IP -";

if (result != NO_ERROR) {
log << " failed with code " << result;
Log(log.str());
*log_msg = log.str();
return boost::none;
}

log << " selected interface index " << row.InterfaceIndex << " LUID " << row.InterfaceLuid.Value << " route protocol " << row.Protocol;
log << " interface index " << row.InterfaceIndex << " LUID " << row.InterfaceLuid.Value << " route protocol " << row.Protocol;

if (row.Protocol != MIB_IPPROTO_LOCAL) {
log << "; route is not local, aborting";
Log(log.str());
log << "; route is not local, aborting!";
*log_msg = log.str();
return boost::none;
}
Log(log.str());
*log_msg = log.str();

return row.InterfaceLuid;
}

std::unique_ptr<NetEntryDeleter> CreateTransientLocalNetEntry(SOCKADDR_INET destination, unsigned char macAddress[6], string sDev) {
const auto luid = GetLocalInterface(destination, sDev);
std::unique_ptr<NetEntryDeleter> CreateTransientLocalNetEntry(SOCKADDR_INET destination, unsigned char macAddress[6], string sDev, string * log_msg) {
const auto luid = GetLocalInterface(destination, sDev, log_msg);
if (!luid.has_value())
return nullptr;

Expand Down Expand Up @@ -428,6 +429,7 @@ void DisplayPowerOnThread(settings::DEVICE* CallingSessionParameters, bool* Call
thread wolthread(WOLthread, CallingSessionParameters, CallingSessionThreadRunning, Timeout);
wolthread.detach();
}
Sleep(500);

// build the appropriate WebOS handshake
if (key == "")
Expand All @@ -439,7 +441,7 @@ void DisplayPowerOnThread(settings::DEVICE* CallingSessionParameters, bool* Call
handshake.replace(ckf, ck.length(), key);
}

//try waking up the display ten times, but not longer than timeout user preference
//try waking up the display, but not longer than timeout user preference
while (time(0) - origtim < (Timeout + 1))
{
time_t looptim = time(0);
Expand Down Expand Up @@ -506,7 +508,7 @@ void DisplayPowerOnThread(settings::DEVICE* CallingSessionParameters, bool* Call
//debuggy
{
string tt = beast::buffers_to_string(buffer.data());
string st = SSL ? "[DEBUG] (SSL) ON response 1: " : "[DEBUG] ON Response 1: ";
string st = device; st += SSL ? ", [DEBUG] (SSL) ON response 1: " : ", [DEBUG] ON Response 1: ";
st += tt;
Log(st);
}
Expand Down Expand Up @@ -547,7 +549,7 @@ void DisplayPowerOnThread(settings::DEVICE* CallingSessionParameters, bool* Call
string t = beast::buffers_to_string(buffer.data());

//debuggy
string ss = SSL ? "[DEBUG] (SSL) ON response key: " : "[DEBUG] ON response key: ";
string ss = device; ss += SSL ? ", [DEBUG] (SSL) ON response key: " : ", [DEBUG] ON response key: ";
ss += t;
Log(ss);

Expand Down Expand Up @@ -591,7 +593,7 @@ void DisplayPowerOnThread(settings::DEVICE* CallingSessionParameters, bool* Call
//debuggy
{
string tt = beast::buffers_to_string(buffer.data());
string st = SSL ? "[DEBUG] (SSL) ON response 2: " : "[DEBUG] ON response 2: ";
string st = device; st += SSL ? ", [DEBUG] (SSL) ON response 2: " : ", [DEBUG] ON response 2: ";
st += tt;
Log(st);
}
Expand All @@ -611,7 +613,7 @@ void DisplayPowerOnThread(settings::DEVICE* CallingSessionParameters, bool* Call
//debuggy
{
string tt = beast::buffers_to_string(buffer.data());
string st = SSL ? "[DEBUG] (SSL) ON response 3: " : "[DEBUG] ON response 3: ";
string st = device; st += SSL ? ", [DEBUG] (SSL) ON response 3: " : ", [DEBUG] ON response 3: ";
st += tt;
Log(st);
}
Expand Down Expand Up @@ -651,8 +653,8 @@ void DisplayPowerOnThread(settings::DEVICE* CallingSessionParameters, bool* Call
}
time_t endtim = time(0);
time_t execution_time = endtim - looptim;
if (execution_time < 6)
Sleep((6 - (DWORD)execution_time) * 1000);
if (execution_time >= 0 && execution_time < 1)
Sleep((1 - (DWORD)execution_time) * 1000);
}

while (!mMutex.try_lock())
Expand All @@ -677,6 +679,7 @@ void WOLthread(settings::DEVICE* CallingSessionParameters, bool* CallingSessionT

SOCKET WOLsocket = INVALID_SOCKET;
string logmsg;
string IPentry_saved;

try
{
Expand Down Expand Up @@ -776,7 +779,7 @@ void WOLthread(settings::DEVICE* CallingSessionParameters, bool* CallingSessionT
MAC.erase(remove(MAC.begin(), MAC.end(), CharsToRemove[i]), MAC.end());
}

//send WOL packet every other second until timeout, or until the calling thread has ended
//send WOL packet every second until timeout, or until the calling thread has ended
while (time(0) - origtim < (Timeout + 1))
{
time_t looptim = time(0);
Expand All @@ -801,8 +804,16 @@ void WOLthread(settings::DEVICE* CallingSessionParameters, bool* CallingSessionT
memcpy(&Message[i * 6], &MACstr, 6 * sizeof(unsigned char));

std::unique_ptr<NetEntryDeleter> netEntryDeleter;
if (WOLtype == WOL_IPSEND) netEntryDeleter = CreateTransientLocalNetEntry(reinterpret_cast<const SOCKADDR_INET&>(LANDestination), MACstr, device);

if (WOLtype == WOL_IPSEND)
{
string log_msg;
netEntryDeleter = CreateTransientLocalNetEntry(reinterpret_cast<const SOCKADDR_INET&>(LANDestination), MACstr, device, &log_msg);
if (log_msg != IPentry_saved)
{
Log(log_msg);
IPentry_saved = log_msg;
}
}
// Send Wake On LAN packet
if (sendto(WOLsocket, (char*)&Message, 102, 0, reinterpret_cast<sockaddr*>(&LANDestination), sizeof(LANDestination)) == SOCKET_ERROR)
{
Expand Down Expand Up @@ -831,8 +842,8 @@ void WOLthread(settings::DEVICE* CallingSessionParameters, bool* CallingSessionT

time_t endtim = time(0);
time_t execution_time = endtim - looptim;
if (execution_time < 2)
Sleep((2 - (DWORD)execution_time) * 1000);
if (execution_time >= 0 && execution_time < 1)
Sleep((1 - (DWORD)execution_time) * 1000);

while (!mMutex.try_lock())
Sleep(MUTEX_WAIT);
Expand Down Expand Up @@ -971,7 +982,7 @@ void DisplayPowerOffThread(settings::DEVICE* CallingSessionParameters, bool* Cal
//debuggy
{
string tt = beast::buffers_to_string(buffer.data());
string st = SSL ? "[DEBUG] (SSL) OFF response 1: " : "[DEBUG] OFF response 1: ";
string st = device; st += SSL ? ", [DEBUG] (SSL) OFF response 1: " : ", [DEBUG] OFF response 1: ";
st += tt;
Log(st);
}
Expand Down Expand Up @@ -1006,7 +1017,7 @@ void DisplayPowerOffThread(settings::DEVICE* CallingSessionParameters, bool* Cal
//debuggy
{
string tt = beast::buffers_to_string(buffer.data());
string st = SSL ? "[DEBUG] (SSL) OFF response 2: " : "[DEBUG] OFF response 2: ";
string st = device; st += SSL ? ", [DEBUG] (SSL) OFF response 2: " : ", [DEBUG] OFF response 2: ";
st += tt;
Log(st);
}
Expand Down Expand Up @@ -1066,7 +1077,7 @@ void DisplayPowerOffThread(settings::DEVICE* CallingSessionParameters, bool* Cal
//debuggy
{
string tt = beast::buffers_to_string(buffer.data());
string st = SSL ? "[DEBUG] (SSL) OFF response 3: " : "[DEBUG] OFF response 3: ";
string st = device; st += SSL ? ", [DEBUG] (SSL) OFF response 3: " : ", [DEBUG] OFF response 3: ";
st += tt;
Log(st);
}
Expand Down Expand Up @@ -1125,7 +1136,7 @@ void DisplayPowerOffThread(settings::DEVICE* CallingSessionParameters, bool* Cal
//debuggy
{
string tt = beast::buffers_to_string(buffer.data());
string st = SSL ? "[DEBUG] (SSL) OFF response 4: " : "[DEBUG] OFF response 4: ";
string st = device; st += SSL ? ", [DEBUG] (SSL) OFF response 4: " : ", [DEBUG] OFF response 4: ";
st += tt;
Log(st);
}
Expand Down Expand Up @@ -1208,7 +1219,7 @@ void SetDisplayHdmiInputThread(settings::DEVICE* CallingSessionParameters, bool*
ckf = setinputmess.find(hdmino);
setinputmess.replace(ckf, hdmino.length(), inp);

//try ten times, but not longer than timeout user preference
//try, but not longer than timeout user preference
while (time(0) - origtim < (Timeout + 1))
{
time_t looptim = time(0);
Expand Down Expand Up @@ -1289,8 +1300,8 @@ void SetDisplayHdmiInputThread(settings::DEVICE* CallingSessionParameters, bool*
}
time_t endtim = time(0);
time_t execution_time = endtim - looptim;
if (execution_time < 6)
Sleep((6 - (DWORD)execution_time) * 1000);
if (execution_time >= 0 && execution_time < 2)
Sleep((2 - (DWORD)execution_time) * 1000);
}

while (!mMutex.try_lock())
Expand Down
2 changes: 1 addition & 1 deletion LGTV Companion Setup/Product.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
<?define LGTV Companion Service_TargetDir=$(var.LGTV Companion Service.TargetDir)?><?define LGTV Companion UI_TargetDir=$(var.LGTV Companion UI.TargetDir)?>
<Product Id="938FB27C-7EDB-401F-BDE0-FE189134FD9A" Name="LGTV Companion" Language="1033" Version="2.1.2" Manufacturer="J Persson" UpgradeCode="0BA17E5B-11CE-491D-B1A1-05DD2D9F610A">
<Product Id="E611A82C-7FE8-4C3F-8C40-016ABA16C760" Name="LGTV Companion" Language="1033" Version="2.1.5" Manufacturer="J Persson" UpgradeCode="0BA17E5B-11CE-491D-B1A1-05DD2D9F610A">
<Package Id="*" InstallerVersion="301" Compressed="yes" InstallScope="perMachine" Platform='x64' Description="LGTV Companion installer" InstallPrivileges="elevated" AdminImage="yes"/>
<Media Id="1" Cabinet="LGTVapp.cab" EmbedCab="yes" />

Expand Down
20 changes: 19 additions & 1 deletion LGTV Companion User/Daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ UINT shellhookMessage;
DWORD daemon_startup_user_input_time = 0;
UINT ManualUserIdleMode = 0;
HBRUSH hBackbrush;
time_t TimeOfLastTopologyChange = 0;

settings::Preferences Prefs;

Expand Down Expand Up @@ -423,6 +424,11 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
KillTimer(hWnd, (UINT_PTR)REMOTE_DISCONNECT);
CommunicateWithService("-daemon remote_disconnect");
}break;
case TIMER_TOPOLOGY_COLLECTION:
{
KillTimer(hWnd, (UINT_PTR)TIMER_TOPOLOGY_COLLECTION);
PostMessage(hWnd, USER_DISPLAYCHANGE, NULL, NULL);
}break;
case TIMER_TOPOLOGY:
{
KillTimer(hWnd, TIMER_TOPOLOGY);
Expand All @@ -432,7 +438,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
int result = VerifyTopology();

if (result == TOPOLOGY_OK)
{
PostMessage(hWnd, USER_DISPLAYCHANGE, NULL, NULL);
}
else if (result == TOPOLOGY_UNDETERMINED)
{
Log(L"No active devices detected when verifying Windows Monitor Topology. Topology feature has been disabled");
Expand Down Expand Up @@ -548,14 +556,24 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
}break;
case USER_DISPLAYCHANGE:
{
TimeOfLastTopologyChange = time(0);
CheckDisplayTopology();
}break;

case WM_DISPLAYCHANGE:
{
if (Prefs.AdhereTopology)
{
PostMessage(hWnd, USER_DISPLAYCHANGE, NULL, NULL);
time_t now = time(0);
if (now - TimeOfLastTopologyChange > 10)
{
PostMessage(hWnd, USER_DISPLAYCHANGE, NULL, NULL);
}
else
{
TimeOfLastTopologyChange = now;
SetTimer(hWnd, TIMER_TOPOLOGY_COLLECTION, TIMER_TOPOLOGY_COLLECTION_DELAY, (TIMERPROC)NULL);
}
}
}break;

Expand Down
2 changes: 2 additions & 0 deletions LGTV Companion User/Daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@
#define TIMER_IDLE 19
#define TIMER_TOPOLOGY 20
#define TIMER_CHECK_PROCESSES 21
#define TIMER_TOPOLOGY_COLLECTION 22
#define TIMER_MAIN_DELAY_WHEN_BUSY 5000
#define TIMER_MAIN_DELAY_WHEN_IDLE 100
#define TIMER_REMOTE_DELAY 10000
#define TIMER_TOPOLOGY_DELAY 8000
#define TIMER_CHECK_PROCESSES_DELAY 5000
#define TIMER_TOPOLOGY_COLLECTION_DELAY 3000
#define APP_NEW_VERSION WM_USER+9
#define USER_DISPLAYCHANGE WM_USER+10
#define APP_USER_IDLE_ON WM_USER+20
Expand Down

0 comments on commit fa931e3

Please sign in to comment.