如何最好地使网络函数从其他类中访问
How to best make network functions accessible from other classes?
c 新手。
我试图通过研究一个项目来创建一个机器人,该机器人从中获取命令并将遥控器发送回远程服务器来教授自己。
我在机器人上有一个TcpCom
类,其中包含套接字连接和公共功能以从服务器发送消息并接收消息:
#ifndef TCPCOM_H
#define TCPCOM_H
#define BOOST_DATE_TIME_NO_LIB
#include <boost/asio.hpp>
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <deque>
#include <mutex>
#include "COM.h"
class TcpCom : public COM
{
public:
TcpCom() : io_srv(), tcpSocket(io_srv), remoteHost(""), remotePort(""), connectedToRemoteHost(false), outboundMsgQueue(), outboundMsgQueueMutex(),
messagesInOutboundMsgQueue(0), incomingMsgQueue(), incomingMsgQueueMutex()
{}
int initialize();
void connectToRemoteHost(const std::string host, const std::string port);
void disconnectFromRemoteHost();
bool messagesWaitingInIncomingMsgQueue();
SoftwareBusMsg getMsgFromIncomingMsgQueue();
void addMsgToOutboundMsgQueue(SoftwareBusMsg& sbMsg);
bool isConnected();
private:
void writeOutboundMsgToSocket();
void deserializeHeader(std::string headerStr, MsgHeader& msgHdr);
void addMessageToIncomingMsgQueue(SoftwareBusMsg& sbMsg);
void readIncomingMsgHeader(MsgHeader& msgHdr);
std::string readIncomingMsgData(uint32_t msgDataLength);
SoftwareBusMsg readMsgFromSocket();
void incomingMsgThread();
void outboundMsgThread();
void startReadAndWriteThreads();
boost::asio::io_service io_srv;
boost::asio::ip::tcp::socket tcpSocket;
std::string remoteHost;
std::string remotePort;
bool connectedToRemoteHost;
std::deque<std::string> outboundMsgQueue;
std::mutex outboundMsgQueueMutex;
boost::interprocess::interprocess_semaphore messagesInOutboundMsgQueue;
std::deque<SoftwareBusMsg> incomingMsgQueue;
std::mutex incomingMsgQueueMutex;
//boost::interprocess::interprocess_semaphore messagesInIncomingMsgQueue;
};
#endif
我希望其他类别(例如负责电动机控制的类别)具有将消息发送到服务器进行遥测/错误报告的能力。我可能在这里错了,但是直接将TcpCom
类的实例传递给需要将消息发送到服务器的每个类的设计似乎很差。
相反,我尝试创建一个具有私人成员的EventReporter
类,该类是对TcpCom
类的引用。这将允许封装用于处理不同类型的事件的代码(信息,错误),我可以将初始化的" EventReporter"对象传递给所有需要它的事件。
#include "TcpCom.hpp"
class EventReporter
{
public:
EventReporter(TcpCom& tcpComIn) : tcpCom(tcpComIn)
{}
//Will contain call to tcpCom.addMsgToOutboundMsgQueue()
void reportEvent(std::string eventType, std::string message);
private:
TcpCom tcpCom;
};
当我尝试编译此代码时,我遇到了一些错误:
error: use of deleted function 'TcpCom::TcpCom(const TcpCom&)'
error: use of deleted function 'boost::asio::io_service(const boost::asio::io_service&)'
看来我的新课程将试图制作TcpCom
的副本,我认为我通过参考来避免了它。
我应该使用诸如unique_ptr
之类的东西避免复制TcpCom
,还是有更好的方法使网络功能可以从其他类中访问?
谢谢!
是抛弃是回答还是打字,所以我会发布并让问题的询问告诉我。
在
中class EventReporter
{
public:
EventReporter(TcpCom& tcpComIn) : tcpCom(tcpComIn)
{}
//Will contain call to tcpCom.addMsgToOutboundMsgQueue()
void reportEvent(std::string eventType, std::string message);
private:
TcpCom tcpCom; //<- this is not a reference
};
TcpCom tcpCom;
定义了 TcpCom
的实例,而不是对 TcpCom
的引用,如问候者所说的那样,因此在成员初始化列表中的 tcpCom(tcpComIn)
(顺便说一句,在使用列表上,对他们来说很好。许多认为自己是的C 程序员不再学习似乎不知道它们存在)执行他们试图避免通过参数列表中的参考来避免的副本。
错误消息是由成员产生的(至少是std :: mutex。静音的多个副本是不好的)TcpCom
是不可复制的,因此即使您愿意,也无法复制一个。
简单的解决方案是
class EventReporter
{
public:
EventReporter(TcpCom& tcpComIn) : tcpCom(tcpComIn)
{}
//Will contain call to tcpCom.addMsgToOutboundMsgQueue()
void reportEvent(std::string eventType, std::string message);
private:
TcpCom & tcpCom; //<- change made here
};
除非询问器具有其他不可复制的对象,否则在其代码或 EventReporter
实例中的其他位置也可以超过源TcpCom
,它们应该很好。
- 如何从其他功能C++访问参数?
- 从其他类访问类时遇到问题
- 线程消息传递或更好:在"大师班"中访问其他班级的成员
- 如何从其他结构访问受保护的结构变量
- 为什么我可以访问其他班级的私人成员?
- C++:从其他类访问结构的私有成员向量
- Unity3d 中还有其他方法可以访问设备相机吗?
- 访问从 CPP 文件到其他头文件的静态变量
- 在其他线程中循环访问该concurrent_vector时调用 concurrency::concurrent_vect
- 如何创建一个接口,允许我访问C++中的按钮(和其他ui)函数,该函数是使用python中的MFC实现的
- C++使用其他命名空间中的符号,而不使它们可从外部访问
- 变量 z 无法从其他函数访问变量 x
- 访问其他类中多个对象的私有成员向量
- 在类中使用映射并通过其他类的 get() 和 set() 函数访问值
- 访问其他线程堆栈变量如何在C++中工作?
- 访问其他网站后如何获得完整(精确)的URL / QUrl地址?
- 结构 #ifndef 灰显,其他源文件无法访问
- C++ 子类无法访问其他子类向量
- 当从其他方法返回 vector 时,C++无法访问矢量元素
- Windows 10 是否保护您不访问其他程序正在使用的内存?