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.12 by root, Thu Dec 14 00:01:35 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 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->lastmap, 0, sizeof (struct Map)); 127 memset (&lastmap, 0, sizeof (struct Map));
122 memset (ns->faces_sent, 0, ns->faces_sent_len * sizeof (*ns->faces_sent)); 128 memset (faces_sent, 0, faces_sent_len * sizeof (*faces_sent));
123 memset (&ns->anims_sent, 0, sizeof (ns->anims_sent)); 129 memset (&anims_sent, 0, sizeof (anims_sent));
124 memset (&ns->stats, 0, sizeof (struct statsinfo)); 130 memset (&stats, 0, sizeof (struct statsinfo));
125 /* 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
126 * 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
127 * face information. 133 * face information.
128 */ 134 */
129 ns->faces_sent[0] = NS_FACESENT_FACE; 135 faces_sent[0] = NS_FACESENT_FACE;
130 136
131 ns->outputbuffer.start = 0; 137 outputbuffer.start = 0;
132 ns->outputbuffer.len = 0; 138 outputbuffer.len = 0;
133 ns->can_write = 1; 139 can_write = 1;
134 ns->password_fails = 0; 140 password_fails = 0;
135 141
136 ns->current_map = 0; 142 current_map = 0;
137 ns->current_x = 0; 143 current_x = 0;
138 ns->current_y = 0; 144 current_y = 0;
139 ns->client[0] = 0; 145 client[0] = 0;
140 ns->buggy_mapscroll = 0; 146 buggy_mapscroll = 0;
141 147
142 ns->sent_scroll = 0; 148 sent_scroll = 0;
143 ns->host = strdup_local (from_ip); 149 host = strdup_local (from_ip);
150
151 {
152 packet sl;
144 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);
145 sl.buf = (unsigned char *) buf; 154 send_packet (sl);
146 sl.len = strlen ((char *) buf); 155 }
147 Send_With_Handling (ns, &sl); 156
148#ifdef CS_LOGSTATS
149 if (socket_info.nconns > cst_tot.max_conn) 157 if (socket_info.nconns > cst_tot.max_conn)
150 cst_tot.max_conn = socket_info.nconns; 158 cst_tot.max_conn = socket_info.nconns;
151 if (socket_info.nconns > cst_lst.max_conn) 159 if (socket_info.nconns > cst_lst.max_conn)
152 cst_lst.max_conn = socket_info.nconns; 160 cst_lst.max_conn = socket_info.nconns;
153#endif
154} 161}
155 162
156 163
157/** 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. */
158void 165void
166 173
167 socket_info.timeout.tv_sec = 0; 174 socket_info.timeout.tv_sec = 0;
168 socket_info.timeout.tv_usec = 0; 175 socket_info.timeout.tv_usec = 0;
169 socket_info.nconns = 0; 176 socket_info.nconns = 0;
170 177
171#ifdef CS_LOGSTATS
172 memset (&cst_tot, 0, sizeof (CS_Stats)); 178 memset (&cst_tot, 0, sizeof (CS_Stats));
173 memset (&cst_lst, 0, sizeof (CS_Stats)); 179 memset (&cst_lst, 0, sizeof (CS_Stats));
174 cst_tot.time_start = time (NULL); 180 cst_tot.time_start = time (NULL);
175 cst_lst.time_start = time (NULL); 181 cst_lst.time_start = time (NULL);
176#endif
177 182
178 LOG (llevDebug, "Initialize new client/server data\n"); 183 LOG (llevDebug, "Initialize new client/server data\n");
179 socket_info.nconns = 1; 184 socket_info.nconns = 1;
180 init_sockets = (NewSocket *) malloc (sizeof (NewSocket)); 185 init_sockets = (client_socket *) malloc (sizeof (client_socket));
181 init_sockets[0].faces_sent = NULL; /* unused */ 186 init_sockets[0].faces_sent = NULL; /* unused */
182 socket_info.allocated_sockets = 1; 187 socket_info.allocated_sockets = 1;
183 188
184 protox = getprotobyname ("tcp"); 189 protox = getprotobyname ("tcp");
185 if (protox == NULL) 190 if (protox == NULL)
251 * 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
252 * update the list 257 * update the list
253 */ 258 */
254 259
255void 260void
256free_newsocket (NewSocket *ns) 261free_newsocket (client_socket *ns)
257{ 262{
258 close (ns->fd); 263 close (ns->fd);
259 264
260 if (ns->stats.range) 265 if (ns->stats.range)
261 free (ns->stats.range); 266 free (ns->stats.range);
268 273
269/** Sends the 'goodbye' command to the player, and closes connection. */ 274/** Sends the 'goodbye' command to the player, and closes connection. */
270void 275void
271final_free_player (player *pl) 276final_free_player (player *pl)
272{ 277{
273 cs_write_string (&pl->socket, "goodbye", 8); 278 pl->socket.send_packet ("goodbye");
274 free_newsocket (&pl->socket); 279 free_newsocket (&pl->socket);
275 free_player (pl); 280 free_player (pl);
276} 281}
282

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines