X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fttt-server.c;h=316a27021ec6919a7985e80df95fafde2c3b5fc3;hp=2cac1177209caf16d7cafb6b2394e384490352e0;hb=5d56d3cd8b4685e905ed604277ac9cb32c876127;hpb=152ea9285ef3804783540d46263a7fc9802bc7db diff --git a/src/ttt-server.c b/src/ttt-server.c index 2cac117..316a270 100644 --- a/src/ttt-server.c +++ b/src/ttt-server.c @@ -1,4 +1,4 @@ -/* ttt.c - client-server tic-tac-toe game +/* ttt-server.c - tic-tac-toe game server * * Copyright © 2005 Carl Worth * @@ -20,28 +20,56 @@ */ #include "ttt.h" +#include "ttt-client.h" #include "ttt-socket.h" +struct _ttt_server { + pthread_mutex_t mutex; + + ttt_client_t **clients; + int num_clients; +}; + static void -ttt_dispatch (int connected_socket) +ttt_server_init (ttt_server_t *server) { -#define BUF_SIZE 1024 - - 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); - } + pthread_mutex_init (&server->mutex, NULL); + + server->clients = NULL; + server->num_clients = 0; +} + +static void +ttt_server_add_client (ttt_server_t *server, ttt_client_t *client) +{ + pthread_mutex_lock (&server->mutex); + + server->num_clients++; + server->clients = xrealloc (server->clients, + server->num_clients * sizeof (ttt_client_t *)); + + server->clients [server->num_clients - 1] = client; + + pthread_mutex_unlock (&server->mutex); +} + +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); + + ttt_server_add_client (server, client); + + 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 TCP/IP server that handles one client at a time. The server is\n" -"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" @@ -52,29 +80,31 @@ static const char *WELCOME_MESSAGE = ", then \"close\" (and ) at the \"telnet> \" prompt.\n" "\nHave fun!\n" "-Carl\n" -"\nPS. The server handles only one client at a time, but multiple clients\n" -"may be able to connect simultaneously. Subsequent clients will see no\n" -"output from the server until all previous clients exit.\n" -"\nExtending the server to fork a new process for each client would be a fun\n" -"project for a motivated 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; + ttt_server_t server; int socket; ttt_args_parse (&args, argc, argv); if (args.log_file) - stderr = xfreopen (args.log_file, "a", stderr); + xfreopen (args.log_file, "a", stderr); socket = ttt_socket_create_server (args.host, args.port); printf (WELCOME_MESSAGE, args.host, args.port, args.host, args.port); + ttt_server_init (&server); + while (1) - ttt_socket_accept (socket, ttt_dispatch); + ttt_socket_accept (socket, _accept_client, &server); /* We only reach here if something bad happened. */ return 1;