2012-08-02 33 views
0

這是第一次使用libsigC++信號插槽, 我有兩個函數,他們工作正常,我的場景: 我寫了一個函數作爲布爾函數,如果何時插座有recv的數據,它返回true:libsigC++和函數作爲信號連接到另一個函數作爲插槽

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服務器和初始化在構造函數中的一切,所以我需要告訴每當isD ataReady()爲true,readDatagram本身執行。如何使用libsigC++編寫代碼? 我讀過他們的教程,但是它的教程並沒有提到函數爲sig。

回答

1

我知道你在你們班的身體來定義

sigc::signal<bool> ptr; 

。 然後,如果你想在你的類連接,請致電:

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

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