]> git.cworth.org Git - ttt/blobdiff - src/ttt-server.c
2005-11-11 Carl Worth <cworth@cworth.org>
[ttt] / src / ttt-server.c
index d10d442e621f4b024c9e5cc31f6e8ed4f10d77ce..316a27021ec6919a7985e80df95fafde2c3b5fc3 100644 (file)
@@ -1,4 +1,4 @@
-/* ttt.c - client-server tic-tac-toe game
+/* ttt-server.c - tic-tac-toe game server
  *
  * Copyright © 2005 Carl Worth
  *
  */
 
 #include "ttt.h"
+#include "ttt-client.h"
+#include "ttt-socket.h"
 
-static ttt_status_t
-_sockaddr_init (struct sockaddr_in     *addr,
-               const char              *host,
-               const char              *port)
-{
-    struct hostent *hostent;
-    struct servent *servent;
-    char *endptr;
-    long port_long;
-    uint16_t port_short;
-
-    addr->sin_family = AF_INET;
-
-    hostent = gethostbyname (host);
-    if (hostent == NULL)
-    {
-       fprintf (stderr, "Error: Lookup failed for host %s: %s\n",
-                host, hstrerror (h_errno));
-       return TTT_STATUS_FAILURE;
-    }
-    memcpy (&addr->sin_addr.s_addr, hostent->h_addr_list[0], 
-           sizeof (addr->sin_addr.s_addr));
-
-    assert (*port != '\0');
-    port_long = strtol (port, &endptr, 10);
-    if (*endptr == '\0')
-    {
-       /* Numeric port*/
-       if (port_long <= 0 || port_long >= (1 << 16)) {
-           fprintf (stderr, "Error: Port %ld out of range.\n", port_long);
-           return TTT_STATUS_FAILURE;
-       }
-       port_short = port_long;
-       addr->sin_port = htons (port_short);
-    } else {
-       /* Named port */
-       servent = getservbyname (port, "tcp");
-       if (port == NULL) {
-           fprintf (stderr, "Error: Lookup failed for port %s: %s\n",
-                    port, hstrerror (h_errno));
-           return TTT_STATUS_FAILURE;
-       }
-       addr->sin_port = servent->s_port;
-    }
-
-    return TTT_STATUS_SUCCESS;
-}
+struct _ttt_server {
+    pthread_mutex_t mutex;
+
+    ttt_client_t **clients;
+    int num_clients;
+};
 
-static int
-_wait_for_connection (int listen_socket)
+static void
+ttt_server_init (ttt_server_t *server)
 {
-    fd_set read_set;
-    int num_selected;
-    int flags, err;
-    int connected_socket;
+    pthread_mutex_init (&server->mutex, NULL);
 
-    FD_ZERO (&read_set);
-    FD_SET (listen_socket, &read_set);
+    server->clients = NULL;
+    server->num_clients = 0;
+}
 
-    while (1) {
-       num_selected = select (listen_socket + 1,
-                              &read_set, NULL, NULL,
-                              NULL);
+static void
+ttt_server_add_client (ttt_server_t *server, ttt_client_t *client)
+{
+    pthread_mutex_lock (&server->mutex);
 
-       flags = fcntl (listen_socket, F_GETFL);
-       flags |= O_NONBLOCK;
-       xfcntl (listen_socket, F_SETFL, flags);
+    server->num_clients++;
+    server->clients = xrealloc (server->clients,
+                               server->num_clients * sizeof (ttt_client_t *));
 
-       connected_socket = accept (listen_socket, 0, 0);
-       err = errno;
+    server->clients [server->num_clients - 1] = client;
 
-       flags &= ~O_NONBLOCK;
-       xfcntl (listen_socket, F_SETFL, flags);
+    pthread_mutex_unlock (&server->mutex);
+}
 
-       if (connected_socket != -1)
-           return connected_socket;
+static void
+_accept_client (void *closure, int client_socket)
+{
+    ttt_server_t *server = closure;
+    ttt_client_t *client;
+    
+    client = ttt_client_create (server, client_socket);
 
-       if (err == EWOULDBLOCK || err == EAGAIN)
-           continue;
+    ttt_server_add_client (server, client);
 
-       fprintf (stderr, "Error: accept failed: %s. Aborting.\n",
-                strerror (err));
-       exit (1);
-    }
+    ttt_client_handle_requests (client);
 }
 
 static const char *WELCOME_MESSAGE = 
 "Welcome to ttt-server. So far, this program is simply a demonstration\n"
-"of a one-shot TCP/IP server. The server is currently listening on:\n"
+"of a TCP/IP server capable of handling multiple simultaneous clients.\n"
+"The server is currently listening on:\n"
 "\n    %s:%s\n"
 "\nTo test this program, simply connect a client to that host and port.\n"
 "For example:\n"
@@ -120,56 +80,32 @@ static const char *WELCOME_MESSAGE =
 "<Enter>, then \"close\" (and <Enter>) at the \"telnet> \" prompt.\n"
 "\nHave fun!\n"
 "-Carl\n"
-"\nPS. By one-shot, I mean that the server will only accept a connection\n"
-"from a single client, and the server will exit when that client disconnects\n"
-"To test the server again, you will need to manually restart it again.\n"
-"Extending the server to start listening again, or to handle multiple\n"
-"clients simultaneously would both be fun projects for a motivated\n"
-"student (as would writing a custom client program).\n\n";
+"\nPS. The server does support multiple clients, but there is not yet any\n"
+"interaction between the clients. The next step is probably to turn the\n"
+"server into a simple chat server. After that, we should have the necessary\n"
+"structure in place to start implementing the real tic-tac-toe protocol.\n\n";
 
 int 
 main (int argc, char **argv)
 {
     ttt_args_t args;
-    int listen_socket, connected_socket;
-    struct sockaddr_in addr;
+    ttt_server_t server;
+    int socket;
 
     ttt_args_parse (&args, argc, argv);
 
     if (args.log_file)
-       stderr = xfreopen (args.log_file, "a", stderr);
-
-    listen_socket = xsocket (PF_INET, SOCK_STREAM, 0);
-
-    _sockaddr_init (&addr, args.host, args.port);
+       xfreopen (args.log_file, "a", stderr);
 
-#ifdef SO_REUSEADDR
-    {
-       int one = 1;
-       setsockopt (listen_socket, SOL_SOCKET,
-                   SO_REUSEADDR, (char *) &one, sizeof (int));
-    }
-#endif
-
-    xbind (listen_socket, (struct sockaddr *) &addr, sizeof (addr));
-
-    xlisten (listen_socket, SOMAXCONN);
+    socket = ttt_socket_create_server (args.host, args.port);
 
     printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port);
 
-    connected_socket = _wait_for_connection (listen_socket);
-
-#define BUF_SIZE 1024
+    ttt_server_init (&server);
 
-    while (1) {
-       char buf[BUF_SIZE];
-       int cnt;
-       cnt = read (connected_socket, buf, BUF_SIZE);
-       if (cnt == 0)
-           break;
-       write (0, buf, cnt);
-       write (connected_socket, buf, cnt);
-    }
+    while (1)
+       ttt_socket_accept (socket, _accept_client, &server);
 
-    return 0;
+    /* We only reach here if something bad happened. */
+    return 1;
 }