machinarium: rework io api using opaque objects

This commit is contained in:
Dmitry Simonenko 2017-06-13 14:49:50 +03:00
parent b2bd1cdea0
commit fcb4d681b8
24 changed files with 97 additions and 98 deletions

View File

@ -26,12 +26,11 @@ typedef void (*machine_coroutine_t)(void *arg);
/* library handles */
typedef struct machine_channel_ref machine_channel_t;
typedef struct machine_queue_ref machine_queue_t;
typedef struct machine_msg_ref machine_msg_t;
typedef struct machine_tls_ref machine_tls_t;
typedef void* machine_io_t;
typedef struct machine_msg_private machine_msg_t;
typedef struct machine_channel_private machine_channel_t;
typedef struct machine_queue_private machine_queue_t;
typedef struct machine_tls_private machine_tls_t;
typedef struct machine_io_private machine_io_t;
/* main */
@ -160,43 +159,43 @@ machine_tls_set_key_file(machine_tls_t*, char*);
/* io control */
MACHINE_API machine_io_t
MACHINE_API machine_io_t*
machine_io_create(void);
MACHINE_API void
machine_io_free(machine_io_t);
machine_io_free(machine_io_t*);
MACHINE_API int
machine_io_attach(machine_io_t);
machine_io_attach(machine_io_t*);
MACHINE_API int
machine_io_detach(machine_io_t);
machine_io_detach(machine_io_t*);
MACHINE_API char*
machine_error(machine_io_t);
machine_error(machine_io_t*);
MACHINE_API int
machine_fd(machine_io_t);
machine_fd(machine_io_t*);
MACHINE_API int
machine_set_nodelay(machine_io_t, int enable);
machine_set_nodelay(machine_io_t*, int enable);
MACHINE_API int
machine_set_keepalive(machine_io_t, int enable, int delay);
machine_set_keepalive(machine_io_t*, int enable, int delay);
MACHINE_API int
machine_set_readahead(machine_io_t, int size);
machine_set_readahead(machine_io_t*, int size);
MACHINE_API int
machine_set_tls(machine_io_t, machine_tls_t*);
machine_set_tls(machine_io_t*, machine_tls_t*);
/* dns */
MACHINE_API int
machine_getsockname(machine_io_t, struct sockaddr*, int*);
machine_getsockname(machine_io_t*, struct sockaddr*, int*);
MACHINE_API int
machine_getpeername(machine_io_t, struct sockaddr*, int*);
machine_getpeername(machine_io_t*, struct sockaddr*, int*);
MACHINE_API int
machine_getaddrinfo(char *addr, char *service,
@ -207,24 +206,24 @@ machine_getaddrinfo(char *addr, char *service,
/* io */
MACHINE_API int
machine_connect(machine_io_t, struct sockaddr*, uint32_t time_ms);
machine_connect(machine_io_t*, struct sockaddr*, uint32_t time_ms);
MACHINE_API int
machine_connected(machine_io_t);
machine_connected(machine_io_t*);
MACHINE_API int
machine_bind(machine_io_t, struct sockaddr*);
machine_bind(machine_io_t*, struct sockaddr*);
MACHINE_API int
machine_accept(machine_io_t, machine_io_t*, int backlog, uint32_t time_ms);
machine_accept(machine_io_t*, machine_io_t**, int backlog, uint32_t time_ms);
MACHINE_API int
machine_read(machine_io_t, char *buf, int size, uint32_t time_ms);
machine_read(machine_io_t*, char *buf, int size, uint32_t time_ms);
MACHINE_API int
machine_write(machine_io_t, char *buf, int size, uint32_t time_ms);
machine_write(machine_io_t*, char *buf, int size, uint32_t time_ms);
MACHINE_API int
machine_close(machine_io_t);
machine_close(machine_io_t*);
#endif

View File

@ -20,7 +20,7 @@ mm_accept_on_read_cb(mm_fd_t *handle)
}
static int
mm_accept(mm_io_t *io, int backlog, machine_io_t *client, uint32_t time_ms)
mm_accept(mm_io_t *io, int backlog, machine_io_t **client, uint32_t time_ms)
{
mm_machine_t *machine = mm_self;
mm_errno_set(0);
@ -100,7 +100,7 @@ mm_accept(mm_io_t *io, int backlog, machine_io_t *client, uint32_t time_ms)
*client = NULL;
return -1;
}
rc = machine_io_attach(client_io);
rc = machine_io_attach((machine_io_t*)client_io);
if (rc == -1) {
machine_close(*client);
machine_io_free(*client);
@ -111,10 +111,10 @@ mm_accept(mm_io_t *io, int backlog, machine_io_t *client, uint32_t time_ms)
}
MACHINE_API int
machine_accept(machine_io_t obj, machine_io_t *client,
machine_accept(machine_io_t *obj, machine_io_t **client,
int backlog, uint32_t time_ms)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
int rc;
rc = mm_accept(io, backlog, client, time_ms);
if (rc == -1)

View File

@ -9,9 +9,9 @@
#include <machinarium_private.h>
MACHINE_API int
machine_bind(machine_io_t obj, struct sockaddr *sa)
machine_bind(machine_io_t *obj, struct sockaddr *sa)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
mm_errno_set(0);
if (io->connected) {
mm_errno_set(EINPROGRESS);
@ -31,7 +31,7 @@ machine_bind(machine_io_t obj, struct sockaddr *sa)
mm_errno_set(errno);
goto error;
}
rc = machine_io_attach(io);
rc = machine_io_attach(obj);
if (rc == -1)
goto error;
return 0;

View File

@ -9,15 +9,15 @@
#include <machinarium_private.h>
MACHINE_API int
machine_close(machine_io_t obj)
machine_close(machine_io_t *obj)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
if (io->fd == -1) {
mm_errno_set(EBADF);
return -1;
}
if (io->attached)
machine_io_detach(io);
machine_io_detach(obj);
int rc;
rc = close(io->fd);
if (rc == -1)

View File

@ -52,7 +52,7 @@ mm_connect(mm_io_t *io, struct sockaddr *sa, uint32_t time_ms)
}
/* add socket to event loop */
rc = machine_io_attach(io);
rc = machine_io_attach((machine_io_t*)io);
if (rc == -1)
goto error;
@ -97,9 +97,9 @@ error:
}
MACHINE_API int
machine_connect(machine_io_t obj, struct sockaddr *sa, uint32_t time_ms)
machine_connect(machine_io_t *obj, struct sockaddr *sa, uint32_t time_ms)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
int rc = mm_connect(io, sa, time_ms);
if (rc == -1)
return -1;
@ -107,8 +107,8 @@ machine_connect(machine_io_t obj, struct sockaddr *sa, uint32_t time_ms)
}
MACHINE_API int
machine_connected(machine_io_t obj)
machine_connected(machine_io_t *obj)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
return io->connected;
}

