Files
ConnectTools/tcp/tcp_server.h
Ayndpa 0f76252c16 Add initial implementation of ConnectTool with Dear ImGui and Steam Networking
- Created project files for ConnectTool including filters and user settings.
- Added README.md with setup instructions and prerequisites for building the project.
- Implemented imgui_hello.cpp as a simple Dear ImGui application using GLFW and OpenGL.
- Developed online_game_tool.cpp for hosting and joining game rooms using Steam Networking.
- Created p2p_chat.cpp for a peer-to-peer chat application utilizing Steam Networking.
- Implemented steam_friends.cpp to display the user's Steam friends list.
- Added TCP client and server classes for handling network communication.
- Integrated TCP server and client functionality into online_game_tool for real-time data exchange.
2025-11-17 20:41:03 +08:00

45 lines
1.2 KiB
C++

#pragma once
#include <boost/asio.hpp>
#include <memory>
#include <vector>
#include <string>
#include <thread>
#include <mutex>
#include <isteamnetworkingsockets.h>
#include <isteamnetworkingutils.h>
#include <steamnetworkingtypes.h>
using boost::asio::ip::tcp;
// Extern declarations for global variables used in TCPServer
extern HSteamNetConnection g_hConnection;
extern bool g_isConnected;
extern ISteamNetworkingSockets* m_pInterface;
extern bool forwarding;
// TCP Server class
class TCPServer {
public:
TCPServer(int port);
~TCPServer();
bool start();
void stop();
void sendToAll(const std::string& message, std::shared_ptr<tcp::socket> excludeSocket = nullptr);
void sendToAll(const char* data, size_t size, std::shared_ptr<tcp::socket> excludeSocket = nullptr);
int getClientCount();
private:
void start_accept();
void start_read(std::shared_ptr<tcp::socket> socket);
int port_;
bool running_;
boost::asio::io_context io_context_;
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work_;
tcp::acceptor acceptor_;
std::vector<std::shared_ptr<tcp::socket>> clients_;
std::mutex clientsMutex_;
std::thread serverThread_;
};