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.
This commit is contained in:
81
tcp/tcp_client.cpp
Normal file
81
tcp/tcp_client.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "tcp_client.h"
|
||||
#include <iostream>
|
||||
|
||||
TCPClient::TCPClient(const std::string& host, int port) : host_(host), port_(port), connected_(false), socket_(std::make_shared<tcp::socket>(io_context_)), work_(boost::asio::make_work_guard(io_context_)), buffer_(1024) {}
|
||||
|
||||
TCPClient::~TCPClient() { disconnect(); }
|
||||
|
||||
bool TCPClient::connect() {
|
||||
try {
|
||||
tcp::resolver resolver(io_context_);
|
||||
auto endpoints = resolver.resolve(host_, std::to_string(port_));
|
||||
boost::asio::connect(*socket_, endpoints);
|
||||
connected_ = true;
|
||||
clientThread_ = std::thread([this]() {
|
||||
std::cout << "Client thread started" << std::endl;
|
||||
io_context_.run();
|
||||
std::cout << "Client thread stopped" << std::endl;
|
||||
});
|
||||
start_read();
|
||||
std::cout << "Connected to " << host_ << ":" << port_ << std::endl;
|
||||
return true;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Failed to connect: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void TCPClient::disconnect() {
|
||||
connected_ = false;
|
||||
io_context_.stop();
|
||||
if (clientThread_.joinable()) {
|
||||
clientThread_.join();
|
||||
}
|
||||
if (socket_->is_open()) {
|
||||
socket_->close();
|
||||
}
|
||||
}
|
||||
|
||||
void TCPClient::send(const std::string& message) {
|
||||
send(message.c_str(), message.size());
|
||||
}
|
||||
|
||||
void TCPClient::send(const char* data, size_t size) {
|
||||
if (!connected_) return;
|
||||
// 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) {
|
||||
if (error) {
|
||||
std::cerr << "Send failed: " << error.message() << std::endl;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void TCPClient::setReceiveCallback(std::function<void(const std::string&)> callback) {
|
||||
receiveCallback_ = callback;
|
||||
}
|
||||
|
||||
void TCPClient::setReceiveCallback(std::function<void(const char*, size_t)> callback) {
|
||||
receiveCallbackBytes_ = callback;
|
||||
}
|
||||
|
||||
void TCPClient::start_read() {
|
||||
socket_->async_read_some(boost::asio::buffer(buffer_), [this](const boost::system::error_code& error, std::size_t bytes_transferred) {
|
||||
handle_read(error, bytes_transferred);
|
||||
});
|
||||
}
|
||||
|
||||
void TCPClient::handle_read(const boost::system::error_code& error, std::size_t bytes_transferred) {
|
||||
if (!error) {
|
||||
// std::cout << "Received " << bytes_transferred << " bytes" << std::endl;
|
||||
if (receiveCallbackBytes_) {
|
||||
receiveCallbackBytes_(buffer_.data(), bytes_transferred);
|
||||
} else if (receiveCallback_) {
|
||||
std::string message(buffer_.data(), bytes_transferred);
|
||||
receiveCallback_(message);
|
||||
}
|
||||
start_read();
|
||||
} else {
|
||||
std::cerr << "Read failed: " << error.message() << std::endl;
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
40
tcp/tcp_client.h
Normal file
40
tcp/tcp_client.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
// TCP Client class
|
||||
class TCPClient {
|
||||
public:
|
||||
TCPClient(const std::string& host, int port);
|
||||
~TCPClient();
|
||||
|
||||
bool connect();
|
||||
void disconnect();
|
||||
void send(const std::string& message);
|
||||
void send(const char* data, size_t size);
|
||||
void setReceiveCallback(std::function<void(const std::string&)> callback);
|
||||
void setReceiveCallback(std::function<void(const char*, size_t)> callback);
|
||||
|
||||
private:
|
||||
void start_read();
|
||||
void handle_read(const boost::system::error_code& error, std::size_t bytes_transferred);
|
||||
|
||||
std::string host_;
|
||||
int port_;
|
||||
bool connected_;
|
||||
boost::asio::io_context io_context_;
|
||||
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work_;
|
||||
std::shared_ptr<tcp::socket> socket_;
|
||||
std::thread clientThread_;
|
||||
std::mutex socketMutex_;
|
||||
std::function<void(const std::string&)> receiveCallback_;
|
||||
std::function<void(const char*, size_t)> receiveCallbackBytes_;
|
||||
std::vector<char> buffer_;
|
||||
};
|
||||
97
tcp/tcp_server.cpp
Normal file
97
tcp/tcp_server.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "tcp_server.h"
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
TCPServer::TCPServer(int port) : port_(port), running_(false), acceptor_(io_context_), work_(boost::asio::make_work_guard(io_context_)) {}
|
||||
|
||||
TCPServer::~TCPServer() { stop(); }
|
||||
|
||||
bool TCPServer::start() {
|
||||
try {
|
||||
tcp::endpoint endpoint(tcp::v4(), port_);
|
||||
acceptor_.open(endpoint.protocol());
|
||||
acceptor_.set_option(tcp::acceptor::reuse_address(true));
|
||||
acceptor_.bind(endpoint);
|
||||
acceptor_.listen();
|
||||
|
||||
running_ = true;
|
||||
serverThread_ = std::thread([this]() {
|
||||
std::cout << "Server thread started" << std::endl;
|
||||
io_context_.run();
|
||||
std::cout << "Server thread stopped" << std::endl;
|
||||
});
|
||||
start_accept();
|
||||
std::cout << "TCP server started on port " << port_ << std::endl;
|
||||
return true;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Failed to start TCP server: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void TCPServer::stop() {
|
||||
running_ = false;
|
||||
io_context_.stop();
|
||||
if (serverThread_.joinable()) {
|
||||
serverThread_.join();
|
||||
}
|
||||
acceptor_.close();
|
||||
}
|
||||
|
||||
void TCPServer::sendToAll(const std::string& message, std::shared_ptr<tcp::socket> excludeSocket) {
|
||||
sendToAll(message.c_str(), message.size(), excludeSocket);
|
||||
}
|
||||
|
||||
void TCPServer::sendToAll(const char* data, size_t size, std::shared_ptr<tcp::socket> excludeSocket) {
|
||||
std::lock_guard<std::mutex> lock(clientsMutex_);
|
||||
for (auto& client : clients_) {
|
||||
if (client != excludeSocket) {
|
||||
boost::asio::async_write(*client, boost::asio::buffer(data, size), [](const boost::system::error_code&, std::size_t) {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int TCPServer::getClientCount() {
|
||||
std::lock_guard<std::mutex> lock(clientsMutex_);
|
||||
return clients_.size();
|
||||
}
|
||||
|
||||
void TCPServer::start_accept() {
|
||||
auto socket = std::make_shared<tcp::socket>(io_context_);
|
||||
acceptor_.async_accept(*socket, [this, socket](const boost::system::error_code& error) {
|
||||
if (!error) {
|
||||
std::cout << "New client connected" << std::endl;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(clientsMutex_);
|
||||
clients_.push_back(socket);
|
||||
}
|
||||
start_read(socket);
|
||||
}
|
||||
if (running_) {
|
||||
start_accept();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void TCPServer::start_read(std::shared_ptr<tcp::socket> socket) {
|
||||
auto buffer = std::make_shared<std::vector<char>>(1024);
|
||||
socket->async_read_some(boost::asio::buffer(*buffer), [this, socket, buffer](const boost::system::error_code& error, std::size_t bytes_transferred) {
|
||||
if (!error) {
|
||||
// std::cout << "Received " << bytes_transferred << " bytes from client" << std::endl;
|
||||
if (!forwarding) {
|
||||
forwarding = true;
|
||||
if (g_isConnected) {
|
||||
m_pInterface->SendMessageToConnection(g_hConnection, buffer->data(), bytes_transferred, k_nSteamNetworkingSend_Reliable, nullptr);
|
||||
}
|
||||
forwarding = false;
|
||||
}
|
||||
sendToAll(buffer->data(), bytes_transferred, socket);
|
||||
start_read(socket);
|
||||
} else {
|
||||
std::cout << "Client disconnected" << std::endl;
|
||||
// Remove client
|
||||
std::lock_guard<std::mutex> lock(clientsMutex_);
|
||||
clients_.erase(std::remove(clients_.begin(), clients_.end(), socket), clients_.end());
|
||||
}
|
||||
});
|
||||
}
|
||||
45
tcp/tcp_server.h
Normal file
45
tcp/tcp_server.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#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_;
|
||||
};
|
||||
Reference in New Issue
Block a user