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.11 by root, Wed Dec 13 02:55:51 2006 UTC vs.
Revision 1.15 by root, Thu Dec 14 02:37:37 2006 UTC

49# include <arpa/inet.h> 49# include <arpa/inet.h>
50#endif 50#endif
51#include <newserver.h> 51#include <newserver.h>
52 52
53Socket_Info socket_info; 53Socket_Info socket_info;
54NewSocket *init_sockets; 54client_socket *init_sockets;
55
56client_socket::client_socket ()
57{
58}
59
60client_socket::~client_socket ()
61{
62}
55 63
56/** 64/**
57 * Initializes a connection. Really, it just sets up the data structure, 65 * Initializes a connection. Really, it just sets up the data structure,
58 * socket setup is handled elsewhere. We do send a version to the 66 * socket setup is handled elsewhere. We do send a version to the
59 * client. 67 * client.
60 */ 68 */
61void 69void
62InitConnection (NewSocket *ns, const char *from_ip) 70client_socket::init (const char *from_ip)
63{ 71{
64 SockList sl;
65 char buf[256];
66 int bufsize = 65535; /*Supposed absolute upper limit */ 72 int bufsize = 65535; /*Supposed absolute upper limit */
67 int oldbufsize; 73 int oldbufsize;
68 int buflen = sizeof (int); 74 int buflen = sizeof (int);
69 75
70 if (fcntl (ns->fd, F_SETFL, O_NONBLOCK) == -1) 76 if (fcntl (fd, F_SETFL, O_NONBLOCK) == -1)
71 LOG (llevError, "InitConnection: Error on fcntl.\n"); 77 LOG (llevError, "InitConnection: Error on fcntl.\n");
72 78
73 if (getsockopt (ns->fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen) == -1) 79 if (getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen) == -1)
74 oldbufsize = 0; 80 oldbufsize = 0;
75 81
76 if (oldbufsize < bufsize) 82 if (oldbufsize < bufsize)
77 if (setsockopt (ns->fd, SOL_SOCKET, SO_SNDBUF, (char *) &bufsize, sizeof (&bufsize))) 83 if (setsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &bufsize, sizeof (&bufsize)))
78 LOG (llevError, "InitConnection: setsockopt unable to set output buf size to %d\n", bufsize); 84 LOG (llevError, "InitConnection: setsockopt unable to set output buf size to %d\n", bufsize);
79 85
80 buflen = sizeof (oldbufsize); 86 buflen = sizeof (oldbufsize);
81 getsockopt (ns->fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); 87 getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen);
82#ifdef ESRV_DEBUG 88#ifdef ESRV_DEBUG
83 LOG (llevDebug, "Socket buffer size now %d bytes\n", oldbufsize); 89 LOG (llevDebug, "Socket buffer size now %d bytes\n", oldbufsize);
84#endif 90#endif
85 91
86 ns->faceset = 0; 92 faceset = 0;
87 ns->facecache = 0; 93 facecache = 0;
88 ns->image2 = 0; 94 image2 = 0;
89 ns->sound = 0; 95 sound = 0;
90 ns->exp64 = 0; 96 exp64 = 0;
91 ns->monitor_spells = 0; 97 monitor_spells = 0;
92 ns->mapmode = Map0Cmd; 98 mapmode = Map0Cmd;
93 ns->darkness = 1; 99 darkness = 1;
94 ns->status = Ns_Add; 100 status = Ns_Add;
95 ns->mapx = 11; 101 mapx = 11;
96 ns->mapy = 11; 102 mapy = 11;
97 ns->newmapcmd = 0; 103 newmapcmd = 0;
98 ns->itemcmd = 1; /* Default is version item1 command */ 104 itemcmd = 1; /* Default is version item1 command */
99 ns->ext_mapinfos = 0; /*extendedmapinfo datas */ 105 ext_mapinfos = 0; /*extendedmapinfo datas */
100 ns->EMI_smooth = 0; 106 EMI_smooth = 0;
101 ns->look_position = 0; 107 look_position = 0;
102 ns->update_look = 0; 108 update_look = 0;
103 ns->has_readable_type = 0; 109 has_readable_type = 0;
104 ns->supported_readables = 0; 110 supported_readables = 0;
105 ns->monitor_spells = 0; 111 monitor_spells = 0;
106 ns->mapinfocmd = 0; 112 mapinfocmd = 0;
107 ns->extcmd = 0; 113 extcmd = 0;
108 ns->extmap = 0; 114 extmap = 0;
109 115
110 /* we should really do some checking here - if total clients overflows 116 /* we should really do some checking here - if total clients overflows
111 * we need to do something more intelligent, because client id's will start 117 * we need to do something more intelligent, because client id's will start
112 * duplicating (not likely in normal cases, but malicous attacks that 118 * duplicating (not likely in normal cases, but malicous attacks that
113 * just open and close connections could get this total up. 119 * just open and close connections could get this total up.
114 */ 120 */
115 ns->inbuf.len = 0; 121 inbuf_len = 0;
116 ns->inbuf.buf = (unsigned char *) malloc (sizeof (unsigned char) * MAXSOCKBUF); 122
117 /* Basic initialization. Needed because we do a check in 123 /* Basic initialization. Needed because we do a check in
118 * HandleClient for oldsocketmode without checking the 124 * HandleClient for oldsocketmode without checking the
119 * length of data. 125 * length of data.
120 */ 126 */
121 memset (ns->inbuf.buf, 0, sizeof (unsigned char) * MAXSOCKBUF);
122 memset (&ns->lastmap, 0, sizeof (struct Map)); 127 memset (&lastmap, 0, sizeof (struct Map));
123 memset (ns->faces_sent, 0, ns->faces_sent_len * sizeof (*ns->faces_sent)); 128 memset (faces_sent, 0, faces_sent_len * sizeof (*faces_sent));
124 memset (&ns->anims_sent, 0, sizeof (ns->anims_sent)); 129 memset (&anims_sent, 0, sizeof (anims_sent));
125 memset (&ns->stats, 0, sizeof (struct statsinfo)); 130 memset (&stats, 0, sizeof (struct statsinfo));
126 /* Do this so we don't send a face command for the client for 131 /* Do this so we don't send a face command for the client for
127 * this face. Face 0 is sent to the client to say clear 132 * this face. Face 0 is sent to the client to say clear
128 * face information. 133 * face information.
129 */ 134 */
130 ns->faces_sent[0] = NS_FACESENT_FACE; 135 faces_sent[0] = NS_FACESENT_FACE;
131 136
132 ns->outputbuffer.start = 0; 137 outputbuffer.start = 0;
133 ns->outputbuffer.len = 0; 138 outputbuffer.len = 0;
134 ns->can_write = 1; 139 can_write = 1;
135 ns->password_fails = 0; 140 password_fails = 0;
136 141
137 ns->current_map = 0; 142 current_map = 0;
138 ns->current_x = 0; 143 current_x = 0;
139 ns->current_y = 0; 144 current_y = 0;
140 ns->client[0] = 0; 145 client[0] = 0;
141 ns->buggy_mapscroll = 0; 146 buggy_mapscroll = 0;
142 147
143 ns->sent_scroll = 0; 148 sent_scroll = 0;
144 ns->host = strdup_local (from_ip); 149 host = strdup_local (from_ip);
150
151 {
152 packet sl;
145 sprintf ((char *) buf, "version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); 153 sl.printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO);
146 sl.buf = (unsigned char *) buf; 154 send_packet (sl);
147 sl.len = strlen ((char *) buf); 155 }
148 Send_With_Handling (ns, &sl); 156
149#ifdef CS_LOGSTATS
150 if (socket_info.nconns > cst_tot.max_conn) 157 if (socket_info.nconns > cst_tot.max_conn)
151 cst_tot.max_conn = socket_info.nconns; 158 cst_tot.max_conn = socket_info.nconns;
152 if (socket_info.nconns > cst_lst.max_conn) 159 if (socket_info.nconns > cst_lst.max_conn)
153 cst_lst.max_conn = socket_info.nconns; 160 cst_lst.max_conn = socket_info.nconns;
154#endif
155} 161}
156 162
157 163
158/** This sets up the socket and reads all the image information into memory. */ 164/** This sets up the socket and reads all the image information into memory. */
159void 165void
167 173
168 socket_info.timeout.tv_sec = 0; 174 socket_info.timeout.tv_sec = 0;
169 socket_info.timeout.tv_usec = 0; 175 socket_info.timeout.tv_usec = 0;
170 socket_info.nconns = 0; 176 socket_info.nconns = 0;
171 177
172#ifdef CS_LOGSTATS
173 memset (&cst_tot, 0, sizeof (CS_Stats)); 178 memset (&cst_tot, 0, sizeof (CS_Stats));
174 memset (&cst_lst, 0, sizeof (CS_Stats)); 179 memset (&cst_lst, 0, sizeof (CS_Stats));
175 cst_tot.time_start = time (NULL); 180 cst_tot.time_start = time (NULL);
176 cst_lst.time_start = time (NULL); 181 cst_lst.time_start = time (NULL);
177#endif
178 182
179 LOG (llevDebug, "Initialize new client/server data\n"); 183 LOG (llevDebug, "Initialize new client/server data\n");
180 socket_info.nconns = 1; 184 socket_info.nconns = 1;
181 init_sockets = (NewSocket *) malloc (sizeof (NewSocket)); 185 init_sockets = (client_socket *) malloc (sizeof (client_socket));
182 init_sockets[0].faces_sent = NULL; /* unused */ 186 init_sockets[0].faces_sent = NULL; /* unused */
183 socket_info.allocated_sockets = 1; 187 socket_info.allocated_sockets = 1;
184 188
185 protox = getprotobyname ("tcp"); 189 protox = getprotobyname ("tcp");
186 if (protox == NULL) 190 if (protox == NULL)
252 * might be associated with the socket. It is up to the caller to 256 * might be associated with the socket. It is up to the caller to
253 * update the list 257 * update the list
254 */ 258 */
255 259
256void 260void
257free_newsocket (NewSocket *ns) 261free_newsocket (client_socket *ns)
258{ 262{
259 close (ns->fd); 263 close (ns->fd);
260 264
261 if (ns->stats.range) 265 if (ns->stats.range)
262 free (ns->stats.range); 266 free (ns->stats.range);
263 267
264 if (ns->stats.title) 268 if (ns->stats.title)
265 free (ns->stats.title); 269 free (ns->stats.title);
266 270
267 free (ns->host); 271 free (ns->host);
268 free (ns->inbuf.buf);
269} 272}
270 273
271/** Sends the 'goodbye' command to the player, and closes connection. */ 274/** Sends the 'goodbye' command to the player, and closes connection. */
272void 275void
273final_free_player (player *pl) 276final_free_player (player *pl)
274{ 277{
275 cs_write_string (&pl->socket, "goodbye", 8); 278 pl->socket.send_packet ("goodbye");
276 free_newsocket (&pl->socket); 279 free_newsocket (&pl->socket);
277 free_player (pl); 280 free_player (pl);
278} 281}
282

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines