| 246 |
int ret = 0; |
int ret = 0; |
| 247 |
int i; |
int i; |
| 248 |
int j; |
int j; |
| 249 |
|
int user_cnt; |
| 250 |
|
int guest_cnt; |
| 251 |
|
|
| 252 |
if (db == NULL || p_list == NULL) |
if (db == NULL || p_list == NULL) |
| 253 |
{ |
{ |
| 258 |
snprintf(sql, sizeof(sql), |
snprintf(sql, sizeof(sql), |
| 259 |
"SELECT SID, UID, ip, current_action, UNIX_TIMESTAMP(login_tm), " |
"SELECT SID, UID, ip, current_action, UNIX_TIMESTAMP(login_tm), " |
| 260 |
"UNIX_TIMESTAMP(last_tm) FROM user_online " |
"UNIX_TIMESTAMP(last_tm) FROM user_online " |
| 261 |
"WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND) AND UID <> 0 " |
"WHERE last_tm >= SUBDATE(NOW(), INTERVAL %d SECOND) " |
| 262 |
"ORDER BY last_tm DESC", |
"ORDER BY last_tm DESC", |
| 263 |
BBS_user_off_line); |
BBS_user_off_line); |
| 264 |
|
|
| 277 |
} |
} |
| 278 |
|
|
| 279 |
i = 0; |
i = 0; |
| 280 |
|
user_cnt = 0; |
| 281 |
|
guest_cnt = 0; |
| 282 |
while ((row = mysql_fetch_row(rs))) |
while ((row = mysql_fetch_row(rs))) |
| 283 |
{ |
{ |
| 284 |
|
if (atoi(row[1]) == 0) // guest |
| 285 |
|
{ |
| 286 |
|
guest_cnt++; |
| 287 |
|
continue; |
| 288 |
|
} |
| 289 |
|
else |
| 290 |
|
{ |
| 291 |
|
user_cnt++; |
| 292 |
|
} |
| 293 |
|
|
| 294 |
p_list->users[i].id = i; |
p_list->users[i].id = i; |
| 295 |
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); |
| 296 |
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'; |
| 331 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 332 |
rs = NULL; |
rs = NULL; |
| 333 |
|
|
| 334 |
if (i > 0) |
if (user_cnt > 0) |
| 335 |
{ |
{ |
| 336 |
// Rebuild index |
// Rebuild index |
| 337 |
for (j = 0; j < i; j++) |
for (j = 0; j < user_cnt; j++) |
| 338 |
{ |
{ |
| 339 |
p_list->index_uid[j].uid = p_list->users[j].user_info.uid; |
p_list->index_uid[j].uid = p_list->users[j].user_info.uid; |
| 340 |
p_list->index_uid[j].id = j; |
p_list->index_uid[j].id = j; |
| 341 |
} |
} |
| 342 |
|
|
| 343 |
qsort(p_list->index_uid, (size_t)i, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp); |
qsort(p_list->index_uid, (size_t)user_cnt, sizeof(USER_INFO_INDEX_UID), user_info_index_uid_comp); |
| 344 |
|
|
| 345 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 346 |
log_error("Rebuild index of %d online users\n", i); |
log_error("Rebuild index of %d online users\n", user_cnt); |
| 347 |
#endif |
#endif |
| 348 |
} |
} |
| 349 |
|
|
| 350 |
p_list->user_count = i; |
p_list->user_count = user_cnt; |
| 351 |
|
p_list->guest_count = guest_cnt; |
| 352 |
|
|
| 353 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 354 |
log_error("Loaded %d online users\n", p_list->user_count); |
log_error("Loaded %d online users and %d guest users\n", p_list->user_count, p_list->guest_count); |
| 355 |
#endif |
#endif |
| 356 |
|
|
| 357 |
cleanup: |
cleanup: |
| 871 |
return ret; |
return ret; |
| 872 |
} |
} |
| 873 |
|
|
| 874 |
|
int get_user_list_count(int *p_user_cnt) |
| 875 |
|
{ |
| 876 |
|
if (p_user_cnt == NULL) |
| 877 |
|
{ |
| 878 |
|
log_error("NULL pointer error\n"); |
| 879 |
|
return -1; |
| 880 |
|
} |
| 881 |
|
|
| 882 |
|
// acquire lock of user list |
| 883 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 884 |
|
{ |
| 885 |
|
log_error("user_list_rd_lock() error\n"); |
| 886 |
|
return -2; |
| 887 |
|
} |
| 888 |
|
|
| 889 |
|
*p_user_cnt = p_user_list_pool->p_current->user_count; |
| 890 |
|
|
| 891 |
|
// release lock of user list |
| 892 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 893 |
|
{ |
| 894 |
|
log_error("user_list_rd_unlock() error\n"); |
| 895 |
|
return -2; |
| 896 |
|
} |
| 897 |
|
|
| 898 |
|
return 0; |
| 899 |
|
} |
| 900 |
|
|
| 901 |
|
int get_user_online_list_count(int *p_user_cnt, int *p_guest_cnt) |
| 902 |
|
{ |
| 903 |
|
if (p_user_cnt == NULL || p_guest_cnt == NULL) |
| 904 |
|
{ |
| 905 |
|
log_error("NULL pointer error\n"); |
| 906 |
|
return -1; |
| 907 |
|
} |
| 908 |
|
|
| 909 |
|
// acquire lock of user list |
| 910 |
|
if (user_list_rd_lock(p_user_list_pool->semid) < 0) |
| 911 |
|
{ |
| 912 |
|
log_error("user_list_rd_lock() error\n"); |
| 913 |
|
return -2; |
| 914 |
|
} |
| 915 |
|
|
| 916 |
|
*p_user_cnt = p_user_list_pool->p_online_current->user_count; |
| 917 |
|
*p_guest_cnt = p_user_list_pool->p_online_current->guest_count; |
| 918 |
|
|
| 919 |
|
// release lock of user list |
| 920 |
|
if (user_list_rd_unlock(p_user_list_pool->semid) < 0) |
| 921 |
|
{ |
| 922 |
|
log_error("user_list_rd_unlock() error\n"); |
| 923 |
|
return -2; |
| 924 |
|
} |
| 925 |
|
|
| 926 |
|
return 0; |
| 927 |
|
} |
| 928 |
|
|
| 929 |
int query_user_info(int32_t id, USER_INFO *p_user) |
int query_user_info(int32_t id, USER_INFO *p_user) |
| 930 |
{ |
{ |
| 931 |
int ret = 0; |
int ret = 0; |