Skip to content

Commit

Permalink
Added remote server client list
Browse files Browse the repository at this point in the history
Client now sends nickname to remote server, and remote server has new /list and /quit commands. Also updated Linux build for server.
  • Loading branch information
theokyr authored Jul 7, 2024
2 parents fb3f5ba + ef1c878 commit 4bf642b
Show file tree
Hide file tree
Showing 17 changed files with 654 additions and 275 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/build-cs2remoteconsole-server-linux.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Build CS2RemoteConsole-server (Linux)

on:
push:
branches: [ master ]
paths:
- 'CS2RemoteConsole-server/**'
pull_request:
branches: [ master ]
paths:
- 'CS2RemoteConsole-server/**'
workflow_dispatch:

jobs:
build-cs2remoteconsole-server-linux:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up GCC
uses: egor-tensin/setup-gcc@v1
with:
version: latest
platform: x64

- name: Build CS2RemoteConsole-server
run: |
cd CS2RemoteConsole-server
make
- name: Prepare artifact directory
run: |
mkdir -p artifact
cp CS2RemoteConsole-server/CS2RemoteConsole-server artifact/
- name: Verify artifact contents
run: |
echo "Verifying artifact contents:"
ls -l artifact
- name: Upload CS2RemoteConsole-server artifact
uses: actions/upload-artifact@v2
with:
name: CS2RemoteConsole-server-linux-build
path: artifact
1 change: 0 additions & 1 deletion CS2RemoteConsole-client/CS2RemoteConsole-client.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
<ClInclude Include="src\connection\connection_cs2console.h"/>
<ClInclude Include="src\connection\connection_remoteserver.h"/>
<ClInclude Include="src\constants.h" />
<ClInclude Include="src\logging.h"/>
<ClInclude Include="src\singletons.h" />
<ClInclude Include="src\tui\tui.h"/>
<ClInclude Include="src\utils.h"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
<ClInclude Include="src\connection_remoteserver.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\logging.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\payloads.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void cs2ConsoleConnectorLoop()
cs2ListenerThread.join();
}
cs2ListenerThread = std::thread(listenForCS2ConsoleData);
vconsole.sendCmd("name"); // Get current player's in-game name
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "connection_remoteserver.h"
#include <iostream>
#include <ws2tcpip.h>
#include <chrono>
#include <spdlog/spdlog.h>

Expand All @@ -10,6 +9,8 @@ std::atomic<bool> remoteServerConnected(false);
std::thread remoteServerListenerThread;
std::thread remoteServerConnectorThread;

ClientInfo globalClientInfo("");

bool connectToRemoteServer()
{
const std::string ip = Config::getInstance().get("remote_server_ip", "127.0.0.1");
Expand Down Expand Up @@ -39,6 +40,24 @@ bool connectToRemoteServer()
return true;
}

bool sendMessageToRemoteServer(const std::string& message)
{
if (remoteServerSock == INVALID_SOCKET)
{
spdlog::error("[RemoteServerConnection] Cannot send message: Not connected to remote server");
return false;
}

int sendResult = send(remoteServerSock, message.c_str(), static_cast<int>(message.length()), 0);
if (sendResult == SOCKET_ERROR)
{
spdlog::error("[RemoteServerConnection] Failed to send message to remote server: {}", WSAGetLastError());
return false;
}

return true;
}

void remoteServerConnectorLoop()
{
const int reconnect_delay = Config::getInstance().getInt("remote_server_reconnect_delay", 5000);
Expand All @@ -56,6 +75,20 @@ void remoteServerConnectorLoop()
remoteServerListenerThread.join();
}
remoteServerListenerThread = std::thread(listenForRemoteServerData);

