]> git.cworth.org Git - ttt/blobdiff - src/ttt-client.c
2005-11-14 Carl Worth <cworth@cworth.org>
[ttt] / src / ttt-client.c
index 80fca846e13ee46e4fae37b87ee966901a91de9a..e6c97a1c6ad9c2f16ba1f07d9bbc0336e68daa93 100644 (file)
  */
 
 #include "ttt-client.h"
-
-struct _ttt_client {
-    ttt_server_t *server;
-    int socket;
-};
+#include "ttt-socket.h"
 
 ttt_client_t *
-ttt_client_create (ttt_server_t *server, int socket)
+ttt_client_create (ttt_server_t *server, int socket, int id)
 {
     ttt_client_t *client;
 
@@ -36,31 +32,112 @@ ttt_client_create (ttt_server_t *server, int socket)
     client->server = server;
     client->socket = socket;
 
+    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)
 {
-    close (client->socket);
+    shutdown (client->socket, SHUT_RDWR);
+
+    free (client->request);
 
     free (client);
 }
 
-void
-ttt_client_handle_requests (ttt_client_t *client)
+static void
+_append_to_request (ttt_client_t       *client,
+                   const char          *buf,
+                   int                  size)
 {
-#define BUF_SIZE 1024
+    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) {
-       char buf[BUF_SIZE];
-       int cnt;
-       cnt = read (client->socket, buf, BUF_SIZE);
-       if (cnt == 0)
-           break;
-       write (0, buf, cnt);
-       write (client->socket, buf, cnt);
+
+       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));
+}
+