0

这是第一次使用 libsigc++ 作为信号槽,我有两个工作正常的函数,我的场景:我写了一个布尔函数,如果套接字有数据用于接收,它返回 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;

}

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

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() 何时为真,readDatagram 本身执行。如何用 libsigc++ 编写代码?我读了他们的教程,但是它的教程并没有说 func as sig。

4

1 回答 1

1

我知道你定义

sigc::signal<bool> ptr;

在你班级的正文中。然后,如果您想在课堂上联系,请致电:

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

但我不知道剩下的故事......

于 2012-08-03T20:36:45.953 回答