コード例 #1
0
ファイル: HandleMessage.cpp プロジェクト: fU9ANg/hacks
/*
=====================
 转发教师端数据到所有学生端
=====================
*/
bool CHandleMessage::postTeacherToAllStudent (Buf* p, enum CommandType iCommandType)
{
#if 0
    cout << "postTeacherToAllStudent ------------------------ " <<endl;
    if (p == NULL)
        return false;

    int iLen = ((MSG_HEAD*)p->ptr())->cLen;
    int iHeadLen = sizeof (MSG_HEAD);
    Buf* pbuf = NULL;

    CRoom* pc = ROOMMANAGER->get_room_by_fd (p->getfd());
    if (pc != NULL) {

        int i = 0;
        CRoom::STUDENTMAP::iterator it;
        for (it = pc->m_student_map.begin(); \
             it != pc->m_student_map.end (); ++it)
        {
            i++;
            pbuf = SINGLE->bufpool.malloc ();
            if (pbuf != NULL) {
                MSG_HEAD* head = (MSG_HEAD*)pbuf->ptr();
                head->cLen = iLen;
                head->cType = iCommandType;
                if (iLen > iHeadLen) {
                    memcpy (head->cData(), (char*)p->ptr() + iHeadLen, iLen - iHeadLen);
                }
                pbuf->setsize (head->cLen);
                pbuf->setfd (it->first);
                SINGLE->sendqueue.enqueue (pbuf);
            }
            else {
                cout << "Error: out of memory" << endl;
                p->reset();
                pbuf->reset();
                SINGLE->bufpool.free(p);
                SINGLE->bufpool.free(pbuf);
                return false;
            }
        }
        if ( 0 == i ) {
            printf("send to %d students!\n", i);
        }
    }
    else {
        cout << __FILE__ << ":" <<__FUNCTION__<< ":" << __LINE__<<"Error: not found 'teacher_fd' in Room" << endl;
        //p->reset();
        SINGLE->bufpool.free(p);
        return false;
    }

    p->reset();
    SINGLE->bufpool.free(p);
#endif
    return true;
}
コード例 #2
0
ファイル: SendTask.cpp プロジェクト: sakishum/teachserver
int SendTask::work() {
    while(true){
        Buf* p = NULL;
        if (0 != SINGLE->sendqueue.dequeue(p, 3)) {
            continue;
        }

        int fd = p->getfd();

        if (0 == fd) {
            printf(" fd = 0 !\n");
            continue;
        }

#if 0
        MSG_HEAD* pp = (MSG_HEAD*)p->ptr();
        if ( pp->cType > 9999 ) {
            struct sGetAllStudentInfo* xxx = (struct sGetAllStudentInfo*)pp->cData();
            printf("id %d pic %s  --  len:=%d\n", xxx->iStudentId, xxx->sPicName, pp->cLen);

        }

        unsigned int len = 0;
        int cnt;
        while (len < p->size()) {
            cnt = send(fd, (char *)p->ptr() + len, p->size() - len, 0);
            if (cnt == -1) {
                cout << "send erroooooooooooooooooooooor" << endl;
                break;
            }
            len += cnt;
        }

        printf ("ERRNO: %d, message:%s len = %d, p->size() = %ld\n", errno, strerror (errno), len ,p->size());
#else
        int bytes_left = p->size ();
        int written_bytes;
        char* ptr = (char*) p->ptr();
        while (bytes_left > 0)
        {
            written_bytes = send (fd, ptr, bytes_left, 0);
            if (written_bytes <= 0) {
                if (errno == EINTR) {
                    if (written_bytes < 0) written_bytes = 0;
                    continue;
                }
                else if (errno == EAGAIN) {
                    if (written_bytes < 0) written_bytes = 0;
                    sleep (1);
                    continue;
                }
                else
                    break;
            }
            bytes_left -= written_bytes;
            ptr += written_bytes;
        }
#endif
        printf("Send data...finished. packetLength=%ld, from FD=[%d]\n", p->size(), fd);

        p->reset();
        SINGLE->bufpool.free(p);
    }
    return 0;
}
コード例 #3
0
ファイル: SendTask.cpp プロジェクト: fU9ANg/hacks
int SendTask::work ()
{
    while (true)
    {
        Buf* p = NULL;
        if (SINGLE->sendqueue.dequeue (p, 3) != 0)
        {
            continue;
        }

        int fd = p->getfd ();

        if (fd <= 0)
        {
            cout << "[SEND] -- fd of buffer <= 0" << endl;
            SINGLE->bufpool.free (p);
            continue;
        }

        //
#if 0
        cout << "Send cType: " << ((MSG_HEAD*)p->ptr())->cType <<endl;
        printf ("address: %p\n", p);
        if (((MSG_HEAD*)p->ptr())->cType == 187) {
        cout << "-----------------------BEGIN-------------------------------------" << endl;
        cout << "CTYPE: " << ((MSG_HEAD*) (p->ptr()))->cType << endl;
        cout << "LEN: " << *(unsigned int*) ((char*)p->ptr() + MSG_HEAD_LEN) << endl;
        cout << "STUDENT_ID: " << *(unsigned int*) ((char*)p->ptr() + MSG_HEAD_LEN + sizeof (unsigned int)) << endl;
        cout << "STATUS: " << *(unsigned int*) ((char*)p->ptr() + MSG_HEAD_LEN + sizeof (int) + sizeof (int)) << endl;
        cout << "STUDENT_ID: " << *(unsigned int*) ((char*)p->ptr() + MSG_HEAD_LEN + sizeof (int) + sizeof (int) + sizeof (int)) << endl;
        cout << "STATUS: " << *(unsigned int*) ((char*)p->ptr() + MSG_HEAD_LEN + sizeof (int) + sizeof (int)+ sizeof (int) + sizeof (int)) << endl;

        cout << "-------------------------END-----------------------------------" << endl;
        }
#endif

        // send fck message to clients
        // must be need written bytes data finished
        debugProtocol (p);
        int bytes_left = p->size ();
        int written_bytes;
        char* ptr = (char*) p->ptr();
        while (bytes_left > 0)
        {
            written_bytes = send (fd, ptr, bytes_left, 0);
            if (written_bytes <= 0) {
                if (errno == EINTR) {
                    if (written_bytes < 0) {
                        written_bytes = 0;
                        cout << "EINTR......" << endl;
                        continue;
                    }
                }
                else if (errno == EAGAIN) {
                    if (written_bytes < 0) {
                        written_bytes = 0;
                        usleep (50);
                        cout << "EAGAIN......" << endl;
                        continue;
                    }
                }
                else {
                    break;
                }
            }
            bytes_left -= written_bytes;
            ptr += written_bytes;
        }

        //printf("Send data...finished. packetLength=%ld, from FD=[%d]\n", p->size(), fd);
        LOG(INFO) << "Send data ... finished. packet len=" << p->size() << ", from FD=" << fd << endl;

        p->reset ();
        SINGLE->bufpool.free (p);
    }
    return 0;
}