Example #1
0
nh_bool nhnet_exit_game(int exit_type)
{
    json_t *jmsg;
    int ret;

    if (!nhnet_active())
        return nh_exit_game(exit_type);

    xmalloc_cleanup();

    if (!api_entry())
        return 0;

    jmsg = json_pack("{si}", "exit_type", exit_type);
    jmsg = send_receive_msg("exit_game", jmsg);
    if (json_unpack(jmsg, "{si!}", "return", &ret) == -1) {
        print_error("Incorrect return object in nhnet_exit_game");
        ret = 0;
    }
    json_decref(jmsg);

    current_game = 0;

    api_exit();
    return ret;
}
Example #2
0
static void
ccmd_exit_game(json_t * params)
{
    int etype, status;

    if (json_unpack(params, "{si*}", "exit_type", &etype) == -1)
        exit_client("Bad set of parameters for exit_game");

    status = nh_exit_game(etype);
    if (status) {
        db_update_game(gameid, player_info.moves, player_info.z,
                       player_info.level_desc);
        log_msg("%s has closed game %d", user_info.username, gameid);
        gameid = 0;
        close(gamefd);
        gamefd = -1;
    }

    client_msg("exit_game", json_pack("{si}", "return", status));
}
Example #3
0
void exit_client(const char *err)
{
    const char *msg = err ? err : "";
    json_t *exit_obj;
    
    if (err)
	log_msg("Client error: %s. Exit.", err);
    
    if (outfd != -1) {
	exit_obj = json_object();
	
	json_object_set_new(exit_obj, "error", err ? json_true() : json_false());
	json_object_set_new(exit_obj, "message", json_string(msg));
	if (can_send_msg)
	    client_msg("server_error", exit_obj);
	else
	    json_decref(exit_obj);
	
	usleep(100); /* try to make sure the server process handles write() before close(). */
	close(infd);
	close(outfd);
	infd = outfd = -1;
    }
    
    termination_flag = 3; /* make sure the command loop exits if nh_exit_game jumps there */
    if (!sigsegv_flag)
	nh_exit_game(EXIT_FORCE_SAVE); /* might not return here */
    nh_lib_exit();
    close_database();
    if (user_info.username)
	free(user_info.username);
    free_config();
    reset_cached_diplaydata();
    end_logging();
    exit(err != NULL);
}