aboutsummaryrefslogtreecommitdiff
path: root/src/supplemental/http/http_conn.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2019-01-21 22:40:10 -0800
committerGarrett D'Amore <garrett@damore.org>2019-02-16 19:22:27 -0800
commit5cf750697624d4fd63cfe26921209d7c30e1a2d2 (patch)
treebf11695e5f1ec5e400c87da0cc6ff23935a2eeff /src/supplemental/http/http_conn.c
parentca655b9db689ee0e655248b1a9f166b8db6cc984 (diff)
downloadnng-5cf750697624d4fd63cfe26921209d7c30e1a2d2.tar.gz
nng-5cf750697624d4fd63cfe26921209d7c30e1a2d2.tar.bz2
nng-5cf750697624d4fd63cfe26921209d7c30e1a2d2.zip
fixes #872 create unified nng_stream API
This is a major change, and includes changes to use a polymorphic stream API for all transports. There have been related bugs fixed along the way. Additionally the man pages have changed. The old non-polymorphic APIs are removed now. This is a breaking change, but the old APIs were never part of any released public API.
Diffstat (limited to 'src/supplemental/http/http_conn.c')
-rw-r--r--src/supplemental/http/http_conn.c125
1 files changed, 23 insertions, 102 deletions
diff --git a/src/supplemental/http/http_conn.c b/src/supplemental/http/http_conn.c
index 7c6159cd..1fc2c34e 100644
--- a/src/supplemental/http/http_conn.c
+++ b/src/supplemental/http/http_conn.c
@@ -1,7 +1,7 @@
//
-// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
-// Copyright 2018 Devolutions <info@devolutions.net>
+// Copyright 2019 Devolutions <info@devolutions.net>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
@@ -40,25 +40,6 @@ enum write_flavor {
HTTP_WR_RES,
};
-typedef void (*http_read_fn)(void *, nni_aio *);
-typedef void (*http_write_fn)(void *, nni_aio *);
-typedef void (*http_close_fn)(void *);
-typedef void (*http_fini_fn)(void *);
-typedef int (*http_addr_fn)(void *, nni_sockaddr *);
-typedef int (*http_getopt_fn)(
- void *, const char *, void *, size_t *, nni_type);
-typedef int (*http_setopt_fn)(
- void *, const char *, const void *, size_t, nni_type);
-
-typedef struct {
- http_read_fn h_read;
- http_write_fn h_write;
- http_getopt_fn h_getopt;
- http_setopt_fn h_setopt;
- http_close_fn h_close;
- http_fini_fn h_fini;
-} http_tran;
-
#define SET_RD_FLAVOR(aio, f) \
nni_aio_set_prov_extra(aio, 0, ((void *) (intptr_t)(f)))
#define GET_RD_FLAVOR(aio) (int) ((intptr_t) nni_aio_get_prov_extra(aio, 0))
@@ -67,17 +48,11 @@ typedef struct {
#define GET_WR_FLAVOR(aio) (int) ((intptr_t) nni_aio_get_prov_extra(aio, 0))
struct nng_http_conn {
- void * sock;
- http_read_fn rd;
- http_write_fn wr;
- http_setopt_fn setopt;
- http_getopt_fn getopt;
- http_close_fn close;
- http_fini_fn fini;
- void * ctx;
- bool closed;
- nni_list rdq; // high level http read requests
- nni_list wrq; // high level http write requests
+ nng_stream *sock;
+ void * ctx;
+ bool closed;
+ nni_list rdq; // high level http read requests
+ nni_list wrq; // high level http write requests
nni_aio *rd_uaio; // user aio for read
nni_aio *wr_uaio; // user aio for write
@@ -138,7 +113,7 @@ http_close(nni_http_conn *conn)
}
if (conn->sock != NULL) {
- conn->close(conn->sock);
+ nng_stream_close(conn->sock);
}
}
@@ -204,7 +179,7 @@ http_rd_buf(nni_http_conn *conn, nni_aio *aio)
// to get *any* data for a partial RAW read.)
nni_aio_set_data(conn->rd_aio, 1, NULL);
nni_aio_set_iov(conn->rd_aio, niov, iov);
- conn->rd(conn->sock, conn->rd_aio);
+ nng_stream_recv(conn->sock, conn->rd_aio);
return (NNG_EAGAIN);
case HTTP_RD_REQ:
@@ -220,7 +195,7 @@ http_rd_buf(nni_http_conn *conn, nni_aio *aio)
iov1.iov_len = conn->rd_bufsz - conn->rd_put;
nni_aio_set_iov(conn->rd_aio, 1, &iov1);
nni_aio_set_data(conn->rd_aio, 1, aio);
- conn->rd(conn->sock, conn->rd_aio);
+ nng_stream_recv(conn->sock, conn->rd_aio);
}
return (rv);
@@ -237,7 +212,7 @@ http_rd_buf(nni_http_conn *conn, nni_aio *aio)
iov1.iov_len = conn->rd_bufsz - conn->rd_put;
nni_aio_set_iov(conn->rd_aio, 1, &iov1);
nni_aio_set_data(conn->rd_aio, 1, aio);
- conn->rd(conn->sock, conn->rd_aio);
+ nng_stream_recv(conn->sock, conn->rd_aio);
}
return (rv);
@@ -254,7 +229,7 @@ http_rd_buf(nni_http_conn *conn, nni_aio *aio)
iov1.iov_len = conn->rd_bufsz - conn->rd_put;
nni_aio_set_iov(conn->rd_aio, 1, &iov1);
nni_aio_set_data(conn->rd_aio, 1, aio);
- conn->rd(conn->sock, conn->rd_aio);
+ nng_stream_recv(conn->sock, conn->rd_aio);
}
return (rv);
}
@@ -428,7 +403,7 @@ http_wr_start(nni_http_conn *conn)
nni_aio_get_iov(aio, &niov, &iov);
nni_aio_set_iov(conn->wr_aio, niov, iov);
- conn->wr(conn->sock, conn->wr_aio);
+ nng_stream_send(conn->sock, conn->wr_aio);
}
static void
@@ -475,7 +450,7 @@ http_wr_cb(void *arg)
if (nni_aio_iov_count(aio) > 0) {
// We have more to transmit - start another and leave
// (we will get called again when it is done).
- conn->wr(conn->sock, aio);
+ nng_stream_send(conn->sock, aio);
nni_mtx_unlock(&conn->mtx);
return;
}
@@ -680,7 +655,7 @@ nni_http_conn_getopt(
if (conn->closed) {
rv = NNG_ECLOSED;
} else {
- rv = conn->getopt(conn->sock, name, buf, szp, t);
+ rv = nni_stream_getx(conn->sock, name, buf, szp, t);
}
nni_mtx_unlock(&conn->mtx);
return (rv);
@@ -695,7 +670,7 @@ nni_http_conn_setopt(nni_http_conn *conn, const char *name, const void *buf,
if (conn->closed) {
rv = NNG_ECLOSED;
} else {
- rv = conn->setopt(conn->sock, name, buf, sz, t);
+ rv = nni_stream_setx(conn->sock, name, buf, sz, t);
}
nni_mtx_unlock(&conn->mtx);
return (rv);
@@ -709,8 +684,8 @@ nni_http_conn_fini(nni_http_conn *conn)
nni_mtx_lock(&conn->mtx);
http_close(conn);
- if ((conn->sock != NULL) && (conn->fini != NULL)) {
- conn->fini(conn->sock);
+ if (conn->sock != NULL) {
+ nng_stream_free(conn->sock);
conn->sock = NULL;
}
nni_mtx_unlock(&conn->mtx);
@@ -723,7 +698,7 @@ nni_http_conn_fini(nni_http_conn *conn)
}
static int
-http_init(nni_http_conn **connp, http_tran *tran, void *data)
+http_init(nni_http_conn **connp, nng_stream *data)
{
nni_http_conn *conn;
int rv;
@@ -747,73 +722,19 @@ http_init(nni_http_conn **connp, http_tran *tran, void *data)
return (rv);
}
- conn->sock = data;
- conn->rd = tran->h_read;
- conn->wr = tran->h_write;
- conn->close = tran->h_close;
- conn->fini = tran->h_fini;
- conn->getopt = tran->h_getopt;
- conn->setopt = tran->h_setopt;
+ conn->sock = data;
*connp = conn;
return (0);
}
-static http_tran http_tcp_ops = {
- .h_read = (http_read_fn) nni_tcp_conn_recv,
- .h_write = (http_write_fn) nni_tcp_conn_send,
- .h_close = (http_close_fn) nni_tcp_conn_close,
- .h_fini = (http_fini_fn) nni_tcp_conn_fini,
- .h_getopt = (http_getopt_fn) nni_tcp_conn_getopt,
- .h_setopt = (http_setopt_fn) nni_tcp_conn_setopt,
-};
-
int
-nni_http_conn_init_tcp(nni_http_conn **connp, nni_tcp_conn *tcp)
+nni_http_conn_init(nni_http_conn **connp, nng_stream *stream)
{
int rv;
- if ((rv = http_init(connp, &http_tcp_ops, tcp)) != 0) {
- nni_tcp_conn_fini(tcp);
- }
- return (rv);
-}
-
-#ifdef NNG_SUPP_TLS
-static http_tran http_tls_ops = {
- .h_read = (http_read_fn) nni_tls_recv,
- .h_write = (http_write_fn) nni_tls_send,
- .h_close = (http_close_fn) nni_tls_close,
- .h_fini = (http_fini_fn) nni_tls_fini,
- .h_getopt = (http_getopt_fn) nni_tls_getopt,
- .h_setopt = (http_setopt_fn) nni_tls_setopt,
-};
-
-int
-nni_http_conn_init_tls(
- nni_http_conn **connp, struct nng_tls_config *cfg, nni_tcp_conn *tcp)
-{
- nni_tls *tls;
- int rv;
-
- if ((rv = nni_tls_init(&tls, cfg, tcp)) != 0) {
- nni_tcp_conn_fini(tcp);
- return (rv);
- }
-
- if ((rv = http_init(connp, &http_tls_ops, tls)) != 0) {
- nni_tls_fini(tls);
+ if ((rv = http_init(connp, stream)) != 0) {
+ nng_stream_free(stream);
}
return (rv);
}
-#else
-int
-nni_http_conn_init_tls(
- nni_http_conn **connp, struct nng_tls_config *cfg, nni_tcp_conn *tcp)
-{
- NNI_ARG_UNUSED(connp);
- NNI_ARG_UNUSED(cfg);
- nni_tcp_conn_fini(tcp);
- return (NNG_ENOTSUP);
-}
-#endif // NNG_SUPP_TLS