]> git.cworth.org Git - ttt/blobdiff - src/ttt-socket.c
2005-11-29 Carl Worth <cworth@cworth.org>
[ttt] / src / ttt-socket.c
index f185d363780af7e5c43cb334f9e7ae42c52a258d..20b96e263056103b87752fe6da432b5c8a89b63b 100644 (file)
@@ -150,16 +150,67 @@ ttt_socket_accept (int                             listen_socket,
                   ttt_socket_accept_func_t      accept,
                   void                         *closure)
 {
-    pid_t pid;
     int connected_socket;
 
     connected_socket = _wait_for_connection (listen_socket);
 
-    pid = xfork();
+    (accept) (closure, connected_socket);
+}
+
+/* Exported: see wdo-socket.h for documentation. */
+ttt_status_t
+ttt_socket_create_client (const char   *host,
+                         const char    *port,
+                         int           *socket_ret)
+{
+    ttt_status_t status;
+    struct sockaddr_in addr;
+    int socket;
+
+    *socket_ret = -1;
+
+    socket = xsocket (PF_INET, SOCK_STREAM, 0);
+
+    _sockaddr_init (&addr, host, port);
+
+    status = xconnect (socket, (struct sockaddr *) &addr, sizeof (addr));
+    if (status) {
+       shutdown (socket, SHUT_RDWR);
+       return status;
+    }
+
+    *socket_ret = socket;
+    return TTT_STATUS_SUCCESS;
+}
+
+/* Exported: see ttt-socket.h for documentation. */
+void
+ttt_socket_read (int    socket,
+                void   *buf,
+                size_t  count)
+{
+    int bytes_read;
+    char *cbuf = buf;
+
+    while (count) {
+       bytes_read = xread (socket, cbuf, count);
+       cbuf += bytes_read;
+       count -= bytes_read;
+    }
+}
+
+/* Exported: see ttt-socket.h for documentation. */
+void
+ttt_socket_write (int           socket,
+                 const void    *buf,
+                 size_t         count)
+{
+    int written;
+    const char *cbuf = buf;
 
-    if (pid == 0) {
-       /* Child process. */
-       (accept) (closure, connected_socket);
-       return;
+    while (count) {
+       written = xwrite (socket, cbuf, count);
+       cbuf += written;
+       count -= written;
     }
 }