整理代码

This commit is contained in:
蒲爽 2026-09-28 21:33:04 +08:00
commit c7b4e178ec
364 changed files with 59691 additions and 0 deletions

6
network/CMakeLists.txt Normal file
View file

@ -0,0 +1,6 @@
set(target network)
aux_source_directory(. NETWORK_SOURCES)
add_library(${target} STATIC
${NETWORK_SOURCES}
)

53
network/HTTPServer.cpp Normal file
View file

@ -0,0 +1,53 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "HTTPServer.h"
using namespace Poco::Net;
namespace Network
{
class RouterRequestHandlerFactory: public HTTPRequestHandlerFactory
{
public:
HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request) override
{
throw Poco::NotFoundException("Not found");
}
};
/*
*******************************************************************************
* CLASS DEFINE
*******************************************************************************
*/
std::string HTTPServer::HOST = "host";
std::string HTTPServer::PORT = "port";
HTTPServer::HTTPServer(): _pHTTPServer(nullptr)
{
setParameter(HOST, std::string("::"));
setParameter(PORT, 80);
}
HTTPServer::~HTTPServer()
{
}
void HTTPServer::start()
{
Poco::Net::SocketAddress address(getParameter<std::string>(HOST), getParameter<int>(PORT));
Poco::Net::ServerSocket socket(address);
Poco::Net::HTTPServerParams* pParams = new Poco::Net::HTTPServerParams();
pParams->setKeepAlive(false);
_pHTTPServer = new Poco::Net::HTTPServer(new RouterRequestHandlerFactory, socket, pParams);
_pHTTPServer->start();
}
void HTTPServer::stop()
{
_pHTTPServer->stopAll();
}
}

37
network/HTTPServer.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef DC9BEC60_0C80_41F2_B1F7_289C63388DC1
#define DC9BEC60_0C80_41F2_B1F7_289C63388DC1
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "Poco/Net/HTTPServer.h"
#include "Poco/SharedPtr.h"
#include "util/Configuration.h"
namespace Network
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class HTTPServer: public Util::Configuration
{
public:
static std::string HOST;
static std::string PORT;
public:
HTTPServer();
~HTTPServer();
void start();
void stop();
private:
Poco::SharedPtr<Poco::Net::HTTPServer> _pHTTPServer;
};
}
#endif

View file

@ -0,0 +1,40 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "NetworkSubsystem.h"
#include "Application.h"
#include "Logger.h"
#include "Poco/Exception.h"
#include "Poco/Net/DNS.h"
namespace Network
{
/*
*******************************************************************************
* CLASS DEFINE
*******************************************************************************
*/
NetworkSubsystem::NetworkSubsystem()
{
}
NetworkSubsystem::~NetworkSubsystem()
{
}
const char* NetworkSubsystem::name() const
{
return "Network";
}
void NetworkSubsystem::initialize(Poco::Util::Application& app)
{
}
void NetworkSubsystem::uninitialize()
{
stopAll();
}
}

View file

@ -0,0 +1,47 @@
#ifndef __NETWORK_SUBSYSTEM__H__
#define __NETWORK_SUBSYSTEM__H__
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "Poco/Util/Application.h"
#include "util/Subsystem.h"
namespace Network
{
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
class NetworkSubsystem: public Util::Subsystem
{
public:
NetworkSubsystem();
~NetworkSubsystem();
virtual const char* name() const override;
protected:
virtual void initialize(Poco::Util::Application& app);
virtual void uninitialize();
public:
struct ZLStreamPushMessage
{
std::string appname;
std::string streamid;
short type;
ZLStreamPushMessage(const std::string& appname, const std::string& streamid, short type)
: appname(appname), streamid(streamid), type(type)
{
}
};
Poco::BasicEvent<ZLStreamPushMessage> ZLStreamPushMessageEvent;
private:
};
}
#endif

148
network/TCPClient.cpp Normal file
View file

