Revision 143
improved the data encapsulation of connection pool
trunk/code/projects/colonet/ColonetServer/includes/ConnectionPool.h | ||
---|---|---|
40 | 40 |
int add_client(int client_file_descriptor); |
41 | 41 |
int remove_client(int pool_index); |
42 | 42 |
int check_clients(); |
43 |
|
|
43 | 44 |
int write_to_client(int pool_index, char * message, int length); |
44 |
void set_listen_socket_in_ready_set(int listen_socket); |
|
45 |
|
|
46 |
void add_new_socket_to_pool(int new_socket); |
|
47 |
|
|
45 | 48 |
int perform_select(int listen_socket); |
49 |
|
|
46 | 50 |
int is_socket_ready_to_read(int socket); |
47 | 51 |
|
48 |
int get_max_file_descriptor(); |
|
49 |
void set_max_file_descriptor(int new_max_file_descriptor); |
|
50 |
|
|
51 | 52 |
int get_number_clients_ready(); |
52 | 53 |
|
53 | 54 |
private: |
trunk/code/projects/colonet/ColonetServer/ConnectionPool.cpp | ||
---|---|---|
292 | 292 |
* |
293 | 293 |
* @return void |
294 | 294 |
*/ |
295 |
void ConnectionPool::set_listen_socket_in_ready_set(int listen_socket) {
|
|
296 |
if (listen_socket < 0)
|
|
295 |
void ConnectionPool::add_new_socket_to_pool(int new_socket) {
|
|
296 |
if (new_socket < 0)
|
|
297 | 297 |
return; |
298 | 298 |
|
299 |
FD_SET(listen_socket, &ready_set); |
|
299 |
FD_SET(new_socket, &ready_set); |
|
300 |
|
|
301 |
if (new_socket > max_file_descriptor) { |
|
302 |
max_file_descriptor = new_socket; |
|
303 |
} |
|
300 | 304 |
} |
301 | 305 |
|
302 | 306 |
/** |
... | ... | |
331 | 335 |
return FD_ISSET(socket, &read_set); |
332 | 336 |
} |
333 | 337 |
|
334 |
int ConnectionPool::get_max_file_descriptor() { |
|
335 |
return max_file_descriptor; |
|
336 |
} |
|
337 |
|
|
338 |
void ConnectionPool::set_max_file_descriptor(int new_max_file_descriptor) { |
|
339 |
max_file_descriptor = new_max_file_descriptor; |
|
340 |
} |
|
341 |
|
|
342 | 338 |
int ConnectionPool::get_number_clients_ready() { |
343 | 339 |
return number_clients_ready; |
344 | 340 |
} |
trunk/code/projects/colonet/ColonetServer/ColonetServer.cpp | ||
---|---|---|
86 | 86 |
* @brief Starts the server running (starts an infinite loop) |
87 | 87 |
*/ |
88 | 88 |
int ColonetServer::run_server() { |
89 |
connection_pool.set_listen_socket_in_ready_set(listen_socket);
|
|
89 |
connection_pool.add_new_socket_to_pool(listen_socket);
|
|
90 | 90 |
|
91 |
//TODO: why is all of this in that if statement and not just conn_pool.maxfd = listen_socket ?
|
|
92 |
if (listen_socket > connection_pool.get_max_file_descriptor()) {
|
|
93 |
connection_pool.set_max_file_descriptor(listen_socket);
|
|
91 |
int accept_socket = 0;
|
|
92 |
struct sockaddr_in client_addr;
|
|
93 |
socklen_t client_addr_size = sizeof(client_addr);
|
|
94 | 94 |
|
95 |
int accept_socket = 0; |
|
96 |
struct sockaddr_in client_addr; |
|
97 |
socklen_t client_addr_size = sizeof(client_addr); |
|
95 |
logger.log_message("Server initialized. About to start listening for connections"); |
|
98 | 96 |
|
99 |
logger.log_message("Server initialized. About to start listening for connections"); |
|
97 |
while(1) { |
|
98 |
connection_pool.perform_select(listen_socket); |
|
100 | 99 |
|
101 |
while(1) { |
|
102 |
connection_pool.perform_select(listen_socket); |
|
100 |
//either no descriptors are ready or there was an error |
|
101 |
if (connection_pool.get_number_clients_ready() <= 0) { |
|
102 |
continue; |
|
103 |
} |
|
103 | 104 |
|
104 |
//either no descriptors are ready or there was an error |
|
105 |
if (connection_pool.get_number_clients_ready() <= 0) { |
|
106 |
continue; |
|
105 |
if (connection_pool.is_socket_ready_to_read(listen_socket)) { |
|
106 |
printf("Something is trying to connect...\n"); |
|
107 |
if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) { |
|
108 |
if (errno == EMFILE) { |
|
109 |
printf("\tWhen attempting to accept a connection, " |
|
110 |
"reached the per process limit of file descriptors." |
|
111 |
" Dropping the new connection.\n"); |
|
112 |
continue; |
|
113 |
} else { |
|
114 |
printf("\tThere was an error when attempting to accept a connection"); |
|
115 |
} |
|
116 |
continue; |
|
107 | 117 |
} |
108 | 118 |
|
109 |
if (connection_pool.is_socket_ready_to_read(listen_socket)) { |
|
110 |
printf("Something is trying to connect...\n"); |
|
111 |
if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) { |
|
112 |
if (errno == EMFILE) { |
|
113 |
printf("\tWhen attempting to accept a connection, " |
|
114 |
"reached the per process limit of file descriptors." |
|
115 |
" Dropping the new connection.\n"); |
|
116 |
continue; |
|
117 |
} else { |
|
118 |
printf("\tThere was an error when attempting to accept a connection"); |
|
119 |
} |
|
120 |
continue; |
|
121 |
} |
|
119 |
char log_buffer[LOG_BUFFER_LENGTH]; |
|
120 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.", |
|
121 |
inet_ntoa(client_addr.sin_addr)); |
|
122 |
logger.log_string(LOG_TYPE_CONNECT, log_buffer); |
|
122 | 123 |
|
123 |
char log_buffer[LOG_BUFFER_LENGTH]; |
|
124 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.", |
|
125 |
inet_ntoa(client_addr.sin_addr)); |
|
126 |
logger.log_string(LOG_TYPE_CONNECT, log_buffer); |
|
127 |
|
|
128 |
if (connection_pool.add_client(accept_socket) < 0) { |
|
129 |
printf("\tThere was an error when trying to add a client to the connection pool."); |
|
130 |
continue; |
|
131 |
} |
|
132 |
|
|
133 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.", |
|
134 |
inet_ntoa(client_addr.sin_addr)); |
|
135 |
logger.log_string(LOG_TYPE_CONNECT, log_buffer); |
|
124 |
if (connection_pool.add_client(accept_socket) < 0) { |
|
125 |
printf("\tThere was an error when trying to add a client to the connection pool."); |
|
126 |
continue; |
|
136 | 127 |
} |
137 | 128 |
|
138 |
if (connection_pool.check_clients() < 0) { |
|
139 |
printf("\tThere was an error trying to update the clients."); |
|
140 |
continue; |
|
141 |
} |
|
129 |
snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.", |
|
130 |
inet_ntoa(client_addr.sin_addr)); |
|
131 |
logger.log_string(LOG_TYPE_CONNECT, log_buffer); |
|
142 | 132 |
} |
133 |
|
|
134 |
if (connection_pool.check_clients() < 0) { |
|
135 |
printf("\tThere was an error trying to update the clients."); |
|
136 |
continue; |
|
137 |
} |
|
143 | 138 |
} |
144 | 139 |
|
145 | 140 |
return 0; |
Also available in: Unified diff