Project

General

Profile

Revision 27

Added by Jason knichel over 16 years ago

moved around some braces and whitespace and renamed variables to make the code comply with style guidelines a bit more

View differences:

ColonetServer.cpp
41 41

  
42 42
  parseCmdLine(argc, argv);
43 43

  
44
  if (initialize_connection(optionsG.listen_port) < 0)
44
  if (initialize_connection(optionsG.listen_port) < 0) {
45 45
    return -1;
46
  }
46 47

  
47 48
  if (initialize_wireless() < 0) {
48 49
    fprintf(stderr, "%s: initWireless failed\n", __FUNCTION__);
......
55 56
int ColonetServer::start_listening() {
56 57
  if (listen(listen_socket, LISTEN_BACKLOG) < 0) {
57 58
    log_error("\t\nThere was an error telling the socket to "
58
                             "listen for connections from clients.  Terminating Server...\n");
59
              "listen for connections from clients.  Terminating Server...\n");
59 60
    return -1;
60 61
  }
61 62
  return 0;
......
76 77
  if (listen_socket > connection_pool.get_max_file_descriptor()) {
77 78
    connection_pool.set_max_file_descriptor(listen_socket);
78 79

  
79
    int acceptSocket = 0;
80
    struct sockaddr_in clientAddr;
81
    socklen_t clen = sizeof(clientAddr);
82
    struct timeval selectTimeout;
80
    int accept_socket = 0;
81
    struct sockaddr_in client_addr;
82
    socklen_t client_addr_size = sizeof(client_addr);
83
    struct timeval select_timeout;
83 84
    
84
    memset(&selectTimeout,0,sizeof(selectTimeout));
85
    memset(&select_timeout,0,sizeof(select_timeout));
85 86

  
86
    logger.logMessage(LOG_TYPE_MESSAGE, "Server initialized.  About to start listening for connections");
87
    log_message("Server initialized.  About to start listening for connections");
87 88

  
88 89
    while(1) {
89
      connection_pool.perform_select(listen_socket, &selectTimeout);
90
      connection_pool.perform_select(listen_socket, &select_timeout);
90 91

  
91 92
      //either no descriptors are ready or there was an error
92 93
      //TODO: check for specific errors
......
96 97

  
97 98
      if (connection_pool.is_socket_ready_to_read(listen_socket)) {
98 99
        printf("Something is trying to connect...\n");
99
        if ((acceptSocket = accept(listen_socket, (struct sockaddr*) &clientAddr, &clen)) < 0) {
100
        if ((accept_socket = accept(listen_socket, (struct sockaddr*) &client_addr, &client_addr_size)) < 0) {
100 101
          if (errno == EMFILE) {
101 102
            printf("\tWhen attempting to accept a connection, "
102 103
                   "reached the per process limit of file descriptors."
......
109 110
          continue;
110 111
        }
111 112

  
112
        char logBuffer[LOG_BUFFER_LENGTH];
113
        snprintf(logBuffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.", inet_ntoa(clientAddr.sin_addr));
114
        logger.logMessage(LOG_TYPE_CONNECT, logBuffer);
113
        char log_buffer[LOG_BUFFER_LENGTH];
114
        snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s attempting to connect.", 
115
                 inet_ntoa(client_addr.sin_addr));
116
        logger.logMessage(LOG_TYPE_CONNECT, log_buffer);
115 117

  
116
        //TODO: remove this
117
        //printf("Attempting to add a client.\n");
118

  
119

  
120
        if (connection_pool.add_client(acceptSocket) < 0) {
121
          printf("\tThere was an error when trying to add a client to the "
122
                 "connection pool.");
118
        if (connection_pool.add_client(accept_socket) < 0) {
119
          printf("\tThere was an error when trying to add a client to the connection pool.");
123 120
          continue;
124 121
        }
125 122

  
126
        snprintf(logBuffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.", inet_ntoa(clientAddr.sin_addr));
127
        logger.logMessage(LOG_TYPE_CONNECT, logBuffer);
128
        
123
        snprintf(log_buffer, LOG_BUFFER_LENGTH, "Client at address %s successfully added to connection pool.", 
124
                 inet_ntoa(client_addr.sin_addr));
125
        logger.logMessage(LOG_TYPE_CONNECT, log_buffer);
129 126
      }
130 127

  
131 128
      if (connection_pool.check_clients(wireless) < 0) {
......
142 139
  return &connection_pool;
143 140
}
144 141

  
145
int ColonetServer::initialize_wireless()
146
{
142
int ColonetServer::initialize_wireless() {
147 143
  char* log_filename = NULL;
148 144

  
149 145
  if (optionsG.logging_enabled) {
......
163 159
	return 0;
164 160
}
165 161

  
166
int ColonetServer::initialize_connection(int port)
167
{
162
int ColonetServer::initialize_connection(int port) {
168 163
  printf("Initializing connection that will be used to listen for " 
169 164
         "clients...\n");
170
  int opts = 1;
171
  struct sockaddr_in my_addr;
165
  int options = 1;
166
  struct sockaddr_in my_address;
172 167

  
173 168
  //get a socket fd
174 169
  if ((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
......
177 172
  }
178 173

  
179 174
  //set up the address struct
180
  memset(&my_addr,'\0',sizeof(my_addr));
181
  my_addr.sin_family = AF_INET;
182
  my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
183
  my_addr.sin_port = htons(port);
175
  memset(&my_address,'\0',sizeof(my_address));
176
  my_address.sin_family = AF_INET;
177
  my_address.sin_addr.s_addr = htonl(INADDR_ANY);
178
  my_address.sin_port = htons(port);
184 179
  
185
  setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts));
180
  setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(options));
186 181

  
187 182
  //get the current socket options
188
  if ((opts = fcntl(listen_socket, F_GETFL)) < 0) {
183
  if ((options = fcntl(listen_socket, F_GETFL)) < 0) {
189 184
    printf("\tThere was an error getting the socket options.\n");
190 185
    return -1;
191 186
  }
192 187

  
193 188
  //set the socket to non blocking
194
  opts = (opts | O_NONBLOCK);
195
  if (fcntl(listen_socket, F_SETFL, opts) < 0) {
189
  options = (options | O_NONBLOCK);
190
  if (fcntl(listen_socket, F_SETFL, options) < 0) {
196 191
    printf("\tThere was an error setting the socket to be non blocking.\n");
197 192
    return -1;
198 193
  }
199 194
  
200 195
  //bind the socket to listen on the specified port
201
  if (bind(listen_socket, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) {
196
  if (bind(listen_socket, (struct sockaddr *) &my_address, sizeof(my_address)) < 0) {
202 197
    printf("\tThere was an error binding the socket\n");
203 198
    return -1;
204 199
  }

Also available in: Unified diff