Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / ColonetServer / includes / ConnectionPool.h @ 14

History | View | Annotate | Download (1.85 KB)

1
/**
2
 * @author Jason Knichel
3
 * @date 7/22/07
4
 */
5

    
6
#ifndef CONNECTION_POOL_H
7
#define CONNECTION_POOL_H
8

    
9
#include <sys/select.h>
10

    
11
#define MAX_TOKENS 15
12
#define MAX_TOKEN_SIZE 30
13

    
14
#define ROBOT_COMMAND_OFFSET 1
15
#define ROBOT_COMMAND_LEN    3
16

    
17
#define ERROR_INVALID_CLIENT_DESCRIPTOR -1
18
#define ERROR_TOO_MANY_CLIENTS          -2
19
#define ERROR_ALLOCATING_MEMORY         -3
20

    
21
#define MAX_CONNECTIONS 250
22
#define READ_BUFFER_SIZE 1024
23
#define WRITE_BUFFER_SIZE 1024
24

    
25
class ConnectionPool {
26

    
27
public:
28
  ConnectionPool();
29
  ~ConnectionPool();
30

    
31
  int add_client(int client_file_descriptor);
32
  int remove_client(int client_file_descriptor);
33
  int check_clients();
34
  int write_to_client(int pool_index, char * message, int length);
35
  void set_listen_socket_in_ready_set(int listen_socket);
36
  int perform_select(int listen_socket, struct timeval * select_timeout);
37
  int is_socket_ready_to_read(int socket);
38
  int is_socket_ready_to_write(int socket);
39

    
40

    
41
  int get_max_file_descriptor();
42
  void set_max_file_descriptor(int new_max_file_descriptor);
43

    
44
  int get_next_available_slot();
45

    
46
  int get_number_clients_ready();
47
  void set_number_clients_ready(int new_number_clients_ready);
48

    
49
  fd_set get_ready_set();
50
  
51
  fd_set get_read_set();
52
  void set_read_set(fd_set new_set);
53

    
54
  fd_set get_write_set();
55
  void set_write_set(fd_set new_set);
56

    
57
private:
58
  int max_file_descriptor;
59
  int next_available_slot;
60
  int number_clients_ready;
61
  fd_set ready_set;
62
  fd_set read_set;
63
  fd_set write_set;
64
  int client_file_descriptor_array[MAX_CONNECTIONS];
65
  char * read_buffer[MAX_CONNECTIONS];
66
  int read_buffer_size[MAX_CONNECTIONS];
67
  char * write_buffer[MAX_CONNECTIONS];
68
  int write_buffer_size[MAX_CONNECTIONS];
69

    
70
  int parse_command(char* command, int pool_index);
71
  int tokenize_command(char* command, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]);
72
  int check_tokens(unsigned char* tokens, int numTokens);
73
};
74

    
75
#endif