3 * $Id: tx-socket.c,v 1.1 2002/01/25 19:34:45 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.1 2002/01/25 19:34:45 mdw
37 /*----- Header files ------------------------------------------------------*/
49 #include <sys/types.h>
51 #include <sys/socket.h>
55 #include <mLib/alloc.h>
56 #include <mLib/darray.h>
61 #include "tx-socket.h"
65 /*----- Data structures ---------------------------------------------------*/
67 typedef struct txsock {
68 txport tx; /* Transport base */
69 int fd; /* File descriptor */
72 /*----- Main code ---------------------------------------------------------*/
74 /* --- @txsock_create@ --- *
76 * Arguments: @const char *file@ = filename for socket
77 * @const char *config@ = configuration string
79 * Returns: Pointer to created transport block.
81 * Use: Creates a socket transport.
84 txport *txsock_create(const char *file, const char *config)
88 struct sockaddr_un *sun;
91 /* --- Parse the configuration --- */
93 if (config && *config) {
94 err_report(ERR_TXPORT, ERRTX_CONFIG, 0,
95 "bad configuration for socket transport");
99 /* --- Set up the address block --- */
101 len = strlen(file) + 1;
102 sunsz = offsetof(struct sockaddr_un, sun_path) + len;
103 sun = xmalloc(sunsz);
104 sun->sun_family = AF_UNIX;
105 memcpy(sun->sun_path, file, len);
107 /* --- Create the socket --- */
109 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
110 err_report(ERR_TXPORT, ERRTX_CREATE, errno,
111 "error creating socket: %s", strerror(errno));
115 /* --- Connect --- */
117 if (connect(fd, (struct sockaddr *)sun, sunsz)) {
118 err_report(ERR_TXPORT, ERRTX_CREATE, errno,
119 "couldn't connect to `%s': %s", file, strerror(errno));
130 /* --- Tidy up because it all went horribly wrong --- */
140 /* --- @txsock_write@ --- *
142 * Arguments: @txport *txg@ = pointer to transport context
143 * @const void *p@ = pointer to buffer
144 * @size_t sz@ = size of the buffer
146 * Returns: Number of bytes written, or @-1@ on error.
148 * Use: Writes data to a transport.
151 ssize_t txsock_write(txport *txg, const void *p, size_t sz)
153 txsock *tx = (txsock *)txg;
155 return (write(tx->fd, p, sz));
158 /* --- @txsock_fetch@ --- *
160 * Arguments: @void *txv@ = pointer to transport context
162 * Returns: Nothing of interest.
164 * Use: Thread to fetch data from a socket.
167 void *txsock_fetch(void *txv)
170 unsigned char buf[BUFSIZ];
174 /* --- Read data while it arrives --- */
177 n = read(tx->fd, buf, sizeof(buf));
179 err_report(ERR_TXPORT, ERRTX_READ, errno,
180 "error reading from socket: %s", strerror(errno));
185 if ((e = pthread_mutex_lock(&tx->tx.mx)) != 0) {
186 err_report(ERR_TXPORT, ERRTX_READ, e,
187 "error locking mutex: %s", strerror(e));
190 DA_ENSURE(&tx->tx.buf, n);
191 memcpy(DA(&tx->tx.buf) + DA_LEN(&tx->tx.buf), buf, n);
192 DA_EXTEND(&tx->tx.buf, n);
193 pthread_cond_signal(&tx->tx.cv);
194 pthread_mutex_unlock(&tx->tx.mx);
197 /* --- Deal with crapness --- */
199 e = pthread_mutex_lock(&tx->tx.mx);
201 pthread_cond_signal(&tx->tx.cv);
202 pthread_mutex_unlock(&tx->tx.mx);
206 /* --- @txsock_destroy@ --- *
208 * Arguments: @txport *txg@ = pointer to transport context
212 * Use: Destroys a socket transport.
215 void txsock_destroy(txport *txg)
217 txsock *tx = (txsock *)txg;
223 /*----- That's all, folks -------------------------------------------------*/