//
// Created by Carl.Guo on 2024/6/28.
//

#ifndef EUSPACES_JAVA_COMMON_H
#define EUSPACES_JAVA_COMMON_H



#define MIN_LEN 13
#define MAX_LEN 1022
//#define MAX_BUFFER_SIZE 65536

/**
 * key  :  7
 * ': ' :  2
 * val  : 123
 * \r\n :  2
 */
#define HEADER_LEN 134


struct sock_t {
	int size;
	int header_offset_idx;
	char payload[MAX_LEN];
};

struct {
	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
	__type(key, int);
	__type(value, struct sock_t);
	__uint(max_entries, 1);
} socket_heap SEC(".maps");

// 定义一个大小为 65536 字节的 Map
struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__type(key, int); // 键类型为int
	__uint(value_size, MAX_BUFFER_SIZE); // 数组的每个值的大小
	__uint(max_entries, 2); // 最大条目数为2
} large_array_map SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
	__type(key, int);
	__type(value, struct sock_t);
	__uint(max_entries, 1);
} socket_res SEC(".maps");


static __always_inline void span_context_to_cw_string_stream(struct apm_span_context *ctx, char *buff, char type_from) {
	// W3C format: version (2 chars) - trace id (32 chars) - span id (16 chars) - sampled (2 chars)
	//	[type_from]:[sample]:[host_id]:[app_id]:[instance_id]:[trace_id]:[assumed_app_i d]:[span_id]
	//	2           2         16        16      16            32         16                16
	char *out = buff;
	*out++ = 'c';
	*out++ = 'w';
	*out++ = 't';
	*out++ = 'r';
	*out++ = 'a';
	*out++ = 'c';
	*out++ = 'e';
	*out++ = ':';
	*out++ = ' ';

	// Write type_from
	*out++ = '0';
	*out++ = type_from;
	*out++ = ':';

	// Write sample
	*out++ = '0';
	*out++ = '0';
	*out++ = ':';

	// Write host_id
	bytes_to_hex_string(ctx->host_id, APM_HOST_ID_SIZE, out);
	out += APM_HOST_ID_STRING_SIZE;
	*out++ = ':';

	// Write app_id
	bytes_to_hex_string(ctx->app_id, APM_APP_ID_SIZE, out);
	out += APM_APP_ID_STRING_SIZE;
	*out++ = ':';


	// Write instance_id
	bytes_to_hex_string(ctx->instance_id, APM_INSTANCE_ID_SIZE, out);
	out += APM_INSTANCE_ID_STRING_SIZE;
	*out++ = ':';

	// Write trace_id
	bytes_to_hex_string(ctx->trace_id, APM_TRACE_ID_SIZE, out);
	out += APM_TRACE_ID_STRING_SIZE;
	*out++ = ':';
	// Write assumed_app_id
	bytes_to_hex_string(ctx->assumed_app_id, APM_ASSUMED_APP_ID_SIZE, out);
	out += APM_ASSUMED_APP_ID_STRING_SIZE;
	*out++ = ':';

	// Write span_id
	bytes_to_hex_string(ctx->span_id, APM_SPAN_ID_SIZE, out);
	out += APM_SPAN_ID_STRING_SIZE;

	*out++ = '\r';
	*out++ = '\n';
	out += 2;

//	// Write trace id
//	bytes_to_hex_string(ctx->TraceID, TRACE_ID_SIZE, out);
//	out += TRACE_ID_STRING_SIZE;
//	*out++ = '-';
//	// Write trace id
//	bytes_to_hex_string(ctx->TraceID, TRACE_ID_SIZE, out);
//	out += TRACE_ID_STRING_SIZE;
//	*out++ = '-';
//	// Write trace id
//	bytes_to_hex_string(ctx->TraceID, TRACE_ID_SIZE, out);
//	out += TRACE_ID_STRING_SIZE;
//	*out++ = '-';
//
//	// Write span id
//	bytes_to_hex_string(ctx->SpanID, SPAN_ID_SIZE, out);
//	out += SPAN_ID_STRING_SIZE;
//	*out++ = '-';

	// Write sampled
//	*out++ = '0';
//	*out = '1';
}

#endif //EUSPACES_JAVA_COMMON_H
