2016-11-08 12:47:46 +00:00
|
|
|
|
|
|
|
/*
|
2016-11-08 14:53:52 +00:00
|
|
|
* flint.
|
2016-11-08 12:47:46 +00:00
|
|
|
*
|
|
|
|
* Cooperative multitasking engine.
|
|
|
|
*/
|
|
|
|
|
2016-11-08 14:53:52 +00:00
|
|
|
#include <flint_private.h>
|
|
|
|
#include <flint.h>
|
2016-11-08 12:47:46 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
ft_io_accept_cb(uv_stream_t *handle, int status)
|
|
|
|
{
|
|
|
|
ftio *io = handle->data;
|
|
|
|
io->accept_status = status;
|
|
|
|
ft_wakeup(io->f, io->accept_fiber);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ftio_t
|
|
|
|
ft_io_accept_client(ftio *io)
|
|
|
|
{
|
|
|
|
ftio *client = (ftio*)ft_io_new(io->f);
|
|
|
|
if (client == NULL) {
|
|
|
|
io->accept_status = -ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
io->accept_status =
|
|
|
|
uv_accept((uv_stream_t*)&io->handle,
|
|
|
|
(uv_stream_t*)&client->handle);
|
|
|
|
if (io->accept_status < 0) {
|
|
|
|
ft_close(client);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
client->connected = 1;
|
|
|
|
uv_fileno((uv_handle_t*)&client->handle,
|
|
|
|
&client->fd);
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
2016-11-08 14:53:52 +00:00
|
|
|
FLINT_API int
|
2016-11-08 12:47:46 +00:00
|
|
|
ft_accept(ftio_t iop, ftio_t *client)
|
|
|
|
{
|
|
|
|
ftio *io = iop;
|
2016-11-18 13:27:59 +00:00
|
|
|
ftfiber *current = ft_current(io->f);
|
|
|
|
if (ft_fiber_is_cancel(current))
|
|
|
|
return -ECANCELED;
|
2016-11-08 12:47:46 +00:00
|
|
|
if (io->accept_fiber)
|
|
|
|
return -1;
|
|
|
|
io->accept_status = 0;
|
2016-11-18 13:27:59 +00:00
|
|
|
io->accept_fiber = current;
|
2016-11-08 12:47:46 +00:00
|
|
|
int rc;
|
|
|
|
rc = uv_listen((uv_stream_t*)&io->handle, 128, ft_io_accept_cb);
|
|
|
|
if (rc < 0) {
|
|
|
|
io->accept_fiber = NULL;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
ft_scheduler_yield(&io->f->scheduler);
|
|
|
|
rc = io->accept_status;
|
|
|
|
io->accept_fiber = NULL;
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
*client = ft_io_accept_client(io);
|
|
|
|
if (*client == NULL)
|
|
|
|
rc = io->accept_status;
|
|
|
|
return rc;
|
|
|
|
}
|