View File

@ -44,9 +44,9 @@ machine_getaddrinfo(char *addr, char *service,
}
MACHINE_API int
machine_getsockname(machine_io_t obj, struct sockaddr *sa, int *salen)
machine_getsockname(machine_io_t *obj, struct sockaddr *sa, int *salen)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
mm_errno_set(0);
socklen_t slen = *salen;
int rc = mm_socket_getsockname(io->fd, sa, &slen);
@ -59,9 +59,9 @@ machine_getsockname(machine_io_t obj, struct sockaddr *sa, int *salen)
}
MACHINE_API int
machine_getpeername(machine_io_t obj, struct sockaddr *sa, int *salen)
machine_getpeername(machine_io_t *obj, struct sockaddr *sa, int *salen)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
mm_errno_set(0);
socklen_t slen = *salen;
int rc = mm_socket_getpeername(io->fd, sa, &slen);

View File

@ -8,7 +8,7 @@
#include <machinarium.h>
#include <machinarium_private.h>
MACHINE_API machine_io_t
MACHINE_API machine_io_t*
machine_io_create(void)
{
mm_errno_set(0);
@ -26,13 +26,13 @@ machine_io_create(void)
/* read */
io->readahead_size = 8192;
mm_buf_init(&io->readahead_buf);
return io;
return (machine_io_t*)io;
}
MACHINE_API void
machine_io_free(machine_io_t obj)
machine_io_free(machine_io_t *obj)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
mm_errno_set(0);
mm_buf_free(&io->readahead_buf);
mm_tlsio_free(&io->tls);
@ -40,9 +40,9 @@ machine_io_free(machine_io_t obj)
}
MACHINE_API char*
machine_error(machine_io_t obj)
machine_error(machine_io_t *obj)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
if (io->tls.error)
return io->tls.error_msg;
int errno_ = mm_errno_get();
@ -52,16 +52,16 @@ machine_error(machine_io_t obj)
}
MACHINE_API int
machine_fd(machine_io_t obj)
machine_fd(machine_io_t *obj)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
return io->fd;
}
MACHINE_API int
machine_set_nodelay(machine_io_t obj, int enable)
machine_set_nodelay(machine_io_t *obj, int enable)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
mm_errno_set(0);
io->opt_nodelay = enable;
if (io->fd != -1) {
@ -76,9 +76,9 @@ machine_set_nodelay(machine_io_t obj, int enable)
}
MACHINE_API int
machine_set_keepalive(machine_io_t obj, int enable, int delay)
machine_set_keepalive(machine_io_t *obj, int enable, int delay)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
mm_errno_set(0);
io->opt_keepalive = enable;
io->opt_keepalive_delay = delay;
@ -94,9 +94,9 @@ machine_set_keepalive(machine_io_t obj, int enable, int delay)
}
MACHINE_API int
machine_io_attach(machine_io_t obj)
machine_io_attach(machine_io_t *obj)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
mm_errno_set(0);
if (io->attached) {
mm_errno_set(EINPROGRESS);
@ -113,9 +113,9 @@ machine_io_attach(machine_io_t obj)
}
MACHINE_API int
machine_io_detach(machine_io_t obj)
machine_io_detach(machine_io_t *obj)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
mm_errno_set(0);
if (! io->attached) {
mm_errno_set(ENOTCONN);

View File

@ -180,18 +180,18 @@ int mm_read(mm_io_t *io, char *buf, int size, uint32_t time_ms)
}
MACHINE_API int
machine_read(machine_io_t obj, char *buf, int size, uint32_t time_ms)
machine_read(machine_io_t *obj, char *buf, int size, uint32_t time_ms)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
if (mm_tls_is_active(&io->tls))
return mm_tlsio_read(&io->tls, buf, size, time_ms);
return mm_read(io, buf, size, time_ms);
}
MACHINE_API int
machine_set_readahead(machine_io_t obj, int size)
machine_set_readahead(machine_io_t *obj, int size)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
mm_errno_set(0);
int rc;
rc = mm_buf_ensure(&io->readahead_buf, size);