@ -0,0 +1,148 @@
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include "TCPClient.h"
#include <Poco/Net/NetException.h>
namespace Network
{
TCPClient::TCPClient(std::string host, int port, Poco::Timespan timeout, uint32_t sz)
: Poco::Task("TCPClient"), _address(host, port), _timeout(timeout), _bufsz(sz), _pBuffer(new uint8_t[sz])
{
}
TCPClient::~TCPClient()
{
}
int TCPClient::send(const uint8_t* data, uint32_t len)
{
try
{
int rc = _socket.sendBytes(data, len);
return rc;
}
catch (const std::exception& e)
{
}
return 0;
}
void TCPClient::start()
{
if (!_thread.isRunning())
_thread.start(*this);
}
void TCPClient::stop()
{
if (_thread.isRunning())
{
cancel();
_thread.join();
}
}
void TCPClient::setStateChangedCallback(const StateChangedCallback& callback)
{
_stateChanged = std::move(callback);
}
void TCPClient::setDataIncomingCallback(const DataIncomingCallback& callback)
{
_dataIncoming = std::move(callback);
}
bool TCPClient::connect()
{
try
{
_socket.connect(_address, _timeout);
_socket.setReceiveTimeout(Poco::Timespan(1000));
setState(State::CONNECTED, "Connected");
return true;
}
catch (Poco::TimeoutException& e)
{
setState(State::CONNECT_TIMEOUT, e.displayText());
}
catch (Poco::Net::ConnectionRefusedException& e)
{
setState(State::CONNECT_REFUSED, e.displayText());
}
catch (Poco::Exception& e)
{
setState(State::CONNECT_REFUSED, e.displayText());
}
return false;
}
void TCPClient::runTask()
{
while (!isCancelled())
{
while (!isCancelled())
{
setState(State::CONNECTING, "Connecting");
if (connect())
break;
Poco::Thread::sleep(1000);
}
while (!isCancelled())
{
try
{
int rc = _socket.receiveBytes(_pBuffer, _bufsz);
if (rc <= 0)
{
setState(State::DISCONNECTED, "Connection lost");
_socket.close();
break;
}
receive(_pBuffer, rc);
}
catch (Poco::TimeoutException& e)
{
}
catch (Poco::Exception& e)
{
setState(State::DISCONNECTED, "Connection lost");
_socket.close();
break;
}
}
Poco::Thread::sleep(1000);
}
try
{
_socket.close();
}
catch (...)
{
}
}
void TCPClient::setState(State state, const std::string& desc)
{
if (_stateChanged)
_stateChanged(state, desc);
}
void TCPClient::receive(const uint8_t* data, uint32_t len)
{
if (_dataIncoming)
_dataIncoming(data, len);
}
}

65
network/TCPClient.h Normal file
View file

@ -0,0 +1,65 @@
#ifndef __TCP_CLIENT_H__
#define __TCP_CLIENT_H__
/*
*******************************************************************************
* INCLUDE
*******************************************************************************
*/
#include <Poco/Net/StreamSocket.h>
#include <Poco/Task.h>
#include <Poco/Timespan.h>
#include <functional>
/*
*******************************************************************************
* CLASS DECLARE
*******************************************************************************
*/
namespace Network
{
class TCPClient: public Poco::Task
{
public:
enum class State
{
DISCONNECTED,
CONNECT_TIMEOUT,
CONNECT_REFUSED,
CONNECTING,
CONNECTED,
};
using StateChangedCallback = std::function<void(State, const std::string&)>;
using DataIncomingCallback = std::function<void(const uint8_t*, uint32_t)>;
public:
TCPClient(std::string host, int port, Poco::Timespan timeout, uint32_t sz = 10240);
~TCPClient();
int send(const uint8_t* data, uint32_t len);
void start();
void stop();
void setStateChangedCallback(const StateChangedCallback& callback);
void setDataIncomingCallback(const DataIncomingCallback& callback);
protected:
bool connect();
void receive(const uint8_t* data, uint32_t len);
virtual void runTask() override;
void setState(State state, const std::string& desc);
private:
Poco::Thread _thread;
Poco::Net::StreamSocket _socket;
Poco::Net::SocketAddress _address;
Poco::Timespan _timeout;
uint32_t _bufsz;
Poco::SharedPtr<uint8_t> _pBuffer;
StateChangedCallback _stateChanged;
DataIncomingCallback _dataIncoming;
};
}
#endif

3
network/network.cmake Normal file
View file

@ -0,0 +1,3 @@
add_subdirectory(network)
include_directories(network)
target_link_libraries(${PROJNAME} PRIVATE network)