| 71 |
{"BBS_NET", "站点穿梭"}, |
{"BBS_NET", "站点穿梭"}, |
| 72 |
{"CHICKEN", "电子小鸡"}, |
{"CHICKEN", "电子小鸡"}, |
| 73 |
{"EDIT_ARTICLE", "修改文章"}, |
{"EDIT_ARTICLE", "修改文章"}, |
| 74 |
|
{"LOGIN", "进入大厅"}, |
| 75 |
{"MENU", "菜单选择"}, |
{"MENU", "菜单选择"}, |
| 76 |
{"POST_ARTICLE", "撰写文章"}, |
{"POST_ARTICLE", "撰写文章"}, |
| 77 |
{"REPLY_ARTICLE", "回复文章"}, |
{"REPLY_ARTICLE", "回复文章"}, |
| 78 |
{"USER_LIST", "查花名册"}, |
{"USER_LIST", "查花名册"}, |
| 79 |
{"USER_ONLINE", "环顾四周"}, |
{"USER_ONLINE", "环顾四周"}, |
| 80 |
{"VIEW_ARTICLE", "阅读文章"}, |
{"VIEW_ARTICLE", "阅读文章"}, |
| 81 |
{"VIEW_FILE", "查看文档"}}; |
{"VIEW_FILE", "查看文档"}, |
| 82 |
|
{"WWW", "Web浏览"}}; |
| 83 |
|
|
| 84 |
const int user_action_map_size = 11; |
const int user_action_map_size = sizeof(user_action_map) / sizeof(USER_ACTION_MAP); |
|
|
|
|
static int user_list_try_rd_lock(int semid, int wait_sec); |
|
|
static int user_list_try_rw_lock(int semid, int wait_sec); |
|
|
static int user_list_rd_unlock(int semid); |
|
|
static int user_list_rw_unlock(int semid); |
|
|
static int user_list_rd_lock(int semid); |
|
|
static int user_list_rw_lock(int semid); |
|
| 85 |
|
|
| 86 |
static int user_list_load(MYSQL *db, USER_LIST *p_list); |
static int user_list_load(MYSQL *db, USER_LIST *p_list); |
| 87 |
static int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_list); |
static int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_list); |
| 88 |
|
|
| 89 |
|
static int user_info_index_uid_comp(const void *ptr1, const void *ptr2) |
| 90 |
|
{ |
| 91 |
|
const USER_INFO_INDEX_UID *p1 = ptr1; |
| 92 |
|
const USER_INFO_INDEX_UID *p2 = ptr2; |
| 93 |
|
|
| 94 |
|
if (p1->uid < p2->uid) |
| 95 |
|
{ |
| 96 |
|
return -1; |
| 97 |
|
} |
| 98 |
|
else if (p1->uid > p2->uid) |
| 99 |
|
{ |
| 100 |
|
return 1; |
| 101 |
|
} |
| 102 |
|
else if (p1->id < p2->id) |
| 103 |
|
{ |
| 104 |
|
return -1; |
| 105 |
|
} |
| 106 |
|
else if (p1->id > p2->id) |
| 107 |
|
{ |
| 108 |
|
return 1; |
| 109 |
|
} |
| 110 |
|
return 0; |
| 111 |
|
} |
| 112 |
|
|
| 113 |
int user_list_load(MYSQL *db, USER_LIST *p_list) |
int user_list_load(MYSQL *db, USER_LIST *p_list) |
| 114 |
{ |
{ |
| 115 |
MYSQL_RES *rs = NULL; |
MYSQL_RES *rs = NULL; |
| 117 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 118 |
int ret = 0; |
int ret = 0; |
| 119 |
int i; |
int i; |
| 120 |
|
int j; |
| 121 |
|
int32_t last_uid; |
| 122 |
|
size_t intro_buf_offset; |
| 123 |
|
size_t intro_len; |
| 124 |
|
|
| 125 |
if (db == NULL || p_list == NULL) |
if (db == NULL || p_list == NULL) |
| 126 |
{ |
{ |
| 128 |
return -1; |
return -1; |
| 129 |
} |
} |
| 130 |
|
|
| 131 |
|
if (p_list->user_count > 0) |
| 132 |
|
{ |
| 133 |
|
last_uid = p_list->users[p_list->user_count - 1].uid; |
| 134 |
|
} |
| 135 |
|
else |
| 136 |
|
{ |
| 137 |
|
last_uid = -1; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 141 |
"SELECT user_list.UID AS UID, username, nickname, gender, gender_pub, life, exp, " |
"SELECT user_list.UID AS UID, username, nickname, gender, gender_pub, life, exp, visit_count, " |
| 142 |
"UNIX_TIMESTAMP(signup_dt), UNIX_TIMESTAMP(last_login_dt), UNIX_TIMESTAMP(birthday) " |
"UNIX_TIMESTAMP(signup_dt), UNIX_TIMESTAMP(last_login_dt), UNIX_TIMESTAMP(birthday), `introduction` " |
| 143 |
"FROM user_list INNER JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID " |
"FROM user_list INNER JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID " |
| 144 |
"INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID " |
"INNER JOIN user_reginfo ON user_list.UID = user_reginfo.UID " |
| 145 |
"WHERE enable ORDER BY UID"); |
"WHERE enable ORDER BY username"); |
| 146 |
|
|
| 147 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 148 |
{ |
{ |
| 158 |
goto cleanup; |
goto cleanup; |
| 159 |
} |
} |
| 160 |
|
|
| 161 |
|
intro_buf_offset = 0; |
| 162 |
i = 0; |
i = 0; |
| 163 |
while ((row = mysql_fetch_row(rs))) |
while ((row = mysql_fetch_row(rs))) |
| 164 |
{ |
{ |
| 165 |
|
// record |
| 166 |
|
p_list->users[i].id = i; |
| 167 |
p_list->users[i].uid = atoi(row[0]); |
p_list->users[i].uid = atoi(row[0]); |
| 168 |
strncpy(p_list->users[i].username, row[1], sizeof(p_list->users[i].username) - 1); |
strncpy(p_list->users[i].username, row[1], sizeof(p_list->users[i].username) - 1); |
| 169 |
p_list->users[i].username[sizeof(p_list->users[i].username) - 1] = '\0'; |
p_list->users[i].username[sizeof(p_list->users[i].username) - 1] = '\0'; |
| 173 |
p_list->users[i].gender_pub = (int8_t)(row[4] == NULL ? 0 : atoi(row[4])); |
p_list->users[i].gender_pub = (int8_t)(row[4] == NULL ? 0 : atoi(row[4])); |
| 174 |
p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5])); |
p_list->users[i].life = (row[5] == NULL ? 0 : atoi(row[5])); |
| 175 |
p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6])); |
p_list->users[i].exp = (row[6] == NULL ? 0 : atoi(row[6])); |
| 176 |
p_list->users[i].signup_dt = (row[7] == NULL ? 0 : atol(row[7])); |
p_list->users[i].visit_count = (row[7] == NULL ? 0 : atoi(row[7])); |
| 177 |
p_list->users[i].last_login_dt = (row[8] == NULL ? 0 : atol(row[8])); |
p_list->users[i].signup_dt = (row[8] == NULL ? 0 : atol(row[8])); |
| 178 |
p_list->users[i].birthday = (row[9] == NULL ? 0 : atol(row[9])); |
p_list->users[i].last_login_dt = (row[9] == NULL ? 0 : atol(row[9])); |
| 179 |
|
p_list->users[i].birthday = (row[10] == NULL ? 0 : atol(row[10])); |
| 180 |
|
intro_len = strlen((row[11] == NULL ? "" : row[11])); |
| 181 |
|
if (intro_len >= sizeof(p_list->user_intro_buf) - 1 - intro_buf_offset) |
| 182 |
|
{ |
| 183 |
|
log_error("OOM for user introduction: len=%d, i=%d\n", intro_len, i); |
| 184 |
|
break; |
| 185 |
|
} |
| 186 |
|
memcpy(p_list->user_intro_buf + intro_buf_offset, |
| 187 |
|
(row[11] == NULL ? "" : row[11]), |
| 188 |
|
intro_len + 1); |
| 189 |
|
p_list->users[i].intro = p_list->user_intro_buf + intro_buf_offset; |
| 190 |
|
intro_buf_offset += (intro_len + 1); |
| 191 |
|
|
| 192 |
i++; |
i++; |
| 193 |
if (i >= BBS_max_user_count) |
if (i >= BBS_max_user_count) |
| 199 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 200 |
rs = NULL; |
rs = NULL; |
| 201 |
|
|
| 202 |
|
if (i != p_list->user_count || p_list->users[i - 1].uid != last_uid) // Count of users changed |
| 203 |
|
{ |
| 204 |
|
// Rebuild index |
| 205 |
|
for (j = 0; j < i; j++) |
| 206 |
|
{ |
| 207 |
|
p_list->index_uid[j].uid = p_list->users[j].uid; |
| 208 |
|
p_list->index_uid[j].id = j; |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp); |
| 212 |
|
|
| 213 |
|
#ifdef _DEBUG |
| 214 |
|
log_error("Rebuild index of %d users, last_uid=%d\n", i, p_list->users[i - 1].uid); |
| 215 |
|
#endif |
| 216 |
|
} |
| 217 |
|
|
| 218 |
p_list->user_count = i; |
p_list->user_count = i; |
| 219 |
|
|
| 220 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 234 |
char sql[SQL_BUFFER_LEN]; |
char sql[SQL_BUFFER_LEN]; |
| 235 |
int ret = 0; |
int ret = 0; |
| 236 |
int i; |
int i; |
| 237 |
|
int j; |
| 238 |
|
|
| 239 |
if (db == NULL || p_list == NULL) |
if (db == NULL || p_list == NULL) |
| 240 |
{ |
{ |
| 245 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 246 |
"SELECT SID, UID, ip, current_action, UNIX_TIMESTAMP(login_tm), " |
"SELECT SID, UID, ip, current_action, UNIX_TIMESTAMP(login_tm), " |
| 247 |
"UNIX_TIMESTAMP(last_tm) FROM user_online " |
"UNIX_TIMESTAMP(last_tm) FROM user_online " |
| 248 |
"WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND) " |
"WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND) AND UID <> 0 " |
| 249 |
"ORDER BY last_tm DESC", |
"ORDER BY last_tm DESC", |
| 250 |
BBS_user_off_line); |
BBS_user_off_line); |
| 251 |
|
|
| 266 |
i = 0; |
i = 0; |
| 267 |
while ((row = mysql_fetch_row(rs))) |
while ((row = mysql_fetch_row(rs))) |
| 268 |
{ |
{ |
| 269 |
|
p_list->users[i].id = i; |
| 270 |
strncpy(p_list->users[i].session_id, row[0], sizeof(p_list->users[i].session_id) - 1); |
strncpy(p_list->users[i].session_id, row[0], sizeof(p_list->users[i].session_id) - 1); |
|
|
|
| 271 |
p_list->users[i].session_id[sizeof(p_list->users[i].session_id) - 1] = '\0'; |
p_list->users[i].session_id[sizeof(p_list->users[i].session_id) - 1] = '\0'; |
| 272 |
if (query_user_info(atoi(row[1]), &(p_list->users[i].user_info)) <= 0) |
|
| 273 |
|
if ((ret = query_user_info_by_uid(atoi(row[1]), &(p_list->users[i].user_info))) <= 0) |
| 274 |
{ |
{ |
| 275 |
log_error("query_user_info(%d) error\n", atoi(row[1])); |
log_error("query_user_info_by_uid(%d) error\n", atoi(row[1])); |
| 276 |
continue; |
continue; |
| 277 |
} |
} |
| 278 |
|
|
| 284 |
p_list->users[i].current_action_title = NULL; |
p_list->users[i].current_action_title = NULL; |
| 285 |
if (p_list->users[i].current_action[0] == '\0') |
if (p_list->users[i].current_action[0] == '\0') |
| 286 |
{ |
{ |
| 287 |
p_list->users[i].current_action_title = "Web浏览"; |
p_list->users[i].current_action_title = ""; |
| 288 |
} |
} |
| 289 |
else if (trie_dict_get(p_trie_action_dict, p_list->users[i].current_action, (int64_t *)(&(p_list->users[i].current_action_title))) < 0) |
else if (trie_dict_get(p_trie_action_dict, p_list->users[i].current_action, (int64_t *)(&(p_list->users[i].current_action_title))) < 0) |
| 290 |
{ |
{ |
| 306 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 307 |
rs = NULL; |
rs = NULL; |
| 308 |
|
|
| 309 |
|
if (i > 0) |
| 310 |
|
{ |
| 311 |
|
// Rebuild index |
| 312 |
|
for (j = 0; j < i; j++) |
| 313 |
|
{ |
| 314 |
|
p_list->index_uid[j].uid = p_list->users[j].user_info.uid; |
| 315 |
|
p_list->index_uid[j].id = j; |
| 316 |
|
} |
| 317 |
|
|
| 318 |
|
qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp); |
| 319 |
|
|
| 320 |
|
#ifdef _DEBUG |
| 321 |
|
log_error("Rebuild index of %d online users\n", i); |
| 322 |
|
#endif |
| 323 |
|
} |
| 324 |
|
|
| 325 |
p_list->user_count = i; |
p_list->user_count = i; |
| 326 |
|
|
| 327 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 328 |
log_error("Loaded %d users\n", p_list->user_count); |
log_error("Loaded %d online users\n", p_list->user_count); |
| 329 |
#endif |
#endif |
| 330 |
|
|
| 331 |
cleanup: |
cleanup: |
| 507 |
MYSQL *db = NULL; |
MYSQL *db = NULL; |
| 508 |
USER_LIST *p_tmp; |
USER_LIST *p_tmp; |
| 509 |
USER_ONLINE_LIST *p_online_tmp; |
USER_ONLINE_LIST *p_online_tmp; |
| 510 |
|
int ret = 0; |
| 511 |
|
|
| 512 |
if (p_user_list_pool == NULL) |
if (p_user_list_pool == NULL) |
| 513 |
{ |
{ |
| 527 |
if (user_online_list_load(db, p_user_list_pool->p_online_new) < 0) |
if (user_online_list_load(db, p_user_list_pool->p_online_new) < 0) |
| 528 |
{ |
{ |
| 529 |
log_error("user_online_list_load() error\n"); |
log_error("user_online_list_load() error\n"); |
| 530 |
return -2; |
ret = -2; |
| 531 |
|
goto cleanup; |
| 532 |
} |
} |
| 533 |
} |
} |
| 534 |
else |
else |
| 536 |
if (user_list_load(db, p_user_list_pool->p_new) < 0) |
if (user_list_load(db, p_user_list_pool->p_new) < 0) |
| 537 |
{ |
{ |
| 538 |
log_error("user_list_load() error\n"); |
log_error("user_list_load() error\n"); |
| 539 |
return -2; |
ret = -2; |
| 540 |
|
goto cleanup; |
| 541 |
} |
} |
| 542 |
} |
} |
| 543 |
|
|
| 544 |
mysql_close(db); |
mysql_close(db); |
| 545 |
|
db = NULL; |
| 546 |
|
|
| 547 |
if (user_list_rw_lock(p_user_list_pool->semid) < 0) |
if (user_list_rw_lock(p_user_list_pool->semid) < 0) |
| 548 |
{ |
{ |
| 549 |
log_error("user_list_rw_lock() error\n"); |
log_error("user_list_rw_lock() error\n"); |
| 550 |
return -3; |
ret = -3; |
| 551 |
|
goto cleanup; |
| 552 |
} |
} |
| 553 |
|
|
| 554 |
if (online_user) |
if (online_user) |
| 569 |
if (user_list_rw_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rw_unlock(p_user_list_pool->semid) < 0) |
| 570 |
{ |
{ |
| 571 |
log_error("user_list_rw_unlock() error\n"); |
log_error("user_list_rw_unlock() error\n"); |
| 572 |
return -3; |
ret = -3; |
| 573 |
|
goto cleanup; |
| 574 |
} |
} |
| 575 |
|
|
| 576 |
return 0; |
cleanup: |
| 577 |
|
mysql_close(db); |
| 578 |
|
|
| 579 |
|
return ret; |
| 580 |
} |
} |
| 581 |
|
|
| 582 |
int user_list_try_rd_lock(int semid, int wait_sec) |
int user_list_try_rd_lock(int semid, int wait_sec) |
| 741 |
return -1; |
return -1; |
| 742 |
} |
} |
| 743 |
|
|
| 744 |
|
*p_user_count = 0; |
| 745 |
|
*p_page_count = 0; |
| 746 |
|
|
| 747 |
// acquire lock of user list |
// acquire lock of user list |
| 748 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 749 |
{ |
{ |
| 797 |
return -1; |
return -1; |
| 798 |
} |
} |
| 799 |
|
|
| 800 |
|
*p_user_count = 0; |
| 801 |
|
*p_page_count = 0; |
| 802 |
|
|
| 803 |
// acquire lock of user list |
// acquire lock of user list |
| 804 |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 805 |
{ |
{ |
| 843 |
return ret; |
return ret; |
| 844 |
} |
} |
| 845 |
|
|
| 846 |
int query_user_info(int32_t uid, USER_INFO *p_user) |
int query_user_info(int32_t id, USER_INFO *p_user) |
| 847 |
|
{ |
| 848 |
|
int ret = 0; |
| 849 |
|
|
| 850 |
|
if (p_user == NULL) |
| 851 |
|
{ |
| 852 |
|
log_error("NULL pointer error\n"); |
| 853 |
|
return -1; |
| 854 |
|
} |
| 855 |
|
|
| 856 |
|
// acquire lock of user list |
| 857 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 858 |
|
{ |
| 859 |
|
log_error("user_list_rd_lock() error\n"); |
| 860 |
|
return -2; |
| 861 |
|
} |
| 862 |
|
|
| 863 |
|
if (id >= 0 && id < p_user_list_pool->p_current->user_count) // Found |
| 864 |
|
{ |
| 865 |
|
*p_user = p_user_list_pool->p_current->users[id]; |
| 866 |
|
ret = 1; |
| 867 |
|
} |
| 868 |
|
|
| 869 |
|
// release lock of user list |
| 870 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 871 |
|
{ |
| 872 |
|
log_error("user_list_rd_unlock() error\n"); |
| 873 |
|
ret = -1; |
| 874 |
|
} |
| 875 |
|
|
| 876 |
|
return ret; |
| 877 |
|
} |
| 878 |
|
|
| 879 |
|
int query_user_info_by_uid(int32_t uid, USER_INFO *p_user) |
| 880 |
{ |
{ |
| 881 |
int left; |
int left; |
| 882 |
int right; |
int right; |
| 883 |
int mid; |
int mid; |
| 884 |
|
int32_t id; |
| 885 |
int ret = 0; |
int ret = 0; |
| 886 |
|
|
| 887 |
if (p_user == NULL) |
if (p_user == NULL) |
| 903 |
while (left < right) |
while (left < right) |
| 904 |
{ |
{ |
| 905 |
mid = (left + right) / 2; |
mid = (left + right) / 2; |
| 906 |
if (uid < p_user_list_pool->p_current->users[mid].uid) |
if (uid < p_user_list_pool->p_current->index_uid[mid].uid) |
| 907 |
{ |
{ |
| 908 |
right = mid; |
right = mid; |
| 909 |
} |
} |
| 910 |
else if (uid > p_user_list_pool->p_current->users[mid].uid) |
else if (uid > p_user_list_pool->p_current->index_uid[mid].uid) |
| 911 |
{ |
{ |
| 912 |
left = mid + 1; |
left = mid + 1; |
| 913 |
} |
} |
| 914 |
else // if (uid == p_user_list_pool->p_current->users[mid].uid) |
else // if (uid == p_user_list_pool->p_current->index_uid[mid].uid) |
| 915 |
{ |
{ |
| 916 |
left = mid; |
left = mid; |
| 917 |
break; |
break; |
| 918 |
} |
} |
| 919 |
} |
} |
| 920 |
|
|
| 921 |
if (uid == p_user_list_pool->p_current->users[left].uid) // Found |
if (uid == p_user_list_pool->p_current->index_uid[left].uid) // Found |
| 922 |
{ |
{ |
| 923 |
*p_user = p_user_list_pool->p_current->users[left]; |
id = p_user_list_pool->p_current->index_uid[left].id; |
| 924 |
|
*p_user = p_user_list_pool->p_current->users[id]; |
| 925 |
ret = 1; |
ret = 1; |
| 926 |
} |
} |
| 927 |
|
|
| 928 |
// release lock of user list |
// release lock of user list |
| 929 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 930 |
|
{ |
| 931 |
|
log_error("user_list_rd_unlock() error\n"); |
| 932 |
|
ret = -1; |
| 933 |
|
} |
| 934 |
|
|
| 935 |
|
return ret; |
| 936 |
|
} |
| 937 |
|
|
| 938 |
|
int query_user_online_info(int32_t id, USER_ONLINE_INFO *p_user) |
| 939 |
|
{ |
| 940 |
|
int ret = 0; |
| 941 |
|
|
| 942 |
|
if (p_user == NULL) |
| 943 |
|
{ |
| 944 |
|
log_error("NULL pointer error\n"); |
| 945 |
|
return -1; |
| 946 |
|
} |
| 947 |
|
|
| 948 |
|
// acquire lock of user list |
| 949 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 950 |
|
{ |
| 951 |
|
log_error("user_list_rd_lock() error\n"); |
| 952 |
|
return -2; |
| 953 |
|
} |
| 954 |
|
|
| 955 |
|
if (id >= 0 && id < p_user_list_pool->p_online_current->user_count) // Found |
| 956 |
|
{ |
| 957 |
|
*p_user = p_user_list_pool->p_online_current->users[id]; |
| 958 |
|
ret = 1; |
| 959 |
|
} |
| 960 |
|
|
| 961 |
|
// release lock of user list |
| 962 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 963 |
|
{ |
| 964 |
|
log_error("user_list_rd_unlock() error\n"); |
| 965 |
|
ret = -1; |
| 966 |
|
} |
| 967 |
|
|
| 968 |
|
return ret; |
| 969 |
|
} |
| 970 |
|
|
| 971 |
|
int query_user_online_info_by_uid(int32_t uid, USER_ONLINE_INFO *p_users, int *p_user_cnt, int start_id) |
| 972 |
|
{ |
| 973 |
|
int left; |
| 974 |
|
int right; |
| 975 |
|
int mid; |
| 976 |
|
int32_t id; |
| 977 |
|
int ret = 0; |
| 978 |
|
int i; |
| 979 |
|
|
| 980 |
|
if (p_users == NULL || p_user_cnt == NULL) |
| 981 |
|
{ |
| 982 |
|
log_error("NULL pointer error\n"); |
| 983 |
|
return -1; |
| 984 |
|
} |
| 985 |
|
|
| 986 |
|
// acquire lock of user list |
| 987 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 988 |
|
{ |
| 989 |
|
log_error("user_list_rd_lock() error\n"); |
| 990 |
|
return -2; |
| 991 |
|
} |
| 992 |
|
|
| 993 |
|
left = start_id; |
| 994 |
|
right = p_user_list_pool->p_online_current->user_count - 1; |
| 995 |
|
|
| 996 |
|
while (left < right) |
| 997 |
|
{ |
| 998 |
|
mid = (left + right) / 2; |
| 999 |
|
if (uid < p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1000 |
|
{ |
| 1001 |
|
right = mid; |
| 1002 |
|
} |
| 1003 |
|
else if (uid > p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1004 |
|
{ |
| 1005 |
|
left = mid + 1; |
| 1006 |
|
} |
| 1007 |
|
else // if (uid == p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1008 |
|
{ |
| 1009 |
|
left = mid; |
| 1010 |
|
break; |
| 1011 |
|
} |
| 1012 |
|
} |
| 1013 |
|
|
| 1014 |
|
if (uid == p_user_list_pool->p_online_current->index_uid[left].uid) |
| 1015 |
|
{ |
| 1016 |
|
right = left; |
| 1017 |
|
left = start_id; |
| 1018 |
|
|
| 1019 |
|
while (left < right) |
| 1020 |
|
{ |
| 1021 |
|
mid = (left + right) / 2; |
| 1022 |
|
if (uid - 1 < p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1023 |
|
{ |
| 1024 |
|
right = mid; |
| 1025 |
|
} |
| 1026 |
|
else // if (uid - 1 >= p_user_list_pool->p_online_current->index_uid[mid].uid) |
| 1027 |
|
{ |
| 1028 |
|
left = mid + 1; |
| 1029 |
|
} |
| 1030 |
|
} |
| 1031 |
|
|
| 1032 |
|
for (i = 0; |
| 1033 |
|
left < p_user_list_pool->p_online_current->user_count && i < *p_user_cnt && |
| 1034 |
|
uid == p_user_list_pool->p_online_current->index_uid[left].uid; |
| 1035 |
|
left++, i++) |
| 1036 |
|
{ |
| 1037 |
|
id = p_user_list_pool->p_online_current->index_uid[left].id; |
| 1038 |
|
p_users[i] = p_user_list_pool->p_online_current->users[id]; |
| 1039 |
|
} |
| 1040 |
|
|
| 1041 |
|
if (i > 0) |
| 1042 |
|
{ |
| 1043 |
|
*p_user_cnt = i; |
| 1044 |
|
ret = 1; |
| 1045 |
|
} |
| 1046 |
|
} |
| 1047 |
|
|
| 1048 |
|
// release lock of user list |
| 1049 |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 1050 |
{ |
{ |
| 1051 |
log_error("user_list_rd_unlock() error\n"); |
log_error("user_list_rd_unlock() error\n"); |