ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/loop.C
Revision: 1.33
Committed: Mon Dec 25 11:25:50 2006 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.32: +4 -19 lines
Log Message:
- small, but subtle, rewrite of object management
- perl will now keep attachable objects alive
- objects are now refcounted
- refcouts need to be tested explicitly (refcnt_chk)
- explicit destroy is required current
- explicit destroy asks "nicely" for the object to self destruct, if possible
- refcounts will be used during mortal killing
- minor bugfixes, optimisations etc.
- some former hacks removed.

File Contents

# Content
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2002-2003 Mark Wedel & The Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 The author can be reached via e-mail to <crossfire@schmorp.de>
22 */
23
24 /**
25 * \file
26 * Main client/server loops.
27 *
28 * \date 2003-12-02
29 *
30 * loop.c mainly deals with initialization and higher level socket
31 * maintanance (checking for lost connections and if data has arrived.)
32 */
33
34
35 #include <global.h>
36 #include <sproto.h>
37 #include <sockproto.h>
38
39 #include <sys/types.h>
40 #include <sys/time.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netdb.h>
44
45 #include <unistd.h>
46 #include <arpa/inet.h>
47
48 #include <loader.h>
49
50 #define MAX_QUEUE_DEPTH 500 //TODO
51 #define MAX_QUEUE_BACKLOG 5. //TODO
52
53 void
54 client::reset_state ()
55 {
56 if (!pl)
57 return;
58
59 pl->run_on = 0;
60 pl->fire_on = 0;
61 }
62
63 void
64 client::queue_command (packet_type *handler, char *data, int datalen)
65 {
66 tstamp stamp = now ();
67
68 if (cmd_queue.size () >= MAX_QUEUE_DEPTH)
69 {
70 //TODO: just disconnect here?
71 reset_state ();
72 send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED);
73 }
74 else
75 {
76 cmd_queue.push_back (command ());
77 command &cmd = cmd_queue.back ();
78 cmd.stamp = stamp;
79 cmd.handler = handler;
80 cmd.data = salloc<char> (datalen + 1, data);
81 cmd.datalen = datalen;
82 }
83 }
84
85 bool
86 client::handle_command ()
87 {
88 bool skipping = false;
89
90 while (!cmd_queue.empty ()
91 && !(state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0))
92 {
93 command &cmd = cmd_queue.front ();
94
95 if (cmd.stamp + MAX_QUEUE_BACKLOG < now ())
96 {
97 if (!skipping)
98 {
99 skipping = true;
100 reset_state ();
101 send_packet_printf ("drawinfo %d ignoring delayed commands.", NDI_RED);
102 }
103
104 cmd_queue.pop_front ();
105 }
106 else
107 {
108 execute (cmd.handler, cmd.data, cmd.datalen);
109 cmd_queue.pop_front ();
110 return true;
111 }
112 }
113
114 return false;
115 }
116
117 void
118 flush_sockets (void)
119 {
120 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i)
121 (*i)->flush ();
122 }
123
124 /**
125 * This checks the sockets for input, does the right thing.
126 *
127 * A bit of this code is grabbed out of socket.c
128 * There are 2 lists we need to look through - init_sockets is a list
129 *
130 */
131 void
132 doeric_server (void)
133 {
134 #ifdef CS_LOGSTATS
135 if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME)
136 write_cs_stats ();
137 #endif
138
139 //TODO: should not be done here, either
140 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i)
141 {
142 client *s = *i;
143
144 if (player *pl = s->pl)
145 {
146 /* Update the players stats once per tick. More efficient than
147 * sending them whenever they change, and probably just as useful
148 */
149 esrv_update_stats (pl);
150
151 if (pl->last_weight != -1 && pl->last_weight != WEIGHT (pl->ob))
152 {
153 esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob);
154 if (pl->last_weight != WEIGHT (pl->ob))
155 LOG (llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n",
156 (unsigned long) pl->last_weight, WEIGHT (pl->ob));
157 }
158
159 draw_client_map (pl->ob);
160
161 if (s->update_look)
162 esrv_draw_look (pl->ob);
163 }
164
165 s->refcnt_chk ();
166 }
167 }
168