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 /*----- Revision history --------------------------------------------------*
31 * $Log: tx-socket.c,v $
32 * Revision 1.2 2002/01/30 09:25:35 mdw
33 * Minor modifications for the transport configuration change.
35 * Revision 1.1 2002/01/25 19:34:45 mdw
40 /*----- Header files ------------------------------------------------------*/
52 #include <sys/types.h>
54 #include <sys/socket.h>
58 #include <mLib/alloc.h>
59 #include <mLib/darray.h>
64 #include "tx-socket.h"
68 /*----- Data structures ---------------------------------------------------*/
70 typedef struct txsock {
71 txport tx; /* Transport base */
72 int fd; /* File descriptor */
75 /*----- Main code ---------------------------------------------------------*/
77 /* --- @txsock_create@ --- *
79 * Arguments: @const char *file@ = filename for socket
81 * Returns: Pointer to created transport block.
83 * Use: Creates a socket transport.
86 txport *txsock_create(const char *file)
90 struct sockaddr_un *sun;
93 /* --- Set up the address block --- */
95 len = strlen(file) + 1;
96 sunsz = offsetof(struct sockaddr_un, sun_path) + len;
98 sun->sun_family = AF_UNIX;
99 memcpy(sun->sun_path, file, len);
101 /* --- Create the socket --- */
103 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
104 err_report(ERR_TXPORT, ERRTX_CREATE, errno,
105 "error creating socket: %s", strerror(errno));
109 /* --- Connect --- */
111 if (connect(fd, (struct sockaddr *)sun, sunsz)) {
112 err_report(ERR_TXPORT, ERRTX_CREATE, errno,
113 "couldn't connect to `%s': %s", file, strerror(errno));
124 /* --- Tidy up because it all went horribly wrong --- */
133 /* --- @txsock_write@ --- *
135 * Arguments: @txport *txg@ = pointer to transport context
136 * @const void *p@ = pointer to buffer
137 * @size_t sz@ = size of the buffer
139 * Returns: Number of bytes written, or @-1@ on error.
141 * Use: Writes data to a transport.
144 ssize_t txsock_write(txport *txg, const void *p, size_t sz)
146 txsock *tx = (txsock *)txg;
148 return (write(tx->fd, p, sz));
151 /* --- @txsock_fetch@ --- *
153 * Arguments: @void *txv@ = pointer to transport context
155 * Returns: Nothing of interest.
157 * Use: Thread to fetch data from a socket.
160 void *txsock_fetch(void *txv)
163 unsigned char buf[BUFSIZ];
167 /* --- Read data while it arrives --- */
170 n = read(tx->fd, buf, sizeof(buf));
172 err_report(ERR_TXPORT, ERRTX_READ, errno,
173 "error reading from socket: %s", strerror(errno));
178 if ((e = pthread_mutex_lock(&tx->tx.mx)) != 0) {
179 err_report(ERR_TXPORT, ERRTX_READ, e,
180 "error locking mutex: %s", strerror(e));
183 DA_ENSURE(&tx->tx.buf, n);
184 memcpy(DA(&tx->tx.buf) + DA_LEN(&tx->tx.buf), buf, n);
185 DA_EXTEND(&tx->tx.buf, n);
186 pthread_cond_signal(&tx->tx.cv);
187 pthread_mutex_unlock(&tx->tx.mx);
190 /* --- Deal with crapness --- */
192 e = pthread_mutex_lock(&tx->tx.mx);
194 pthread_cond_signal(&tx->tx.cv);
195 pthread_mutex_unlock(&tx->tx.mx);
199 /* --- @txsock_destroy@ --- *
201 * Arguments: @txport *txg@ = pointer to transport context
205 * Use: Destroys a socket transport.
208 void txsock_destroy(txport *txg)
210 txsock *tx = (txsock *)txg;
216 /*----- That's all, folks -------------------------------------------------*/