mirror of
https://github.com/maxDcb/C2TeamServer
synced 2026-06-06 16:14:27 +00:00
BeaconTCP
This commit is contained in:
@@ -2,6 +2,14 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define DEBUG_BUILD
|
||||
|
||||
#ifdef DEBUG_BUILD
|
||||
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
|
||||
#define DEBUG(x) do { std::cout << __FILENAME__ << " - " << __func__ << ": " << x << std::endl; } while (0)
|
||||
#else
|
||||
#define DEBUG(x) do {} while (0)
|
||||
#endif
|
||||
|
||||
using boost::asio::ip::tcp;
|
||||
using namespace SocketHandler;
|
||||
@@ -24,34 +32,20 @@ void readSocketTcp(boost::asio::ip::tcp::socket* socket_, char* data, int nbByte
|
||||
Server::Server(int port)
|
||||
{
|
||||
m_port = port;
|
||||
m_initDone=false;
|
||||
}
|
||||
|
||||
|
||||
void Server::reset()
|
||||
{
|
||||
if(m_socketTcp)
|
||||
delete m_socketTcp;
|
||||
|
||||
m_initDone=false;
|
||||
}
|
||||
|
||||
|
||||
void Server::initServer()
|
||||
{
|
||||
creatServerTcp(m_port);
|
||||
m_initDone=true;
|
||||
}
|
||||
|
||||
Server::~Server()
|
||||
{
|
||||
delete m_socketTcp;
|
||||
}
|
||||
|
||||
|
||||
void Server::creatServerTcp(int port)
|
||||
void Server::initServer()
|
||||
{
|
||||
tcp::acceptor acceptor_(m_ioService, tcp::endpoint(tcp::v4(), port));
|
||||
DEBUG("initServer");
|
||||
|
||||
tcp::acceptor acceptor_(m_ioService, tcp::endpoint(tcp::v4(), m_port));
|
||||
m_socketTcp=new tcp::socket(m_ioService);
|
||||
acceptor_.accept(*m_socketTcp);
|
||||
}
|
||||
@@ -59,19 +53,15 @@ void Server::creatServerTcp(int port)
|
||||
|
||||
bool Server::sendData(std::string& data)
|
||||
{
|
||||
if(!m_initDone)
|
||||
{
|
||||
// std::cerr << "Server: No connection" << std::endl;
|
||||
return false;
|
||||
}
|
||||
DEBUG("sendData");
|
||||
|
||||
int nbBytes = data.size();
|
||||
sendSocketTcp(m_socketTcp, (char*)&nbBytes, sizeof(int), &m_error);
|
||||
|
||||
if(m_error)
|
||||
{
|
||||
// std::cerr << "send failed: " << m_error.message() << std::endl;
|
||||
reset();
|
||||
DEBUG("sendData failed: " << m_error.message());
|
||||
closeConnection();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -81,8 +71,8 @@ bool Server::sendData(std::string& data)
|
||||
|
||||
if(m_error)
|
||||
{
|
||||
// std::cerr << "send failed: " << m_error << std::endl;
|
||||
reset();
|
||||
DEBUG("sendData failed: " << m_error.message());
|
||||
closeConnection();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -93,11 +83,7 @@ bool Server::sendData(std::string& data)
|
||||
|
||||
bool Server::receive(std::string& data)
|
||||
{
|
||||
if(!m_initDone)
|
||||
{
|
||||
// std::cerr << "Server: No connection" << std::endl;
|
||||
return false;
|
||||
}
|
||||
DEBUG("receive");
|
||||
|
||||
int nbBytes=0;
|
||||
readSocketTcp(m_socketTcp, (char*)&nbBytes, sizeof(int), &m_error);
|
||||
@@ -105,8 +91,8 @@ bool Server::receive(std::string& data)
|
||||
|
||||
if(m_error)
|
||||
{
|
||||
// std::cerr << "receive failed: " << m_error.message() << std::endl;
|
||||
reset();
|
||||
DEBUG("receive failed: " << m_error.message());
|
||||
closeConnection();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -116,8 +102,8 @@ bool Server::receive(std::string& data)
|
||||
|
||||
if(m_error)
|
||||
{
|
||||
// std::cerr << "receive failed: " << m_error.message() << std::endl;
|
||||
reset();
|
||||
DEBUG("receive failed: " << m_error.message());
|
||||
closeConnection();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -126,6 +112,15 @@ bool Server::receive(std::string& data)
|
||||
}
|
||||
|
||||
|
||||
void Server::closeConnection()
|
||||
{
|
||||
DEBUG("closeConnection");
|
||||
|
||||
if(m_socketTcp)
|
||||
delete m_socketTcp;
|
||||
}
|
||||
|
||||
|
||||
Client::Client(std::string& ip, int port)
|
||||
{
|
||||
m_ipServer = ip;
|
||||
@@ -143,41 +138,13 @@ Client::~Client()
|
||||
}
|
||||
|
||||
|
||||
bool Client::reset()
|
||||
{
|
||||
if(m_socketTcp)
|
||||
{
|
||||
delete m_socketTcp;
|
||||
m_socketTcp=nullptr;
|
||||
}
|
||||
|
||||
bool res = creatClientTcp(m_port, m_ipServer);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
bool Client::initConnection()
|
||||
{
|
||||
bool res = reset();
|
||||
return res;
|
||||
}
|
||||
DEBUG("initConnection");
|
||||
|
||||
|
||||
void Client::closeConnection()
|
||||
{
|
||||
if(m_socketTcp)
|
||||
{
|
||||
delete m_socketTcp;
|
||||
m_socketTcp=nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Client::creatClientTcp(int port, std::string& ip)
|
||||
{
|
||||
boost::system::error_code error;
|
||||
m_socketTcp=new tcp::socket(m_ioService);
|
||||
m_socketTcp->connect( tcp::endpoint( boost::asio::ip::address::from_string(ip), port ), error);
|
||||
m_socketTcp->connect( tcp::endpoint( boost::asio::ip::address::from_string(m_ipServer), m_port ), error);
|
||||
if(error)
|
||||
return false;
|
||||
|
||||
@@ -187,12 +154,15 @@ bool Client::creatClientTcp(int port, std::string& ip)
|
||||
|
||||
bool Client::sendData(std::string& data)
|
||||
{
|
||||
DEBUG("sendData");
|
||||
|
||||
int nbBytes = data.size();
|
||||
sendSocketTcp(m_socketTcp, (char*)&nbBytes, sizeof(int), &m_error);
|
||||
|
||||
if(m_error)
|
||||
{
|
||||
// std::cerr << "send failed: " << m_error.message() << std::endl;
|
||||
DEBUG("sendData failed: " << m_error.message());
|
||||
closeConnection();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -202,7 +172,8 @@ bool Client::sendData(std::string& data)
|
||||
|
||||
if(m_error)
|
||||
{
|
||||
// std::cerr << "send failed: " << m_error << std::endl;
|
||||
DEBUG("sendData failed: " << m_error.message());
|
||||
closeConnection();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -213,13 +184,16 @@ bool Client::sendData(std::string& data)
|
||||
|
||||
bool Client::receive(std::string& data)
|
||||
{
|
||||
DEBUG("receive");
|
||||
|
||||
int nbBytes=0;
|
||||
readSocketTcp(m_socketTcp, (char*)&nbBytes, sizeof(int), &m_error);
|
||||
data.resize(nbBytes);
|
||||
|
||||
if(m_error)
|
||||
{
|
||||
// std::cerr << "receive failed: " << m_error.message() << std::endl;
|
||||
DEBUG("receive failed: " << m_error.message());
|
||||
closeConnection();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -229,7 +203,8 @@ bool Client::receive(std::string& data)
|
||||
|
||||
if(m_error)
|
||||
{
|
||||
// std::cerr << "receive failed: " << m_error.message() << std::endl;
|
||||
DEBUG("receive failed: " << m_error.message());
|
||||
closeConnection();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -238,3 +213,15 @@ bool Client::receive(std::string& data)
|
||||
}
|
||||
|
||||
|
||||
void Client::closeConnection()
|
||||
{
|
||||
DEBUG("closeConnection");
|
||||
|
||||
if(m_socketTcp)
|
||||
{
|
||||
delete m_socketTcp;
|
||||
m_socketTcp=nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user