#pragma once

#include <stddef.h>
#include <stdint.h>
#include <tfe_types.h>
#include <tfe_utils.h>
#include <tfe_cmsg.h>

enum tfe_stream_proto
{
	STREAM_PROTO_PLAIN = 0,
	STREAM_PROTO_SSL,
	STREAM_PROTO_QUIC,
	STREAM_PROTO_SPDY
};

enum tfe_app_proto
{
	APP_PROTO_HTTP1,
	APP_PROTO_HTTP2,
	APP_PROTO_WS,        //websocket
	APP_PROTO_QUIC       //QUIC is a protocol that cross session layer and application layer.
};


enum tfe_conn_status
{
	CONN_STATUS_NONE,
	CONN_STATUS_ESTABLISHED,
	CONN_STATUS_CLOSED,
};

/* single dst or src socket bufferevent descriptor */
struct tfe_conn
{
	enum tfe_conn_status status;
};

struct tfe_stream
{
	const char * str_stream_info;
	struct tfe_stream_addr * addr;
	enum tfe_stream_proto proto;
	unsigned int thread_id;
	struct tfe_conn upstream;
	struct tfe_conn downstream;
};

enum tfe_stream_action
{
	ACTION_FORWARD_DATA,
	ACTION_DEFER_DATA,
	ACTION_DROP_DATA
};

enum tfe_stream_action_opt
{
	ACTION_OPT_FOWARD_BYTES,    //value is size_t, default: forward entire data
	ACTION_OPT_DEFER_TIME_TV,    //value is "struct timeval " which defines in <time.h>, default: time defer is not enabled
	ACTION_OPT_DEFER_BYTES,        //value is size_t, default: defer entire data
	ACTION_OPT_DROP_BYTES        //value is size_t, default: drop entire data
};

enum tfe_stream_close_reason
{
	REASON_PASSIVE_CLOSED,
	REASON_ACTIVE_CLOSED,
	REASON_ERROR
};

enum tfe_stream_info
{
	/* OFFSET IN BYTES, FROM BEGIN OF THE DIRECTION
	 * TYPE: SIZE_T */
	INFO_FROM_DOWNSTREAM_RX_OFFSET,
	INFO_FROM_UPSTREAM_RX_OFFSET,
};

int tfe_stream_action_set_opt(const struct tfe_stream * stream, enum tfe_stream_action_opt type,
	void * value, size_t size);

int tfe_stream_info_get(const struct tfe_stream * stream, enum tfe_stream_info type,
	void * value, size_t size);

enum tfe_stream_opt_level
{
	STREAM_OPT_LEVEL_TCP,
	STREAM_OPT_LEVEL_SSL	//see tfe_types.h enum SSL_STREAM_OPT
};
int tfe_stream_set_integer_opt(struct tfe_stream * stream, enum tfe_stream_opt_level level, int type, int val);
/*
@return 0 if successful, or -1 if an error occurred
*/
int tfe_stream_write(const struct tfe_stream * stream, enum tfe_conn_dir dir, const unsigned char * data, size_t len);

struct tfe_stream_write_ctx;
//following tfe_stream_write_xx functions are NOT thread safe, MUST be called in the stream process thread.
struct tfe_stream_write_ctx * tfe_stream_write_frag_start(const struct tfe_stream * stream, enum tfe_conn_dir dir);
/*
@return 0 if successful, or -1 if an error occurred
*/
int tfe_stream_write_frag(struct tfe_stream_write_ctx * w_ctx, const unsigned char * data, size_t size);
void tfe_stream_write_frag_end(struct tfe_stream_write_ctx * w_ctx);

void tfe_stream_detach(const struct tfe_stream * stream);
int tfe_stream_preempt(const struct tfe_stream * stream);
void tfe_stream_suspend(const struct tfe_stream * stream, enum tfe_conn_dir by);
void tfe_stream_resume(const struct tfe_stream * stream);

//close both sides of the stream.
int tfe_stream_shutdown(const struct tfe_stream * stream);
int tfe_stream_shutdown_dir(const struct tfe_stream * stream, enum tfe_conn_dir dir);
void tfe_stream_kill(const struct tfe_stream * stream);

/* stream's cmsg */
struct tfe_cmsg * tfe_stream_get0_cmsg(const struct tfe_stream * stream);
void tfe_stream_cmsg_setup(const struct tfe_stream * stream, struct tfe_cmsg * cmsg);

/**
 * @brief Write linear text for given stream
 */
void tfe_stream_write_access_log(const struct tfe_stream * stream, int level, const char * fmt, ...);
