… | |
… | |
41 | #include <netinet/in.h> |
41 | #include <netinet/in.h> |
42 | #include <netdb.h> |
42 | #include <netdb.h> |
43 | |
43 | |
44 | #include <unistd.h> |
44 | #include <unistd.h> |
45 | #include <arpa/inet.h> |
45 | #include <arpa/inet.h> |
46 | #include <newserver.h> |
|
|
47 | |
46 | |
48 | sockvec client_sockets; |
47 | sockvec clients; |
49 | |
48 | |
50 | /** |
49 | /** |
51 | * Initializes a connection. Really, it just sets up the data structure, |
50 | * Initializes a connection. Really, it just sets up the data structure, |
52 | * 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 |
53 | * client. |
52 | * client. |
54 | */ |
53 | */ |
55 | client_socket::client_socket (int fd, const char *peername) |
54 | client::client (int fd, const char *peername) |
56 | : fd (fd), host (strdup (peername)), |
55 | : fd (fd), host (strdup (peername)), |
57 | socket_ev (this, &client_socket::socket_cb) |
56 | socket_ev (this, &client::socket_cb), |
|
|
57 | cmd_ev (this, &client::cmd_cb) |
58 | { |
58 | { |
59 | { |
59 | { |
60 | struct linger linger_opt; |
60 | struct linger linger_opt; |
61 | |
61 | |
62 | linger_opt.l_onoff = 0; |
62 | linger_opt.l_onoff = 0; |
… | |
… | |
101 | * 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 |
102 | * face information. |
102 | * face information. |
103 | */ |
103 | */ |
104 | faces_sent[0] = NS_FACESENT_FACE; |
104 | faces_sent[0] = NS_FACESENT_FACE; |
105 | |
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); |
106 | 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); |
107 | |
115 | |
108 | // initialisation done, kick it! |
116 | // initialisation done, kick it! |
109 | |
117 | |
110 | { |
|
|
111 | packet sl; |
|
|
112 | 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); |
113 | send_packet (sl); |
|
|
114 | flush (); |
119 | flush (); |
115 | } |
|
|
116 | |
120 | |
117 | #if 0//TODO |
121 | #if 0//TODO |
118 | socket_info.nconns++; |
122 | socket_info.nconns++; |
119 | 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; |
120 | 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; |
121 | #endif |
125 | #endif |
122 | } |
126 | } |
123 | |
127 | |
124 | client_socket::~client_socket () |
128 | client::~client () |
|
|
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 () |
125 | { |
143 | { |
126 | status = Ns_Dead; |
144 | status = Ns_Dead; |
|
|
145 | socket_ev.suspend (); |
|
|
146 | cmd_ev.suspend (); |
127 | } |
147 | } |
128 | |
148 | |
129 | /** 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. */ |
130 | void |
150 | void |
131 | init_ericserver (void) |
151 | init_ericserver (void) |
132 | { |
152 | { |
133 | struct sockaddr_in insock; |
|
|
134 | struct linger linger_opt; |
|
|
135 | |
|
|
136 | memset (&cst_tot, 0, sizeof (CS_Stats)); |
|
|
137 | memset (&cst_lst, 0, sizeof (CS_Stats)); |
|
|
138 | cst_tot.time_start = time (NULL); |
153 | cst_tot.time_start = time (NULL); |
139 | cst_lst.time_start = time (NULL); |
154 | cst_lst.time_start = time (NULL); |
140 | |
155 | |
141 | LOG (llevDebug, "Initialize new client/server data\n"); |
156 | LOG (llevDebug, "Initialize new client/server data\n"); |
142 | |
157 | |
… | |
… | |
156 | { |
171 | { |
157 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
172 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
158 | free_socket_images (); |
173 | free_socket_images (); |
159 | } |
174 | } |
160 | |
175 | |
161 | /** |
|
|
162 | * Frees a socket. |
|
|
163 | * Basically, all we need to do here is free all data structures that |
|
|
164 | * might be associated with the socket. It is up to the caller to |
|
|
165 | * update the list |
|
|
166 | */ |
|
|
167 | |
|
|
168 | void |
|
|
169 | free_newsocket (client_socket *ns) |
|
|
170 | { |
|
|
171 | if (ns->fd >= 0) |
|
|
172 | close (ns->fd); |
|
|
173 | |
|
|
174 | if (ns->stats.range) |
|
|
175 | free (ns->stats.range); |
|
|
176 | |
|
|
177 | if (ns->stats.title) |
|
|
178 | free (ns->stats.title); |
|
|
179 | |
|
|
180 | free (ns->host); |
|
|
181 | |
|
|
182 | ns->fd = -1; |
|
|
183 | ns->stats.range = 0; |
|
|
184 | ns->stats.title = 0; |
|
|
185 | ns->host = 0; |
|
|
186 | } |
|
|
187 | |
|
|
188 | /** Sends the 'goodbye' command to the player, and closes connection. */ |
176 | /** Sends the 'goodbye' command to the player, and closes connection. */ |
189 | void |
177 | void |
190 | final_free_player (player *pl) |
178 | final_free_player (player *pl) |
191 | { |
179 | { |
192 | pl->socket->send_packet ("goodbye"); |
|
|
193 | free_newsocket (pl->socket); |
|
|
194 | free_player (pl); |
180 | free_player (pl); |
195 | } |
181 | } |
196 | |
182 | |
197 | bool |
183 | bool |
198 | add_client_socket (int fd, const char *peername) |
184 | add_client (int fd, const char *peername) |
199 | { |
185 | { |
200 | if (checkbanned (NULL, peername)) |
186 | if (checkbanned (NULL, peername)) |
201 | { |
187 | { |
202 | LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername); |
188 | LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername); |
203 | return false; |
189 | return false; |
204 | } |
190 | } |
205 | else |
191 | else |
206 | { |
192 | { |
207 | client_socket *ns = new client_socket (dup (fd), peername); |
193 | client *ns = new client (dup (fd), peername); |
208 | client_sockets.push_back (ns); |
194 | clients.push_back (ns); |
209 | return true; |
195 | return true; |
210 | } |
196 | } |
211 | } |
197 | } |
212 | |
198 | |