#ifndef __DORIS_HTTP_TRANSFER_H__
#define __DORIS_HTTP_TRANSFER_H__

#include <event2/event.h>  
#include <event.h>

#include <curl/curl.h>

struct doris_http_callback
{
	void *userp;
	void (*header_cb)(const char *ptr, size_t bytes, CURLcode code, long res_code, void *userp); //HEAD/GET不可空
	void (*write_cb)(const char *ptr, size_t bytes, CURLcode code, long res_code, void *userp);  //GET时不为空；其他为空时不回调
	void (*read_process_cb)(void *data, size_t data_offset, void *userp); //PUT时，为空时不回调
	void (*transfer_done_cb)(CURLcode res, long res_code, const char *err, void *userp); //不可空
};

struct doris_http_ctx
{
	struct doris_curl_multihd *multidata;
	CURL *curl;
	struct curl_slist *headers;
	char error[CURL_ERROR_SIZE];	
	struct doris_http_callback cb;
	CURLcode res;
	long res_code;
	int32_t transfering;

	char *put_data;
	struct evbuffer *put_evbuf;
	size_t put_length;
	size_t put_offset;
	struct doris_http_instance *instance;
};

struct dst_host_cnn_balance;
struct doris_curl_multihd
{
	struct dst_host_cnn_balance *host;
	volatile long long sessions;     //当前正在进行GET/PUT的会话数
	CURLM *multi_hd;
	struct event timer_event;        //curl动作的定时器
	struct event_base* evbase;
};

struct doris_curl_multihd *doris_initialize_multihd_for_host(struct doris_http_instance *instance, struct dst_host_cnn_balance *host);
struct doris_http_ctx *doris_http_ctx_new(struct doris_http_instance *instance, struct doris_http_callback *cb, u_int64_t balance_seed, char *host, int32_t size);
void doris_http_ctx_destroy(struct doris_http_ctx *ctx);
void doris_http_ctx_reset(struct doris_http_ctx *ctx, struct doris_http_callback *cb);

void doris_http_ctx_add_header(struct doris_http_ctx *ctx, const char *header);

int doris_http_launch_get_request(struct doris_http_ctx *ctx, const char *uri);
int doris_http_launch_post_request(struct doris_http_ctx *ctx, const char *uri, const char *data, size_t data_len);
long long caculate_http_sessions_sum(const struct doris_http_instance *instance);


struct curl_socket_data
{
	struct event sock_event;
};

#endif

