libsigc ++并将函数作为信号连接到另一个函数作为插槽

时间:2012-08-02 14:57:11

标签: c++ linux sockets signals-slots libsigc++

这是第一次将libsigc ++用于信号槽, 我有两个功能,他们工作正常,我的方案: 我写了一个函数作为布尔函数,如果socket有recv的数据,它返回true:

bool Socket::isDataReady()
{
    void *buffer = malloc (FRAMEBUFFER + 6);
    sockaddr_in from;
    socklen_t fromLength = sizeof( from );

    if (::recvfrom(this->socketFD,buffer,FRAMEBUFFER + 6, 0, (sockaddr *)&this->getSocketAddressStructureOfServer(), &fromLength ) == -1)
    {
        if (errno == EAGAIN || errno == EWOULDBLOCK)
            return false;
    }
    return true;

}

所以,我写了一个从net获取数据的函数:

int Socket::readDatagrams(unsigned char *buffer, string &srcAddress, unsigned short int & srcPort)
{
    unsigned int maximumPacketSize = FRAMEBUFFER + 6;
    int returnValue ;
    sockaddr_in from;
    socklen_t fromLength = sizeof( from );
    int receivedBytes;

    fromLength = sizeof(this->getSocketAddressStructureOfServer());
    receivedBytes = recvfrom( this->socketFD, buffer, maximumPacketSize, 0, (sockaddr *)&this->getSocketAddressStructureOfServer(), &fromLength );

    returnValue = receivedBytes;
    if ( receivedBytes <= 0 )
        returnValue = -1;

    /// exporting data
    //
    srcAddress = inet_ntoa(this->getSocketAddressStructureOfServer().sin_addr);
    srcPort = ntohs( ( unsigned short int)this->getSocketAddressStructureOfServer().sin_port );


    return returnValue;
}

我有UDP服务器并在构造函数中初始化所有内容,所以我需要告诉每当isDataReady()为true时,readDatagram本身就会执行。如何用libsigc ++编写代码? 我读了他们的教程,但是它的教程没有说func为sig。

1 个答案:

答案 0 :(得分:1)

我知道你定义了

sigc::signal<bool> ptr;

在你班级的身体里。 然后,如果您想要在班级中进行连接,请致电:

this->ptr.connect(sigc::mem_fun(*this,&NetworkSocket::createThread));

但我不知道故事的其余部分......