3 * $Id: tx-socket.c,v 1.2 2002/01/30 09:25:35 mdw Exp $
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Jog: Programming for a jogging machine.
14 * Jog is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Jog is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Jog; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
41 #include <sys/types.h>
43 #include <sys/socket.h>
47 #include <mLib/alloc.h>
48 #include <mLib/darray.h>
53 #include "tx-socket.h"
57 /*----- Data structures ---------------------------------------------------*/
59 typedef struct txsock {
60 txport tx; /* Transport base */
61 int fd; /* File descriptor */
64 /*----- Main code ---------------------------------------------------------*/
66 /* --- @txsock_create@ --- *
68 * Arguments: @const char *file@ = filename for socket
70 * Returns: Pointer to created transport block.
72 * Use: Creates a socket transport.
75 txport *txsock_create(const char *file)
79 struct sockaddr_un *sun;
82 /* --- Set up the address block --- */
84 len = strlen(file) + 1;
85 sunsz = offsetof(struct sockaddr_un, sun_path) + len;
87 sun->sun_family = AF_UNIX;
88 memcpy(sun->sun_path, file, len);
90 /* --- Create the socket --- */
92 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
93 err_report(ERR_TXPORT, ERRTX_CREATE, errno,
94 "error creating socket: %s", strerror(errno));
100 if (connect(fd, (struct sockaddr *)sun, sunsz)) {
101 err_report(ERR_TXPORT, ERRTX_CREATE, errno,
102 "couldn't connect to `%s': %s", file, strerror(errno));
113 /* --- Tidy up because it all went horribly wrong --- */
122 /* --- @txsock_write@ --- *
124 * Arguments: @txport *txg@ = pointer to transport context
125 * @const void *p@ = pointer to buffer
126 * @size_t sz@ = size of the buffer
128 * Returns: Number of bytes written, or @-1@ on error.
130 * Use: Writes data to a transport.
133 ssize_t txsock_write(txport *txg, const void *p, size_t sz)
135 txsock *tx = (txsock *)txg;
137 return (write(tx->fd, p, sz));
140 /* --- @txsock_fetch@ --- *
142 * Arguments: @void *txv@ = pointer to transport context
144 * Returns: Nothing of interest.
146 * Use: Thread to fetch data from a socket.
149 void *txsock_fetch(void *txv)
152 unsigned char buf[BUFSIZ];
156 /* --- Read data while it arrives --- */
159 n = read(tx->fd, buf, sizeof(buf));
161 err_report(ERR_TXPORT, ERRTX_READ, errno,
162 "error reading from socket: %s", strerror(errno));
167 if ((e = pthread_mutex_lock(&tx->tx.mx)) != 0) {
168 err_report(ERR_TXPORT, ERRTX_READ, e,
169 "error locking mutex: %s", strerror(e));
172 DA_ENSURE(&tx->tx.buf, n);
173 memcpy(DA(&tx->tx.buf) + DA_LEN(&tx->tx.buf), buf, n);
174 DA_EXTEND(&tx->tx.buf, n);
175 pthread_cond_signal(&tx->tx.cv);
176 pthread_mutex_unlock(&tx->tx.mx);
179 /* --- Deal with crapness --- */
181 e = pthread_mutex_lock(&tx->tx.mx);
183 pthread_cond_signal(&tx->tx.cv);
184 pthread_mutex_unlock(&tx->tx.mx);
188 /* --- @txsock_destroy@ --- *
190 * Arguments: @txport *txg@ = pointer to transport context
194 * Use: Destroys a socket transport.
197 void txsock_destroy(txport *txg)
199 txsock *tx = (txsock *)txg;
205 /*----- That's all, folks -------------------------------------------------*/