Exemple #1
0
int main(int argc, char** args) {
    try {
        uint16_t port = 8080;
        if (argc > 1) {
            port = (uint16_t) std::stoi(args[1]);
        }

        epoll_wrap epoll(200);
        resolver<proxy_server::resolver_extra> ip_resolver;
        proxy_server proxy(epoll, ip_resolver, port, 200);

        std::string tag = "server on port " + std::to_string(port);
        signal_fd sig_fd({SIGINT, SIGPIPE}, {signal_fd::SIMPLE});

        epoll_registration signal_registration(epoll, std::move(sig_fd), fd_state::IN);
        signal_registration.update([&signal_registration, &epoll, tag](fd_state state) mutable {
            if (state.is(fd_state::IN)) {
                struct signalfd_siginfo sinf;
                long size = signal_registration.get_fd().read(&sinf, sizeof(struct signalfd_siginfo));
                if (size != sizeof(struct signalfd_siginfo)) {
                    return;
                }
                if (sinf.ssi_signo == SIGINT) {
                    log("\n" + tag, "stopped");
                    epoll.stop_wait();
                }
            }
        });

        log(tag, "started");
        epoll.start_wait();

    } catch (annotated_exception const &e) {
        log(e);
    }
}
void *server_master_sync_listen (void *arg)
{
  struct sockaddr_in slave_addr = { 0 };
  socklen_t slave_sock_len      = 0;
  int listen_fd                 = 0;
  int slave_sock_fd             = 0;
  char read_write_buffer[MAXLINE];

  if (master_sync == NULL) {
    LOG_SERVER_MESSAGE_TO_FILE ("ERROR", "Argument to thread is no good");
    pthread_exit (NULL);
  }

  /* Register for the signals */
  signal_registration();

  /* Get socket descriptor from global structure */
  listen_fd = master_sync->tcp_listen_fd;

  LOG_SERVER_MESSAGE_TO_FILE ("INFO", "Attempting listen on a new thread, on socket %d",
              listen_fd);

  if (master_sync->slave_count >= SLAVE_COUNT_MAX) {
    LOG_SERVER_MESSAGE_TO_FILE ("ERROR", "Cannot accept any more clients. Exiting "
                                "thread. Maybe change the macro?!");
    pthread_exit (NULL);
  }

  /* Listen for new connections */
  if (listen (listen_fd, SERVER_MASTER_LISTEN_Q_LEN) < 0) {
    LOG_SERVER_MESSAGE_TO_FILE ("ERROR", "Listen on TCP master socket failed");
    pthread_exit (NULL);
  }

  while (1) {
    /* Not creating new threads to handle multiple slave servers.
    * Not that many slaves needed */
    slave_sock_len = sizeof(slave_addr);
    if ((slave_sock_fd = accept (listen_fd, (struct sockaddr *)&slave_addr,
                                &slave_sock_len)) < 0) 
    {
        LOG_SERVER_MESSAGE_TO_FILE ("ERROR", "Accept failed");
        continue;
    } 

    if (0 > (read(slave_sock_fd, read_write_buffer, MAXLINE)))
    {
        LOG_SERVER_MESSAGE_TO_FILE ("ERROR", "read error");
        close(slave_sock_fd); 
        continue;
    }
    if (0 != strncmp(read_write_buffer, SECURITY_KEY, strlen(SECURITY_KEY)))
    {
        close(slave_sock_fd); 
        LOG_SERVER_MESSAGE_TO_FILE ("ERROR", "Security breach"); 
    }
    else
    {  
        /* Add to master_sync list of slave connections */
        master_sync->slave_sock_fd_list[master_sync->slave_count] = \
                                                    slave_sock_fd;
        master_sync->slave_count++;
        LOG_SERVER_MESSAGE_TO_FILE ("INFO", "Slave connected. Slave count is %d",
                                    master_sync->slave_count);   
        server_sync_master_query_db();
    }  
  }

  pthread_exit (NULL);
}