… | |
… | |
51 | * socket setup is handled elsewhere. We do send a version to the |
51 | * socket setup is handled elsewhere. We do send a version to the |
52 | * client. |
52 | * client. |
53 | */ |
53 | */ |
54 | client::client (int fd, const char *peername) |
54 | client::client (int fd, const char *peername) |
55 | : fd (fd), host (strdup (peername)), |
55 | : fd (fd), host (strdup (peername)), |
56 | socket_ev (this, &client::socket_cb) |
56 | socket_ev (this, &client::socket_cb), |
|
|
57 | cmd_ev (this, &client::cmd_cb) |
57 | { |
58 | { |
58 | { |
59 | { |
59 | struct linger linger_opt; |
60 | struct linger linger_opt; |
60 | |
61 | |
61 | linger_opt.l_onoff = 0; |
62 | linger_opt.l_onoff = 0; |
… | |
… | |
89 | status = Ns_Add; |
90 | status = Ns_Add; |
90 | mapx = 11; |
91 | mapx = 11; |
91 | mapy = 11; |
92 | mapy = 11; |
92 | itemcmd = 1; /* Default is version item1 command */ |
93 | itemcmd = 1; /* Default is version item1 command */ |
93 | |
94 | |
94 | faces_sent_len = nrofpixmaps; |
95 | faces_sent = salloc<uint8> (nrofpixmaps, 0); |
95 | faces_sent = (uint8 *)calloc (nrofpixmaps, sizeof (*faces_sent)); |
|
|
96 | if (!faces_sent) |
|
|
97 | fatal (OUT_OF_MEMORY); |
|
|
98 | |
96 | |
99 | /* Do this so we don't send a face command for the client for |
97 | /* Do this so we don't send a face command for the client for |
100 | * this face. Face 0 is sent to the client to say clear |
98 | * this face. Face 0 is sent to the client to say clear |
101 | * face information. |
99 | * face information. |
102 | */ |
100 | */ |
103 | faces_sent[0] = NS_FACESENT_FACE; |
101 | faces_sent[0] = NS_FACESENT_FACE; |
104 | |
102 | |
|
|
103 | // socket I/O is low priority |
|
|
104 | // we prefer to have the server run instead of handling client requests |
|
|
105 | socket_ev.prio (PE_PRIO_NORMAL + 1); |
105 | socket_ev.fd (fd); |
106 | socket_ev.fd (fd); |
|
|
107 | socket_ev.poll (PE_R); |
|
|
108 | |
|
|
109 | // command handling is even lower priority |
|
|
110 | // that way no client can monopolise the server |
|
|
111 | cmd_ev.prio (PE_PRIO_NORMAL + 2); |
106 | |
112 | |
107 | // initialisation done, kick it! |
113 | // initialisation done, kick it! |
108 | |
|
|
109 | { |
|
|
110 | packet sl; |
|
|
111 | sl.printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
114 | send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
112 | send_packet (sl); |
|
|
113 | flush (); |
115 | flush (); |
114 | } |
|
|
115 | |
116 | |
116 | #if 0//TODO |
117 | #if 0//TODO |
117 | socket_info.nconns++; |
118 | socket_info.nconns++; |
118 | if (socket_info.nconns > cst_tot.max_conn) cst_tot.max_conn = socket_info.nconns; |
119 | if (socket_info.nconns > cst_tot.max_conn) cst_tot.max_conn = socket_info.nconns; |
119 | if (socket_info.nconns > cst_lst.max_conn) cst_lst.max_conn = socket_info.nconns; |
120 | if (socket_info.nconns > cst_lst.max_conn) cst_lst.max_conn = socket_info.nconns; |
120 | #endif |
121 | #endif |
121 | } |
122 | } |
122 | |
123 | |
123 | client::~client () |
124 | client::~client () |
124 | { |
125 | { |
|
|
126 | if (pl) |
|
|
127 | pl->socket = 0; |
|
|
128 | |
|
|
129 | if (fd >= 0) |
|
|
130 | close (fd); |
|
|
131 | |
|
|
132 | free (faces_sent); |
|
|
133 | free (stats.range); |
|
|
134 | free (stats.title); |
|
|
135 | free (host); |
|
|
136 | } |
|
|
137 | |
|
|
138 | void |
|
|
139 | client::destroy () |
|
|
140 | { |
125 | status = Ns_Dead; |
141 | status = Ns_Dead; |
|
|
142 | socket_ev.suspend (); |
|
|
143 | cmd_ev.suspend (); |
126 | } |
144 | } |
127 | |
145 | |
128 | /** This sets up the socket and reads all the image information into memory. */ |
146 | /** This sets up the socket and reads all the image information into memory. */ |
129 | void |
147 | void |
130 | init_ericserver (void) |
148 | init_ericserver (void) |
131 | { |
149 | { |
132 | struct sockaddr_in insock; |
|
|
133 | struct linger linger_opt; |
|
|
134 | |
|
|
135 | memset (&cst_tot, 0, sizeof (CS_Stats)); |
|
|
136 | memset (&cst_lst, 0, sizeof (CS_Stats)); |
|
|
137 | cst_tot.time_start = time (NULL); |
150 | cst_tot.time_start = time (NULL); |
138 | cst_lst.time_start = time (NULL); |
151 | cst_lst.time_start = time (NULL); |
139 | |
152 | |
140 | LOG (llevDebug, "Initialize new client/server data\n"); |
153 | LOG (llevDebug, "Initialize new client/server data\n"); |
141 | |
154 | |
… | |
… | |
155 | { |
168 | { |
156 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
169 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
157 | free_socket_images (); |
170 | free_socket_images (); |
158 | } |
171 | } |
159 | |
172 | |
160 | /** |
|
|
161 | * Frees a socket. |
|
|
162 | * Basically, all we need to do here is free all data structures that |
|
|
163 | * might be associated with the socket. It is up to the caller to |
|
|
164 | * update the list |
|
|
165 | */ |
|
|
166 | |
|
|
167 | void |
|
|
168 | free_newsocket (client *ns) |
|
|
169 | { |
|
|
170 | if (ns->fd >= 0) |
|
|
171 | close (ns->fd); |
|
|
172 | |
|
|
173 | if (ns->stats.range) |
|
|
174 | free (ns->stats.range); |
|
|
175 | |
|
|
176 | if (ns->stats.title) |
|
|
177 | free (ns->stats.title); |
|
|
178 | |
|
|
179 | free (ns->host); |
|
|
180 | |
|
|
181 | ns->fd = -1; |
|
|
182 | ns->stats.range = 0; |
|
|
183 | ns->stats.title = 0; |
|
|
184 | ns->host = 0; |
|
|
185 | } |
|
|
186 | |
|
|
187 | /** Sends the 'goodbye' command to the player, and closes connection. */ |
173 | /** Sends the 'goodbye' command to the player, and closes connection. */ |
188 | void |
174 | void |
189 | final_free_player (player *pl) |
175 | final_free_player (player *pl) |
190 | { |
176 | { |
191 | pl->socket->send_packet ("goodbye"); |
|
|
192 | free_newsocket (pl->socket); |
|
|
193 | free_player (pl); |
177 | free_player (pl); |
194 | } |
178 | } |
195 | |
179 | |
196 | bool |
180 | bool |
197 | add_client (int fd, const char *peername) |
181 | add_client (int fd, const char *peername) |