2020-01-25 15:31:22 +00:00
|
|
|
/*
|
2023-04-06 11:50:44 +00:00
|
|
|
* Copyright (c) 2020 - 2023 Andri Yngvason
|
2020-01-25 15:31:22 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2023-04-06 11:50:44 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-01-25 15:31:22 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "sys/queue.h"
|
|
|
|
#include "rcbuf.h"
|
|
|
|
|
2020-07-20 22:37:40 +00:00
|
|
|
#include <stdint.h>
|
2023-04-07 10:50:10 +00:00
|
|
|
#include <stdbool.h>
|
2020-07-20 22:37:40 +00:00
|
|
|
|
2020-01-25 15:31:22 +00:00
|
|
|
#ifdef ENABLE_TLS
|
|
|
|
#include <gnutls/gnutls.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
enum stream_state {
|
|
|
|
STREAM_STATE_NORMAL = 0,
|
|
|
|
STREAM_STATE_CLOSED,
|
|
|
|
#ifdef ENABLE_TLS
|
|
|
|
STREAM_STATE_TLS_HANDSHAKE,
|
|
|
|
STREAM_STATE_TLS_READY,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
enum stream_req_status {
|
|
|
|
STREAM_REQ_DONE = 0,
|
|
|
|
STREAM_REQ_FAILED,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum stream_event {
|
|
|
|
STREAM_EVENT_READ,
|
|
|
|
STREAM_EVENT_REMOTE_CLOSED,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct stream;
|
|
|
|
|
|
|
|
typedef void (*stream_event_fn)(struct stream*, enum stream_event);
|
|
|
|
typedef void (*stream_req_fn)(void*, enum stream_req_status);
|
2023-04-08 13:02:43 +00:00
|
|
|
typedef struct rcbuf* (*stream_exec_fn)(struct stream*, void* userdata);
|
2020-01-25 15:31:22 +00:00
|
|
|
|
|
|
|
struct stream_req {
|
|
|
|
struct rcbuf* payload;
|
|
|
|
stream_req_fn on_done;
|
2023-04-08 13:02:43 +00:00
|
|
|
stream_exec_fn exec;
|
2020-01-25 15:31:22 +00:00
|
|
|
void* userdata;
|
|
|
|
TAILQ_ENTRY(stream_req) link;
|
|
|
|
};
|
|
|
|
|
|
|
|
TAILQ_HEAD(stream_send_queue, stream_req);
|
|
|
|
|
2023-04-06 11:50:44 +00:00
|
|
|
struct stream_impl {
|
|
|
|
int (*close)(struct stream*);
|
|
|
|
void (*destroy)(struct stream*);
|
|
|
|
ssize_t (*read)(struct stream*, void* dst, size_t size);
|
|
|
|
int (*send)(struct stream*, struct rcbuf* payload,
|
|
|
|
stream_req_fn on_done, void* userdata);
|
2023-04-07 10:50:10 +00:00
|
|
|
int (*send_first)(struct stream*, struct rcbuf* payload);
|
2023-04-08 13:02:43 +00:00
|
|
|
void (*exec_and_send)(struct stream*, stream_exec_fn, void* userdata);
|
2023-04-06 11:50:44 +00:00
|
|
|
};
|
|
|
|
|
2020-01-25 15:31:22 +00:00
|
|
|
struct stream {
|
2023-04-06 11:50:44 +00:00
|
|
|
struct stream_impl *impl;
|
|
|
|
|
2020-01-25 15:31:22 +00:00
|
|
|
enum stream_state state;
|
|
|
|
|
|
|
|
int fd;
|
2020-03-16 20:09:22 +00:00
|
|
|
struct aml_handler* handler;
|
2020-01-25 15:31:22 +00:00
|
|
|
stream_event_fn on_event;
|
|
|
|
void* userdata;
|
|
|
|
|
|
|
|
struct stream_send_queue send_queue;
|
|
|
|
|
|
|
|
#ifdef ENABLE_TLS
|
|
|
|
gnutls_session_t tls_session;
|
|
|
|
#endif
|
2020-07-20 22:37:40 +00:00
|
|
|
|
|
|
|
uint32_t bytes_sent;
|
|
|
|
uint32_t bytes_received;
|
2023-04-07 10:50:10 +00:00
|
|
|
|
|
|
|
bool cork;
|
2020-01-25 15:31:22 +00:00
|
|
|
};
|
|
|
|
|
2023-04-06 21:02:01 +00:00
|
|
|
#ifdef ENABLE_WEBSOCKET
|
|
|
|
struct stream* stream_ws_new(int fd, stream_event_fn on_event, void* userdata);
|
|
|
|
#endif
|
|
|
|
|
2020-01-25 15:31:22 +00:00
|
|
|
struct stream* stream_new(int fd, stream_event_fn on_event, void* userdata);
|
|
|
|
int stream_close(struct stream* self);
|
|
|
|
void stream_destroy(struct stream* self);
|
|
|
|
ssize_t stream_read(struct stream* self, void* dst, size_t size);
|
2020-01-25 15:46:52 +00:00
|
|
|
int stream_write(struct stream* self, const void* payload, size_t len,
|
2020-01-25 15:31:22 +00:00
|
|
|
stream_req_fn on_done, void* userdata);
|
2020-01-25 15:46:52 +00:00
|
|
|
int stream_send(struct stream* self, struct rcbuf* payload,
|
|
|
|
stream_req_fn on_done, void* userdata);
|
2023-04-07 10:50:10 +00:00
|
|
|
int stream_send_first(struct stream* self, struct rcbuf* payload);
|
2020-01-25 15:31:22 +00:00
|
|
|
|
2023-04-08 13:02:43 +00:00
|
|
|
// Queue a pure function to be executed when time comes to send it.
|
|
|
|
void stream_exec_and_send(struct stream* self, stream_exec_fn, void* userdata);
|
|
|
|
|
2020-01-25 15:31:22 +00:00
|
|
|
#ifdef ENABLE_TLS
|
|
|
|
int stream_upgrade_to_tls(struct stream* self, void* context);
|
|
|
|
#endif
|