sndio/libsndio/mio_aucat.c

144 lines
3.6 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 <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);
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,
mio_aucat_pollfd,
mio_aucat_revents,
2010-08-19 15:38:45 -05:00
};
2010-10-22 12:05:45 -05:00
static struct mio_hdl *
2011-04-18 18:52:03 -05:00
mio_xxx_open(const char *str, unsigned mode, int nbio, int isaudio)
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;
2011-04-18 18:52:03 -05:00
if (!aucat_open(&hdl->aucat, str, mode, isaudio))
2011-04-15 11:02:18 -05:00
goto bad;
mio_create(&hdl->mio, &mio_aucat_ops, mode, nbio);
2011-04-15 11:02:18 -05:00
if (!aucat_setfl(&hdl->aucat, nbio, &hdl->mio.eof))
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;
}
struct mio_hdl *
mio_midithru_open(const char *str, unsigned mode, int nbio)
2010-08-19 15:38:45 -05:00
{
2011-04-18 18:52:03 -05:00
return mio_xxx_open(str, mode, nbio, 0);
2010-08-19 15:38:45 -05:00
}
struct mio_hdl *
mio_aucat_open(const char *str, unsigned mode, int nbio)
2010-08-19 15:38:45 -05:00
{
2011-04-18 18:52:03 -05:00
return mio_xxx_open(str, mode, nbio, 1);
2010-08-19 15:38:45 -05:00
}
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)
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 (!aucat_rmsg(&hdl->aucat, &hdl->mio.eof))
return 0;
2010-08-19 15:38:45 -05:00
}
2011-04-15 11:02:18 -05: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;
2011-04-15 11:02:18 -05:00
return aucat_wdata(&hdl->aucat, buf, len, 1, &hdl->mio.eof);
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;
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 (!aucat_rmsg(&hdl->aucat, &hdl->mio.eof))
break;
}
if (hdl->aucat.rstate != RSTATE_DATA)
revents &= ~POLLIN;
}
if (hdl->mio.eof)
return POLLHUP;
return revents & (hdl->events | POLLHUP);
2010-08-19 15:38:45 -05:00
}