hio/lib/pipe.c

581 lines
15 KiB
C
Raw Normal View History

2020-05-21 10:15:57 +00:00
/*
Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
2020-05-21 10:15:57 +00:00
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
2020-05-21 10:15:57 +00:00
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
2020-05-21 10:15:57 +00:00
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
2022-06-11 05:32:01 +00:00
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2020-05-21 10:15:57 +00:00
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2020-05-21 10:15:57 +00:00
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2021-07-22 07:30:20 +00:00
#include <hio-pipe.h>
#include "hio-prv.h"
2020-05-21 10:15:57 +00:00
#include <unistd.h>
#include <errno.h>
#include <sys/uio.h>
/* ========================================================================= */
struct slave_info_t
{
2021-07-22 07:30:20 +00:00
hio_dev_pipe_make_t* mi;
hio_syshnd_t pfd;
2020-05-21 10:15:57 +00:00
int dev_cap;
2021-07-22 07:30:20 +00:00
hio_dev_pipe_sid_t id;
2020-05-21 10:15:57 +00:00
};
typedef struct slave_info_t slave_info_t;
2021-07-22 07:30:20 +00:00
static hio_dev_pipe_slave_t* make_slave (hio_t* hio, slave_info_t* si);
2020-05-21 10:15:57 +00:00
/* ========================================================================= */
2021-07-22 07:30:20 +00:00
static int dev_pipe_make_master (hio_dev_t* dev, void* ctx)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_t* hio = dev->hio;
hio_dev_pipe_t* rdev = (hio_dev_pipe_t*)dev;
hio_dev_pipe_make_t* info = (hio_dev_pipe_make_t*)ctx;
hio_syshnd_t pfds[2] = { HIO_SYSHND_INVALID, HIO_SYSHND_INVALID };
2020-05-21 10:15:57 +00:00
slave_info_t si;
int i;
/* TODO: support a named pipe. use mkfifo()?
* support socketpair */
#if defined(HAVE_PIPE2) && defined(O_CLOEXEC) && defined(O_NONBLOCK)
if (pipe2(pfds, O_CLOEXEC | O_NONBLOCK) == -1)
{
if (errno != ENOSYS) goto pipe_error;
}
else goto pipe_done;
#endif
2020-05-21 10:15:57 +00:00
if (pipe(pfds) == -1)
{
#if defined(HAVE_PIPE2) && defined(O_CLOEXEC) && defined(O_NONBLOCK)
pipe_error:
#endif
2021-07-22 07:30:20 +00:00
hio_seterrwithsyserr (hio, 0, errno);
2020-05-21 10:15:57 +00:00
goto oops;
}
2021-07-22 07:30:20 +00:00
if (hio_makesyshndasync(hio, pfds[0]) <= -1 ||
hio_makesyshndasync(hio, pfds[1]) <= -1) goto oops;
2020-05-21 10:15:57 +00:00
2021-07-22 07:30:20 +00:00
if (hio_makesyshndcloexec(hio, pfds[0]) <= -1 ||
hio_makesyshndcloexec(hio, pfds[1]) <= -1) goto oops;
#if defined(HAVE_PIPE2) && defined(O_CLOEXEC) && defined(O_NONBLOCK)
pipe_done:
#endif
2020-05-21 10:15:57 +00:00
si.mi = info;
si.pfd = pfds[0];
2021-07-22 07:30:20 +00:00
si.dev_cap = HIO_DEV_CAP_IN | HIO_DEV_CAP_STREAM;
si.id = HIO_DEV_PIPE_IN;
pfds[0] = HIO_SYSHND_INVALID;
rdev->slave[HIO_DEV_PIPE_IN] = make_slave(hio, &si);
if (!rdev->slave[HIO_DEV_PIPE_IN]) goto oops;
2020-05-21 10:15:57 +00:00
rdev->slave_count++;
si.mi = info;
si.pfd = pfds[1];
2021-07-22 07:30:20 +00:00
si.dev_cap = HIO_DEV_CAP_OUT | HIO_DEV_CAP_STREAM;
si.id = HIO_DEV_PIPE_OUT;
pfds[1] = HIO_SYSHND_INVALID;
rdev->slave[HIO_DEV_PIPE_OUT] = make_slave(hio, &si);
if (!rdev->slave[HIO_DEV_PIPE_OUT]) goto oops;
2020-05-21 10:15:57 +00:00
rdev->slave_count++;
for (i = 0; i < HIO_COUNTOF(rdev->slave); i++)
2020-05-21 10:15:57 +00:00
{
if (rdev->slave[i]) rdev->slave[i]->master = rdev;
}
2021-07-22 07:30:20 +00:00
rdev->dev_cap = HIO_DEV_CAP_VIRTUAL; /* the master device doesn't perform I/O */
2020-05-21 10:15:57 +00:00
rdev->on_read = info->on_read;
rdev->on_write = info->on_write;
rdev->on_close = info->on_close;
return 0;
oops:
2021-07-22 07:30:20 +00:00
if (pfds[0] != HIO_SYSHND_INVALID) close (pfds[0]);
if (pfds[1] != HIO_SYSHND_INVALID) close (pfds[0]);
if (rdev->slave[0])
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_kill ((hio_dev_t*)rdev->slave[0]);
rdev->slave[0] = HIO_NULL;
}
if (rdev->slave[1])
{
2021-07-22 07:30:20 +00:00
hio_dev_kill ((hio_dev_t*)rdev->slave[1]);
rdev->slave[1] = HIO_NULL;
2020-05-21 10:15:57 +00:00
}
rdev->slave_count = 0;
2020-05-21 10:15:57 +00:00
return -1;
}
2021-07-22 07:30:20 +00:00
static int dev_pipe_make_slave (hio_dev_t* dev, void* ctx)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_pipe_slave_t* rdev = (hio_dev_pipe_slave_t*)dev;
2020-05-21 10:15:57 +00:00
slave_info_t* si = (slave_info_t*)ctx;
rdev->dev_cap = si->dev_cap;
rdev->id = si->id;
rdev->pfd = si->pfd;
2021-07-22 07:30:20 +00:00
/* keep rdev->master to HIO_NULL. it's set to the right master
2020-05-21 10:15:57 +00:00
* device in dev_pipe_make() */
return 0;
}
2021-07-22 07:30:20 +00:00
static int dev_pipe_kill_master (hio_dev_t* dev, int force)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
/*hio_t* hio = dev->hio;*/
hio_dev_pipe_t* rdev = (hio_dev_pipe_t*)dev;
2020-05-21 10:15:57 +00:00
int i;
if (rdev->slave_count > 0)
{
2021-07-22 07:30:20 +00:00
for (i = 0; i < HIO_COUNTOF(rdev->slave); i++)
2020-05-21 10:15:57 +00:00
{
if (rdev->slave[i])
{
2021-07-22 07:30:20 +00:00
hio_dev_pipe_slave_t* sdev = rdev->slave[i];
2020-05-21 10:15:57 +00:00
/* nullify the pointer to the slave device
2021-07-22 07:30:20 +00:00
* before calling hio_dev_kill() on the slave device.
2020-05-21 10:15:57 +00:00
* the slave device can check this pointer to tell from
* self-initiated termination or master-driven termination */
2021-07-22 07:30:20 +00:00
rdev->slave[i] = HIO_NULL;
2020-05-21 10:15:57 +00:00
2021-07-22 07:30:20 +00:00
hio_dev_kill ((hio_dev_t*)sdev);
2020-05-21 10:15:57 +00:00
}
}
}
2021-07-22 07:30:20 +00:00
if (rdev->on_close) rdev->on_close (rdev, HIO_DEV_PIPE_MASTER);
2020-05-21 10:15:57 +00:00
return 0;
}
2021-07-22 07:30:20 +00:00
static int dev_pipe_kill_slave (hio_dev_t* dev, int force)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_t* hio = dev->hio;
hio_dev_pipe_slave_t* rdev = (hio_dev_pipe_slave_t*)dev;
2020-05-21 10:15:57 +00:00
if (rdev->master)
{
2021-07-22 07:30:20 +00:00
hio_dev_pipe_t* master;
2020-05-21 10:15:57 +00:00
master = rdev->master;
2021-07-22 07:30:20 +00:00
rdev->master = HIO_NULL;
2020-05-21 10:15:57 +00:00
/* indicate EOF */
if (master->on_close) master->on_close (master, rdev->id);
2021-07-22 07:30:20 +00:00
HIO_ASSERT (hio, master->slave_count > 0);
2020-05-21 10:15:57 +00:00
master->slave_count--;
if (master->slave[rdev->id])
{
/* this call is started by the slave device itself.
* if this is the last slave, kill the master also */
if (master->slave_count <= 0)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_kill ((hio_dev_t*)master);
2020-05-21 10:15:57 +00:00
/* the master pointer is not valid from this point onwards
2021-07-22 07:30:20 +00:00
* as the actual master device object is freed in hio_dev_kill() */
2020-05-21 10:15:57 +00:00
}
else
{
/* this call is initiated by this slave device itself.
2021-07-22 07:30:20 +00:00
* if it were by the master device, it would be HIO_NULL as
2020-05-21 10:15:57 +00:00
* nullified by the dev_pipe_kill() */
2021-07-22 07:30:20 +00:00
master->slave[rdev->id] = HIO_NULL;
2020-05-21 10:15:57 +00:00
}
}
}
2021-07-22 07:30:20 +00:00
if (rdev->pfd != HIO_SYSHND_INVALID)
2020-05-21 10:15:57 +00:00
{
close (rdev->pfd);
2021-07-22 07:30:20 +00:00
rdev->pfd = HIO_SYSHND_INVALID;
2020-05-21 10:15:57 +00:00
}
return 0;
}
static void dev_pipe_fail_before_make_slave (void* ctx)
{
slave_info_t* si = (slave_info_t*)ctx;
close (si->pfd);
}
2021-07-22 07:30:20 +00:00
static int dev_pipe_read_slave (hio_dev_t* dev, void* buf, hio_iolen_t* len, hio_devaddr_t* srcaddr)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_pipe_slave_t* pipe = (hio_dev_pipe_slave_t*)dev;
2020-05-21 10:15:57 +00:00
ssize_t x;
2021-07-22 07:30:20 +00:00
if (HIO_UNLIKELY(pipe->pfd == HIO_SYSHND_INVALID))
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_seterrnum (pipe->hio, HIO_EBADHND);
2020-05-21 10:15:57 +00:00
return -1;
}
x = read(pipe->pfd, buf, *len);
if (x <= -1)
{
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data available */
if (errno == EINTR) return 0;
2021-07-22 07:30:20 +00:00
hio_seterrwithsyserr (pipe->hio, 0, errno);
2020-05-21 10:15:57 +00:00
return -1;
}
*len = x;
return 1;
}
2021-07-22 07:30:20 +00:00
static int dev_pipe_write_slave (hio_dev_t* dev, const void* data, hio_iolen_t* len, const hio_devaddr_t* dstaddr)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_pipe_slave_t* pipe = (hio_dev_pipe_slave_t*)dev;
2020-05-21 10:15:57 +00:00
ssize_t x;
2021-07-22 07:30:20 +00:00
if (HIO_UNLIKELY(pipe->pfd == HIO_SYSHND_INVALID))
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_seterrnum (pipe->hio, HIO_EBADHND);
2020-05-21 10:15:57 +00:00
return -1;
}
2021-07-22 07:30:20 +00:00
if (HIO_UNLIKELY(*len <= 0))
2020-05-21 10:15:57 +00:00
{
/* this is an EOF indicator */
2021-07-22 07:30:20 +00:00
/*hio_dev_halt (dev);*/ /* halt this slave device to indicate EOF on the lower-level handle */
if (HIO_LIKELY(pipe->pfd != HIO_SYSHND_INVALID)) /* halt() doesn't close the pipe immediately. so close the underlying pipe */
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_watch (dev, HIO_DEV_WATCH_STOP, 0);
2020-05-21 10:15:57 +00:00
close (pipe->pfd);
2021-07-22 07:30:20 +00:00
pipe->pfd = HIO_SYSHND_INVALID;
2020-05-21 10:15:57 +00:00
}
return 1; /* indicate that the operation got successful. the core will execute on_write() with 0. */
}
x = write(pipe->pfd, data, *len);
if (x <= -1)
{
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */
if (errno == EINTR) return 0;
2021-07-22 07:30:20 +00:00
hio_seterrwithsyserr (pipe->hio, 0, errno);
2020-05-21 10:15:57 +00:00
return -1;
}
*len = x;
return 1;
}
2021-07-22 07:30:20 +00:00
static int dev_pipe_writev_slave (hio_dev_t* dev, const hio_iovec_t* iov, hio_iolen_t* iovcnt, const hio_devaddr_t* dstaddr)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_pipe_slave_t* pipe = (hio_dev_pipe_slave_t*)dev;
2020-05-21 10:15:57 +00:00
ssize_t x;
2021-07-22 07:30:20 +00:00
if (HIO_UNLIKELY(pipe->pfd == HIO_SYSHND_INVALID))
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_seterrnum (pipe->hio, HIO_EBADHND);
2020-05-21 10:15:57 +00:00
return -1;
}
2021-07-22 07:30:20 +00:00
if (HIO_UNLIKELY(*iovcnt <= 0))
2020-05-21 10:15:57 +00:00
{
/* this is an EOF indicator */
2021-07-22 07:30:20 +00:00
/*hio_dev_halt (dev);*/ /* halt this slave device to indicate EOF on the lower-level handle */
if (HIO_LIKELY(pipe->pfd != HIO_SYSHND_INVALID)) /* halt() doesn't close the pipe immediately. so close the underlying pipe */
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_watch (dev, HIO_DEV_WATCH_STOP, 0);
2020-05-21 10:15:57 +00:00
close (pipe->pfd);
2021-07-22 07:30:20 +00:00
pipe->pfd = HIO_SYSHND_INVALID;
2020-05-21 10:15:57 +00:00
}
return 1; /* indicate that the operation got successful. the core will execute on_write() with 0. */
}
x = writev(pipe->pfd, iov, *iovcnt);
if (x <= -1)
{
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */
if (errno == EINTR) return 0;
2021-07-22 07:30:20 +00:00
hio_seterrwithsyserr (pipe->hio, 0, errno);
2020-05-21 10:15:57 +00:00
return -1;
}
*iovcnt = x;
return 1;
}
2021-07-22 07:30:20 +00:00
static hio_syshnd_t dev_pipe_getsyshnd (hio_dev_t* dev)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
return HIO_SYSHND_INVALID;
2020-05-21 10:15:57 +00:00
}
2021-07-22 07:30:20 +00:00
static hio_syshnd_t dev_pipe_getsyshnd_slave (hio_dev_t* dev)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_pipe_slave_t* pipe = (hio_dev_pipe_slave_t*)dev;
return (hio_syshnd_t)pipe->pfd;
2020-05-21 10:15:57 +00:00
}
2021-07-22 07:30:20 +00:00
static int dev_pipe_ioctl (hio_dev_t* dev, int cmd, void* arg)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_t* hio = dev->hio;
hio_dev_pipe_t* rdev = (hio_dev_pipe_t*)dev;
2020-05-21 10:15:57 +00:00
switch (cmd)
{
2021-07-22 07:30:20 +00:00
case HIO_DEV_PIPE_CLOSE:
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_pipe_sid_t sid = *(hio_dev_pipe_sid_t*)arg;
2020-05-21 10:15:57 +00:00
2021-07-22 07:30:20 +00:00
if (sid != HIO_DEV_PIPE_IN && sid != HIO_DEV_PIPE_OUT)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_seterrnum (hio, HIO_EINVAL);
2020-05-21 10:15:57 +00:00
return -1;
}
if (rdev->slave[sid])
{
/* unlike dev_pipe_kill_master(), i don't nullify rdev->slave[sid].
* so i treat the closing ioctl as if it's a kill request
2020-05-21 10:15:57 +00:00
* initiated by the slave device itself. */
2021-07-22 07:30:20 +00:00
hio_dev_kill ((hio_dev_t*)rdev->slave[sid]);
2020-05-21 10:15:57 +00:00
}
return 0;
}
default:
2021-07-22 07:30:20 +00:00
hio_seterrnum (hio, HIO_EINVAL);
2020-05-21 10:15:57 +00:00
return -1;
}
}
static hio_dev_mth_t dev_pipe_methods =
2020-05-21 10:15:57 +00:00
{
dev_pipe_make_master,
dev_pipe_kill_master,
2021-07-22 07:30:20 +00:00
HIO_NULL,
2020-05-21 10:15:57 +00:00
dev_pipe_getsyshnd,
HIO_NULL,
dev_pipe_ioctl,
2020-05-21 10:15:57 +00:00
2021-07-22 07:30:20 +00:00
HIO_NULL,
HIO_NULL,
HIO_NULL,
HIO_NULL, /* sendfile */
2020-05-21 10:15:57 +00:00
};
2021-07-22 07:30:20 +00:00
static hio_dev_mth_t dev_pipe_methods_slave =
2020-05-21 10:15:57 +00:00
{
dev_pipe_make_slave,
dev_pipe_kill_slave,
dev_pipe_fail_before_make_slave,
2020-05-21 10:15:57 +00:00
dev_pipe_getsyshnd_slave,
HIO_NULL,
dev_pipe_ioctl,
2020-05-21 10:15:57 +00:00
dev_pipe_read_slave,
dev_pipe_write_slave,
dev_pipe_writev_slave,
HIO_NULL, /* sendfile */
2020-05-21 10:15:57 +00:00
};
/* ========================================================================= */
2021-07-22 07:30:20 +00:00
static int pipe_ready (hio_dev_t* dev, int events)
2020-05-21 10:15:57 +00:00
{
/* virtual device. no I/O */
2021-07-22 07:30:20 +00:00
hio_seterrnum (dev->hio, HIO_EINTERN);
2020-05-21 10:15:57 +00:00
return -1;
}
2021-07-22 07:30:20 +00:00
static int pipe_on_read (hio_dev_t* dev, const void* data, hio_iolen_t len, const hio_devaddr_t* srcaddr)
2020-05-21 10:15:57 +00:00
{
/* virtual device. no I/O */
2021-07-22 07:30:20 +00:00
hio_seterrnum (dev->hio, HIO_EINTERN);
2020-05-21 10:15:57 +00:00
return -1;
}
2021-07-22 07:30:20 +00:00
static int pipe_on_write (hio_dev_t* dev, hio_iolen_t wrlen, void* wrctx, const hio_devaddr_t* dstaddr)
2020-05-21 10:15:57 +00:00
{
/* virtual device. no I/O */
2021-07-22 07:30:20 +00:00
hio_seterrnum (dev->hio, HIO_EINTERN);
2020-05-21 10:15:57 +00:00
return -1;
}
2021-07-22 07:30:20 +00:00
static hio_dev_evcb_t dev_pipe_event_callbacks =
2020-05-21 10:15:57 +00:00
{
pipe_ready,
pipe_on_read,
pipe_on_write
};
/* ========================================================================= */
2021-07-22 07:30:20 +00:00
static int pipe_ready_slave (hio_dev_t* dev, int events)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_t* hio = dev->hio;
/*hio_dev_pipe_t* pipe = (hio_dev_pipe_t*)dev;*/
2020-05-21 10:15:57 +00:00
2021-07-22 07:30:20 +00:00
if (events & HIO_DEV_EVENT_ERR)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_seterrnum (hio, HIO_EDEVERR);
2020-05-21 10:15:57 +00:00
return -1;
}
2021-07-22 07:30:20 +00:00
if (events & HIO_DEV_EVENT_HUP)
2020-05-21 10:15:57 +00:00
{
if (events & (HIO_DEV_EVENT_PRI | HIO_DEV_EVENT_IN | HIO_DEV_EVENT_OUT))
2020-05-21 10:15:57 +00:00
{
/* pipebably half-open? */
return 1;
}
2021-07-22 07:30:20 +00:00
hio_seterrnum (hio, HIO_EDEVHUP);
2020-05-21 10:15:57 +00:00
return -1;
}
return 1; /* the device is ok. carry on reading or writing */
}
2021-07-22 07:30:20 +00:00
static int pipe_on_read_slave (hio_dev_t* dev, const void* data, hio_iolen_t len, const hio_devaddr_t* srcaddr)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_pipe_slave_t* pipe = (hio_dev_pipe_slave_t*)dev;
2020-05-21 10:15:57 +00:00
return pipe->master->on_read(pipe->master, data, len);
}
2021-07-22 07:30:20 +00:00
static int pipe_on_write_slave (hio_dev_t* dev, hio_iolen_t wrlen, void* wrctx, const hio_devaddr_t* dstaddr)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_pipe_slave_t* pipe = (hio_dev_pipe_slave_t*)dev;
2020-05-21 10:15:57 +00:00
return pipe->master->on_write(pipe->master, wrlen, wrctx);
}
2021-07-22 07:30:20 +00:00
static hio_dev_evcb_t dev_pipe_event_callbacks_slave_in =
2020-05-21 10:15:57 +00:00
{
pipe_ready_slave,
pipe_on_read_slave,
2021-07-22 07:30:20 +00:00
HIO_NULL
2020-05-21 10:15:57 +00:00
};
2021-07-22 07:30:20 +00:00
static hio_dev_evcb_t dev_pipe_event_callbacks_slave_out =
2020-05-21 10:15:57 +00:00
{
pipe_ready_slave,
2021-07-22 07:30:20 +00:00
HIO_NULL,
2020-05-21 10:15:57 +00:00
pipe_on_write_slave
};
/* ========================================================================= */
2021-07-22 07:30:20 +00:00
static hio_dev_pipe_slave_t* make_slave (hio_t* hio, slave_info_t* si)
2020-05-21 10:15:57 +00:00
{
switch (si->id)
{
2021-07-22 07:30:20 +00:00
case HIO_DEV_PIPE_IN:
return (hio_dev_pipe_slave_t*)hio_dev_make(
hio, HIO_SIZEOF(hio_dev_pipe_t),
2020-05-21 10:15:57 +00:00
&dev_pipe_methods_slave, &dev_pipe_event_callbacks_slave_in, si);
2021-07-22 07:30:20 +00:00
case HIO_DEV_PIPE_OUT:
return (hio_dev_pipe_slave_t*)hio_dev_make(
hio, HIO_SIZEOF(hio_dev_pipe_t),
2020-05-21 10:15:57 +00:00
&dev_pipe_methods_slave, &dev_pipe_event_callbacks_slave_out, si);
default:
2021-07-22 07:30:20 +00:00
hio_seterrnum (hio, HIO_EINVAL);
return HIO_NULL;
2020-05-21 10:15:57 +00:00
}
}
2021-07-22 07:30:20 +00:00
hio_dev_pipe_t* hio_dev_pipe_make (hio_t* hio, hio_oow_t xtnsize, const hio_dev_pipe_make_t* info)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
return (hio_dev_pipe_t*)hio_dev_make(
hio, HIO_SIZEOF(hio_dev_pipe_t) + xtnsize,
2020-05-21 10:15:57 +00:00
&dev_pipe_methods, &dev_pipe_event_callbacks, (void*)info);
}
2021-07-22 07:30:20 +00:00
void hio_dev_pipe_kill (hio_dev_pipe_t* dev)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_kill ((hio_dev_t*)dev);
2020-05-21 10:15:57 +00:00
}
2021-07-22 07:30:20 +00:00
void hio_dev_pipe_halt (hio_dev_pipe_t* dev)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
hio_dev_halt ((hio_dev_t*)dev);
2020-05-21 10:15:57 +00:00
}
2021-07-22 07:30:20 +00:00
int hio_dev_pipe_read (hio_dev_pipe_t* dev, int enabled)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
if (dev->slave[HIO_DEV_PIPE_IN])
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
return hio_dev_read((hio_dev_t*)dev->slave[HIO_DEV_PIPE_IN], enabled);
2020-05-21 10:15:57 +00:00
}
else
{
2021-07-22 07:30:20 +00:00
hio_seterrnum (dev->hio, HIO_ENOCAPA); /* TODO: is it the right error number? */
2020-05-21 10:15:57 +00:00
return -1;
}
}
2021-07-22 07:30:20 +00:00
int hio_dev_pipe_timedread (hio_dev_pipe_t* dev, int enabled, const hio_ntime_t* tmout)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
if (dev->slave[HIO_DEV_PIPE_IN])
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
return hio_dev_timedread((hio_dev_t*)dev->slave[HIO_DEV_PIPE_IN], enabled, tmout);
2020-05-21 10:15:57 +00:00
}
else
{
2021-07-22 07:30:20 +00:00
hio_seterrnum (dev->hio, HIO_ENOCAPA); /* TODO: is it the right error number? */
2020-05-21 10:15:57 +00:00
return -1;
}
}
2021-07-22 07:30:20 +00:00
int hio_dev_pipe_write (hio_dev_pipe_t* dev, const void* data, hio_iolen_t dlen, void* wrctx)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
if (dev->slave[HIO_DEV_PIPE_OUT])
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
return hio_dev_write((hio_dev_t*)dev->slave[HIO_DEV_PIPE_OUT], data, dlen, wrctx, HIO_NULL);
2020-05-21 10:15:57 +00:00
}
else
{
2021-07-22 07:30:20 +00:00
hio_seterrnum (dev->hio, HIO_ENOCAPA); /* TODO: is it the right error number? */
2020-05-21 10:15:57 +00:00
return -1;
}
}
2021-07-22 07:30:20 +00:00
int hio_dev_pipe_timedwrite (hio_dev_pipe_t* dev, const void* data, hio_iolen_t dlen, const hio_ntime_t* tmout, void* wrctx)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
if (dev->slave[HIO_DEV_PIPE_OUT])
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
return hio_dev_timedwrite((hio_dev_t*)dev->slave[HIO_DEV_PIPE_OUT], data, dlen, tmout, wrctx, HIO_NULL);
2020-05-21 10:15:57 +00:00
}
else
{
2021-07-22 07:30:20 +00:00
hio_seterrnum (dev->hio, HIO_ENOCAPA); /* TODO: is it the right error number? */
2020-05-21 10:15:57 +00:00
return -1;
}
}
2021-07-22 07:30:20 +00:00
int hio_dev_pipe_close (hio_dev_pipe_t* dev, hio_dev_pipe_sid_t sid)
2020-05-21 10:15:57 +00:00
{
2021-07-22 07:30:20 +00:00
return hio_dev_ioctl((hio_dev_t*)dev, HIO_DEV_PIPE_CLOSE, &sid);
2020-05-21 10:15:57 +00:00
}