Пример #1
0
void handleRegist(int connfd,int index){
    strcpy(username,childthread[index].recvpkg.src);
    strcpy(password,childthread[index].recvpkg.message);
    printf("User regist, the username id %s,the password is %s   ",username,password);
    int i;
    for(i = 0 ; i < user ; i++){
        if(strcmp(userList[i].username,username) == 0)
            break;
    }
    if(i == user){//未被注册过
        strcpy(userList[user].username,username);
        strcpy(userList[user].password,password);
        user++;
        memset(&childthread[index].sendpkg, 0 , MAXLINE);
        init_pkg(&childthread[index].sendpkg);
        childthread[index].sendpkg.service = REGIST;
        strcpy(childthread[index].sendpkg.src, username);
        childthread[index].sendpkg.status = ACCEPT;
        send(connfd, (void *)&childthread[index].sendpkg, HEADLINE, 0);
        printf("Regist successfully\n");
    }
    else{//注册过
        memset(&childthread[index].sendpkg, 0 , MAXLINE);
        init_pkg(&childthread[index].sendpkg);
        childthread[index].sendpkg.service = REGIST;
        strcpy(childthread[index].sendpkg.src, username);
        childthread[index].sendpkg.status = REFUSE;
        send(connfd, (void *)&childthread[index].sendpkg, HEADLINE, 0);
        printf("Regist failed\n");
    }
}
Пример #2
0
void handleInform(int connfd,int index){
    int i;
    memset(&sendpkg, 0 , MAXLINE);
    init_pkg(&sendpkg);
    sendpkg.service = INFORM;
    strcpy(sendpkg.src, childthread[index].recvpkg.src);
    strcpy(sendpkg.message, childthread[index].recvpkg.message);
    sendpkg.length = childthread[index].recvpkg.length;
    pthread_mutex_lock(&mutex);
    for(i = 0 ; i < online ; i++){
        if(strcmp(onlineUser[i].username,childthread[index].recvpkg.src)!=0){
            send(onlineUser[i].connfd, (void *)&sendpkg, sendpkg.length + HEADLINE, 0);
        }
    }
    pthread_mutex_unlock(&mutex);
}
Пример #3
0
void broadcast(char *username, int type){		//通知所有在线用户,某用户上线或下线,上线1,下线0
    int i;
    memset(&sendpkg, 0 , MAXLINE);
    init_pkg(&sendpkg);
    sendpkg.service = UPDATE;
    if(type)	sendpkg.status = LOGON;
    else sendpkg.status = LOGOFF;
    strcpy(sendpkg.src, username);
    pthread_mutex_lock(&mutex);
    for(i = 0 ; i < online ; i++){
        if(strcmp(onlineUser[i].username,username)!=0){
            send(onlineUser[i].connfd, (void *)&sendpkg, HEADLINE, 0);
        }
    }
    pthread_mutex_unlock(&mutex);
}
Пример #4
0
void handleOnline(int connfd,int index){
    strcpy(username,childthread[index].recvpkg.src);
    printf("%s want to know online friends    ",username);
    memset(&childthread[index].sendpkg, 0 , MAXLINE);
    init_pkg(&childthread[index].sendpkg);
    childthread[index].sendpkg.service = ONLINE;
    strcpy(childthread[index].sendpkg.src, username);
    childthread[index].sendpkg.status = 0x00 + online;
    int i;
    pthread_mutex_lock(&mutex);
    for(i = 0 ; i < online ; i++){
        strcpy(&childthread[index].sendpkg.message[10*i],onlineUser[i].username);
    }
    send(connfd, (void *)&childthread[index].sendpkg, HEADLINE + 10*online , 0);
    pthread_mutex_unlock(&mutex);	
    printf("success \n");
}
Пример #5
0
//duda_package_t *duda_package_load(const char *pkgname)
duda_package_t *duda_package_load(const char *pkgname, struct duda_api_objects *api)
{
    int ret;
    char *package = NULL;
    void *handle = NULL;
    unsigned long len;
    struct file_info finfo;
    duda_package_t *(*init_pkg)() = NULL;
    duda_package_t *package_info;

    mk_api->str_build(&package, &len, "%s/%s.dpkg", packages_root, pkgname);
    ret = mk_api->file_get_info(package, &finfo);

    if (ret != 0) {
        mk_err("Duda: Package '%s' not found", pkgname);
        mk_api->mem_free(package);
        exit(EXIT_FAILURE);
    }

    if (finfo.is_file == MK_FALSE) {
        mk_warn("Duda: Invalid Package '%s'", pkgname);
        mk_api->mem_free(package);
        return NULL;
    }

    handle = duda_load_library(package);
    if (!handle) {
        mk_warn("Duda: Invalid Package format '%s'", pkgname);
        mk_api->mem_free(package);
        return NULL;
    }

    init_pkg = duda_load_symbol(handle, "init_duda_package");
    if (!init_pkg) {
        mk_err("Duda: the package '%s' is broken", pkgname);
        exit(EXIT_FAILURE);
    }

    package_info = init_pkg(api);
    mk_api->mem_free(package);

    return package_info;
}
Пример #6
0
void handleMessage(int connfd,int index){
    char dst[10];
    strcpy(username,childthread[index].recvpkg.src);
    strcpy(dst,childthread[index].recvpkg.dst);
    printf("%s send message to %s : %s   ",username,dst,childthread[index].recvpkg.message);
    memset(&childthread[index].sendpkg, 0 , MAXLINE);
    init_pkg(&childthread[index].sendpkg);
    childthread[index].sendpkg.service = MESSAGE;
    strcpy(childthread[index].sendpkg.src, username);
    strcpy(childthread[index].sendpkg.dst, dst);
    strcpy(childthread[index].sendpkg.message, childthread[index].recvpkg.message);
    childthread[index].sendpkg.length = strlen(childthread[index].sendpkg.message);
    int fd,i;
    pthread_mutex_lock(&mutex);
    for(i = 0 ; i < online ; i++){
        if(strcmp(dst,onlineUser[i].username)==0){
            fd = onlineUser[i].connfd;
            send(fd, (void *)&childthread[index].sendpkg, childthread[index].sendpkg.length + HEADLINE, 0);
            printf("send successfully\n");
            break;
        }
    }
    pthread_mutex_unlock(&mutex);
}
Пример #7
0
void yf::LoadBalance::Impl::process(mp::Package &package)
{
    bool is_closed_front = false;

    // checking for closed front end packages
    if (package.session().is_closed())
    {
        is_closed_front = true;
    }

    Z_GDU *gdu_req = package.request().get();

    // passing anything but z3950 packages
    if (gdu_req && gdu_req->which == Z_GDU_Z3950)
    {
        // target selecting only on Z39.50 init request
        if (gdu_req->u.z3950->which == Z_APDU_initRequest)
        {
            yazpp_1::GDU base_req(gdu_req);
            Z_APDU *apdu = base_req.get()->u.z3950;

            Z_InitRequest *org_init = base_req.get()->u.z3950->u.initRequest;
            mp::odr odr_en(ODR_ENCODE);

            std::list<std::string> vhosts;
            mp::util::remove_vhost_otherinfo(&(org_init->otherInfo), vhosts);
            // get lowest of all vhosts.. Remove them if individually if
            // they turn out to be bad..
            while (1)
            {
                std::list<std::string>::iterator ivh = vhosts.begin();
                std::list<std::string>::iterator ivh_pick = vhosts.end();

                Package init_pkg(package.session(), package.origin());
                init_pkg.copy_filter(package);

                unsigned int cost_i = std::numeric_limits<unsigned int>::max();
                {
                    boost::mutex::scoped_lock scoped_lock(m_mutex);

                    for (; ivh != vhosts.end(); ivh++)
                    {
                        if ((*ivh).size() != 0)
                        {
                            unsigned int cost
                                = yf::LoadBalance::Impl::cost(*ivh);

                            std::ostringstream os;
                            os  << "LB" << " "
                                << package << " "
                                << "0.000000" << " "
                                << "Consider " << *ivh
                                << " cost=" << cost;
                            yaz_log(YLOG_LOG, "%s", os.str().c_str());
                            if (cost_i > cost)
                            {
                                ivh_pick = ivh;
                                cost_i = cost;
                            }
                        }
                    }
                }
                if (ivh_pick == vhosts.end())
                    break;
                std::string target = *ivh_pick;
                vhosts.erase(ivh_pick);
                // copying new target into init package
                yazpp_1::GDU init_gdu(base_req);
                Z_InitRequest *init_req = init_gdu.get()->u.z3950->u.initRequest;

                mp::util::set_vhost_otherinfo(&(init_req->otherInfo),
                                              odr_en, target, 1);

                init_pkg.request() = init_gdu;

                // moving all package types
                init_pkg.move();

                // checking for closed back end packages
                if (!init_pkg.session().is_closed())
                {
                    add_session(package.session().id(), target);

                    package.response() = init_pkg.response();
                    return;
                }
                std::ostringstream os;
                os  << "LB" << " "
                    << package << " "
                    << "0.000000" << " "
                    << "Failed " << target;
                yaz_log(YLOG_LOG, "%s", os.str().c_str());
            }
            mp::odr odr;
            package.response() = odr.create_initResponse(
                apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
                "load_balance: no available targets");
            package.session().close();
            return;
        }
        // frontend Z39.50 close request is added to statistics and marked
        else if (gdu_req->u.z3950->which == Z_APDU_close)
        {
            is_closed_front = true;
            boost::mutex::scoped_lock scoped_lock(m_mutex);
            add_package(package.session().id());
        }
        // any other Z39.50 package is added to statistics
        else
        {
            boost::mutex::scoped_lock scoped_lock(m_mutex);
            add_package(package.session().id());
        }
    }

    // moving all package types
    package.move();

    bool is_closed_back = false;

    // checking for closed back end packages
    if (package.session().is_closed())
        is_closed_back = true;

    Z_GDU *gdu_res = package.response().get();

    // passing anything but z3950 packages
    if (gdu_res && gdu_res->which == Z_GDU_Z3950)
    {
        // session closing only on Z39.50 close response
        if (gdu_res->u.z3950->which == Z_APDU_close)
        {
            is_closed_back = true;
            boost::mutex::scoped_lock scoped_lock(m_mutex);
            remove_package(package.session().id());
        }
        // any other Z39.50 package is removed from statistics
        else
        {
            boost::mutex::scoped_lock scoped_lock(m_mutex);
            remove_package(package.session().id());
        }
    }

    // finally removing sessions and marking deads
    if (is_closed_back || is_closed_front)
    {
        boost::mutex::scoped_lock scoped_lock(m_mutex);

        // marking backend dead if backend closed without fronted close
        if (is_closed_front == false)
            add_dead(package.session().id());

        remove_session(package.session().id());

        // making sure that package is closed
        package.session().close();
    }
}
Пример #8
0
void handleWatch(int connfd, int index){
    int i = 0;
    switch (childthread[index].recvpkg.status) {
        case REQUEST:
            strcpy(username,childthread[index].recvpkg.src);
            printf("%s want to battle ",username);
            memset(&childthread[index].sendpkg, 0 , MAXLINE);
            init_pkg(&childthread[index].sendpkg);
            childthread[index].sendpkg.service = WATCH;
            childthread[index].sendpkg.status = CHOOSE;
            strcpy(childthread[index].sendpkg.src, username);
            int number,j;
            
            for (i = 0; i < couple; i++) {
                if (battle[i].user1[0] != 0){
                    for (j = 0; j < online; j++) {
                        if(onlineUser[j].battling == i){
                            if(number == 0)
                                strcpy(&childthread[index].sendpkg.message[1],onlineUser[j].username);
                            else
                                strcpy(&childthread[index].sendpkg.message[20*number],onlineUser[j].username);
                            break;
                        }
                    }
                    for (j = 0; j < online; j++) {
                        if(onlineUser[j].battling == i){
                            strcpy(&childthread[index].sendpkg.message[20*number+10],onlineUser[j].username);
                            break;
                        }
                    }
                    number++;
                }
            }
            childthread[index].sendpkg.message[0] = number;
            childthread[index].sendpkg.length = strlen(childthread[index].sendpkg.message);
            send(connfd, (void *)&childthread[index].sendpkg, HEADLINE + childthread[index].sendpkg.length, 0);
            break;
        case CHOOSE:
            strcpy(username,childthread[index].recvpkg.src);
            printf("%s want to watch battle ",username);
            char name[10];
            strcpy(name, &childthread[index].sendpkg.message[0]);
            printf("%s",name);
            int battlenum = 0;
            for (i = 0; i < online; i++) {
                if(strcmp(name, onlineUser[i].username) == 0){//find his battling number
                    battlenum = onlineUser[i].battling;
                        for (j = 0; j < 10; j++) {//add the audience to the battle's watching group
                            if (battle[battlenum].watch_connfd[j] == 0) {
                                battle[battlenum].watch_connfd[j] = connfd;
                                break;
                            }
                        }
                    break;
                }
            }
            break;
        default:
            break;
    }
}
Пример #9
0
void handleBattle(int connfd,int index){
    int i = 0;
    int m = 0;
    int fd = 0;
    int num = 0;
    int blood1 = 0;
    int blood2 = 0;
    switch(childthread[index].recvpkg.status){
        case(REQUEST):	{
            char dst[10];
            strcpy(username,childthread[index].recvpkg.src);
            strcpy(dst,childthread[index].recvpkg.dst);
            printf("%s want to battle with %s", username,dst);
            memset(&childthread[index].sendpkg, 0 , MAXLINE);
            init_pkg(&childthread[index].sendpkg);
            childthread[index].sendpkg.service = BATTLE;
            for(i = 0 ; i < online ; i++){//查找目标用户的connfd
                if(strcmp(dst,onlineUser[i].username)==0)
                    break;
            }
            if(onlineUser[i].battling != 0){//对方正在对战,通知等待
                childthread[index].sendpkg.status = WAIT;
                strcpy(childthread[index].sendpkg.src, username);
                send(connfd, (void *)&childthread[index].sendpkg, HEADLINE, 0);
                printf("send battle wait successfully\n");
                break;
            }
            else{//向对方发送对战请求
                fd = onlineUser[i].connfd;
                childthread[index].sendpkg.status = REQUEST;
                strcpy(childthread[index].sendpkg.src, username);
                strcpy(childthread[index].sendpkg.dst, dst);
                //pthread_mutex_lock(&mutex);
                /*for(i = 0 ; i < online ; i++){
                 if(strcmp(dst,onlineUser[i].username)==0){
                 fd = onlineUser[i].connfd;*/
                send(fd, (void *)&childthread[index].sendpkg, HEADLINE, 0);
                printf("send battle request successfully\n");
                break;
            }
            //pthread_mutex_unlock(&mutex);
            break;
        }
        case(ACCEPT):{
            char dst[10];
            strcpy(username,childthread[index].recvpkg.src);
            strcpy(dst,childthread[index].recvpkg.dst);
            printf("%s accept to battle with %s", username,dst);
            memset(&childthread[index].sendpkg, 0 , MAXLINE);
            init_pkg(&childthread[index].sendpkg);
            childthread[index].sendpkg.service = BATTLE;
            childthread[index].sendpkg.status = ACCEPT;
            strcpy(childthread[index].sendpkg.src, username);
            strcpy(childthread[index].sendpkg.dst, dst);
            pthread_mutex_lock(&mutex);
            for (m = 0; m < couple; m++) {//取得申请者的fd
                if(strcmp(dst,onlineUser[m].username)==0){
                    fd = onlineUser[m].connfd;
                    break;
                }
            }
            for (i = 0; i < couple; i++) {//前面有空battle位,i等于该空位,否则,i等于couple
                if (battle[i].user1[0] == 0)
                    break;
            }
            battle[i].user1[0] = connfd;
            battle[i].user1[1] = 10;
            battle[i].user2[0] = fd;
            battle[i].user2[1] = 10;
            int j;
            for (j = 0; j < 10; j++)
                battle[i].watch_connfd[j] = 0;
            
            for(j = 0 ; j < online ; j++){
                if(strcmp(username,onlineUser[j].username)==0)
                    onlineUser[j].battling = i;
                }
            onlineUser[m].battling = i;
            send(fd, (void *)&childthread[index].sendpkg, HEADLINE, 0);
            printf("send battle accept successfully\n");
            pthread_mutex_unlock(&mutex);
            break;
        }
        case(REFUSE):{
            char dst[10];
            strcpy(username,childthread[index].recvpkg.src);
            strcpy(dst,childthread[index].recvpkg.dst);
            printf("%s refuse to battle with %s", username,dst);
            memset(&childthread[index].sendpkg, 0 , MAXLINE);
            init_pkg(&childthread[index].sendpkg);
            childthread[index].sendpkg.service = BATTLE;
            childthread[index].sendpkg.status = REFUSE;
            strcpy(childthread[index].sendpkg.src, username);
            strcpy(childthread[index].sendpkg.dst, dst);
            pthread_mutex_lock(&mutex);
            for(i = 0 ; i < online ; i++){
                if(strcmp(dst,onlineUser[i].username)==0){
                    fd = onlineUser[i].connfd;
                    send(fd, (void *)&childthread[index].sendpkg, HEADLINE, 0);
                    printf("send battle refuse successfully\n");
                    break;
                }
            }
            pthread_mutex_unlock(&mutex);
            break;
        }
        case(BATTLE):{
            char dst[10];
            strcpy(username,childthread[index].recvpkg.src);
            strcpy(dst,childthread[index].recvpkg.dst);
            for(i = 0 ; i < online ; i++)
                if(strcmp(username,onlineUser[i].username)==0){
                   // fd = onlineUser[i].connfd;
                    num = onlineUser[i].battling;
                    break;
                }
            if (battle[num].user1[0] == connfd) {//connfd is user1
                battle[num].user1[2]++;//round1++
                battle[num].user1[3] = (int)&childthread[index].recvpkg.message[0];//get gesture
                if (battle[num].user1[2] == battle[num].user2[2]) {//both have gestured, the same round
                    pthread_mutex_lock(&mutex);
                    int result = compare_gesture(battle[num].user1[3],battle[num].user2[3]);
                    battle[num].user1[1] += result;//blood1
                    battle[num].user2[1] -= result;//blood2
                    memset(&childthread[index].sendpkg, 0 , MAXLINE);
                    init_pkg(&childthread[index].sendpkg);
                    strcpy(childthread[index].sendpkg.src, username);
                    strcpy(childthread[index].sendpkg.dst, dst);
                    childthread[index].sendpkg.service = BATTLE;
                    
                    childthread[index].sendpkg.message[0] = battle[num].user1[2];//round
                    
                    childthread[index].sendpkg.message[1] = 1 + result;
                    childthread[index].sendpkg.message[2] = battle[num].user1[3];//user gesture
                    childthread[index].sendpkg.message[3] = battle[num].user1[1];//user blood
                    childthread[index].sendpkg.message[4] = battle[num].user2[3];
                    childthread[index].sendpkg.message[5] = battle[num].user2[1];
                    
                    if (battle[num].user1[1] == 0 || battle[num].user2[1] == 0)
                        childthread[index].sendpkg.status = END;
                    else
                        childthread[index].sendpkg.status = BATTLE;
                    childthread[index].sendpkg.length = strlen(childthread[index].sendpkg.message);
                    send(connfd, (void *)&childthread[index].sendpkg, HEADLINE+childthread[index].sendpkg.length, 0);
                    
                    memset(&childthread[index].sendpkg.src, 0 , 10);
                    memset(&childthread[index].sendpkg.dst, 0 , 10);
                    strcpy(childthread[index].sendpkg.src, dst);
                    strcpy(childthread[index].sendpkg.dst, username);
                    childthread[index].sendpkg.message[1] = 1 - result;
                    childthread[index].sendpkg.message[2] = battle[num].user2[3];//user gesture
                    childthread[index].sendpkg.message[3] = battle[num].user2[1];//user blood
                    childthread[index].sendpkg.message[4] = battle[num].user1[3];
                    childthread[index].sendpkg.message[5] = battle[num].user1[1];
                    send(fd, (void *)&childthread[index].sendpkg, HEADLINE+childthread[index].sendpkg.length, 0);
                    
                    childthread[index].sendpkg.service = WATCH;
                    if (battle[num].user1[1] == 0 || battle[num].user2[1] == 0)
                        childthread[index].sendpkg.status = END;
                    else
                        childthread[index].sendpkg.status = BATTLE;
                    for(i = 0; i < 10; i++)
                        if (battle[num].watch_connfd[i] != 0) {
                            send(battle[num].watch_connfd[i], (void *)&childthread[index].sendpkg, HEADLINE+childthread[index].sendpkg.length, 0);
                        }
                    pthread_mutex_unlock(&mutex);
                }
            }
            else if(battle[num].user2[0] == connfd) {
                battle[num].user2[2]++;//round1++
                battle[num].user2[3] = (int)&childthread[index].recvpkg.message[0];//get gesture
                if (battle[num].user1[2] == battle[num].user2[2]) {//both have gestured, the same round
                    pthread_mutex_lock(&mutex);
                    int result = compare_gesture(battle[num].user2[3],battle[num].user1[3]);
                    battle[num].user2[1] += result;//blood1
                    battle[num].user1[1] -= result;//blood2
                    memset(&childthread[index].sendpkg, 0 , MAXLINE);
                    init_pkg(&childthread[index].sendpkg);
                    strcpy(childthread[index].sendpkg.src, username);
                    strcpy(childthread[index].sendpkg.dst, dst);
                    childthread[index].sendpkg.service = BATTLE;
                    
                    childthread[index].sendpkg.message[0] = battle[num].user2[2];//round
                    
                    childthread[index].sendpkg.message[1] = 1 + result;
                    childthread[index].sendpkg.message[2] = battle[num].user2[3];//user gesture
                    childthread[index].sendpkg.message[3] = battle[num].user2[1];//user blood
                    childthread[index].sendpkg.message[4] = battle[num].user1[3];
                    childthread[index].sendpkg.message[5] = battle[num].user1[1];
                    
                    if (battle[num].user1[1] == 0 || battle[num].user2[1] == 0)
                        childthread[index].sendpkg.status = END;
                    else
                        childthread[index].sendpkg.status = BATTLE;
                    childthread[index].sendpkg.length = strlen(childthread[index].sendpkg.message);
                    send(connfd, (void *)&childthread[index].sendpkg, HEADLINE+childthread[index].sendpkg.length, 0);
                    
                    memset(&childthread[index].sendpkg.src, 0 , 10);
                    memset(&childthread[index].sendpkg.dst, 0 , 10);
                    strcpy(childthread[index].sendpkg.src, dst);
                    strcpy(childthread[index].sendpkg.dst, username);
                    childthread[index].sendpkg.message[1] = 1 - result;
                    childthread[index].sendpkg.message[2] = battle[num].user1[3];//user gesture
                    childthread[index].sendpkg.message[3] = battle[num].user1[1];//user blood
                    childthread[index].sendpkg.message[4] = battle[num].user2[3];
                    childthread[index].sendpkg.message[5] = battle[num].user2[1];
                    send(fd, (void *)&childthread[index].sendpkg, HEADLINE+childthread[index].sendpkg.length, 0);
                    childthread[index].sendpkg.service = WATCH;
                    for(i = 0; i < 10; i++)
                        if (battle[num].watch_connfd[i] != 0) {
                            send(battle[num].watch_connfd[i], (void *)&childthread[index].sendpkg, HEADLINE+childthread[index].sendpkg.length, 0);
                        }
                    pthread_mutex_unlock(&mutex);
                }
            }
            memset(battle[num].watch_connfd, 0, 10);
            battle[num].user1[0] = 0;
            battle[num].user1[2] = 0;
            battle[num].user1[3] = 0;
            battle[num].user2[0] = 0;
            battle[num].user2[2] = 0;
            battle[num].user2[3] = 0;
            break;
        }
        case(END):{
            char dst[10];
            strcpy(username,childthread[index].recvpkg.src);
            strcpy(dst,childthread[index].recvpkg.dst);
            printf("%s quit the battle with %s\n", username,dst);
            memset(&childthread[index].sendpkg, 0 , MAXLINE);
            init_pkg(&childthread[index].sendpkg);
            childthread[index].sendpkg.service = BATTLE;
            childthread[index].sendpkg.status = END;
            strcpy(childthread[index].sendpkg.src, dst);
            strcpy(childthread[index].sendpkg.dst, username);
            for(i = 0 ; i < online ; i++){
                if(strcmp(dst,onlineUser[i].username)==0){
                    fd = onlineUser[i].connfd;
                    num = onlineUser[i].battling;
                    onlineUser[i].battling = 0;//对方恢复空闲状态
                    }
                if (strcmp(username, onlineUser[i].username)==0)
                    onlineUser[i].battling = 0;//恢复空闲状态
            }
            
            //告诉对方游戏结束,对方赢
            childthread[index].sendpkg.message[0] = battle[num].user1[2];//round
            if (battle[num].user1[0] == connfd) {
                childthread[index].sendpkg.message[1] = 0;
                childthread[index].sendpkg.message[2] = battle[num].user1[3];//user gesture
                childthread[index].sendpkg.message[3] = battle[num].user1[1];//user blood
                childthread[index].sendpkg.message[4] = battle[num].user2[3];
                childthread[index].sendpkg.message[5] = battle[num].user2[1];
                childthread[index].sendpkg.length = strlen(childthread[index].sendpkg.message);
                pthread_mutex_lock(&mutex);
                send(connfd, (void *)&childthread[index].sendpkg, HEADLINE+childthread[index].sendpkg.length, 0);
                pthread_mutex_unlock(&mutex);
                childthread[index].sendpkg.message[1] = 2;
                childthread[index].sendpkg.message[2] = battle[num].user2[3];//user gesture
                childthread[index].sendpkg.message[3] = battle[num].user2[1];//user blood
                childthread[index].sendpkg.message[4] = battle[num].user1[3];
                childthread[index].sendpkg.message[5] = battle[num].user1[1];
                strcpy(childthread[index].sendpkg.src, username);
                strcpy(childthread[index].sendpkg.dst, dst);
                pthread_mutex_lock(&mutex);
                send(fd, (void *)&childthread[index].sendpkg, HEADLINE+childthread[index].sendpkg.length, 0);
                pthread_mutex_unlock(&mutex);
            }
            else if (battle[num].user2[0] == connfd) {
                childthread[index].sendpkg.message[1] = 0;
                childthread[index].sendpkg.message[2] = battle[num].user2[3];//user gesture
                childthread[index].sendpkg.message[3] = battle[num].user2[1];//user blood
                childthread[index].sendpkg.message[4] = battle[num].user1[3];
                childthread[index].sendpkg.message[5] = battle[num].user1[1];
                childthread[index].sendpkg.length = strlen(childthread[index].sendpkg.message);
                pthread_mutex_lock(&mutex);
                send(connfd, (void *)&childthread[index].sendpkg, HEADLINE+childthread[index].sendpkg.length, 0);
                pthread_mutex_unlock(&mutex);
                childthread[index].sendpkg.message[1] = 2;
                childthread[index].sendpkg.message[2] = battle[num].user1[3];//user gesture
                childthread[index].sendpkg.message[3] = battle[num].user1[1];//user blood
                childthread[index].sendpkg.message[4] = battle[num].user2[3];
                childthread[index].sendpkg.message[5] = battle[num].user2[1];
                strcpy(childthread[index].sendpkg.src, username);
                strcpy(childthread[index].sendpkg.dst, dst);
                pthread_mutex_lock(&mutex);
                send(fd, (void *)&childthread[index].sendpkg, HEADLINE+childthread[index].sendpkg.length, 0);
                pthread_mutex_unlock(&mutex);
            }
            childthread[index].sendpkg.service = WATCH;
            strcpy(childthread[index].sendpkg.src, dst);
            strcpy(childthread[index].sendpkg.dst, username);
            pthread_mutex_lock(&mutex);
            for(i = 0; i < 10; i++)
                if (battle[num].watch_connfd[i] != 0) {
                    send(battle[num].watch_connfd[i], (void *)&childthread[index].sendpkg, HEADLINE+childthread[index].sendpkg.length, 0);
                }
            pthread_mutex_unlock(&mutex);
            //清空本次battle纪录
            memset(battle[num].watch_connfd, 0, 10);
            battle[num].user1[0] = 0;
            battle[num].user1[2] = 0;
            battle[num].user1[3] = 0;
            battle[num].user2[0] = 0;
            battle[num].user2[2] = 0;
            battle[num].user2[3] = 0;
            break;
        }
        default:break;
        }
    }
