Revision 27
moved around some braces and whitespace and renamed variables to make the code comply with style guidelines a bit more
ConnectionPool.cpp | ||
---|---|---|
71 | 71 |
return 0; |
72 | 72 |
} |
73 | 73 |
|
74 |
int ConnectionPool::remove_client(int client_file_descriptor) {
|
|
75 |
if (client_file_descriptor < 0 || client_file_descriptor >= next_available_slot) {
|
|
74 |
int ConnectionPool::remove_client(int pool_index) {
|
|
75 |
if (pool_index < 0 || pool_index >= next_available_slot) {
|
|
76 | 76 |
return ERROR_INVALID_CLIENT_DESCRIPTOR; |
77 | 77 |
} |
78 | 78 |
|
79 |
int clientfd = client_file_descriptor_array[client_file_descriptor];
|
|
79 |
int client_file_descriptor = client_file_descriptor_array[pool_index];
|
|
80 | 80 |
|
81 |
if (FD_ISSET(clientfd, &ready_set)) {
|
|
82 |
FD_CLR(clientfd, &ready_set);
|
|
81 |
if (FD_ISSET(client_file_descriptor, &ready_set)) {
|
|
82 |
FD_CLR(client_file_descriptor, &ready_set);
|
|
83 | 83 |
} |
84 |
if (FD_ISSET(clientfd, &read_set)) {
|
|
85 |
FD_CLR(clientfd, &read_set);
|
|
84 |
if (FD_ISSET(client_file_descriptor, &read_set)) {
|
|
85 |
FD_CLR(client_file_descriptor, &read_set);
|
|
86 | 86 |
} |
87 |
if (FD_ISSET(clientfd, &write_set)) {
|
|
88 |
FD_CLR(clientfd, &write_set);
|
|
87 |
if (FD_ISSET(client_file_descriptor, &write_set)) {
|
|
88 |
FD_CLR(client_file_descriptor, &write_set);
|
|
89 | 89 |
} |
90 | 90 |
|
91 |
free(read_buffer[client_file_descriptor]);
|
|
92 |
free(write_buffer[client_file_descriptor]);
|
|
93 |
for (int j = client_file_descriptor; j < next_available_slot - 1; j++) {
|
|
94 |
client_file_descriptor_array[client_file_descriptor] = client_file_descriptor_array[client_file_descriptor+1];
|
|
95 |
read_buffer[client_file_descriptor] = read_buffer[client_file_descriptor+1];
|
|
96 |
read_buffer_size[client_file_descriptor] = read_buffer_size[client_file_descriptor+1];
|
|
97 |
write_buffer[client_file_descriptor] = write_buffer[client_file_descriptor+1];
|
|
98 |
write_buffer_size[client_file_descriptor] = write_buffer_size[client_file_descriptor+1];
|
|
91 |
free(read_buffer[pool_index]);
|
|
92 |
free(write_buffer[pool_index]);
|
|
93 |
for (int j = pool_index; j < next_available_slot - 1; j++) {
|
|
94 |
client_file_descriptor_array[pool_index] = client_file_descriptor_array[pool_index+1];
|
|
95 |
read_buffer[pool_index] = read_buffer[pool_index+1];
|
|
96 |
read_buffer_size[pool_index] = read_buffer_size[pool_index+1];
|
|
97 |
write_buffer[pool_index] = write_buffer[pool_index+1];
|
|
98 |
write_buffer_size[pool_index] = write_buffer_size[pool_index+1];
|
|
99 | 99 |
} |
100 | 100 |
next_available_slot--; |
101 | 101 |
int temp_max_file_descriptor = 0; |
... | ... | |
113 | 113 |
|
114 | 114 |
//TODO: test that it drops commands properly if it gets sent too much data |
115 | 115 |
// do we want it to drop the data or drop the connection? |
116 |
//TODO: give variables a better name |
|
117 | 116 |
int ConnectionPool::check_clients(ColonetWireless * wireless) { |
118 |
char temp[READ_BUFFER_SIZE]; |
|
119 |
char tempCommand[READ_BUFFER_SIZE+1];
|
|
117 |
char temporary_buffer[READ_BUFFER_SIZE];
|
|
118 |
char temporary_command_buffer[READ_BUFFER_SIZE+1];
|
|
120 | 119 |
int i; |
121 | 120 |
int client_file_descriptor; |
122 |
int n; |
|
123 |
int len; |
|
121 |
int num_bytes_read;
|
|
122 |
int length;
|
|
124 | 123 |
int sent; |
125 |
int commandLen;
|
|
124 |
int command_length;
|
|
126 | 125 |
|
127 | 126 |
for (i = 0; i < next_available_slot; i++) { |
128 | 127 |
client_file_descriptor = client_file_descriptor_array[i]; |
129 | 128 |
|
130 | 129 |
if (FD_ISSET(client_file_descriptor, &read_set)) { |
131 |
n = read(client_file_descriptor, temp, READ_BUFFER_SIZE);
|
|
130 |
num_bytes_read = read(client_file_descriptor, temporary_buffer, READ_BUFFER_SIZE);
|
|
132 | 131 |
|
133 |
if (n == 0 || (n == -1 && errno == ECONNRESET)) {
|
|
132 |
if (num_bytes_read == 0 || (num_bytes_read == -1 && errno == ECONNRESET)) {
|
|
134 | 133 |
remove_client(i); |
135 | 134 |
i--; |
136 | 135 |
continue; |
137 | 136 |
} |
138 | 137 |
|
139 |
while (n > 0) { |
|
140 |
len = n;
|
|
138 |
while (num_bytes_read > 0) {
|
|
139 |
length = num_bytes_read;
|
|
141 | 140 |
|
142 |
if (len + read_buffer_size[i] > READ_BUFFER_SIZE) { |
|
143 |
len = READ_BUFFER_SIZE - read_buffer_size[i]; |
|
141 |
if (length + read_buffer_size[i] > READ_BUFFER_SIZE) {
|
|
142 |
length = READ_BUFFER_SIZE - read_buffer_size[i];
|
|
144 | 143 |
} |
145 | 144 |
|
146 |
memcpy(read_buffer[i]+read_buffer_size[i], temp, len);
|
|
147 |
read_buffer_size[i] += len; |
|
148 |
n -= len;
|
|
145 |
memcpy(read_buffer[i]+read_buffer_size[i], temporary_buffer, length);
|
|
146 |
read_buffer_size[i] += length;
|
|
147 |
num_bytes_read -= length;
|
|
149 | 148 |
|
150 |
if (n > 0) { |
|
151 |
memmove(temp, temp+len, READ_BUFFER_SIZE - len);
|
|
149 |
if (num_bytes_read > 0) {
|
|
150 |
memmove(temporary_buffer, temporary_buffer+length, READ_BUFFER_SIZE - length);
|
|
152 | 151 |
} |
153 | 152 |
|
154 | 153 |
printf("Read buffer is %s\n", read_buffer[i]); |
155 | 154 |
|
156 |
char* newLine;
|
|
155 |
char* newline_position;
|
|
157 | 156 |
|
158 |
while ((newLine = strstr(read_buffer[i], "\n"))) {
|
|
157 |
while ((newline_position = strstr(read_buffer[i], "\n"))) {
|
|
159 | 158 |
|
160 | 159 |
//if no newline if found in the entire readbuffer (when its full), |
161 | 160 |
//toss out the command |
... | ... | |
163 | 162 |
// to do something bad to the server |
164 | 163 |
//TODO: this is from before all this code was put in the loop. reconsider |
165 | 164 |
// how to check this error condition and do it elsewhere |
166 |
if (!newLine && (read_buffer_size[i] == READ_BUFFER_SIZE)) {
|
|
165 |
if (!newline_position && (read_buffer_size[i] == READ_BUFFER_SIZE)) {
|
|
167 | 166 |
read_buffer_size[i] = 0; |
168 | 167 |
break; |
169 | 168 |
} |
170 | 169 |
|
171 | 170 |
//if no newline is found then there is not a command in the buffer |
172 |
if (!newLine) {
|
|
171 |
if (!newline_position) {
|
|
173 | 172 |
break; |
174 | 173 |
} |
175 | 174 |
|
176 |
commandLen = (newLine - read_buffer[i])+1;
|
|
175 |
command_length = (newline_position - read_buffer[i])+1;
|
|
177 | 176 |
|
178 | 177 |
//the newline was found in garbage in the currently not used portion |
179 | 178 |
// of the read buffer |
180 |
if (commandLen > read_buffer_size[i]) {
|
|
179 |
if (command_length > read_buffer_size[i]) {
|
|
181 | 180 |
break; |
182 | 181 |
} |
183 | 182 |
|
184 |
memcpy(tempCommand, read_buffer[i], commandLen);
|
|
185 |
//do commandLen-1 to get rid of the newline terminating the command
|
|
186 |
tempCommand[commandLen-1] = '\0';
|
|
183 |
memcpy(temporary_command_buffer, read_buffer[i], command_length);
|
|
184 |
//do command_length-1 to get rid of the newline terminating the command
|
|
185 |
temporary_command_buffer[command_length-1] = '\0';
|
|
187 | 186 |
//did this because telnet was putting a \r\n on the end instead of just \n |
188 |
if (isspace(tempCommand[commandLen-2])) {
|
|
189 |
tempCommand[commandLen-2] = '\0';
|
|
187 |
if (isspace(temporary_command_buffer[command_length-2])) {
|
|
188 |
temporary_command_buffer[command_length-2] = '\0';
|
|
190 | 189 |
} |
191 | 190 |
|
192 |
memmove(read_buffer[i], read_buffer[i]+commandLen, read_buffer_size[i] - commandLen);
|
|
193 |
read_buffer_size[i] -= commandLen;
|
|
191 |
memmove(read_buffer[i], read_buffer[i]+command_length, read_buffer_size[i] - command_length);
|
|
192 |
read_buffer_size[i] -= command_length;
|
|
194 | 193 |
|
195 |
if (commandLen > MAX_COMMAND_LEN) {
|
|
194 |
if (command_length > MAX_COMMAND_LEN) {
|
|
196 | 195 |
printf("The command was too long. Tossing command out.\n"); |
197 | 196 |
break; |
198 | 197 |
} |
199 | 198 |
|
200 |
if (parse_command(tempCommand, i, wireless) < 0) {
|
|
199 |
if (parse_command(temporary_command_buffer, i, wireless) < 0) {
|
|
201 | 200 |
printf("There was an error parsing command\n"); |
202 | 201 |
break; |
203 | 202 |
} |
Also available in: Unified diff