… | |
… | |
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; |
… | |
… | |
100 | * this face. Face 0 is sent to the client to say clear |
101 | * this face. Face 0 is sent to the client to say clear |
101 | * face information. |
102 | * face information. |
102 | */ |
103 | */ |
103 | faces_sent[0] = NS_FACESENT_FACE; |
104 | faces_sent[0] = NS_FACESENT_FACE; |
104 | |
105 | |
|
|
106 | // socket I/O is low priority |
|
|
107 | // we prefer to have the server run instead of handling client requests |
|
|
108 | socket_ev.prio (PE_PRIO_NORMAL + 1); |
105 | socket_ev.fd (fd); |
109 | socket_ev.fd (fd); |
|
|
110 | socket_ev.poll (PE_R); |
|
|
111 | |
|
|
112 | // command handling is even lower priority |
|
|
113 | // that way no client can monopolise the server |
|
|
114 | cmd_ev.prio (PE_PRIO_NORMAL + 2); |
106 | |
115 | |
107 | // initialisation done, kick it! |
116 | // initialisation done, kick it! |
108 | |
117 | |
109 | { |
|
|
110 | packet sl; |
|
|
111 | sl.printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
118 | send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
112 | send_packet (sl); |
|
|
113 | flush (); |
119 | flush (); |
114 | } |
|
|
115 | |
120 | |
116 | #if 0//TODO |
121 | #if 0//TODO |
117 | socket_info.nconns++; |
122 | socket_info.nconns++; |
118 | if (socket_info.nconns > cst_tot.max_conn) cst_tot.max_conn = socket_info.nconns; |
123 | 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; |
124 | if (socket_info.nconns > cst_lst.max_conn) cst_lst.max_conn = socket_info.nconns; |
120 | #endif |
125 | #endif |
121 | } |
126 | } |
122 | |
127 | |
123 | client::~client () |
128 | client::~client () |
124 | { |
129 | { |
|
|
130 | if (pl) |
|
|
131 | pl->socket = 0; |
|
|
132 | |
|
|
133 | if (fd >= 0) |
|
|
134 | close (fd), fd = -1; |
|
|
135 | |
|
|
136 | free (stats.range), stats.range = 0; |
|
|
137 | free (stats.title), stats.title = 0; |
|
|
138 | free (host), host = 0; |
|
|
139 | } |
|
|
140 | |
|
|
141 | void |
|
|
142 | client::destroy () |
|
|
143 | { |
125 | status = Ns_Dead; |
144 | status = Ns_Dead; |
|
|
145 | socket_ev.suspend (); |
|
|
146 | cmd_ev.suspend (); |
126 | } |
147 | } |
127 | |
148 | |
128 | /** This sets up the socket and reads all the image information into memory. */ |
149 | /** This sets up the socket and reads all the image information into memory. */ |
129 | void |
150 | void |
130 | init_ericserver (void) |
151 | init_ericserver (void) |
131 | { |
152 | { |
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); |
153 | cst_tot.time_start = time (NULL); |
138 | cst_lst.time_start = time (NULL); |
154 | cst_lst.time_start = time (NULL); |
139 | |
155 | |
140 | LOG (llevDebug, "Initialize new client/server data\n"); |
156 | LOG (llevDebug, "Initialize new client/server data\n"); |
141 | |
157 | |
… | |
… | |
155 | { |
171 | { |
156 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
172 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
157 | free_socket_images (); |
173 | free_socket_images (); |
158 | } |
174 | } |
159 | |
175 | |
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. */ |
176 | /** Sends the 'goodbye' command to the player, and closes connection. */ |
188 | void |
177 | void |
189 | final_free_player (player *pl) |
178 | final_free_player (player *pl) |
190 | { |
179 | { |
191 | pl->socket->send_packet ("goodbye"); |
|
|
192 | free_newsocket (pl->socket); |
|
|
193 | free_player (pl); |
180 | free_player (pl); |
194 | } |
181 | } |
195 | |
182 | |
196 | bool |
183 | bool |
197 | add_client (int fd, const char *peername) |
184 | add_client (int fd, const char *peername) |