Esempio n. 1
0
void ChatHandler::handleMessage(Net::MessageIn &msg)
{
    if (!localChatTab)
        return;

    BLOCK_START("ChatHandler::handleMessage")
    switch (msg.getId())
    {
        case SMSG_WHISPER_RESPONSE:
            processWhisperResponse(msg);
            break;

        // Received whisper
        case SMSG_WHISPER:
            processWhisper(msg);
            break;

        // Received speech from being
        case SMSG_BEING_CHAT:
            processBeingChat(msg, false);
            break;

        // Received speech from being
        case SMSG_BEING_CHAT2:
            processBeingChat(msg, true);
            break;

        case SMSG_PLAYER_CHAT:
        case SMSG_GM_CHAT:
            processChat(msg, msg.getId() == SMSG_PLAYER_CHAT, false);
            break;

        case SMSG_PLAYER_CHAT2:
            processChat(msg, true, true);
            break;

        case SMSG_MVP:
            processMVP(msg);
            break;

        case SMSG_IGNORE_ALL_RESPONSE:
            processIgnoreAllResponse(msg);
            break;

        default:
            break;
    }
    BLOCK_END("ChatHandler::handleMessage")
}
Esempio n. 2
0
void processReceive(ENetEvent& evt) {
	switch (evt.channelID) {
	case narf::net::CHAN_CHAT:
		processChat(evt);
		break;
	case narf::net::CHAN_PLAYERCMD:
		processPlayerCmd(evt);
		break;
	case narf::net::CHAN_CHUNK:
		processChunk(evt);
		break;
	case narf::net::CHAN_ENTITY:
		processEntity(evt);
		break;
	}
}
Esempio n. 3
0
void chatLoop(int fd_accept)
{ 
    unsigned char buffer[BUFF_SIZE];    
    struct sockaddr_in fsin; 
    int fromlen;
    struct timeval stv; 
    int i; 
    int fd_server  = -1; 
    int iMaxFd, iNumInputs, nContinue = 1; 
    int newConnect, nread;
    fd_set fdCheck; 
    
    // The select statement is the key call in this routine. 
    
    for(;nContinue;) 
    { 
        stv.tv_sec = 0; 
        stv.tv_usec = 0; 
        FD_ZERO(&fdCheck); 

// Select mask contains accept socket number
        FD_SET(fd_accept  , &fdCheck); 
        iMaxFd = fd_accept; 
   
// Select mask contains socket number of each chat user's socket
        for (i = 0; i < numChats; i++)
        {
            FD_SET(fd_chats[i], &fdCheck);
            if (fd_chats[i] > iMaxFd) iMaxFd = fd_chats[i];
        }
        iMaxFd += 1; 
        
// No timeout on the select
        iNumInputs = select(iMaxFd, &fdCheck, 0, 0, NULL /* &stv*/); 
        if (iNumInputs == -1) 
        { 
            /* Error*/ 
            printf("\n Bad return from select ==> die\n"); 
            nContinue = 0; 
        } 
        else if (iNumInputs == 0) 
        { 
            /* Timeout -- won't occur for us.*/ 
            printf("\n Timeout from select\n"); 
            nContinue = 0;
        } 
        else 
        {
// Pending connect request of new chat user 
            if (FD_ISSET(fd_accept  , &fdCheck)) 
            { 
                 // Now we can make an accept without blocking
                 fromlen = sizeof(fsin); 
                 newConnect = accept(fd_accept, (struct sockaddr *)&fsin, &fromlen);
                 printf("New Connect : %d\n", newConnect);
                 if (newConnect < 0) 
                    { 
                        fprintf(stderr,"accept err: %s\n", strerror(errno));
                        continue; 
                    }                  

                 if (numChats >= MAX_CHATS)
                 {
                // We are full message sent
                    printf("%s\n", chatFull);
                    write(newConnect, chatFull, strlen(chatFull));
                    close(newConnect);
                 }
                 else
                    addChatter(newConnect); // add new user to tables
              
            }  // end of fd_accept handling
           
   
            for (i =0; i < numChats; i++)
            {
                if (FD_ISSET(fd_chats[i], &fdCheck))
                {
            // Received message from one of the chat clients
                    nread = read(fd_chats[i], buffer, BUFF_SIZE); 
                    if (nread > 0)
                    {
                        if (buffer[nread-1] == '\n') 
                            buffer[nread-1] = 0;
                        else
                            buffer[nread] = 0;

                    // processChat sends out information to all
                    // chatters
                        processChat(i,  buffer, nread);
                        
                    }
                    else
                    {
                        printf("Time to close out chat file descriptor: %d\n",
                            fd_chats[i]);
                        if (nread < 0)
                             fprintf(stderr, "Read err:%s\n", strerror(errno));
                        
                       // close out terminated chat user and compress the
                       // chat tables
                        remove_chatter(i);
                    }
                }
            } // end of for loop on numChats
        } 
    } // End of the select for loop    
}