Example #1
0
/*解析消息线程*/
void *pthread_func()
{
    head_user = (struct user_info *)malloc(sizeof(struct user_info));
    head_user->next = NULL;

    struct user_info *tmp; // 循环变量,用于便利在线用户链表将其发送给第一次登录的或者有刷新请求的客户端
    struct  servmsg *free_tmp;

    int ret= -1;
    while(1)
    {
        while(H->next != NULL)
        {
            //                   free_tmp = H;
            H = H->next; //消息头,处理玩就往后移动
            //                   free(free_tmp);//将处理完的消息所占内存释放掉
            //                   free_tmp = NULL;
            printf("消息链表首地址H :%p\n",H);
            printf("准备解析消息---\n");
            printf("消息来自%s:\tIP:%s(%d)\n",H->recvmsg.self_name,inet_ntoa(H->addr.sin_addr),ntohs(H->addr.sin_port));
            if(H->recvmsg.type == REGISTER)
            {
                register_user();
                continue;
            }
            if(H->recvmsg.type == LOG_IN )
            {
                check_user(); //
                if(find)
                {
                    add_user(head_user,H);// add 
                }
                continue;
            }          
            if(H->recvmsg.type == REFRESH){
                printf("%s刷新消息\n",H->recvmsg.self_name);
                //usleep(50000);
                send_all_online(H->addr);
                continue;
            }
            if (H->recvmsg.type == OFFLINE){
                printf("%s下线\n",H->recvmsg.self_name);
                delete_user(H->recvmsg.self_name);
            }
            if (H->recvmsg.type == CHAT_PRI){
                printf("%s私聊消息\n",H->recvmsg.self_name);
                printf("目标:%s\n",H->recvmsg.dst_name);
                chat_private();
            }
            if(H->recvmsg.type == CHAT_ALL){
                printf("%s群聊消息\n",H->recvmsg.self_name);
                chat_toall();
            }

            printf("-----------------------------------\n");
        }//end while(H)
    }
}
Example #2
0
/*解析消息线程*/
void *pthread_func()
{
       int group_fd;     //组播套接字
       group_fd = udp_link();
 
       head_user = (struct user_info *)malloc(sizeof(struct user_info));
       head_user->next = NULL;
      
       struct user_info *tmp; // 循环变量,用于便利在线用户链表将其发送给第一次登录的或者有刷新请求的客户端
       struct  servmsg *free_tmp;
 
       int ret= -1;
       while(1){
              while(H->next != NULL){
                   free_tmp = H;
                     H = H->next; //消息头,处理玩就往后移动
                   free(free_tmp);//将处理完的消息所占内存释放掉
                   free_tmp = NULL;
                    printf("消息来自:%s  IP:PORT:%s:%d\n",H->recvmsg.self_name,inet_ntoa(H->addr.sin_addr),ntohs(H->addr.sin_port));
					
					switch(H->recvmsg.type)
					{
                     case LOG_IN:
                            printf("%s登录消息\n",H->recvmsg.self_name);
                            send_message_to_all(H,LOGIN_TOALL);
                            add_user(head_user,H);
                     		break;
      				case REFRESH:
                            printf("%s刷新消息\n",H->recvmsg.self_name);
                            //usleep(50000);
                            send_all_online(H->addr);
                            break;
                     case CHAT_PRI:
                            printf("%s私聊消息\n",H->recvmsg.self_name);
                            printf("目标:%s\n",H->recvmsg.dst_name);
                            chat_private();
							break;
                     case CHAT_ALL:
                            printf("%s群聊消息\n",H->recvmsg.self_name);
						   send_message_to_all(H,CHAT_ALL);
                       		break;
					case FILE_NAME:
                            printf("%s申请下载\n",H->recvmsg.self_name);
                            send_dir_allfile(H->addr);
                     		break;
					case DOWNLOAD:
                            printf("%s 确认下载\n",H->recvmsg.self_name);
                            pthread_create(&tid2,NULL,download_send,(void *)&H->addr);
                            pthread_detach(tid2);
							break;
                     case  UPLOAD:
                            printf("%s上传文件\n",H->recvmsg.self_name);
                            pthread_create(&tid3,NULL,(void *)&upload_func,NULL);
                            pthread_detach(tid3);
                     		break;
					case OFFLINE:
                            printf("%s下线\n",H->recvmsg.self_name);
                            delete_user(H->recvmsg.self_name);
                     		break;
					case CREATE_GROUP:
                            printf("%s create group\n",H->recvmsg.self_name);
							create_group(H);						
                     		break;
					case  JOIN_GROUP:
                            printf("%s join group\n",H->recvmsg.self_name);
							join_group(H);
                     		break;
					case LIST_GROUP:
                            printf("%s list group\n",H->recvmsg.self_name);
							list_group(H);
                     		break;
					case GROUP_CHAT:
                            printf("%s group chat\n",H->recvmsg.self_name);
							group_chat(H);
                     		break;
					case GROUP_DELETE:
                            printf("%s delete group\n",H->recvmsg.self_name);
							delete_group(H);
                     		break;
					 default:
							printf("msg type error !\n");
							break;
					 }
                     printf("-----------------------------------\n");
              }
       }
}