| 1 |
sysadm |
1.1 |
/*
|
| 2 |
|
|
* Copyright (C) 2000,2001 Onlyer (onlyer@263.net)
|
| 3 |
|
|
*
|
| 4 |
|
|
* This program is free software; you can redistribute it and/or
|
| 5 |
|
|
* modify it under the terms of the GNU General Public License
|
| 6 |
|
|
* as published by the Free Software Foundation; either version 2
|
| 7 |
|
|
* of the License, or (at your option) any later version.
|
| 8 |
|
|
*
|
| 9 |
|
|
* This program is distributed in the hope that it will be useful,
|
| 10 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
|
|
* GNU General Public License for more details.
|
| 13 |
|
|
*
|
| 14 |
|
|
* You should have received a copy of the GNU General Public License
|
| 15 |
|
|
* along with this program; if not, write to the Free Software
|
| 16 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 17 |
|
|
*/
|
| 18 |
|
|
#include "common/setup_before.h"
|
| 19 |
|
|
#include "setup.h"
|
| 20 |
|
|
|
| 21 |
|
|
#ifdef STDC_HEADERS
|
| 22 |
|
|
# include <stdlib.h>
|
| 23 |
|
|
#else
|
| 24 |
|
|
# ifdef HAVE_MALLOC_H
|
| 25 |
|
|
# include <malloc.h>
|
| 26 |
|
|
# endif
|
| 27 |
|
|
#endif
|
| 28 |
|
|
#ifdef TIME_WITH_SYS_TIME
|
| 29 |
|
|
# include <time.h>
|
| 30 |
|
|
# include <sys/time.h>
|
| 31 |
|
|
#else
|
| 32 |
|
|
# ifdef HAVE_SYS_TIME_H
|
| 33 |
|
|
# include <sys/time.h>
|
| 34 |
|
|
# else
|
| 35 |
|
|
# include <time.h>
|
| 36 |
|
|
# endif
|
| 37 |
|
|
#endif
|
| 38 |
|
|
|
| 39 |
|
|
#include "prefs.h"
|
| 40 |
|
|
#include "serverqueue.h"
|
| 41 |
|
|
#include "common/packet.h"
|
| 42 |
|
|
#include "common/list.h"
|
| 43 |
|
|
#include "common/eventlog.h"
|
| 44 |
|
|
#include "common/xalloc.h"
|
| 45 |
|
|
#include "common/setup_after.h"
|
| 46 |
|
|
|
| 47 |
|
|
static t_list * sqlist_head=NULL;
|
| 48 |
|
|
static unsigned int sqlist_seqno=0;
|
| 49 |
|
|
|
| 50 |
|
|
extern t_list * sqlist(void)
|
| 51 |
|
|
{
|
| 52 |
|
|
return sqlist_head;
|
| 53 |
|
|
}
|
| 54 |
|
|
|
| 55 |
|
|
extern int sqlist_create(void)
|
| 56 |
|
|
{
|
| 57 |
|
|
sqlist_head=list_create();
|
| 58 |
|
|
return 0;
|
| 59 |
|
|
}
|
| 60 |
|
|
|
| 61 |
|
|
extern int sqlist_destroy(void)
|
| 62 |
|
|
{
|
| 63 |
|
|
t_sq * sq;
|
| 64 |
|
|
|
| 65 |
|
|
BEGIN_LIST_TRAVERSE_DATA_CONST(sqlist_head,sq)
|
| 66 |
|
|
{
|
| 67 |
|
|
sq_destroy(sq,(t_elem **)curr_elem_);
|
| 68 |
|
|
}
|
| 69 |
|
|
END_LIST_TRAVERSE_DATA_CONST()
|
| 70 |
|
|
|
| 71 |
|
|
if (list_destroy(sqlist_head)<0) {
|
| 72 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"error destroy server queue list");
|
| 73 |
|
|
return -1;
|
| 74 |
|
|
}
|
| 75 |
|
|
sqlist_head=NULL;
|
| 76 |
|
|
return 0;
|
| 77 |
|
|
}
|
| 78 |
|
|
|
| 79 |
|
|
extern int sqlist_check_timeout(void)
|
| 80 |
|
|
{
|
| 81 |
|
|
t_sq * sq;
|
| 82 |
|
|
time_t now;
|
| 83 |
|
|
|
| 84 |
|
|
now=time(NULL);
|
| 85 |
|
|
BEGIN_LIST_TRAVERSE_DATA(sqlist_head, sq)
|
| 86 |
|
|
{
|
| 87 |
|
|
if (now - sq->ctime > prefs_get_sq_timeout()) {
|
| 88 |
|
|
eventlog(eventlog_level_info,__FUNCTION__,"destroying expired server queue %d",sq->seqno);
|
| 89 |
|
|
sq_destroy(sq,&curr_elem_);
|
| 90 |
|
|
}
|
| 91 |
|
|
}
|
| 92 |
|
|
END_LIST_TRAVERSE_DATA()
|
| 93 |
|
|
return 0;
|
| 94 |
|
|
}
|
| 95 |
|
|
|
| 96 |
|
|
extern t_sq * sqlist_find_sq(unsigned int seqno)
|
| 97 |
|
|
{
|
| 98 |
|
|
t_sq * sq;
|
| 99 |
|
|
|
| 100 |
|
|
BEGIN_LIST_TRAVERSE_DATA_CONST(sqlist_head,sq)
|
| 101 |
|
|
{
|
| 102 |
|
|
if (sq->seqno==seqno) return sq;
|
| 103 |
|
|
}
|
| 104 |
|
|
END_LIST_TRAVERSE_DATA_CONST()
|
| 105 |
|
|
return NULL;
|
| 106 |
|
|
}
|
| 107 |
|
|
|
| 108 |
|
|
extern t_sq * sq_create(unsigned int clientid, t_packet * packet,unsigned int gameid )
|
| 109 |
|
|
{
|
| 110 |
|
|
t_sq * sq;
|
| 111 |
|
|
|
| 112 |
|
|
sq=xmalloc(sizeof(t_sq));
|
| 113 |
|
|
sq->seqno=++sqlist_seqno;
|
| 114 |
|
|
sq->ctime=time(NULL);
|
| 115 |
|
|
sq->clientid=clientid;
|
| 116 |
|
|
sq->gameid=gameid;
|
| 117 |
|
|
sq->packet=packet;
|
| 118 |
|
|
sq->gametoken=0;
|
| 119 |
|
|
if (packet) packet_add_ref(packet);
|
| 120 |
|
|
list_append_data(sqlist_head,sq);
|
| 121 |
|
|
return sq;
|
| 122 |
|
|
}
|
| 123 |
|
|
|
| 124 |
|
|
extern int sq_destroy(t_sq * sq,t_elem ** curr)
|
| 125 |
|
|
{
|
| 126 |
|
|
ASSERT(sq,-1);
|
| 127 |
|
|
if (list_remove_data(sqlist_head,sq,curr)<0) {
|
| 128 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"error remove server queue from list");
|
| 129 |
|
|
return -1;
|
| 130 |
|
|
}
|
| 131 |
|
|
if (sq->packet) packet_del_ref(sq->packet);
|
| 132 |
|
|
xfree(sq);
|
| 133 |
|
|
return 0;
|
| 134 |
|
|
}
|
| 135 |
|
|
|
| 136 |
|
|
extern unsigned int sq_get_clientid(t_sq const * sq)
|
| 137 |
|
|
{
|
| 138 |
|
|
ASSERT(sq,0);
|
| 139 |
|
|
return sq->clientid;
|
| 140 |
|
|
}
|
| 141 |
|
|
|
| 142 |
|
|
extern t_packet * sq_get_packet(t_sq const * sq)
|
| 143 |
|
|
{
|
| 144 |
|
|
ASSERT(sq,NULL);
|
| 145 |
|
|
return sq->packet;
|
| 146 |
|
|
}
|
| 147 |
|
|
|
| 148 |
|
|
extern unsigned int sq_get_gameid(t_sq const * sq)
|
| 149 |
|
|
{
|
| 150 |
|
|
ASSERT(sq,0);
|
| 151 |
|
|
return sq->gameid;
|
| 152 |
|
|
}
|
| 153 |
|
|
|
| 154 |
|
|
extern unsigned int sq_get_seqno(t_sq const * sq)
|
| 155 |
|
|
{
|
| 156 |
|
|
ASSERT(sq,0);
|
| 157 |
|
|
return sq->seqno;
|
| 158 |
|
|
}
|
| 159 |
|
|
|
| 160 |
|
|
extern int sq_set_gametoken(t_sq * sq, unsigned int gametoken)
|
| 161 |
|
|
{
|
| 162 |
|
|
ASSERT(sq,-1);
|
| 163 |
|
|
sq->gametoken=gametoken;
|
| 164 |
|
|
return 0;
|
| 165 |
|
|
}
|
| 166 |
|
|
|
| 167 |
|
|
extern unsigned int sq_get_gametoken(t_sq const * sq)
|
| 168 |
|
|
{
|
| 169 |
|
|
ASSERT(sq,0);
|
| 170 |
|
|
return sq->gametoken;
|
| 171 |
|
|
}
|
| 172 |
|
|
|