Project

General

Profile

Revision 145

Added by Jason knichel over 16 years ago

moved some code into their own private methods

View differences:

ConnectionPool.cpp
4 4
 * @author Jason Knichel
5 5
 * @date 7/22/07
6 6
 *
7
 * @todo make it so check clients doesn't call parse command.  
8
 *       instead somehow make it so colonet server can get the commands somehow and parse them itself
9 7
 */
10 8

  
11 9
#include <sys/select.h>
......
148 146
//TODO: test that it drops commands properly if it gets sent too much data
149 147
//      do we want it to drop the data or drop the connection?
150 148
int ConnectionPool::check_clients() {
151
  char temporary_buffer[READ_BUFFER_SIZE];
152
  char temporary_command_buffer[READ_BUFFER_SIZE+1];
153 149
  int i;
154
  int client_file_descriptor;
155
  int num_bytes_read;
156
  int length;
157
  int sent;
158
  int command_length;
159 150

  
160 151
  for (i = 0; i < next_available_slot; i++) {
161
    client_file_descriptor = client_file_descriptor_array[i];
152
    int client_file_descriptor = client_file_descriptor_array[i];
162 153

  
163 154
    if (FD_ISSET(client_file_descriptor, &read_set)) {
164
      num_bytes_read = read(client_file_descriptor, temporary_buffer, READ_BUFFER_SIZE);
165

  
166
      if (num_bytes_read == 0 || (num_bytes_read == -1 && errno == ECONNRESET)) {
167
        remove_client(i);
168
        i--;
169
        continue;
155
      if (read_data(i, client_file_descriptor) == DECREMENT_INDEX_COUNTER) {
156
	i--;
157
	continue;
170 158
      }
171

  
172
      while (num_bytes_read > 0) {
173
        length = num_bytes_read;
174

  
175
        if (length + read_buffer_size[i] > READ_BUFFER_SIZE) {
176
          length = READ_BUFFER_SIZE - read_buffer_size[i];
177
        }
178

  
179
        memcpy(read_buffer[i]+read_buffer_size[i], temporary_buffer, length);
180
        read_buffer_size[i] += length;
181
        num_bytes_read -= length;
182

  
183
        if (num_bytes_read > 0) {
184
          memmove(temporary_buffer, temporary_buffer+length, READ_BUFFER_SIZE - length);
185
        }
186

  
187
        printf("Read buffer is %s\n", read_buffer[i]);
188

  
189
        char* newline_position;
190

  
191
        while ((newline_position = strstr(read_buffer[i], "\n"))) {
192

  
193
          //if no newline if found in the entire readbuffer (when its full), 
194
          //toss out the command
195
          // because either the command being given is too long or someone is trying
196
          // to do something bad to the server
197
          //TODO: this is from before all this code was put in the loop.  reconsider
198
          //      how to check this error condition and do it elsewhere
199
          if (!newline_position && (read_buffer_size[i] == READ_BUFFER_SIZE)) {
200
            read_buffer_size[i] = 0;
201
            break;
202
          }
203

  
204
          //if no newline is found then there is not a command in the buffer
205
          if (!newline_position) {
206
            break;
207
          }
208

  
209
          command_length = (newline_position - read_buffer[i])+1;
210

  
211
          //the newline was found in garbage in the currently not used portion
212
          // of the read buffer
213
          if (command_length > read_buffer_size[i]) {
214
            break;
215
          }
216

  
217
          memcpy(temporary_command_buffer, read_buffer[i], command_length);
218
          //do command_length-1 to get rid of the newline terminating the command
219
          temporary_command_buffer[command_length-1] = '\0';
220
          //did this because telnet was putting a \r\n on the end instead of just \n
221
          if (isspace(temporary_command_buffer[command_length-2])) {
222
            temporary_command_buffer[command_length-2] = '\0';
223
          }
224

  
225
          memmove(read_buffer[i], read_buffer[i]+command_length, read_buffer_size[i] - command_length);
226
          read_buffer_size[i] -= command_length;
227

  
228
          if (command_length > MAX_COMMAND_LEN) {
229
            printf("The command was too long.  Tossing command out.\n");
230
            break;
231
          }
232

  
233
	  Command command(this);
234
          if (command.parse_command(temporary_command_buffer, i) < 0) {
235
            printf("There was an error parsing command\n");
236
            break;
237
          }
238
        }
239
      }
240 159
    }
241 160

  
242 161
    if (FD_ISSET(client_file_descriptor, &write_set)) {
243
      if (write_buffer_size[i] == 0) {
244
        continue;
245
      }
246

  
247
      sent = write(client_file_descriptor, write_buffer[i], write_buffer_size[i]);
248
      memmove(write_buffer[i], write_buffer[i]+sent, WRITE_BUFFER_SIZE - sent);
249
      write_buffer_size[i] -= sent;
162
      write_data(i, client_file_descriptor);
250 163
    }
251 164
  }
252 165

  
......
343 256
}
344 257

  
345 258

  
259
int ConnectionPool::read_data(int pool_index, int client_file_descriptor) {
260
  char temporary_buffer[READ_BUFFER_SIZE];
261
  char temporary_command_buffer[READ_BUFFER_SIZE+1];
262
  int num_bytes_read;
263
  int length;
264
  int command_length;
265

  
266
  num_bytes_read = read(client_file_descriptor, temporary_buffer, READ_BUFFER_SIZE);
267

  
268
  if (num_bytes_read == 0 || (num_bytes_read == -1 && errno == ECONNRESET)) {
269
    remove_client(pool_index);
270
    return DECREMENT_INDEX_COUNTER;
271
  }
272

  
273
  while (num_bytes_read > 0) {
274
    length = num_bytes_read;
275

  
276
    if (length + read_buffer_size[pool_index] > READ_BUFFER_SIZE) {
277
      length = READ_BUFFER_SIZE - read_buffer_size[pool_index];
278
    }
279

  
280
    memcpy(read_buffer[pool_index]+read_buffer_size[pool_index], temporary_buffer, length);
281
    read_buffer_size[pool_index] += length;
282
    num_bytes_read -= length;
283

  
284
    if (num_bytes_read > 0) {
285
      memmove(temporary_buffer, temporary_buffer+length, READ_BUFFER_SIZE - length);
286
    }
287

  
288
    printf("Read buffer is %s\n", read_buffer[pool_index]);
289

  
290
    char* newline_position;
291

  
292
    while ((newline_position = strstr(read_buffer[pool_index], "\n"))) {
293

  
294
      //if no newline if found in the entire readbuffer (when its full), 
295
      //toss out the command
296
      // because either the command being given is too long or someone is trying
297
      // to do something bad to the server
298
      //TODO: this is from before all this code was put in the loop.  reconsider
299
      //      how to check this error condition and do it elsewhere
300
      if (!newline_position && (read_buffer_size[pool_index] == READ_BUFFER_SIZE)) {
301
	read_buffer_size[pool_index] = 0;
302
	break;
303
      }
304

  
305
      //if no newline is found then there is not a command in the buffer
306
      if (!newline_position) {
307
	break;
308
      }
309

  
310
      command_length = (newline_position - read_buffer[pool_index])+1;
311

  
312
      //the newline was found in garbage in the currently not used portion
313
      // of the read buffer
314
      if (command_length > read_buffer_size[pool_index]) {
315
	break;
316
      }
317

  
318
      memcpy(temporary_command_buffer, read_buffer[pool_index], command_length);
319
      //do command_length-1 to get rid of the newline terminating the command
320
      temporary_command_buffer[command_length-1] = '\0';
321
      //did this because telnet was putting a \r\n on the end instead of just \n
322
      if (isspace(temporary_command_buffer[command_length-2])) {
323
	temporary_command_buffer[command_length-2] = '\0';
324
      }
325

  
326
      memmove(read_buffer[pool_index], read_buffer[pool_index]+command_length, read_buffer_size[pool_index] - command_length);
327
      read_buffer_size[pool_index] -= command_length;
328

  
329
      if (command_length > MAX_COMMAND_LEN) {
330
	printf("The command was too long.  Tossing command out.\n");
331
	break;
332
      }
333

  
334
      Command command(this);
335
      if (command.parse_command(temporary_command_buffer, pool_index) < 0) {
336
	printf("There was an error parsing command\n");
337
	break;
338
      }
339
    }
340
  }
341

  
342
  return 0;
343
}
344

  
345

  
346
int ConnectionPool::write_data(int pool_index, int client_file_descriptor) {
347
  if (write_buffer_size[pool_index] == 0) {
348
    return 0;
349
  }
350

  
351
  int sent = write(client_file_descriptor, write_buffer[pool_index], write_buffer_size[pool_index]);
352
  memmove(write_buffer[pool_index], write_buffer[pool_index]+sent, WRITE_BUFFER_SIZE - sent);
353
  write_buffer_size[pool_index] -= sent;  
354

  
355
  return 0;
356
}

Also available in: Unified diff