Example #1
0
void disconnect(conn *c)
{
    if (c) {
        mdebug("disconnect");
        conn_lock(c);
        user_callback *cb = (user_callback *)(c->data);
        if (STATE_NOT_CONNECTED == c->state) {
            if (cb->disconnect)
                (*(cb->disconnect))(c);
            c->state = STATE_CONNECTED;
        }
        conn_unlock(c);
        conn_decref(c);
    }
}
Example #2
0
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;
}