// Send player name if available
if (!globalClientInfo.name.empty())
{
std::string nameMessage = "PLAYERNAME:" + globalClientInfo.name;
if (sendMessageToRemoteServer(nameMessage))
{
spdlog::info("[RemoteServerConnection] Sent player name to remote server.");
}
else
{
spdlog::error("[RemoteServerConnection] Failed to send player name to remote server.");
}
}
}
else
{
Expand Down
12 changes: 12 additions & 0 deletions CS2RemoteConsole-client/src/connection/connection_remoteserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ extern std::thread remoteServerConnectorThread;
bool connectToRemoteServer();
void remoteServerConnectorLoop();
void listenForRemoteServerData();
bool sendMessageToRemoteServer(const std::string& message);
void cleanupRemoteServer();

struct ClientInfo
{
std::string name;

ClientInfo(const std::string& i): name(i)
{
}
};

extern ClientInfo globalClientInfo;

#endif // CONNECTION_REMOTESERVER_H
28 changes: 28 additions & 0 deletions CS2RemoteConsole-client/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <atomic>
#include <thread>
#include <csignal>
#include <regex>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/callback_sink.h>
Expand Down Expand Up @@ -40,6 +41,29 @@ void gracefulShutdown()
std::cout << "CS2RemoteConsole shutdown complete. Bye-bye!..." << std::endl;
}

void handlePRNT(const PRNT& prnt)
{
static const std::regex nameRegex(R"(name = (.+))");
std::smatch match;

if (std::regex_search(prnt.message, match, nameRegex))
{
globalClientInfo.name = match[1].str();
std::cout << "Player name updated: " << globalClientInfo.name << std::endl;

// Send the name to the remote server
std::string nameMessage = "PLAYERNAME:" + globalClientInfo.name;
if (sendMessageToRemoteServer(nameMessage))
{
std::cout << "Sent player name to remote server." << std::endl;
}
else
{
std::cerr << "Failed to send player name to remote server." << std::endl;
}
}
}

int main()
{
try
Expand Down Expand Up @@ -80,6 +104,10 @@ int main()
vconsole.setOnPRNTReceived([&](const PRNT& PRNT)
{
tui.addConsoleMessage(PRNT.channelID, PRNT.message, PRNT.color);
if (globalClientInfo.name.empty())
{
handlePRNT(PRNT);
}
});

spdlog::info("[Main] Starting {}", application_name);
Expand Down
81 changes: 9 additions & 72 deletions CS2RemoteConsole-server/CS2RemoteConsole-server.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,78 +87,15 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<Optimization>Disabled</Optimization>
<SupportJustMyCode>true</SupportJustMyCode>
<AssemblerOutput>NoListing</AssemblerOutput>
<AssemblerListingLocation>x64\Debug\</AssemblerListingLocation>
<UndefineAllPreprocessorDefinitions>false</UndefineAllPreprocessorDefinitions>
<BrowseInformation>false</BrowseInformation>
<BrowseInformationFile>x64\Debug\</BrowseInformationFile>
<CompileAs>Default</CompileAs>
<ConformanceMode>true</ConformanceMode>
<DiagnosticsFormat>Column</DiagnosticsFormat>
<DisableLanguageExtensions>false</DisableLanguageExtensions>
<ErrorReporting>Prompt</ErrorReporting>
<ExpandAttributedSource>false</ExpandAttributedSource>
<ExceptionHandling>Sync</ExceptionHandling>
<EnableASAN>false</EnableASAN>
<EnableFiberSafeOptimizations>false</EnableFiberSafeOptimizations>
<EnableEnhancedInstructionSet>NotSet</EnableEnhancedInstructionSet>
<FloatingPointModel>Precise</FloatingPointModel>
<ForceConformanceInForLoopScope>true</ForceConformanceInForLoopScope>
<GenerateModuleDependencies>false</GenerateModuleDependencies>
<GenerateSourceDependencies>false</GenerateSourceDependencies>
<GenerateXMLDocumentationFiles>false</GenerateXMLDocumentationFiles>
<InlineFunctionExpansion>Default</InlineFunctionExpansion>
<IntrinsicFunctions>false</IntrinsicFunctions>
<IgnoreStandardIncludePath>false</IgnoreStandardIncludePath>
<LanguageStandard>Default</LanguageStandard>
<LanguageStandard_C>Default</LanguageStandard_C>
<MinimalRebuild>false</MinimalRebuild>
<ModuleDependenciesFile>x64\Debug\</ModuleDependenciesFile>
<ModuleOutputFile>x64\Debug\</ModuleOutputFile>
<OmitDefaultLibName>false</OmitDefaultLibName>
<FavorSizeOrSpeed>Neither</FavorSizeOrSpeed>
<WholeProgramOptimization>false</WholeProgramOptimization>
<ObjectFileName>x64\Debug\</ObjectFileName>
<CallingConvention>Cdecl</CallingConvention>
<ProgramDataBaseFileName>x64\Debug\vc142.pdb</ProgramDataBaseFileName>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>x64\Debug\CS2RemoteConsole-server.pch</PrecompiledHeaderOutputFile>
<PreprocessToFile>false</PreprocessToFile>
<PreprocessKeepComments>false</PreprocessKeepComments>
<PreprocessSuppressLineNumbers>false</PreprocessSuppressLineNumbers>
<RemoveUnreferencedCodeData>true</RemoveUnreferencedCodeData>
<ScanSourceForModuleDependencies>false</ScanSourceForModuleDependencies>
<ShowIncludes>false</ShowIncludes>
<SourceDependenciesFile>x64\Debug\</SourceDependenciesFile>
<SuppressStartupBanner>true</SuppressStartupBanner>
<BufferSecurityCheck>true</BufferSecurityCheck>
<SmallerTypeCheck>false</SmallerTypeCheck>
<SpectreMitigation>false</SpectreMitigation>
<StructMemberAlignment>Default</StructMemberAlignment>
<TrackerLogDirectory>x64\Debug\CS2Remot.3FAB76DA.tlog\</TrackerLogDirectory>
<TranslateIncludes>false</TranslateIncludes>
<MinimalRebuildFromTracking>true</MinimalRebuildFromTracking>
<TreatWarningAsError>false</TreatWarningAsError>
<TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
<UseFullPaths>true</UseFullPaths>
<WarningLevel>Level3</WarningLevel>
<XMLDocumentationFileName>x64\Debug\</XMLDocumentationFileName>
<DebugInformationFormat>EditAndContinue</DebugInformationFormat>
<IntelJCCErratum>false</IntelJCCErratum>
<TreatAngleIncludeAsExternal>false</TreatAngleIncludeAsExternal>
<ExternalWarningLevel>InheritWarningLevel</ExternalWarningLevel>
<TreatExternalTemplatesAsInternal>true</TreatExternalTemplatesAsInternal>
<DisableAnalyzeExternal>false</DisableAnalyzeExternal>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;_UNICODE;UNICODE;</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<LinkCompiled>true</LinkCompiled>
</ClCompile>
<ClCompile Include="src\client_handler.cpp" />
<ClCompile Include="src\main.cpp"/>
<ClCompile Include="src\server.cpp" />
<ClCompile Include="src\utils.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\client_handler.h" />
<ClInclude Include="src\server.h" />
<ClInclude Include="src\utils.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
27 changes: 27 additions & 0 deletions CS2RemoteConsole-server/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -pedantic
LDFLAGS = -pthread

SRC_DIR = src
OBJ_DIR = obj

SOURCES = $(wildcard $(SRC_DIR)/*.cpp)
OBJECTS = $(SOURCES:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o)

TARGET = CS2RemoteConsole-server

.PHONY: all clean

all: $(TARGET)

$(TARGET): $(OBJECTS)
$(CXX) $(OBJECTS) -o $@ $(LDFLAGS)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
$(CXX) $(CXXFLAGS) -c $< -o $@

$(OBJ_DIR):
mkdir -p $@

clean:
rm -rf $(OBJ_DIR) $(TARGET)
Loading

0 comments on commit 4bf642b

Please sign in to comment.