1 /* ttt-client.c - client handling code for tic-tac-toe game server
3 * Copyright © 2005 Carl Worth
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Author: Carl Worth <cworth@cworth.org>
22 #include "ttt-client.h"
24 #include "ttt-server.h"
25 #include "ttt-socket.h"
26 #include "ttt-error.h"
29 pthread_mutex_t mutex;
37 char buf[TTT_CLIENT_BUF_SIZE];
47 _ttt_client_init (ttt_client_t *client,
51 pthread_mutex_init (&client->mutex, NULL);
53 client->server = server;
54 client->socket = socket;
56 client->buf_head = client->buf;
57 client->buf_tail = client->buf;
59 client->request = NULL;
60 client->request_size = 0;
61 client->request_len = 0;
63 /* XXX: Probably want to register only as the result of the HELO
64 command. Not only will that match the protocol correctly, but
65 it will also eliminate a race condition here. */
66 client->id = ttt_server_register_client (server, client);
70 _ttt_client_fini (ttt_client_t *client)
72 pthread_mutex_lock (&client->mutex);
74 ttt_server_unregister_client (client->server, client);
76 shutdown (client->socket, SHUT_RDWR);
78 free (client->request);
80 pthread_mutex_unlock (&client->mutex);
82 pthread_mutex_destroy (&client->mutex);
86 _append_to_request (ttt_client_t *client,
90 int size_needed = client->request_len + size;
92 if (size_needed > client->request_size) {
93 if (client->request_size == 0) {
94 client->request_size = size_needed;
96 while (size_needed > client->request_size)
97 client->request_size *= 2;
100 client->request = xrealloc (client->request, client->request_size);
103 memcpy (client->request + client->request_len,
106 client->request_len += size;
110 _read_into_request_until (ttt_client_t *client, char delimeter)
112 ttt_bool_t found_delimeter = FALSE;
116 client->request_len = 0;
120 if (client->buf_tail >= client->buf_head) {
121 bytes_read = xread (client->socket,
123 TTT_CLIENT_BUF_SIZE);
125 return TTT_STATUS_EOF;
126 client->buf_head = client->buf;
127 client->buf_tail = client->buf_head + bytes_read;
130 for (s = client->buf_head; s < client->buf_tail; s++) {
131 if (*s == delimeter) {
132 found_delimeter = TRUE;
138 _append_to_request (client,
140 s - client->buf_head);
141 client->buf_head = s;
144 return TTT_STATUS_SUCCESS;
149 _read_request (ttt_client_t *client)
152 static const char null_terminator = '\0';
154 status = _read_into_request_until (client, '\n');
158 _append_to_request (client, &null_terminator, 1);
160 return TTT_STATUS_SUCCESS;
164 _execute_request (ttt_client_t *client)
166 ttt_server_broadcast (client->server, client->request);
168 return TTT_ERROR_NONE;
172 _handle_requests_thread (void *closure)
176 ttt_client_t *client = closure;
180 status = _read_request (client);
181 if (status == TTT_STATUS_EOF)
186 error = _execute_request (client);
188 ttt_client_send (client, ttt_error_string (error));
191 _ttt_client_fini (client);
197 /* Exported: See ttt-client.h for documentation. */
199 ttt_client_new (void *closure, int client_socket)
201 ttt_server_t *server = closure;
202 ttt_client_t *client;
205 client = xmalloc (sizeof (ttt_client_t));
207 _ttt_client_init (client, server, client_socket);
209 err = pthread_create (&client->thread, NULL,
210 _handle_requests_thread, client);
212 fprintf (stderr, "Error: pthread_create failed: %s. Aborting.\n",
218 /* Exported: See ttt-client.h for documentation. */
220 ttt_client_send (ttt_client_t *client, const char *message)
222 pthread_mutex_lock (&client->mutex);
224 ttt_socket_write (client->socket, message, strlen (message));
226 pthread_mutex_unlock (&client->mutex);
229 /* Exported: See ttt-client.h for documentation. */
231 ttt_client_get_id (ttt_client_t *client)