Пример #10
0
void handleLogon(int connfd,int index){
    strcpy(username,childthread[index].recvpkg.src);
    strcpy(password,childthread[index].recvpkg.message);
    printf("User logon, the username id %s,the password is %s   ",username,password);
    int i;
    int success = 0;
    for(i = 0 ; i < online ; i++){
        if(strcmp(onlineUser[i].username,username) == 0){
            memset(&childthread[index].sendpkg, 0 , MAXLINE);
            init_pkg(&childthread[index].sendpkg);
            childthread[index].sendpkg.service = LOGON;
            strcpy(childthread[index].sendpkg.src, username);
            childthread[index].sendpkg.status = REPEAT;
            send(connfd, (void *)&childthread[index].sendpkg, HEADLINE, 0);
            return;
        }
    }
    /*if(i < online){
     memset(&childthread[index].sendpkg, 0 , MAXLINE);
     init_pkg(&childthread[index].sendpkg);
     childthread[index].sendpkg.service = LOGON;
     strcpy(childthread[index].sendpkg.src, username);
     childthread[index].sendpkg.status = REPEAT;
     send(connfd, (void *)&childthread[index].sendpkg, HEADLINE, 0);
     }
     else{*/
    for(i = 0 ; i < user ; i++){
        if(strcmp(userList[i].username,username) == 0){
            if(strcmp(userList[i].password,password) == 0){
                pthread_mutex_lock(&mutex);
                strcpy(onlineUser[online].username,username);
                onlineUser[online].connfd = connfd;
                online++;
                pthread_mutex_unlock(&mutex);
                memset(&childthread[index].sendpkg, 0 , MAXLINE);
                init_pkg(&childthread[index].sendpkg);
                childthread[index].sendpkg.service = LOGON;
                strcpy(childthread[index].sendpkg.src, username);
                strcpy(childthread[index].username,username);
                childthread[index].sendpkg.status = ACCEPT;
                send(connfd, (void *)&childthread[index].sendpkg, HEADLINE, 0);
                success = 1;
                childthread[index].logged = 1;
                broadcast(username,1);
                printf("Log on successfully\n");
                return;
            }
        }
    }
    /*if(i < user){
     if(strcmp(userList[i].password,password) == 0){
     pthread_mutex_lock(&mutex);
     strcpy(onlineUser[online].username,username);
     onlineUser[online].connfd = connfd;
     online++;
     pthread_mutex_unlock(&mutex);
     memset(&childthread[index].sendpkg, 0 , MAXLINE);
     init_pkg(&childthread[index].sendpkg);
     childthread[index].sendpkg.service = LOGON;
     strcpy(childthread[index].sendpkg.src, username);
     strcpy(childthread[index].username,username);
     childthread[index].sendpkg.status = ACCEPT;
     send(connfd, (void *)&childthread[index].sendpkg, HEADLINE, 0);
     success = 1;
     childthread[index].logged = 1;
     broadcast(username,1);
     printf("Log on successfully\n");
     }
     }*/
    if(!success){
        memset(&childthread[index].sendpkg, 0 , MAXLINE);
        init_pkg(&childthread[index].sendpkg);
        childthread[index].sendpkg.service = LOGON;
        strcpy(childthread[index].sendpkg.src, username);
        childthread[index].sendpkg.status = REFUSE;
        send(connfd, (void *)&childthread[index].sendpkg, HEADLINE, 0);
        printf("Log on failed\n");
    }
    //}
}