Exemplo n.º 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);
    }
}
void ConnectionManager::removeDroppedConnections()
{
  V_Connection local_dropped;
  {
    boost::mutex::scoped_lock dropped_lock(dropped_connections_mutex_);
    dropped_connections_.swap(local_dropped);
  }

  boost::mutex::scoped_lock conn_lock(connections_mutex_);

  V_Connection::iterator conn_it = local_dropped.begin();
  V_Connection::iterator conn_end = local_dropped.end();
  for (;conn_it != conn_end; ++conn_it)
  {
    const ConnectionPtr& conn = *conn_it;
    connections_.erase(conn);
  }
}
void ConnectionManager::clear(Connection::DropReason reason)
{
  S_Connection local_connections;
  {
    boost::mutex::scoped_lock conn_lock(connections_mutex_);
    local_connections.swap(connections_);
  }

  for(S_Connection::iterator itr = local_connections.begin();
      itr != local_connections.end();
      itr++)
  {
    const ConnectionPtr& conn = *itr;
    conn->drop(reason);
  }

  boost::mutex::scoped_lock dropped_lock(dropped_connections_mutex_);
  dropped_connections_.clear();
}
Exemplo n.º 4
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;
}