View File

@ -462,9 +462,9 @@ int mm_tlsio_read(mm_tlsio_t *io, char *buf, int size, uint32_t time_ms)
}
MACHINE_API int
machine_set_tls(machine_io_t obj, machine_tls_t *tls_obj)
machine_set_tls(machine_io_t *obj, machine_tls_t *tls_obj)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
mm_tlsio_error_reset(&io->tls);
if (io->tls_obj) {
mm_errno_set(EINPROGRESS);

View File

@ -107,9 +107,9 @@ int mm_write(mm_io_t *io, char *buf, int size, uint32_t time_ms)
}
MACHINE_API int
machine_write(machine_io_t obj, char *buf, int size, uint32_t time_ms)
machine_write(machine_io_t *obj, char *buf, int size, uint32_t time_ms)
{
mm_io_t *io = obj;
mm_io_t *io = mm_cast(mm_io_t*, obj);
if (mm_tls_is_active(&io->tls))
return mm_tlsio_write(&io->tls, buf, size, time_ms);
return mm_write(io, buf, size, time_ms);

View File

@ -13,7 +13,7 @@
static void
test_server(void *arg)
{
machine_io_t server = machine_io_create();
machine_io_t *server = machine_io_create();
test(server != NULL);
struct sockaddr_in sa;
@ -24,7 +24,7 @@ test_server(void *arg)
rc = machine_bind(server, (struct sockaddr*)&sa);
test(rc == 0);
machine_io_t client;
machine_io_t *client;
rc = machine_accept(server, &client, 16, 100);
test(rc == -1);
test(machine_cancelled());

View File

@ -13,7 +13,7 @@
static void
test_server(void *arg)
{
machine_io_t server = machine_io_create();
machine_io_t *server = machine_io_create();
test(server != NULL);
struct sockaddr_in sa;
@ -24,7 +24,7 @@ test_server(void *arg)
rc = machine_bind(server, (struct sockaddr*)&sa);
test(rc == 0);
machine_io_t client;
machine_io_t *client;
rc = machine_accept(server, &client, 16, 100);
test(rc == -1);
test(machine_timedout());

View File

@ -14,7 +14,7 @@
static void
server(void *arg)
{
machine_io_t server = machine_io_create();
machine_io_t *server = machine_io_create();
test(server != NULL);
struct sockaddr_in sa;
@ -25,7 +25,7 @@ server(void *arg)
rc = machine_bind(server, (struct sockaddr*)&sa);
test(rc == 0);
machine_io_t client;
machine_io_t *client;
rc = machine_accept(server, &client, 16, UINT32_MAX);
test(rc == 0);
@ -45,7 +45,7 @@ server(void *arg)
static void
client(void *arg)
{
machine_io_t client = machine_io_create();
machine_io_t *client = machine_io_create();
test(client != NULL);
struct sockaddr_in sa;

View File

@ -14,7 +14,7 @@
static void
server(void *arg)
{
machine_io_t server = machine_io_create();
machine_io_t *server = machine_io_create();
test(server != NULL);
struct sockaddr_in sa;
@ -25,7 +25,7 @@ server(void *arg)
rc = machine_bind(server, (struct sockaddr*)&sa);
test(rc == 0);
machine_io_t client;
machine_io_t *client;
rc = machine_accept(server, &client, 16, UINT32_MAX);
test(rc == 0);
@ -45,7 +45,7 @@ server(void *arg)
static void
client(void *arg)
{
machine_io_t client = machine_io_create();
machine_io_t *client = machine_io_create();
test(client != NULL);
struct sockaddr_in sa;

View File

@ -13,7 +13,7 @@
static void
test_connect_coroutine(void *arg)
{
machine_io_t client = machine_io_create();
machine_io_t *client = machine_io_create();
test(client != NULL);
struct sockaddr_in sa;

View File

@ -13,7 +13,7 @@
static void
test_connect_coroutine(void *arg)
{
machine_io_t client = machine_io_create();
machine_io_t *client = machine_io_create();
test(client != NULL);
struct sockaddr_in sa;

View File

@ -13,7 +13,7 @@
static void
test_connect_coroutine(void *arg)
{
machine_io_t client = machine_io_create();
machine_io_t *client = machine_io_create();
test(client != NULL);
struct sockaddr_in sa;

View File

@ -13,7 +13,7 @@
static void
test_connect_coroutine(void *arg)
{
machine_io_t client = machine_io_create();
machine_io_t *client = machine_io_create();
test(client != NULL);
struct sockaddr_in sa;

View File

@ -11,7 +11,7 @@
static void
coroutine(void *arg)
{
machine_io_t io = machine_io_create();
machine_io_t *io = machine_io_create();
test(io != NULL);
machine_io_free(io);

View File

@ -14,7 +14,7 @@
static void
server(void *arg)
{
machine_io_t server = machine_io_create();
machine_io_t *server = machine_io_create();
test(server != NULL);
struct sockaddr_in sa;
@ -25,7 +25,7 @@ server(void *arg)
rc = machine_bind(server, (struct sockaddr*)&sa);
test(rc == 0);
machine_io_t client;
machine_io_t *client;
rc = machine_accept(server, &client, 16, UINT32_MAX);
test(rc == 0);
@ -55,7 +55,7 @@ server(void *arg)
static void
client(void *arg)
{
machine_io_t client = machine_io_create();
machine_io_t *client = machine_io_create();
test(client != NULL);
struct sockaddr_in sa;

View File

@ -16,7 +16,7 @@ static int client_id;
static void
server(void *arg)
{
machine_io_t server = machine_io_create();
machine_io_t *server = machine_io_create();
test(server != NULL);
struct sockaddr_in sa;
@ -27,7 +27,7 @@ server(void *arg)
rc = machine_bind(server, (struct sockaddr*)&sa);
test(rc == 0);
machine_io_t client;
machine_io_t *client;
rc = machine_accept(server, &client, 16, UINT32_MAX);
test(rc == 0);
@ -47,7 +47,7 @@ server(void *arg)
static void
client(void *arg)
{
machine_io_t client = machine_io_create();
machine_io_t *client = machine_io_create();
test(client != NULL);
struct sockaddr_in sa;

View File

@ -14,7 +14,7 @@
static void
server(void *arg)
{
machine_io_t server = machine_io_create();
machine_io_t *server = machine_io_create();
test(server != NULL);
struct sockaddr_in sa;
@ -25,7 +25,7 @@ server(void *arg)
rc = machine_bind(server, (struct sockaddr*)&sa);
test(rc == 0);
machine_io_t client;
machine_io_t *client;
rc = machine_accept(server, &client, 16, UINT32_MAX);
test(rc == 0);
@ -41,7 +41,7 @@ server(void *arg)
static void
client(void *arg)
{
machine_io_t client = machine_io_create();
machine_io_t *client = machine_io_create();
test(client != NULL);
struct sockaddr_in sa;

View File

@ -14,7 +14,7 @@
static void
server(void *arg)
{
machine_io_t server = machine_io_create();
machine_io_t *server = machine_io_create();
test(server != NULL);
struct sockaddr_in sa;
@ -25,7 +25,7 @@ server(void *arg)
rc = machine_bind(server, (struct sockaddr*)&sa);
test(rc == 0);
machine_io_t client = NULL;
machine_io_t *client = NULL;
rc = machine_accept(server, &client, 16, UINT32_MAX);
test(rc == 0);
test(client != NULL);
@ -64,7 +64,7 @@ server(void *arg)
static void
client(void *arg)
{
machine_io_t client = machine_io_create();
machine_io_t *client = machine_io_create();
test(client != NULL);
struct sockaddr_in sa;

View File

@ -14,7 +14,7 @@
static void
server(void *arg)
{
machine_io_t server = machine_io_create();
machine_io_t *server = machine_io_create();
test(server != NULL);
int rc;
@ -28,7 +28,7 @@ server(void *arg)
rc = machine_bind(server, (struct sockaddr*)&sa);
test(rc == 0);
machine_io_t client;
machine_io_t *client;
rc = machine_accept(server, &client, 16, UINT32_MAX);
test(rc == 0);
@ -76,7 +76,7 @@ server(void *arg)
static void
client(void *arg)
{
machine_io_t client = machine_io_create();
machine_io_t *client = machine_io_create();
test(client != NULL);
int rc;