interface::interface(QObject *parent) : QObject(parent) { connect(&connection, SIGNAL(on_start()), this, SLOT(do_login())); /* Creating the user interface */ w_login = new login; w_gameplay = new gameplay; connect(&connection, SIGNAL(on_login()), w_login, SLOT(do_wait_ready())); connect(&connection, SIGNAL(on_ready()), w_login, SLOT(do_wait())); w_login->show(); }
void client::readMessage() { QByteArray message = socket->readAll(); qDebug() << "reading..."; //qDebug() << message; QList<QByteArray> message_list= message.split('\n'); qDebug() << message_list; QJsonDocument json_document; QJsonObject json_object; QJsonValue type; for (int i=0; i<message_list.size(); i++){ json_document = QJsonDocument::fromJson(message_list.at(i)); json_object = json_document.object(); type = json_object.value("type"); if (type == "login"){ player_id = json_object.value("id").toInt(); emit on_login(); } else if (type == "response"){ if (json_object.value("object") == "rooms"){ rooms = json_object.value("data").toArray(); emit on_refresh_rooms(rooms); } else if (json_object.value("object") == "players"){ players = json_object.value("data").toArray(); emit on_refresh_players(players); } else if (json_object.value("object") == "board"){ board = json_object.value("data").toArray(); emit on_refresh_board(board); } } else if (type == "newroom"){ emit on_create_room(json_object.value("rid").toInt()); } else if (type == "join"){ emit on_join(json_object.value("rid").toInt()); } else if (type == "closegame") { emit on_close_game(); } else if (type == "startgame") { emit on_start_game(); } else if (type == "play") { emit on_update_game(json_object); } else if (type == "win") { emit on_game_over(json_object); } else if (type == "highlight") { emit on_highlight(json_object); } else if (type == "chat") { emit on_chat(json_object); } else if (type == "spectate") { emit on_spectate(json_object.value("rid").toInt()); } } }
///分析服务器回应 void talk_to_svr::analyze_answer(std::string &msg) { if (msg.find("register") == 0)on_register(msg); else if (msg.find("login") == 0)on_login(msg); else if (msg.find("ping") == 0)on_ping(msg); else if (msg.find("ask for clients") == 0)on_ask_client(msg); else if (msg.find("send to") == 0)on_sendto(msg); else if (msg.find("send from") == 0)on_sendfrom(msg); else { //funcSelect(); return; /// 处理错误信息 error answer } }
int main() { /* Variables */ int fd; int stdout_fileno; char *username; char *home; char *conf_file_name; /* Initialize variables */ afterlist = ul_create(8); beforelist = ul_create(8); stdout_fileno = fileno(stdout); /* Get our username */ struct passwd *p = getpwuid(getuid()); if(p == NULL) fatalperror("getpwuid"); /* warning - username will now point to a static area, subsequent getpwuid calls may overwite it */ username = p->pw_name; /* Get our home directory */ home = getenv("HOME"); if(home == NULL) fatalerror("$HOME is not set.\n"); conf_file_name = malloc(strlen(home) + 1 + strlen(conf_file_basename) + 1); strcpy(conf_file_name, home); strcat(conf_file_name, "/"); strcat(conf_file_name, conf_file_basename); /* Set up atexit */ atexit(free_mem_on_exit); /* Read conf file */ config = load_config(conf_file_name); /* If we are supposed to print a user list upon startup, do it now, before fork()ing */ if(config->initialshow) { struct userlist *ls = ul_create(8); ul_populate(ls); ul_sort(ls); if(ls->array[0] == NULL) { printf("No users logged in.\n"); } else { printf("Users logged in: "); for(int i = 0; ls->array[i] != NULL; i++) { if(i > 0 && !strcmp(ls->array[i], ls->array[i-1])) continue; printf("%s, ", ls->array[i]); } printf("\b\b \n"); } ul_free(ls); } /* If we aren't supposed to listen to INs *or* OUTs, no point in continuing */ if(!config->listen_ins && !config->listen_outs) exit(EXIT_SUCCESS); /* If we are forking, fork() and then exit the parent */ if(config->forking) { pid_t pid = fork(); if(pid > 0) exit(0); else if(pid == -1) fatalperror("fork"); /* This setpgid() call changes the process-group ID so 'w' reports the shell (not us!) as the current command */ setpgid(getpid(),getpid()); /* Close stdin, and hang up the TTY, since we really can't access them from the "background" */ close(STDIN_FILENO); vhangup(); } /* Set up child-reaping for login-command */ signal(SIGCHLD, SIG_IGN); /* Start and setup inotify */ fd = inotify_init(); if(fd < 0) fatalperror("inotify_init"); if(inotify_add_watch(fd, _PATH_UTMP, IN_MODIFY) < 0) fatalperror("inotify_add_watch"); while(1) { ul_populate(beforelist); /* If we are fork()ing, we want to monitor stdout, which requires us to use select() with a timeout */ if(config->forking) { watch_and_wait(fd, stdout_fileno); } struct inotify_event evt; if(read(fd, &evt, sizeof(struct inotify_event)) < 0) fatalperror("read"); ul_populate(afterlist); int firstlen = ul_count(beforelist); int secondlen = ul_count(afterlist); if(firstlen == secondlen) { continue; } else if(firstlen > secondlen) { char *r = ul_subtract(beforelist, afterlist); if(r == NULL) continue; if(!strcmp(r, username)) continue; if(config->listen_outs) on_logout(r); } else { char *r = ul_subtract(afterlist, beforelist); if(r == NULL) continue; if(!strcmp(r, username)) continue; if(config->listen_ins) on_login(r); } if(config->oneshot) { exit(0); } } exit(SUCCESS); }