ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/init.C
(Generate patch)

Comparing deliantra/server/socket/init.C (file contents):
Revision 1.23 by root, Sun Dec 17 00:25:44 2006 UTC vs.
Revision 1.32 by root, Wed Dec 27 05:32:26 2006 UTC

29 * 29 *
30 * Mainly deals with initialization and higher level socket 30 * Mainly deals with initialization and higher level socket
31 * maintenance (checking for lost connections and if data has arrived.) 31 * maintenance (checking for lost connections and if data has arrived.)
32 * The reading of data is handled in ericserver.c 32 * The reading of data is handled in ericserver.c
33 */ 33 */
34
35 34
36#include <global.h> 35#include <global.h>
37#include <sproto.h> 36#include <sproto.h>
38#include <sys/types.h> 37#include <sys/types.h>
39#include <sys/time.h> 38#include <sys/time.h>
44#include <netdb.h> 43#include <netdb.h>
45 44
46#include <unistd.h> 45#include <unistd.h>
47#include <arpa/inet.h> 46#include <arpa/inet.h>
48 47
48#include <algorithm>
49
49sockvec clients; 50sockvec clients;
50 51
51/** 52/**
52 * Initializes a connection. Really, it just sets up the data structure, 53 * Initializes a connection. Really, it just sets up the data structure,
53 * socket setup is handled elsewhere. We do send a version to the 54 * socket setup is handled elsewhere. We do send a version to the
54 * client. 55 * client.
55 */ 56 */
56client::client (int fd, const char *peername) 57client::client (int fd, const char *peername)
57: fd (fd), host (strdup (peername)), 58: fd (fd), host (strdup (peername)),
58 socket_ev (this, &client::socket_cb), 59 socket_ev (this, &client::socket_cb),
59 cmd_ev (this, &client::cmd_cb) 60 cmd_ev (this, &client::cmd_cb),
61 cc_inv (this), cc_other (this)
60{ 62{
61 { 63 {
62 struct linger linger_opt; 64 struct linger linger_opt;
63 65
64 linger_opt.l_onoff = 0; 66 linger_opt.l_onoff = 0;
118 120
119 buflen = sizeof (oldbufsize); 121 buflen = sizeof (oldbufsize);
120 getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); 122 getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen);
121 } 123 }
122 124
125 state = ST_SETUP;
123 mapmode = Map0Cmd; 126 mapmode = Map0Cmd;
124 darkness = 1; 127 darkness = 1;
125 status = Ns_Add;
126 mapx = 11; 128 mapx = 11;
127 mapy = 11; 129 mapy = 11;
128 itemcmd = 1; /* Default is version item1 command */ 130 itemcmd = 1; /* Default is version item1 command */
129 131
130 faces_sent = salloc<uint8> (nrofpixmaps, 0); 132 faces_sent = salloc0<uint8> (nrofpixmaps);
131 133
132 /* Do this so we don't send a face command for the client for 134 /* Do this so we don't send a face command for the client for
133 * this face. Face 0 is sent to the client to say clear 135 * this face. Face 0 is sent to the client to say clear
134 * face information. 136 * face information.
135 */ 137 */
147 149
148 // initialisation done, kick it! 150 // initialisation done, kick it!
149 send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); 151 send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO);
150 flush (); 152 flush ();
151 153
154 // clients are externally referenced by the socket
155 refcnt_inc ();
156 clients.push_back (this);
157
152#if 0//TODO 158#if 0//TODO
153 socket_info.nconns++; 159 socket_info.nconns++;
154 if (socket_info.nconns > cst_tot.max_conn) cst_tot.max_conn = socket_info.nconns; 160 if (socket_info.nconns > cst_tot.max_conn) cst_tot.max_conn = socket_info.nconns;
155 if (socket_info.nconns > cst_lst.max_conn) cst_lst.max_conn = socket_info.nconns; 161 if (socket_info.nconns > cst_lst.max_conn) cst_lst.max_conn = socket_info.nconns;
156#endif 162#endif
157} 163}
158 164
159client::~client () 165client::~client ()
160{ 166{
161 if (pl) 167 sockvec::iterator i = find (clients.begin (), clients.end (), this);
162 pl->socket = 0; 168 if (i != clients.end ())
169 clients.erase (i);
163 170
164 if (fd >= 0) 171 sfree<uint8> (faces_sent, nrofpixmaps);
165 close (fd);
166
167 free (faces_sent);
168 free (stats.range); 172 free (stats.range);
169 free (stats.title); 173 free (stats.title);
170 free (host); 174 free (host);
171} 175}
172 176
173void 177void
174client::destroy () 178client::do_destroy ()
175{ 179{
176 status = Ns_Dead; 180 attachable::do_destroy ();
181
182 if (pl)
183 pl->disconnect ();
184
185 if (fd >= 0)
186 close (fd);
187
188 state = ST_DEAD;
189
177 socket_ev.suspend (); 190 socket_ev.suspend ();
178 cmd_ev.suspend (); 191 cmd_ev.suspend ();
192
193 // socket now longer references us
194 refcnt_dec ();
179} 195}
180 196
181/** This sets up the socket and reads all the image information into memory. */ 197/** This sets up the socket and reads all the image information into memory. */
182void 198void
183init_ericserver (void) 199init_ericserver (void)
203{ 219{
204 LOG (llevDebug, "Freeing all new client/server information.\n"); 220 LOG (llevDebug, "Freeing all new client/server information.\n");
205 free_socket_images (); 221 free_socket_images ();
206} 222}
207 223
208/** Sends the 'goodbye' command to the player, and closes connection. */ 224client *
209void
210final_free_player (player *pl)
211{
212 free_player (pl);
213}
214
215bool
216add_client (int fd, const char *peername) 225client::create (int fd, const char *peername)
217{ 226{
218 if (checkbanned (NULL, peername)) 227 if (checkbanned (NULL, peername))
219 { 228 {
220 LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername); 229 LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername);
221 return false; 230 return 0;
222 } 231 }
223 else 232 else
224 { 233 {
225 client *ns = new client (dup (fd), peername); 234 client *ns = new client (dup (fd), peername);
226 clients.push_back (ns); 235
236 ns->instantiate (); // effectively a nop right now
237
238 INVOKE_CLIENT (CONNECT, ns);
239
227 return true; 240 return ns;
228 } 241 }
229} 242}
230 243

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines