sndio/libsndio/mio_aucat.c

184 lines
4.4 KiB
C
Raw Normal View History

2010-08-19 16:00:06 -05:00
/* $OpenBSD$ */
2010-08-19 15:38:45 -05:00
/*
* Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
2010-08-19 15:38:45 -05:00
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2011-04-15 11:02:18 -05:00
#include "aucat.h"
#include "debug.h"
2010-08-19 15:38:45 -05:00
#include "mio_priv.h"
2010-08-19 16:00:06 -05:00
#include "bsd-compat.h"
2010-08-19 15:38:45 -05:00
struct mio_aucat_hdl {
2010-08-19 15:38:45 -05:00
struct mio_hdl mio;
2011-04-15 11:02:18 -05:00
struct aucat aucat;
int events;
2010-08-19 15:38:45 -05:00
};
static void mio_aucat_close(struct mio_hdl *);
static size_t mio_aucat_read(struct mio_hdl *, void *, size_t);
static size_t mio_aucat_write(struct mio_hdl *, const void *, size_t);
2012-10-26 07:30:48 -05:00
static int mio_aucat_nfds(struct mio_hdl *);
static int mio_aucat_pollfd(struct mio_hdl *, struct pollfd *, int);
static int mio_aucat_revents(struct mio_hdl *, struct pollfd *);
static struct mio_ops mio_aucat_ops = {
mio_aucat_close,
mio_aucat_write,
mio_aucat_read,
2012-10-26 07:30:48 -05:00
mio_aucat_nfds,
mio_aucat_pollfd,
2012-10-26 07:30:48 -05:00
mio_aucat_revents
2010-08-19 15:38:45 -05:00
};
/*
* execute the next message, return 0 if blocked
*/
static int
mio_aucat_runmsg(struct mio_aucat_hdl *hdl)
{
int delta;
2013-11-12 02:49:28 -06:00
if (!_aucat_rmsg(&hdl->aucat, &hdl->mio.eof))
return 0;
switch (ntohl(hdl->aucat.rmsg.cmd)) {
case AMSG_DATA:
return 1;
case AMSG_FLOWCTL:
delta = ntohl(hdl->aucat.rmsg.u.ts.delta);
hdl->aucat.maxwrite += delta;
DPRINTF("aucat: flowctl = %d, maxwrite = %d\n",
delta, hdl->aucat.maxwrite);
break;
default:
2016-01-26 03:23:38 -06:00
DPRINTF("mio_aucat_runmsg: unhandled message %u\n",
hdl->aucat.rmsg.cmd);
hdl->mio.eof = 1;
return 0;
}
hdl->aucat.rstate = RSTATE_MSG;
hdl->aucat.rtodo = sizeof(struct amsg);
return 1;
}
2011-10-17 17:29:58 -05:00
struct mio_hdl *
2016-01-26 03:23:38 -06:00
_mio_aucat_open(const char *str, unsigned int mode, int nbio)
2010-08-19 15:38:45 -05:00
{
struct mio_aucat_hdl *hdl;
2010-08-19 15:38:45 -05:00
hdl = malloc(sizeof(struct mio_aucat_hdl));
2010-08-19 15:38:45 -05:00
if (hdl == NULL)
return NULL;
2016-01-26 03:23:38 -06:00
if (!_aucat_open(&hdl->aucat, str, mode))
2011-04-15 11:02:18 -05:00
goto bad;
2013-11-12 02:49:28 -06:00
_mio_create(&hdl->mio, &mio_aucat_ops, mode, nbio);
if (!_aucat_setfl(&hdl->aucat, 1, &hdl->mio.eof))
2011-04-15 11:02:18 -05:00
goto bad;
2010-08-19 15:38:45 -05:00
return (struct mio_hdl *)hdl;
2011-04-15 11:02:18 -05:00
bad:
2010-08-19 15:38:45 -05:00
free(hdl);
return NULL;
}
static void
mio_aucat_close(struct mio_hdl *sh)
2010-08-19 15:38:45 -05:00
{
struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh;
2010-08-19 15:38:45 -05:00
2011-04-15 11:02:18 -05:00
if (!hdl->mio.eof)
2013-11-12 02:49:28 -06:00
_aucat_setfl(&hdl->aucat, 0, &hdl->mio.eof);
_aucat_close(&hdl->aucat, hdl->mio.eof);
2010-08-19 15:38:45 -05:00
free(hdl);
}
static size_t
mio_aucat_read(struct mio_hdl *sh, void *buf, size_t len)
2010-08-19 15:38:45 -05:00
{
struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh;
2011-04-15 11:02:18 -05:00
while (hdl->aucat.rstate == RSTATE_MSG) {
if (!mio_aucat_runmsg(hdl))
2011-04-15 11:02:18 -05:00
return 0;
2010-08-19 15:38:45 -05:00
}
2013-11-12 02:49:28 -06:00
return _aucat_rdata(&hdl->aucat, buf, len, &hdl->mio.eof);
2010-08-19 15:38:45 -05:00
}
static size_t
mio_aucat_write(struct mio_hdl *sh, const void *buf, size_t len)
2010-08-19 15:38:45 -05:00
{
struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh;
size_t n;
if (len <= 0 || hdl->aucat.maxwrite <= 0)
return 0;
if (len > hdl->aucat.maxwrite)
len = hdl->aucat.maxwrite;
2013-11-12 02:49:28 -06:00
n = _aucat_wdata(&hdl->aucat, buf, len, 1, &hdl->mio.eof);
hdl->aucat.maxwrite -= n;
return n;
2010-08-19 15:38:45 -05:00
}
2012-10-26 07:30:48 -05:00
static int
mio_aucat_nfds(struct mio_hdl *sh)
{
return 1;
}
2010-08-19 15:38:45 -05:00
static int
mio_aucat_pollfd(struct mio_hdl *sh, struct pollfd *pfd, int events)
2010-08-19 15:38:45 -05:00
{
struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh;
2010-08-19 15:38:45 -05:00
2011-04-15 11:02:18 -05:00
hdl->events = events;
if (hdl->aucat.maxwrite <= 0)
events &= ~POLLOUT;
2013-11-12 02:49:28 -06:00
return _aucat_pollfd(&hdl->aucat, pfd, events);
2010-08-19 15:38:45 -05:00
}
static int
mio_aucat_revents(struct mio_hdl *sh, struct pollfd *pfd)
2010-08-19 15:38:45 -05:00
{
2011-04-15 11:02:18 -05:00
struct mio_aucat_hdl *hdl = (struct mio_aucat_hdl *)sh;
int revents = pfd->revents;
if (revents & POLLIN) {
while (hdl->aucat.rstate == RSTATE_MSG) {
if (!mio_aucat_runmsg(hdl))
2011-04-15 11:02:18 -05:00
break;
}
if (hdl->aucat.rstate != RSTATE_DATA)
revents &= ~POLLIN;
}
if (revents & POLLOUT) {
if (hdl->aucat.maxwrite <= 0)
revents &= ~POLLOUT;
}
2011-04-15 11:02:18 -05:00
if (hdl->mio.eof)
return POLLHUP;
return revents & (hdl->events | POLLHUP);
2010-08-19 15:38:45 -05:00
}