odyssey/lib/ft_io.c

84 lines
1.8 KiB
C
Raw Normal View History

2016-11-08 12:35:21 +00:00
/*
* fluent.
*
* Cooperative multitasking engine.
*/
#include <fluent_private.h>
#include <fluent.h>
FLUENT_API ftio_t
ft_io_new(ft_t envp)
{
ft *env = envp;
ftio *io = malloc(sizeof(*io));
if (io == NULL)
return NULL;
/* tcp */
io->fd = -1;
io->f = env;
uv_tcp_init(&env->loop, &io->handle);
io->handle.data = io;
/* connect */
memset(&io->connect, 0, sizeof(io->connect));
uv_timer_init(&env->loop, &io->connect_timer);
io->connect.data = io;
io->connect_timer.data = io;
io->connect_timeout = 0;
io->connect_status = 0;
io->connected = 0;
io->connect_fiber = NULL;
/* accept */
io->accept_status = 0;
io->accept_fiber = NULL;
/* read */
ft_bufinit(&io->read_buf);
uv_timer_init(&env->loop, &io->read_timer);
io->read_timer.data = io;
io->read_size = 0;
io->read_status = 0;
io->read_timeout = 0;
io->read_eof = 0;
io->read_fiber = NULL;
/* write */
memset(&io->write, 0, sizeof(io->write));
uv_timer_init(&env->loop, &io->write_timer);
io->write.data = io;
io->write_timer.data = io;
io->write_timeout = 0;
io->write_fiber = NULL;
io->write_status = 0;
return io;
}
FLUENT_API void
2016-11-08 13:23:58 +00:00
ft_close(ftio_t iop)
2016-11-08 12:35:21 +00:00
{
ftio *io = iop;
uv_timer_stop(&io->connect_timer);
uv_timer_stop(&io->read_timer);
uv_timer_stop(&io->write_timer);
if (uv_is_active((uv_handle_t*)&io->connect_timer))
uv_close((uv_handle_t*)&io->connect_timer, NULL);
if (uv_is_active((uv_handle_t*)&io->read_timer))
uv_close((uv_handle_t*)&io->read_timer, NULL);
if (uv_is_active((uv_handle_t*)&io->write_timer))
uv_close((uv_handle_t*)&io->write_timer, NULL);
if (uv_is_active((uv_handle_t*)&io->handle))
uv_close((uv_handle_t*)&io->handle, NULL);
if (io->fd != -1)
close(io->fd);
ft_buffree(&io->read_buf);
free(io);
}
FLUENT_API int
2016-11-08 13:23:58 +00:00
ft_fd(ftio_t iop)
2016-11-08 12:35:21 +00:00
{
ftio *io = iop;
return io->fd;
}