/** * Quit from the game * @param response message following action command * @param user User making action request * @param manager Global resource manager */ void *action_quit( const char *response, User user, Management manager) { user_lock( user ); user_exit( user ); user_unlock( user ); return NULL; }
/** * Speak to all users * @param response message following action command * @param user User making action request * @param manager Global resource manager */ void *action_shout( const char *response, User user, Management manager) { // simple say LinkedList next = manager->users; while( next != NULL ){ User current = (User) next->data; // lock this user user_lock( current ); // if it is this user if( current == user ) { // just say its you client_send( current->client, "You shout: "); } else { // send name of sender client_send( current->client, current->parent->character->name ); // what to do when the client is not the user client_send( current->client, " shouts: " ); } // send message client_send( current->client, (char *)response); next = next_linked_item( next ); // next user_unlock( current ); } return NULL; }
/** * User thread responsible for handling individual clients * @param arg Structure to use to process the thread */ void *user_thread( void *arg ) { User user = new_user( ((Environment)arg)->client ); Management manager = ((Environment)arg)->manager; // increase thread count manager_up_thread( manager, pthread_self() ); // lock user for now user_lock( user ); // add the user to the list of users manager_add_user( manager, user ); // turn client on client_ok( user->client ); // ready user for commo user_unlock( user ); /* --------------------------------------------- * Login screen */ Room login_room = room_get( manager->rooms, 0 ); if( login_room == NULL ){ printf("Error getting login room\n"); //return 0; } char_set_room( user->parent->character, login_room ); // have a look action_look( NULL, user, manager ); // client_prompt( ) // accept input // initialize int buff_len = 256; char *buff = (char *) malloc( buff_len ); memset( buff, 0, 256 ); if( buff == NULL ){ perror("client thread recv buffer malloc"); return NULL; } // user response loop while( manager->cease != 1 && (user->flags & USER_FLAG_EXIT) == 0 ){ // receive response from client int length = client_recv( user->client, &buff, &buff_len ); // handle response if( buff != NULL && length > 0 ){ // parse the response for actionable text action_parse_response( buff, // buffer with response length, // length of response user, // user making response manager ); // manager // reset response memset(buff,0,length); } else { // nothing going on, yield sched_yield(); } } free( buff ); // remove from manager list manager_remove_user( manager, user ); // lock user for this user_lock( user ); // close and destory all structs // manually manage this part close_client( user->client ); destroy_client( user->client ); // user gone from manager so safe to unlock a head user_unlock( user ); // now destroy destroy_user( user ); free( ((Environment)arg) ); // decrease thread count manager_down_thread( manager, pthread_self() ); pthread_exit( 0 ); return NULL; }
static void login_request_cb(conn *c, unsigned char *msg, size_t sz) { Connection_T dbc = ConnectionPool_getConnection(pool); if (NULL == dbc) { mwarn("ConnectionPool_getConnection failed!"); return; } TRY { /* check account && passwd */ login::login_request lr; msg_body<login::login_request>(msg, sz, &lr); ResultSet_T result = Connection_executeQuery(dbc, "SELECT `id` FROM `profile` WHERE `account`='%s' AND `passwd`='%s';", lr.account().c_str(), lr.passwd().c_str()); if (ResultSet_next(result)) { uint64_t uid = ResultSet_getLLong(result, 1); login::error err = login::success; do { int tempid = user_manager_t::get_guid(); user_t *user = (user_t *)malloc(sizeof(user_t)); if (NULL == user) { mdebug("user_t alloc failed!"); err = login::unknow; break; } user->id = tempid; user->c = NULL; user->refcnt = 1; pthread_mutex_init(&user->lock, NULL); if (0 > user_mgr->add_user(user)) { mdebug("add_user failed!"); err = login::unknow; break; } user_lock(user); user->c = c; user_unlock(user); conn_lock(c); c->user = user; conn_unlock(c); /* tell center */ login::user_login_request ulr; ulr.set_tempid(tempid); ulr.set_uid(uid); center_info_t *info = center_info_mgr->get_center_info_incref(1); if (info) { conn_write<login::user_login_request>(info->c, le_user_login_request, &ulr); center_info_decref(info); } return; } while (0); login::login_reply lr; lr.set_err(err); conn_write<login::login_reply>(c, lc_login_reply, &lr); } else { login::login_reply lr; lr.set_err(login::auth); conn_write<login::login_reply>(c, lc_login_reply, &lr); } } CATCH(SQLException) { merror("SQLException -- %s", Exception_frame.message); } FINALLY { Connection_close(dbc); } END_TRY; }
/** * Look at the current room * @param response message following action command * @param user User making action request * @param manager Global resource manager */ void *action_look( const char *response, User user, Management manager) { user_lock( user ); Room current = char_get_room( user->parent->character ); if( current == NULL ){ client_send( user->client, "You do not appear to be anywhere\n"); } // get login int desc_length = 2048; char *desc = malloc( desc_length ); // init memset( desc, 0, desc_length ); int len = room_get_full_description( current, user, &desc, &desc_length ); // send the login screen client_send( user->client, desc ); // loop over exits LinkedList link = room_get_exits( current ); int shown = 0; while( link != NULL ){ Exit exit = (Exit) link->data; // if not hidden if( exit_is_hidden( exit ) == 0 ) { const char *exit_name = exit_get_dir_text( exit ); if( exit_name != NULL ){ if( shown > 0 ) client_send( user->client, (char *)"," ); shown = 1; client_send( user->client, (char *)exit_name ); } } link = next_linked_item( link ); } if( shown > 0 ) client_send( user->client, (char *)"\n" ); // free up the buffer if( desc != NULL ){ free( desc ); } user_unlock( user ); return NULL; }