コード例 #1
0
void SocketConnect::sendMessage()
{
    CCLOG("SEND THREAD HAS BEGAN !\n");
    SOCKET socket = this->socketSet;
    MessageQueue * queue = MessageQueueManager::getInstance()->getSendQueue();
    while (true)
    {
        if (queue->getQueueSize() > 0)
        {
            CCLOG("SENDQUEUE SIZE IS %d\n",queue->getQueueSize());
            
            queue->queueLck();
            SocketMessage *  msg = queue->frontQueue();
            queue->queueUnlck();
            
            CCLOG("SEND TO SERVER FROM SENDQUEUE : %s, DATA LENGTHT : %d\n",msg->getData().c_str(),msg->dataLength());
        
            if (-1 == send(socket, msg->getData().c_str(), msg->dataLength(), 0)) {
                CCLOG("SEND TO SERVER ERROR ! WILL BE TRY TO SEND AGAGIN !\n");
            }
            else{
                CCLOG("SEND TO SERVER SUCCESS !\n");
                
                queue->queueLck();
                queue->popQueue();
                queue->queueUnlck();
                delete msg;
            }
        }
        else{
           // CCLOG("QUEUE IS EMPTY !");
        }
    }
}
コード例 #2
0
void SocketConnect::revMessage()
{
    CCLOG("RECEIVE THREAD HAS BEGAN !\n");
    
    SOCKET socket = this->socketSet;
    
    char buf[1024] = "";
    
    while (true) {
        if (-1 == recv(socket, buf, sizeof(buf), 0)) {
            CCLOG("RECEIVE ERROR !\n");
        }
        else{
            
            MessageQueue * queue = MessageQueueManager::getInstance()->getRevQueue();
            
            SocketMessage * msg = SocketMessage::create();
            msg->setData(buf);
            
            CCLOG("CLIENT HAS RECEIVE : %s",msg->getData().c_str());
            
            if (msg->dataLength() > 0) {
                
                queue->queueLck();
                MessageQueueManager::getInstance()->sendMsgToRevQueue(msg);
                queue->queueUnlck();
                
                memset(buf, 0, 1024);
            }else{
                CCLOG("MAYBE SOMETHING WRONG !\n");
            }
        }
    }
}