我在c++项目调用文件make的时候报错,其中这是AsyncHeartClient.h文件 "#ifndef… · The Chat Archive我在c++项目调用文件make的时候报错,其中这是AsyncHeartClient.h文件 "#ifndef OPENCVDEMOHEARTCLIENTH #define…
Download ▾
我在c++项目调用文件make的时候报错,其中这是AsyncHeartClient.h文件
"#ifndef OPENCVDEMO_HEARTCLIENT_H
#define OPENCVDEMO_HEARTCLIENT_H
#include
#include <boost/date_time/posix_time/posix_time_duration.hpp>
#include <boost/asio/deadline_timer.hpp>
#include "AsyncClient.h"
template <typename receivedMsgT,typename sendMsgT>
class AsyncHeartClient
{
public:
//心跳回调函数,在定时器中会自动发送心跳,也可以通过该函数执行一些其他自定义操作
typedef std::function<void(const sendMsgT &sendedMsg)> TimerCallBack;
public:
AsyncHeartClient() = delete;
code
AsyncHeartClient(const std::string &addr, int port, unsigned int heart_interval, ReadCallBack readCallBack = nullptr,
TimerCallBack timerCallBack = nullptr);
virtual ~AsyncHeartClient();
/*!
* 心跳初始化
* @param heart_interval 心跳间隔
* @param timerCallBack 自定义心跳函数
*/
//void InitHeartBeat(unsigned int heart_interval=200,TimerCallBack timerCallBack= nullptr);
/*!
* 获取接收的数据
* @param data
* @return 接收的数据是否已更新.true:是;false:否
*/
bool GetReceivedData(receivedMsgT &data);
/*!
* 设置发送给PLC的数据.Note:设置完毕后不会立即发送,需要等待定时器下一次执行到才会发送;会自动添加包头,包尾,心跳编号
* @param msg
*/
virtual void UpdateSendData(const sendMsgT &msg);
/*!
* 套接字是否连接.无法判断网线突然不通情况
* @return
*/
bool isConnected() const;
/*!
* 是否需要重新初始化对象.针对网线突然中断情况
* @return true:重新初始化该对象
*/
bool isNeedReConstruct();
private:
/*!
* 心跳初始化
* @param heart_interval 心跳间隔
* @param timerCallBack 自定义心跳函数
*/
void InitHeartBeat(unsigned int heart_interval = 200, TimerCallBack timerCallBack = nullptr);
code
//心跳数据发送.心跳间隔:m_timerInterval
void SendHeartBeat(const boost::system::error_code &ec);
//消息接收回调
void ReceivedCallBack(const std::string &addr, int port, CallBackType type, const std::vector<unsigned char> &data,
const size_t len);
protected:
//通信模块
std::shared_ptr m_pSocket;
Assistant
好的,请继续提供另外两个文件以及在执行 make 时出现的具体错误信息。这将有助于更准确地诊断和解决问题。
如果方便的话,请同时提供以下信息:
- 编译错误信息:完整的错误输出,包括错误代码和相关文件行号。
- 项目结构:简要说明项目的文件组织结构,尤其是涉及的源文件和头文件。
- 编译指令:
Makefile 中的相关部分或您使用的具体编译命令。
这些信息将帮助我更有效地协助您解决编译问题。
//自定义消息处理函数
ReadCallBack m_readCallBack;
//自定义定时执行函数
TimerCallBack m_timerCallBack;
//定时间隔.心跳数据发送间隔
boost::posix_time::milliseconds m_timerInterval;
std::shared_ptr<boost::asio::deadline_timer> m_pTimer;
//心跳编号
std::atomic_ushort m_heartNo;
std::atomic_ushort m_plcHeartNo;//收到的plc心跳编号
std::atomic<sendMsgT> m_sendMsg;
std::atomic<receivedMsgT> m_receivedMsg;
std::atomic_bool m_bRecMsgUpdated;
std::atomic_int m_unchangeNum;
};
#endif //OPENCVDEMO_HEARTCLIENT_H
"
AsyncHeartClient.hpp文件代码
"#include "AsyncHeartClient.h"
#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include
#include
template<typename receivedMsgT, typename sendMsgT>
AsyncHeartClient<receivedMsgT, sendMsgT>::AsyncHeartClient(const std::string &addr, int port,
unsigned int heart_interval, ReadCallBack readCallBack,
TimerCallBack timerCallBack)
:m_pSocket(new AsyncClient(addr, port, std::bind(&AsyncHeartClient::ReceivedCallBack, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5),
sizeof(receivedMsgT))), m_readCallBack(readCallBack), m_timerCallBack(nullptr), m_timerInterval(200), m_pTimer(nullptr),
m_heartNo(0),m_plcHeartNo(0),
m_sendMsg(sendMsgT()), m_receivedMsg(receivedMsgT()), m_bRecMsgUpdated(false), m_unchangeNum(0)
{
InitHeartBeat(heart_interval, timerCallBack);
}
template<typename receivedMsgT, typename sendMsgT>
AsyncHeartClient<receivedMsgT, sendMsgT>::~AsyncHeartClient()
{
m_pTimer->cancel();
m_pTimer = nullptr;
//std::this_thread::sleep_for(std::chrono::milliseconds(2*m_timerInterval.total_milliseconds()));
}
template<typename receivedMsgT, typename sendMsgT>
bool AsyncHeartClient<receivedMsgT, sendMsgT>::isConnected() const
{
return m_pSocket->isConnected();
}
template<typename receivedMsgT, typename sendMsgT>
bool AsyncHeartClient<receivedMsgT, sendMsgT>::isNeedReConstruct()
{
bool bTimeOut = false;
if (m_unchangeNum * m_timerInterval.total_milliseconds() > 5000)
{
m_unchangeNum = 0;
bTimeOut = true;
}
code
return (this->isConnected() && bTimeOut);
template<typename receivedMsgT, typename sendMsgT>
void AsyncHeartClient<receivedMsgT, sendMsgT>::InitHeartBeat(unsigned int heart_interval, TimerCallBack timerCallBack)
{
assert(heart_interval > 0);
code
m_timerInterval = boost::posix_time::milliseconds(heart_interval);
m_pTimer = std::make_shared<boost::asio::deadline_timer>(*(m_pSocket->pIOservice()), m_timerInterval);
m_timerCallBack = timerCallBack;
// Set an expiry time relative to now.
m_pTimer->expires_from_now(m_timerInterval);
// Posts the timer event
m_pTimer->async_wait(boost::bind(&AsyncHeartClient::SendHeartBeat, this, boost::asio::placeholders::error));
template<typename receivedMsgT, typename sendMsgT>
void AsyncHeartClient<receivedMsgT, sendMsgT>::SendHeartBeat(const boost::system::error_code &ec)
{
auto start_timer = &
{
// Set an expiry time relative to now.
m_pTimer->expires_from_now(m_timerInterval);
// Posts the timer event
m_pTimer->async_wait(boost::bind(&AsyncHeartClient::SendHeartBeat, this, boost::asio::placeholders::error));
};
code
if (ec)
{
if (m_pTimer)
start_timer();
return;
}
//1°
++m_unchangeNum;
//2°发送心跳
auto msg = m_sendMsg.load(std::memory_order_relaxed);
msg.head = sendMsgT().head;
msg.heartNo = m_heartNo++;
msg.plcHeartNo = m_plcHeartNo;
msg.tail = sendMsgT().tail;
std::vector<unsigned char> sendBuffer(sizeof(msg), 0);
std::memcpy(&sendBuffer[0], &msg, sizeof(msg));
m_pSocket->AsyncSend(sendBuffer);
//3°执行自定义函数
if (m_timerCallBack) m_timerCallBack(msg);
//4°启动定时器
start_timer();
template<typename receivedMsgT, typename sendMsgT>
bool AsyncHeartClient<receivedMsgT, sendMsgT>::GetReceivedData(receivedMsgT &data)
{
std::memset(&data, 0, sizeof(receivedMsgT));
code
bool bUpdated = m_bRecMsgUpdated;
data = m_receivedMsg.load(std::memory_order_relaxed);
m_bRecMsgUpdated = false;
return bUpdated;
template<typename receivedMsgT, typename sendMsgT>
void AsyncHeartClient<receivedMsgT, sendMsgT>::UpdateSendData(const sendMsgT &msg)
{
m_sendMsg.store(msg, std::memory_order_relaxed);
}
template<typename receivedMsgT, typename sendMsgT>
void AsyncHeartClient<receivedMsgT, sendMsgT>::ReceivedCallBack(const std::string &addr, int port, CallBackType type,
const std::vector &data,
const size_t len)
{
if (m_readCallBack)m_readCallBack(addr, port, type, data, len);
code
switch (type)
{
case RECVED_MSG:
{
m_unchangeNum = 0;
if (len != sizeof(receivedMsgT))
return;
receivedMsgT _rMsg;
std::memcpy(&_rMsg, &data[0], sizeof(receivedMsgT));
if (receivedMsgT().head != _rMsg.head || receivedMsgT().tail != _rMsg.tail)
return;
m_receivedMsg.store(_rMsg, std::memory_order_relaxed);
m_plcHeartNo = _rMsg.heartNo;
m_bRecMsgUpdated = true;
// std::cout<<"Hex code:";
// for (int i = 0; i < len; ++i)
// {
// printf("%02X ",data[i]);
// }
// std::cout<<std::endl;
code
break;
}
default:
break;
}
Not yet included in a published release.