重构代码,删除不必要的文件,添加控制数据包处理和Steam消息处理功能

This commit is contained in:
Ayndpa
2025-11-18 19:05:50 +08:00
parent 3b85a5f3bb
commit b308a644ff
11 changed files with 194 additions and 483 deletions

19
control_packets.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include "control_packets.h"
#include <iostream>
#include <string_view>
// 假设需要访问全局变量,但为了简单,这里只打印
// 如果需要,可以传递引用或使用全局
void handleControlPacket(const char* data, size_t size, HSteamNetConnection conn) {
std::string_view packetData(data, size);
std::cout << "Received control packet: " << packetData << " from connection " << conn << std::endl;
// 这里添加处理逻辑例如解析JSON或命令
// 例如如果data是"ping",回复"pong"
if (packetData == "ping") {
// 发送回复,但需要接口
// 暂时只打印
std::cout << "Responding to ping" << std::endl;
}
// 可以扩展为更多控制命令
}

8
control_packets.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef CONTROL_PACKETS_H
#define CONTROL_PACKETS_H
#include <steamnetworkingtypes.h>
void handleControlPacket(const char* data, size_t size, HSteamNetConnection conn);
#endif // CONTROL_PACKETS_H

View File

@@ -1,72 +0,0 @@
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <iostream>
int main() {
// Initialize GLFW
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return -1;
}
// Create window
GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui Hello World", nullptr, nullptr);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
// Initialize ImGui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
(void)io;
ImGui::StyleColorsDark();
// Initialize ImGui backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 130");
// Main loop
while (!glfwWindowShouldClose(window)) {
// Poll events
glfwPollEvents();
// Start ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// Create a simple window
ImGui::Begin("Hello, World!");
ImGui::Text("Welcome to Dear ImGui!");
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End();
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Swap buffers
glfwSwapBuffers(window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}

View File

@@ -9,6 +9,7 @@
#include <mutex> #include <mutex>
#include <map> #include <map>
#include <algorithm> #include <algorithm>
#include <cstring>
#include <steam_api.h> #include <steam_api.h>
#include <isteamnetworkingsockets.h> #include <isteamnetworkingsockets.h>
#include <isteamnetworkingutils.h> #include <isteamnetworkingutils.h>
@@ -17,6 +18,7 @@
#include <memory> #include <memory>
#include "tcp_server.h" #include "tcp_server.h"
#include "tcp/tcp_client.h" #include "tcp/tcp_client.h"
#include "steam_message_handler.h"
using boost::asio::ip::tcp; using boost::asio::ip::tcp;
@@ -192,6 +194,12 @@ int main() {
g_connectionConfig[0].SetInt32(k_ESteamNetworkingConfig_TimeoutInitial, 10000); // 10 seconds initial timeout g_connectionConfig[0].SetInt32(k_ESteamNetworkingConfig_TimeoutInitial, 10000); // 10 seconds initial timeout
g_connectionConfig[1].SetInt32(k_ESteamNetworkingConfig_NagleTime, 0); // Disable Nagle for UDP g_connectionConfig[1].SetInt32(k_ESteamNetworkingConfig_NagleTime, 0); // Disable Nagle for UDP
// Initialize boost::asio io_context
boost::asio::io_context io_context;
// Create Steam Message Handler
SteamMessageHandler messageHandler(io_context, m_pInterface, connections, clientMap, clientMutex, server, g_isHost, localPort);
// Initialize GLFW // Initialize GLFW
if (!glfwInit()) { if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl; std::cerr << "Failed to initialize GLFW" << std::endl;
@@ -223,17 +231,6 @@ int main() {
ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 130"); ImGui_ImplOpenGL3_Init("#version 130");
// TCP Client for local port forwarding
// Removed: TCPClient* client = nullptr;
// Removed: bool isLocalConnected = false;
// Steam Networking variables
bool isHost = false;
bool isClient = false;
char joinBuffer[256] = "";
char filterBuffer[256] = "";
// Removed: char portBuffer[256] = "";
// Get friends list // Get friends list
std::vector<std::pair<CSteamID, std::string>> friendsList; std::vector<std::pair<CSteamID, std::string>> friendsList;
int friendCount = SteamFriends()->GetFriendCount(k_EFriendFlagAll); int friendCount = SteamFriends()->GetFriendCount(k_EFriendFlagAll);
@@ -243,6 +240,15 @@ int main() {
friendsList.push_back({friendID, name}); friendsList.push_back({friendID, name});
} }
// Start message handler
messageHandler.start();
// Steam Networking variables
bool isHost = false;
bool isClient = false;
char joinBuffer[256] = "";
char filterBuffer[256] = "";
// Main loop // Main loop
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
// Poll events // Poll events
@@ -251,59 +257,6 @@ int main() {
// Run Steam callbacks // Run Steam callbacks
SteamAPI_RunCallbacks(); SteamAPI_RunCallbacks();
// Poll networking
m_pInterface->RunCallbacks();
// Update user info
{
std::lock_guard<std::mutex> lock(clientMutex);
for (auto conn : connections) {
SteamNetConnectionInfo_t info;
SteamNetConnectionRealTimeStatus_t status;
if (m_pInterface->GetConnectionInfo(conn, &info) && m_pInterface->GetConnectionRealTimeStatus(conn, &status, 0, nullptr)) {
if (userMap.count(conn)) {
userMap[conn].ping = status.m_nPing;
userMap[conn].isRelay = (info.m_idPOPRelay != 0);
}
}
}
}
// Receive messages from Steam and forward to TCP server
{
std::lock_guard<std::mutex> lock(clientMutex);
for (auto conn : connections) {
ISteamNetworkingMessage* pIncomingMsg = nullptr;
int numMsgs = m_pInterface->ReceiveMessagesOnConnection(conn, &pIncomingMsg, 1);
if (numMsgs > 0 && pIncomingMsg) {
// std::cout << "Received " << pIncomingMsg->m_cbSize << " bytes" << std::endl;
if (server) {
server->sendToAll((const char*)pIncomingMsg->m_pData, pIncomingMsg->m_cbSize);
}
// Lazy connect: Create TCP Client on first message if not already connected
if (clientMap.find(conn) == clientMap.end() && g_isHost && localPort > 0) {
TCPClient* client = new TCPClient("localhost", localPort);
if (client->connect()) {
client->setReceiveCallback([conn](const char* data, size_t size) {
std::lock_guard<std::mutex> lock(clientMutex);
m_pInterface->SendMessageToConnection(conn, data, size, k_nSteamNetworkingSend_Reliable, nullptr);
});
clientMap[conn] = client;
std::cout << "Created TCP Client for connection on first message" << std::endl;
} else {
std::cerr << "Failed to connect TCP Client for connection" << std::endl;
delete client;
}
}
// Send to corresponding TCP client if exists (for host)
if (clientMap.count(conn)) {
clientMap[conn]->send((const char*)pIncomingMsg->m_pData, pIncomingMsg->m_cbSize);
}
pIncomingMsg->Release();
}
}
}
// Start ImGui frame // Start ImGui frame
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame(); ImGui_ImplGlfw_NewFrame();
@@ -431,6 +384,9 @@ int main() {
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
// Stop message handler
messageHandler.stop();
// Cleanup // Cleanup
if (g_hConnection != k_HSteamNetConnection_Invalid) { if (g_hConnection != k_HSteamNetConnection_Invalid) {
m_pInterface->CloseConnection(g_hConnection, 0, nullptr, false); m_pInterface->CloseConnection(g_hConnection, 0, nullptr, false);

View File

@@ -1,240 +0,0 @@
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <steam_api.h>
#include <isteamnetworkingsockets.h>
#include <isteamnetworkingutils.h>
#include <steamnetworkingtypes.h>
// Global variables for callbacks
HSteamNetConnection g_hConnection = k_HSteamNetConnection_Invalid;
bool g_isConnected = false;
// Callback function for connection status changes
void OnSteamNetConnectionStatusChanged(SteamNetConnectionStatusChangedCallback_t *pInfo)
{
std::cout << "Connection status changed: " << pInfo->m_info.m_eState << std::endl;
if (pInfo->m_eOldState == k_ESteamNetworkingConnectionState_None && pInfo->m_info.m_eState == k_ESteamNetworkingConnectionState_Connecting)
{
// Incoming connection, accept it
SteamNetworkingSockets()->AcceptConnection(pInfo->m_hConn);
g_hConnection = pInfo->m_hConn;
g_isConnected = true;
std::cout << "Accepted incoming connection" << std::endl;
}
else if (pInfo->m_eOldState == k_ESteamNetworkingConnectionState_Connecting && pInfo->m_info.m_eState == k_ESteamNetworkingConnectionState_Connected)
{
// Client connected successfully
g_isConnected = true;
std::cout << "Connected to host" << std::endl;
}
else if (pInfo->m_info.m_eState == k_ESteamNetworkingConnectionState_ClosedByPeer || pInfo->m_info.m_eState == k_ESteamNetworkingConnectionState_ProblemDetectedLocally)
{
// Connection closed
g_isConnected = false;
g_hConnection = k_HSteamNetConnection_Invalid;
std::cout << "Connection closed" << std::endl;
}
}
int main() {
// Initialize Steam API
if (!SteamAPI_Init()) {
std::cerr << "Failed to initialize Steam API" << std::endl;
return 1;
}
// Initialize Steam Networking Sockets
SteamNetworkingUtils()->InitRelayNetworkAccess();
// Set global callback for connection status changes
SteamNetworkingUtils()->SetGlobalCallback_SteamNetConnectionStatusChanged(OnSteamNetConnectionStatusChanged);
// Initialize GLFW
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
SteamAPI_Shutdown();
return -1;
}
// Create window
GLFWwindow* window = glfwCreateWindow(1280, 720, "Steam P2P Chat", nullptr, nullptr);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
SteamAPI_Shutdown();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
// Initialize ImGui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
(void)io;
// Load Chinese font if available
io.Fonts->AddFontFromFileTTF("font.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
ImGui::StyleColorsDark();
// Initialize ImGui backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 130");
// Steam Networking variables
HSteamListenSocket hListenSock = k_HSteamListenSocket_Invalid;
ISteamNetworkingSockets* m_pInterface = SteamNetworkingSockets();
// Chat variables
std::vector<std::string> messages;
char inputBuffer[256] = "";
CSteamID selectedFriend;
bool isHost = false;
bool isClient = false;
char filterBuffer[256] = "";
// Get friends list
std::vector<CSteamID> friendsList;
int friendCount = SteamFriends()->GetFriendCount(k_EFriendFlagAll);
for (int i = 0; i < friendCount; ++i) {
CSteamID friendID = SteamFriends()->GetFriendByIndex(i, k_EFriendFlagAll);
friendsList.push_back(friendID);
}
// Main loop
while (!glfwWindowShouldClose(window)) {
// Poll events
glfwPollEvents();
// Run Steam callbacks
SteamAPI_RunCallbacks();
// Poll networking
m_pInterface->RunCallbacks();
// Receive messages
if (g_isConnected) {
ISteamNetworkingMessage* pIncomingMsg = nullptr;
int numMsgs = m_pInterface->ReceiveMessagesOnConnection(g_hConnection, &pIncomingMsg, 1);
if (numMsgs > 0 && pIncomingMsg) {
std::string msg((char*)pIncomingMsg->m_pData, pIncomingMsg->m_cbSize);
messages.push_back("Friend: " + msg);
pIncomingMsg->Release();
}
}
// Start ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// Main menu
ImGui::Begin("Steam P2P Chat");
if (!isHost && !g_isConnected) {
if (ImGui::Button("Host Chat Room")) {
// Create listen socket
hListenSock = m_pInterface->CreateListenSocketP2P(0, 0, nullptr);
if (hListenSock != k_HSteamListenSocket_Invalid) {
isHost = true;
// Set Rich Presence
std::string connectStr = std::to_string(SteamUser()->GetSteamID().ConvertToUint64());
SteamFriends()->SetRichPresence("connect", connectStr.c_str());
SteamFriends()->SetRichPresence("status", "Hosting Chat Room");
std::cout << "Hosting chat room. Connect string: " << connectStr << std::endl;
}
}
static char joinBuffer[256] = "";
ImGui::InputText("Host Steam ID", joinBuffer, IM_ARRAYSIZE(joinBuffer));
if (ImGui::Button("Join Chat Room")) {
uint64 hostID = std::stoull(joinBuffer);
CSteamID hostSteamID(hostID);
isClient = true;
// Connect to host
SteamNetworkingIdentity identity;
identity.SetSteamID(hostSteamID);
g_hConnection = m_pInterface->ConnectP2P(identity, 0, 0, nullptr);
if (g_hConnection != k_HSteamNetConnection_Invalid) {
// Connection initiated, wait for callback to confirm
std::cout << "Connecting to host..." << std::endl;
}
}
}
if (isHost) {
ImGui::Text("Hosting chat room. Invite friends!");
ImGui::Separator();
ImGui::InputText("Filter Friends", filterBuffer, IM_ARRAYSIZE(filterBuffer));
ImGui::Text("Friends:");
for (size_t i = 0; i < friendsList.size(); ++i) {
const char* name = SteamFriends()->GetFriendPersonaName(friendsList[i]);
std::string nameStr(name);
std::string filterStr(filterBuffer);
// Convert to lowercase for case-insensitive search
std::transform(nameStr.begin(), nameStr.end(), nameStr.begin(), ::tolower);
std::transform(filterStr.begin(), filterStr.end(), filterStr.begin(), ::tolower);
if (filterStr.empty() || nameStr.find(filterStr) != std::string::npos) {
if (ImGui::Button((std::string("Invite ") + name).c_str())) {
SteamFriends()->InviteUserToGame(friendsList[i], "");
}
}
}
}
ImGui::End();
// Chat window
if (g_isConnected) {
ImGui::Begin("Chat Room");
ImGui::Text("Chatting");
// Display messages
ImGui::BeginChild("Messages", ImVec2(0, -ImGui::GetFrameHeightWithSpacing() - 30), true);
for (const auto& msg : messages) {
ImGui::TextWrapped("%s", msg.c_str());
}
ImGui::EndChild();
// Input
if (ImGui::InputText("Message", inputBuffer, IM_ARRAYSIZE(inputBuffer), ImGuiInputTextFlags_EnterReturnsTrue)) {
if (strlen(inputBuffer) > 0) {
uint32 msgSize = static_cast<uint32>(strlen(inputBuffer) + 1);
m_pInterface->SendMessageToConnection(g_hConnection, inputBuffer, msgSize, k_nSteamNetworkingSend_Reliable, nullptr);
messages.push_back("You: " + std::string(inputBuffer));
memset(inputBuffer, 0, sizeof(inputBuffer));
}
}
ImGui::End();
}
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Swap buffers
glfwSwapBuffers(window);
}
// Cleanup
if (g_hConnection != k_HSteamNetConnection_Invalid) {
m_pInterface->CloseConnection(g_hConnection, 0, nullptr, false);
}
if (hListenSock != k_HSteamListenSocket_Invalid) {
m_pInterface->CloseListenSocket(hListenSock);
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
SteamAPI_Shutdown();
return 0;
}

View File

@@ -1,103 +0,0 @@
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <iostream>
#include <vector>
#include <string>
#include <steam_api.h>
int main() {
// Initialize Steam API
if (!SteamAPI_Init()) {
std::cerr << "Failed to initialize Steam API" << std::endl;
return 1;
}
// Initialize GLFW
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
SteamAPI_Shutdown();
return -1;
}
// Create window
GLFWwindow* window = glfwCreateWindow(1280, 720, "Steam Friends List", nullptr, nullptr);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
SteamAPI_Shutdown();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
// Initialize ImGui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
(void)io;
// Load Chinese font
io.Fonts->AddFontFromFileTTF("font.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
ImGui::StyleColorsDark();
// Initialize ImGui backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 130");
// Get friends list
std::vector<std::string> friendsList;
int friendCount = SteamFriends()->GetFriendCount(k_EFriendFlagAll);
for (int i = 0; i < friendCount; ++i) {
CSteamID friendID = SteamFriends()->GetFriendByIndex(i, k_EFriendFlagAll);
const char* name = SteamFriends()->GetFriendPersonaName(friendID);
friendsList.push_back(std::string("Friend ") + std::to_string(i + 1) + ": " + name);
}
// Main loop
while (!glfwWindowShouldClose(window)) {
// Poll events
glfwPollEvents();
// Start ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// Create a window for friends list
ImGui::Begin("Steam Friends List");
ImGui::Text("Number of friends: %d", friendCount);
ImGui::Separator();
if (friendCount > 0) {
ImGui::Columns(4, nullptr, true); // 4 columns, with borders
for (const auto& friendName : friendsList) {
ImGui::Text("%s", friendName.c_str());
ImGui::NextColumn();
}
ImGui::Columns(1); // Reset to 1 column
}
ImGui::End();
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Swap buffers
glfwSwapBuffers(window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
SteamAPI_Shutdown();
return 0;
}

90
steam_message_handler.cpp Normal file
View File

@@ -0,0 +1,90 @@
#include "steam_message_handler.h"
#include <iostream>
#include <cstring>
#include <chrono>
#include <thread>
#include <steam_api.h>
#include <isteamnetworkingsockets.h>
// Constants for control packets
const char* CONTROL_PREFIX = "CONTROL:";
const size_t CONTROL_PREFIX_LEN = 8;
SteamMessageHandler::SteamMessageHandler(boost::asio::io_context& io_context, ISteamNetworkingSockets* interface, std::vector<HSteamNetConnection>& connections, std::map<HSteamNetConnection, TCPClient*>& clientMap, std::mutex& clientMutex, std::unique_ptr<TCPServer>& server, bool& g_isHost, int& localPort)
: io_context_(io_context), m_pInterface_(interface), connections_(connections), clientMap_(clientMap), clientMutex_(clientMutex), server_(server), g_isHost_(g_isHost), localPort_(localPort), running_(false) {}
SteamMessageHandler::~SteamMessageHandler() {
stop();
}
void SteamMessageHandler::start() {
if (running_) return;
running_ = true;
thread_ = std::thread([this]() { run(); });
}
void SteamMessageHandler::stop() {
if (!running_) return;
running_ = false;
if (thread_.joinable()) {
thread_.join();
}
}
void SteamMessageHandler::run() {
while (running_) {
// Poll networking
m_pInterface_->RunCallbacks();
// Update user info (assuming userMap is accessible, but for simplicity, skip or add as param)
// Note: userMap update might need to be handled elsewhere or passed
// Receive messages
pollMessages();
// Sleep a bit to avoid busy loop
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void SteamMessageHandler::pollMessages() {
std::lock_guard<std::mutex> lock(clientMutex_);
for (auto conn : connections_) {
ISteamNetworkingMessage* pIncomingMsgs[10];
int numMsgs = m_pInterface_->ReceiveMessagesOnConnection(conn, pIncomingMsgs, 10);
for (int i = 0; i < numMsgs; ++i) {
ISteamNetworkingMessage* pIncomingMsg = pIncomingMsgs[i];
const char* data = (const char*)pIncomingMsg->m_pData;
size_t size = pIncomingMsg->m_cbSize;
if (size >= CONTROL_PREFIX_LEN && memcmp(data, CONTROL_PREFIX, CONTROL_PREFIX_LEN) == 0) {
// Handle control packet
handleControlPacket(data + CONTROL_PREFIX_LEN, size - CONTROL_PREFIX_LEN, conn);
} else {
// Normal forwarding
if (server_) {
server_->sendToAll((const char*)pIncomingMsg->m_pData, pIncomingMsg->m_cbSize);
}
// Lazy connect: Create TCP Client on first message if not already connected
if (clientMap_.find(conn) == clientMap_.end() && g_isHost_ && localPort_ > 0) {
TCPClient* client = new TCPClient("localhost", localPort_);
if (client->connect()) {
client->setReceiveCallback([conn, this](const char* data, size_t size) {
std::lock_guard<std::mutex> lock(clientMutex_);
m_pInterface_->SendMessageToConnection(conn, data, size, k_nSteamNetworkingSend_Reliable, nullptr);
});
clientMap_[conn] = client;
std::cout << "Created TCP Client for connection on first message" << std::endl;
} else {
std::cerr << "Failed to connect TCP Client for connection" << std::endl;
delete client;
}
}
// Send to corresponding TCP client if exists (for host)
if (clientMap_.count(conn)) {
clientMap_[conn]->send((const char*)pIncomingMsg->m_pData, pIncomingMsg->m_cbSize);
}
}
pIncomingMsg->Release();
}
}
}

39
steam_message_handler.h Normal file
View File

@@ -0,0 +1,39 @@
#ifndef STEAM_MESSAGE_HANDLER_H
#define STEAM_MESSAGE_HANDLER_H
#include <vector>
#include <map>
#include <mutex>
#include <thread>
#include <boost/asio.hpp>
#include <steamnetworkingtypes.h>
#include "tcp_server.h"
#include "tcp/tcp_client.h"
#include "control_packets.h"
class SteamMessageHandler {
public:
SteamMessageHandler(boost::asio::io_context& io_context, ISteamNetworkingSockets* interface, std::vector<HSteamNetConnection>& connections, std::map<HSteamNetConnection, TCPClient*>& clientMap, std::mutex& clientMutex, std::unique_ptr<TCPServer>& server, bool& g_isHost, int& localPort);
~SteamMessageHandler();
void start();
void stop();
private:
void run();
void pollMessages();
boost::asio::io_context& io_context_;
ISteamNetworkingSockets* m_pInterface_;
std::vector<HSteamNetConnection>& connections_;
std::map<HSteamNetConnection, TCPClient*>& clientMap_;
std::mutex& clientMutex_;
std::unique_ptr<TCPServer>& server_;
bool& g_isHost_;
int& localPort_;
std::thread thread_;
bool running_;
};
#endif // STEAM_MESSAGE_HANDLER_H

View File

@@ -43,9 +43,10 @@ void TCPClient::send(const std::string& message) {
void TCPClient::send(const char* data, size_t size) { void TCPClient::send(const char* data, size_t size) {
if (!connected_) return; if (!connected_) return;
// std::cout << "Sending " << size << " bytes" << std::endl; // std::cout << "Sending " << size << " bytes" << std::endl;
boost::asio::async_write(*socket_, boost::asio::buffer(data, size), [](const boost::system::error_code& error, std::size_t) { boost::asio::async_write(*socket_, boost::asio::buffer(data, size), [this](const boost::system::error_code& error, std::size_t) {
if (error) { if (error) {
std::cerr << "Send failed: " << error.message() << std::endl; std::cerr << "Send failed: " << error.message() << std::endl;
disconnect();
} }
}); });
} }
@@ -75,7 +76,11 @@ void TCPClient::handle_read(const boost::system::error_code& error, std::size_t
} }
start_read(); start_read();
} else { } else {
std::cerr << "Read failed: " << error.message() << std::endl; if (error == boost::asio::error::eof) {
std::cout << "Connection closed by peer" << std::endl;
} else {
std::cerr << "Read failed: " << error.message() << std::endl;
}
disconnect(); disconnect();
} }
} }

View File

@@ -2,12 +2,13 @@
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
TCPServer::TCPServer(int port) : port_(port), running_(false), acceptor_(io_context_), work_(boost::asio::make_work_guard(io_context_)) {} TCPServer::TCPServer(int port) : port_(port), running_(false), acceptor_(io_context_), work_(boost::asio::make_work_guard(io_context_)), hasAcceptedConnection_(false) {}
TCPServer::~TCPServer() { stop(); } TCPServer::~TCPServer() { stop(); }
bool TCPServer::start() { bool TCPServer::start() {
try { try {
hasAcceptedConnection_ = false;
tcp::endpoint endpoint(tcp::v4(), port_); tcp::endpoint endpoint(tcp::v4(), port_);
acceptor_.open(endpoint.protocol()); acceptor_.open(endpoint.protocol());
acceptor_.set_option(tcp::acceptor::reuse_address(true)); acceptor_.set_option(tcp::acceptor::reuse_address(true));
@@ -31,6 +32,7 @@ bool TCPServer::start() {
void TCPServer::stop() { void TCPServer::stop() {
running_ = false; running_ = false;
hasAcceptedConnection_ = false;
io_context_.stop(); io_context_.stop();
if (serverThread_.joinable()) { if (serverThread_.joinable()) {
serverThread_.join(); serverThread_.join();
@@ -65,9 +67,10 @@ void TCPServer::start_accept() {
std::lock_guard<std::mutex> lock(clientsMutex_); std::lock_guard<std::mutex> lock(clientsMutex_);
clients_.push_back(socket); clients_.push_back(socket);
} }
hasAcceptedConnection_ = true;
start_read(socket); start_read(socket);
} }
if (running_) { if (running_ && !hasAcceptedConnection_) {
start_accept(); start_accept();
} }
}); });
@@ -92,6 +95,11 @@ void TCPServer::start_read(std::shared_ptr<tcp::socket> socket) {
// Remove client // Remove client
std::lock_guard<std::mutex> lock(clientsMutex_); std::lock_guard<std::mutex> lock(clientsMutex_);
clients_.erase(std::remove(clients_.begin(), clients_.end(), socket), clients_.end()); clients_.erase(std::remove(clients_.begin(), clients_.end(), socket), clients_.end());
// Reset to allow new connection
hasAcceptedConnection_ = false;
if (running_) {
start_accept();
}
} }
}); });
} }

View File

@@ -42,4 +42,5 @@ private:
std::vector<std::shared_ptr<tcp::socket>> clients_; std::vector<std::shared_ptr<tcp::socket>> clients_;
std::mutex clientsMutex_; std::mutex clientsMutex_;
std::thread serverThread_; std::thread serverThread_;
bool hasAcceptedConnection_;
}; };