整理代码
This commit is contained in:
commit
c7b4e178ec
364 changed files with 59691 additions and 0 deletions
65
network/TCPClient.h
Normal file
65
network/TCPClient.h
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue