]> git.cworth.org Git - ttt/blobdiff - src/ttt-client.c
2005-11-14 Carl Worth <cworth@cworth.org>
[ttt] / src / ttt-client.c
index d6b6fe7b4e474d9c7ce4f7962bdbe1fdaab876f4..e6c97a1c6ad9c2f16ba1f07d9bbc0336e68daa93 100644 (file)
@@ -1,4 +1,4 @@
-/* ttt.c - client-server tic-tac-toe game
+/* ttt-client.c - client handling code for tic-tac-toe game server
  *
  * Copyright © 2005 Carl Worth
  *
  * Author: Carl Worth <cworth@cworth.org>
  */
 
-#include "ttt.h"
+#include "ttt-client.h"
+#include "ttt-socket.h"
 
-int 
-main (int argc, char **argv)
+ttt_client_t *
+ttt_client_create (ttt_server_t *server, int socket, int id)
 {
-    ttt_args_t args;
+    ttt_client_t *client;
 
-    ttt_args_parse (&args, argc, argv);
+    client = xmalloc (sizeof (ttt_client_t));
 
-    /* XXX: insert code here */
+    client->server = server;
+    client->socket = socket;
 
-    return 0;
+    client->id = id;
+
+    client->buf_head = client->buf;
+    client->buf_tail = client->buf;
+
+    client->request = NULL;
+    client->request_size = 0;
+    client->request_len = 0;
+
+    return client;
+}
+
+void
+ttt_client_destroy (ttt_client_t *client)
+{
+    shutdown (client->socket, SHUT_RDWR);
+
+    free (client->request);
+
+    free (client);
+}
+
+static void
+_append_to_request (ttt_client_t       *client,
+                   const char          *buf,
+                   int                  size)
+{
+    int size_needed = client->request_len + size;
+
+    if (size_needed > client->request_size) {
+       if (client->request_size == 0) {
+           client->request_size = size_needed;
+       } else {
+           while (size_needed > client->request_size)
+               client->request_size *= 2;
+       }
+
+       client->request = xrealloc (client->request, client->request_size);
+    }
+
+    memcpy (client->request + client->request_len,
+           buf, size);
+
+    client->request_len += size;
 }
+
+static ttt_status_t
+ttt_client_read_into_request_until (ttt_client_t *client, char delimeter)
+{
+    ttt_bool_t found_delimeter = FALSE;
+    int bytes_read;
+    char *s;
+
+    client->request_len = 0;
+
+    while (1) {
+
+       if (client->buf_tail >= client->buf_head) {
+           bytes_read = xread (client->socket,
+                               client->buf,
+                               TTT_CLIENT_BUF_SIZE);
+           if (bytes_read == 0)
+               return TTT_STATUS_EOF;
+           client->buf_head = client->buf;
+           client->buf_tail = client->buf_head + bytes_read;
+       }
+
+       for (s = client->buf_head; s < client->buf_tail; s++) {
+           if (*s == delimeter) {
+               found_delimeter = TRUE;
+               s++;
+               break;
+           }
+       }
+
+       _append_to_request (client,
+                           client->buf_head,
+                           s - client->buf_head);
+       client->buf_head = s;
+
+       if (found_delimeter)
+           return TTT_STATUS_SUCCESS;
+    }
+}
+
+char *
+ttt_client_read_line (ttt_client_t *client)
+{
+    ttt_status_t status;
+    static const char null_terminator = '\0';
+
+    status = ttt_client_read_into_request_until (client, '\n');
+    if (status) {
+       assert (status == TTT_STATUS_EOF);
+       return NULL;
+    }
+
+    _append_to_request (client, &null_terminator, 1);
+
+    return client->request;
+}
+
+void
+ttt_client_send (ttt_client_t *client, const char *message)
+{
+    ttt_socket_write (client->socket, message, strlen (message));
+}
+
+