如何使一个C ++的boost ::信号从封装发射它的对象的对象捕捉? [英] How to make a C++ boost::signal be caught from an object which encapsulates the object which emits it?

查看:155
本文介绍了如何使一个C ++的boost ::信号从封装发射它的对象的对象捕捉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TcpDevice 类,它封装了TCP连接,其中有一个的 onRemoteDisconnect 的方法,每当远端挂断它被调用。然后,有一个是SessionManager 对象,它创建的 TcpSession 对象而采取的 TcpDevice 作为通信信道,将它们插入一个内部指针容器应用程序使用。在情况下,任何的管理 TcpSessions 应该结束了,我想在是SessionManager 实例被通知,然后从容器中删除相应的会话,释放相关联的资源吧。

I have a TcpDevice class which encapsulates a TCP connection, which has an onRemoteDisconnect method which gets called whenever the remote end hangs up. Then, there's a SessionManager object which creates TcpSession objects which take a TcpDevice as a communication channel and inserts them in an internal pointer container for the application to use. In case any of the managed TcpSessions should end, I would like the SessionManager instance to be notified about it and then remove the corresponding session from the container, freeing up the resources associated with it.

我发现我的问题非常类似这样的问题:

I found my problem to be very similar to this question:

对象从容器删除自身

但由于他对检查连接状态的线程,它会从我的,我打算使用的的boost ::信号的解决这个问题的方式略有不同,所以我决定去了新的问题着眼于它 - 我道歉,如果这是错误的方式做到这一点...我仍然得到关于如何正确使用SO的感觉:)

but since he has a thread for checking the connections state, it gets a little different from mine and the way I intended to solve it using boost::signals, so I decided to go for a new question geared towards it - I apologize if it's the wrong way to do it... I'm still getting the feel on how to properly use S.O. :)

由于我有种熟悉QT的信号/插槽,我发现的的boost ::信号提供了一个类似的机制(我已经使用的提高:: ASIO 的而在这个项目中没有QT),所以我决定实施 remoteDeviceDisconnected 以通过发射信号的 TcpDevice的 onRemoteDisconnect ,和我将有一个插槽的是SessionManager ,然后这将删除容器断开的会话和设备。

Since I'm kind of familiar with QT signals/slots, I found boost::signals offers a similar mechanism (I'm already using boost::asio and have no QT in this project), so I decided to implement a remoteDeviceDisconnected signal to be emitted by TcpDevice's onRemoteDisconnect, and for which I would have a slot in SessionManager, which would then delete the disconnected session and device from the container.

要初步尝试一下我声明了信号的公共成员的 TcpDevice 中的 tcpdevice.hpp 的:

To initially try it out I declared the signal as a public member of TcpDevice in tcpdevice.hpp:

class TcpDevice
{
             (...)
  public:
    boost::signal <void ()> remoteDeviceDisconnected;
             (...)
}

然后我发出它的 TcpDevice的 onRemoteDisconnect 的方法是这样的:

remoteDeviceDisconnected();

现在,有没有什么办法这个信号是SessionManager 从 插槽连接到我的会话管理器?我想这样的:

Now, is there any way to connect this signal to my SessionManager slot from inside session manager? I tried this:

unsigned int SessionManager::createSession(TcpDevice* device)
{
  unsigned int session_id = session_counter++;
  boost::mutex::scoped_lock lock(sessions_mutex);
  sessions.push_back(new TcpSession(device, session_id));
  device->remoteDeviceDisconnected.connect(boost::bind(&SessionManager::removeDeadSessionSlot, this));
  return session_id;
}

它编译罚款,但在链接时它抱怨的多个定义的 remoteDeviceDisconnected 的几个对象code文件:

It compiles fine but at link time it complains of multiple definitions of remoteDeviceDisconnected in several object code files:

tcpsession.cpp.o:(.bss+0x0): multiple definition of `remoteDeviceDisconnected'
tcpdevice.cpp.o: (.bss+0x0): first defined here
sessionmanager.cpp.o:(.bss+0x0): multiple definition of `remoteDeviceDisconnected'
tcpdevice.cpp.o: (.bss+0x0): first defined here

我发现了这个奇怪的,因为我没有任何地方重新定义信号,但只是用它在的了createSession 的上述方法。

任何提示将不胜AP preciated!谢谢!

Any tips would be greatly appreciated! Thank you!

推荐答案

我的坏!就像我们都应该期待,连接器是正确的......确实有第二个定义,我只是不能发现它了,因为它没有任何我的课的定义,而只是浮动之一,在我.cpp文件,就像那些在发现提高::信号的范例

My bad! Like we all should expect, the linker was right... there was indeed a second definition, I just couldn't spot it right away because it wasn't defined by any of my classes, but just "floating" around one of my .cpp files, like those found on boost::signals examples.

为了记录,最初的想法工作就像一个魅力:当一个给定的 TcpDevice 会从远端断开时,它发出的的 remoteDeviceDisconnected 的信号,然后将其由是SessionManager 对象持有在 TcpSession 实例指向该 TcpDevice 抓获。一旦接到通知,的是SessionManager的的方法 removeDeadSessionSlot 被执行,通过的会遍历 ptr_list 容器和取出其中一个被切断

Just for the record, the initial idea worked like a charm: when a given TcpDevice gets disconnected from the remote end, it emits the remoteDeviceDisconnected signal, which is then caught by the SessionManager object which holds the TcpSession instance that points to that TcpDevice. Once notified, SessionManager's method removeDeadSessionSlot gets executed, iterating through the sessions ptr_list container and removing the one which was disconnected:

void SessionManager::removeDeadSessionSlot()
{
  boost::mutex::scoped_lock lock(sessions_mutex);
  TcpSession_ptr_list_it it = sessions.begin();
  while (it != sessions.end()) {
    if (!(*it).device->isConnected())
      it = sessions.erase(it);
    else
      ++it;
  }
}

希望,可作为给别人参考!

Hope that may serve as a reference to somebody!

这篇关于如何使一个C ++的boost ::信号从封装发射它的对象的对象捕捉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