//go:build windows

package yu_hpsocket

import (
	yu_http "gogs.qqck.cn/s/tools/http"
	yu_strings "gogs.qqck.cn/s/tools/strings"
	yu_sys "gogs.qqck.cn/s/tools/sys"
	"unsafe"
)

// HttpServer
// @Description: HttpServerListener 对象，请自己创建和销毁，目的是保证安全
type HttpServer struct {
	Server[HttpServer]
	TcpServer

	// 事件----------------------------------
	onMessageBegin      func(t *HttpServer, Sender, ConnID uintptr) HPR
	onRequestLine       func(t *HttpServer, Sender, ConnID uintptr, Method, Url string) HPR
	onHeader            func(t *HttpServer, Sender, ConnID uintptr, Name, Value string) HPR
	onHeadersComplete   func(t *HttpServer, Sender, ConnID uintptr) HPR
	onBody              func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HPR
	onChunkHeader       func(t *HttpServer, Sender, ConnID uintptr, Length uintptr) HPR
	onChunkComplete     func(t *HttpServer, Sender, ConnID uintptr) HPR
	onMessageComplete   func(t *HttpServer, Sender, ConnID uintptr) HPR
	onUpgrade           func(t *HttpServer, Sender, ConnID uintptr, UpgradeType HUT) HPR
	onParseError        func(t *HttpServer, Sender, ConnID uintptr, ErrorCode int, ErrorDesc string) HPR
	onWSMessageHeader   func(t *HttpServer, Sender, ConnID uintptr, Final bool, Reserved, OperationCode byte, Mask []byte, BodyLen int64) HR
	onWSMessageBody     func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR
	onWSMessageComplete func(t *HttpServer, Sender, ConnID uintptr) HR
}

// NewHttpServer
//
//	@Description:  创建 HttpServer 对象
func NewHttpServer() (t *HttpServer) {
	t = &HttpServer{}
	return
}

// Create
//
//	@Description: 创建 HttpServer 环境
func (t *HttpServer) Create(SSL bool) bool {
	t.Close()
	if t.hListener, _, _ = Create_HP_HttpServerListener.Call(); t.hListener == 0 {
		return false
	}
	if t.SSL = SSL; t.SSL {
		t.hServer, _, _ = Create_HP_HttpsServer.Call(t.hListener)
	} else {
		t.hServer, _, _ = Create_HP_HttpServer.Call(t.hListener)
	}
	if t.hServer == 0 {
		Destroy_HP_HttpServerListener.Call(t.hListener)
		t.hListener = 0
		return false
	}
	_server_on_set(t.hServer, &_server_item{
		_type: _server_type_HttpServer,
		t:     t,
	})

	HP_Set_FN_HttpServer_OnHeader.Call(t.hListener, server_http_callback_OnHeader_ptr) // 默认绑定，OnHeader 有问题，会内存溢出

	return true
}

// Close
//
//	@Description: 销毁 HttpServer 环境
func (t *HttpServer) Close() {
	t.Stop()
	t.Wait(-1)
	if t.hServer != 0 {
		if t.SSL {
			Destroy_HP_HttpsServer.Call(t.hServer)
		} else {
			Destroy_HP_HttpServer.Call(t.hServer)
		}
		_server_on_del(t.hServer)
		t.hServer = 0
	}
	if t.hListener != 0 {
		Destroy_HP_HttpServerListener.Call(t.hListener)
		t.hListener = 0
	}
}

// SendResponse
//
//	@Description: 向客户端回复 HTTP 请求
//	@param ConnID 连接 ID
//	@param StatusCode HTTP 状态码
//	@param Desc HTTP 状态描述,空为默认
//	@param Headers 回复请求头
//	@param Data 回复请求体
func (t *HttpServer) SendResponse(ConnID uintptr, StatusCode yu_http.Status, Desc string, Headers []TNVPair, Data []byte) bool {
	if !t.isValid() {
		return false
	}
	if Desc == "" {
		Desc = yu_http.StatusText(StatusCode)
	}
	j_headers_buf := make([][2][]byte, len(Headers))
	j_headers := make([][2]unsafe.Pointer, len(Headers))
	var j_headers_ptr unsafe.Pointer
	if len(Headers) > 0 {
		for j_i := 0; j_i < len(Headers); j_i++ {
			j_headers_buf[j_i][0] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Name)
			j_headers[j_i][0] = unsafe.Pointer(&j_headers_buf[j_i][0][0])

			j_headers_buf[j_i][1] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Value)
			j_headers[j_i][1] = unsafe.Pointer(&j_headers_buf[j_i][1][0])
		}
		j_headers_ptr = unsafe.Pointer(&j_headers[0])
	}
	return HP_HttpServer_SendResponse.CallBool(t.hServer, ConnID,
		uintptr(StatusCode), yu_sys.S{yu_sys.Gbk, Desc},
		j_headers_ptr, len(Headers), Data, len(Data),
	)
}

// SendResponseS
//
//	@Description: 向客户端回复 HTTP 请求
//	@param ConnID 连接 ID
//	@param StatusCode HTTP 状态码
//	@param Desc HTTP 状态描述,空为默认
//	@param Headers 回复请求头
//	@param Data 回复请求体
func (t *HttpServer) SendResponseS(ConnID uintptr, StatusCode yu_http.Status, Desc string, Headers []TNVPair, Data string) bool {
	if !t.isValid() {
		return false
	}
	if Desc == "" {
		Desc = yu_http.StatusText(StatusCode)
	}
	j_headers_buf := make([][2][]byte, len(Headers))
	j_headers := make([][2]unsafe.Pointer, len(Headers))
	var j_headers_ptr unsafe.Pointer
	if len(Headers) > 0 {
		for j_i := 0; j_i < len(Headers); j_i++ {
			j_headers_buf[j_i][0] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Name)
			j_headers[j_i][0] = unsafe.Pointer(&j_headers_buf[j_i][0][0])

			j_headers_buf[j_i][1] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Value)
			j_headers[j_i][1] = unsafe.Pointer(&j_headers_buf[j_i][1][0])
		}
		j_headers_ptr = unsafe.Pointer(&j_headers[0])
	}
	return HP_HttpServer_SendResponse.CallBool(t.hServer, ConnID,
		uintptr(StatusCode), yu_sys.S{yu_sys.Gbk, Desc},
		j_headers_ptr, len(Headers), Data, len(Data),
	)
}

// SendChunkData
//
//	@Description: 向对端发送 Chunked 数据分片
//	@param ConnID 连接 ID
//	@param Data Chunked 数据分片（为 nil 或 len(data)=0 表示结束分片）
//	@param Extensions 扩展属性
func (t *HttpServer) SendChunkData(ConnID uintptr, Data []byte, Extensions int) bool {
	if !t.isValid() {
		return false
	}
	return HP_HttpServer_SendChunkData.CallBool(t.hServer, ConnID, Data, len(Data), Extensions)
}

// SendChunkDataS
//
//	@Description: 向对端发送 Chunked 数据分片
//	@param ConnID 连接 ID
//	@param Data Chunked 数据分片（为 nil 或 len(data)=0 表示结束分片）
//	@param Extensions 扩展属性
func (t *HttpServer) SendChunkDataS(ConnID uintptr, Data string, Extensions int) bool {
	if !t.isValid() {
		return false
	}
	return HP_HttpServer_SendChunkData.CallBool(t.hServer, ConnID, Data, len(Data), Extensions)
}

// SendLocalFile
//
//	@Description: 向指定连接发送 4096 KB 以下的小文件
//	@param ConnID 连接 ID
//	@param FileName 文件路径
//	@param StatusCode HTTP 状态码
//	@param Desc HTTP 状态描述,空为默认
//	@param Headers 回复请求头
func (t *HttpServer) SendLocalFile(ConnID uintptr, FileName string, StatusCode yu_http.Status, Desc string, Headers []TNVPair) bool {
	if !t.isValid() {
		return false
	}
	if Desc == "" {
		Desc = yu_http.StatusText(StatusCode)
	}
	j_headers_buf := make([][2][]byte, len(Headers))
	j_headers := make([][2]unsafe.Pointer, len(Headers))
	var j_headers_ptr unsafe.Pointer
	if len(Headers) > 0 {
		for j_i := 0; j_i < len(Headers); j_i++ {
			j_headers_buf[j_i][0] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Name)
			j_headers[j_i][0] = unsafe.Pointer(&j_headers_buf[j_i][0][0])

			j_headers_buf[j_i][1] = yu_strings.Utf8ToGbkBytes(Headers[j_i].Value)
			j_headers[j_i][1] = unsafe.Pointer(&j_headers_buf[j_i][1][0])
		}
		j_headers_ptr = unsafe.Pointer(&j_headers[0])
	}
	return HP_HttpServer_SendLocalFile.CallBool(t.hServer, ConnID,
		yu_sys.S{yu_sys.Gbk, FileName},
		uintptr(StatusCode), yu_sys.S{yu_sys.Gbk, Desc},
		j_headers_ptr, len(Headers),
	)
}

// SendWSMessage
//
//	@Description: 向对端端发送 WebSocket 消息
//	@param ConnID 连接 ID
//	@param Final 是否结束帧
//	@param Reserved RSV1/RSV2/RSV3 各 1 位
//	@param OperationCode 操作码：0x0 - 0xF
//	@param Data 消息体数据缓冲区
//	@param BodyLen 消息总长度
func (t *HttpServer) SendWSMessage(ConnID uintptr, Final bool, Reserved, OperationCode byte, Data []byte, BodyLen int64) bool {
	if !t.isValid() {
		return false
	}
	return HP_HttpServer_SendWSMessage.CallBool(t.hServer, ConnID, Final, Reserved, OperationCode, Data, len(Data), BodyLen)
}

// SendWSMessageS
//
//	@Description: 向对端端发送 WebSocket 消息
//	@param ConnID 连接 ID
//	@param Final 是否结束帧
//	@param Reserved RSV1/RSV2/RSV3 各 1 位
//	@param OperationCode 操作码：0x0 - 0xF
//	@param Data 消息体数据缓冲区
//	@param BodyLen 消息总长度
func (t *HttpServer) SendWSMessageS(ConnID uintptr, Final bool, Reserved, OperationCode byte, Data string, BodyLen int64) bool {
	if !t.isValid() {
		return false
	}
	return HP_HttpServer_SendWSMessage.CallBool(t.hServer, ConnID, Final, Reserved, OperationCode, Data, len(Data), BodyLen)
}

// Release
//
//	@Description: 把连接放入释放队列，等待某个时间（通过 SetReleaseDelay() 设置）关闭连接
//	@param ConnID 连接 ID
func (t *HttpServer) Release(ConnID uintptr) bool {
	if !t.isValid() {
		return false
	}
	return HP_HttpServer_Release.CallBool(t.hServer, ConnID)
}

// SetReleaseDelay
//
//	@Description: 设置连接释放延时（默认：3000 毫秒）
func (t *HttpServer) SetReleaseDelay(ReleaseDelay int) *HttpServer {
	if !t.isValid() {
		return t
	}
	HP_HttpServer_SetReleaseDelay.Call(t.hServer, ReleaseDelay)
	return t
}

// GetReleaseDelay
//
//	@Description: 获取连接释放延时
func (t *HttpServer) GetReleaseDelay() int {
	if !t.isValid() {
		return 0
	}
	return HP_HttpServer_GetReleaseDelay.CallInt(t.hServer)
}

// GetUrlFieldSet
//
//	@Description: 获取请求行 URL 域掩码（URL 域参考：EnHttpUrlField）
//	@param ConnID 连接 ID
func (t *HttpServer) GetUrlFieldSet(ConnID uintptr) HUF {
	if !t.isValid() {
		return 0
	}
	return HUF(HP_HttpServer_GetUrlFieldSet.CallUintptr(t.hServer, ConnID))
}

// GetUrlField
//
//	@Description: 获取某个 URL 域值
//	@param ConnID 连接 ID
//	@param Field HUF_ 开头常量
func (t *HttpServer) GetUrlField(ConnID uintptr, Field HUF) string {
	if !t.isValid() {
		return ""
	}
	j_value := HP_HttpServer_GetUrlField.CallGbkToUtf8(t.hServer, ConnID, uintptr(Field))
	if Field == HUF_PATH {
		if j_value == "" {
			j_value = "/"
		} else if j_value[len(j_value)-1] != '/' {
			j_value += "/"
		} else {
			for j_i := len(j_value) - 1; j_i > -1; j_i-- {
				if j_value[j_i] == '/' {
					j_value = j_value[:j_i+1]
				} else {
					break
				}
			}
		}
	}
	return j_value
}

// GetMethod
//
//	@Description: 获取请求方法
//	@param ConnID v
func (t *HttpServer) GetMethod(ConnID uintptr) string {
	if !t.isValid() {
		return ""
	}
	return HP_HttpServer_GetMethod.CallGbkToUtf8(t.hServer, ConnID)
}

// SetLocalVersion
//
//	@Description: 设置本地协议版本
func (t *HttpServer) SetLocalVersion(Version HV) *HttpServer {
	if !t.isValid() {
		return t
	}
	HP_HttpServer_SetLocalVersion.Call(t.hServer, uintptr(Version))
	return t
}

// GetLocalVersion
//
//	@Description: 获取本地协议版本
func (t *HttpServer) GetLocalVersion() HV {
	if !t.isValid() {
		return 0
	}
	return HV(HP_HttpServer_GetLocalVersion.CallUintptr(t.hServer))
}

// IsUpgrade
//
//	@Description: 检查是否升级协议
//	@param ConnID 连接 ID
func (t *HttpServer) IsUpgrade(ConnID uintptr) bool {
	if !t.isValid() {
		return false
	}
	return HP_HttpServer_IsUpgrade.CallBool(t.hServer, ConnID)
}

// IsKeepAlive
//
//	@Description: 检查是否有 Keep-Alive 标识
//	@param ConnID 连接 ID
func (t *HttpServer) IsKeepAlive(ConnID uintptr) bool {
	if !t.isValid() {
		return false
	}
	return HP_HttpServer_IsKeepAlive.CallBool(t.hServer, ConnID)
}

// StartHttp
//
//	@Description: 启动 HTTP 通信,当通信组件设置为非自动启动 HTTP 通信时，需要调用本方法启动 HTTP 通信
//	@param ConnID 连接 ID
func (t *HttpServer) StartHttp(ConnID uintptr) bool {
	if !t.isValid() {
		return false
	}
	return HP_HttpServer_StartHttp.CallBool(t.hServer, ConnID)
}

// SetHttpAutoStart
//
//	@Description: 设置 HTTP 启动方式（默认：TRUE，自动启动）
func (t *HttpServer) SetHttpAutoStart(AutoStart bool) *HttpServer {
	if !t.isValid() {
		return t
	}
	HP_HttpServer_SetHttpAutoStart.Call(t.hServer, AutoStart)
	return t
}

// IsHttpAutoStart
//
//	@Description: 获取 HTTP 启动方式
func (t *HttpServer) IsHttpAutoStart() bool {
	if !t.isValid() {
		return false
	}
	return HP_HttpServer_IsHttpAutoStart.CallBool(t.hServer)
}

// GetVersion
//
//	@Description: 获取协议版本
//	@param ConnID ConnID 连接 ID
func (t *HttpServer) GetVersion(ConnID uintptr) HV {
	if !t.isValid() {
		return 0
	}
	return HV(HP_HttpServer_GetVersion.CallUintptr(t.hServer, ConnID))
}

// GetHost
//
//	@Description: 获取主机
//	@param ConnID 连接 ID
func (t *HttpServer) GetHost(ConnID uintptr) string {
	if !t.isValid() {
		return ""
	}
	return HP_HttpServer_GetHost.CallGbkToUtf8(t.hServer, ConnID)
}

// GetContentLength
//
//	@Description: 获取内容长度
//	@param ConnID 连接 ID
func (t *HttpServer) GetContentLength(ConnID uintptr) int64 {
	if !t.isValid() {
		return 0
	}
	return HP_HttpServer_GetContentLength.CallInt64(t.hServer, ConnID)
}

// GetContentType
//
//	@Description: 获取内容类型
//	@param ConnID 连接 ID
func (t *HttpServer) GetContentType(ConnID uintptr) string {
	if !t.isValid() {
		return ""
	}
	return HP_HttpServer_GetContentType.CallGbkToUtf8(t.hServer, ConnID)
}

// GetContentEncoding
//
//	@Description: 获取内容编码
//	@param ConnID 连接 ID
func (t *HttpServer) GetContentEncoding(ConnID uintptr) string {
	if !t.isValid() {
		return ""
	}
	return HP_HttpServer_GetContentEncoding.CallGbkToUtf8(t.hServer, ConnID)
}

// GetTransferEncoding
//
//	@Description: 获取传输编码
//	@param ConnID 连接 ID
func (t *HttpServer) GetTransferEncoding(ConnID uintptr) string {
	if !t.isValid() {
		return ""
	}
	return HP_HttpServer_GetTransferEncoding.CallGbkToUtf8(t.hServer, ConnID)
}

// GetUpgradeType
//
//	@Description: 获取协议升级类型
//	@param ConnID 连接 ID
func (t *HttpServer) GetUpgradeType(ConnID uintptr) HUT {
	if !t.isValid() {
		return 0
	}
	return HUT(HP_HttpServer_GetUpgradeType.CallInt(t.hServer, ConnID))
}

// GetParseErrorCode
//
//	@Description: 获取解析错误代码
//	@param ConnID 连接 ID
//	@return ErrorCode
//	@return ErrorDesc
func (t *HttpServer) GetParseErrorCode(ConnID uintptr) (ErrorCode int, ErrorDesc string) {
	if !t.isValid() {
		return
	}
	var j_ErrorDesc uintptr
	ErrorCode = HP_HttpServer_GetParseErrorCode.CallInt(t.hServer, ConnID, &j_ErrorDesc)
	ErrorDesc = yu_strings.GbkPtrToUtf8(j_ErrorDesc)
	return
}

// GetHeader
//
//	@Description: 获取某个请求头（单值）
//	@param ConnID 连接 ID
func (t *HttpServer) GetHeader(ConnID uintptr, Name string) string {
	if !t.isValid() {
		return ""
	}
	var j_value uintptr
	if !HP_HttpServer_GetHeader.CallBool(t.hServer, ConnID,
		yu_sys.S{yu_sys.Gbk, Name}, &j_value,
	) {
		return ""
	}
	return yu_strings.GbkPtrToUtf8(j_value)
}

// GetHeaders
//
//	@Description: 获取某个请求头（多值）
//	@param ConnID 连接 ID
func (t *HttpServer) GetHeaders(ConnID uintptr, Name string) (values []string) {
	if !t.isValid() {
		return
	}
	var j_count int
	HP_HttpServer_GetHeaders.Call(t.hServer, ConnID, yu_sys.S{yu_sys.Gbk, Name}, 0, &j_count)
	if j_count == 0 {
		return
	}
	j_values := make([]uintptr, j_count)
	if !HP_HttpServer_GetHeaders.CallBool(t.hServer, ConnID, yu_sys.S{yu_sys.Gbk, Name}, j_values, &j_count) {
		return
	}
	values = make([]string, 0, j_count)
	for _, j_value := range j_values {
		values = append(values, yu_strings.GbkPtrToUtf8(j_value))
	}
	return
}

// GetAllHeaders
//
//	@Description: 取得所有HTTP返回协议头
//	@param ConnID 连接 ID
func (t *HttpServer) GetAllHeaders(ConnID uintptr) (values []TNVPair) {
	if !t.isValid() {
		return
	}
	var j_count int
	HP_HttpServer_GetAllHeaders.Call(t.hServer, ConnID, 0, &j_count)
	if j_count == 0 {
		return
	}
	j_values := make([][2]uintptr, j_count)
	if !HP_HttpServer_GetAllHeaders.CallBool(t.hServer, ConnID, unsafe.Pointer(&j_values[0]), &j_count) {
		return
	}
	values = make([]TNVPair, j_count)
	for j_i := 0; j_i < j_count; j_i++ {
		values[j_i].Name = yu_strings.GbkPtrToUtf8(j_values[j_i][0])
		values[j_i].Value = yu_strings.GbkPtrToUtf8(j_values[j_i][1])
	}
	return
}

// GetAllHeaderNames
//
//	@Description: 获取所有请求头名称
//	@param ConnID 连接 ID
func (t *HttpServer) GetAllHeaderNames(ConnID uintptr) (values []string) {
	if !t.isValid() {
		return
	}
	var j_count int
	HP_HttpServer_GetAllHeaderNames.Call(t.hServer, ConnID, 0, &j_count)
	if j_count == 0 {
		return
	}
	j_values := make([]uintptr, j_count)
	if !HP_HttpServer_GetAllHeaderNames.CallBool(t.hServer, ConnID, j_values, &j_count) {
		return
	}
	values = make([]string, 0, j_count)
	for _, j_value := range j_values {
		values = append(values, yu_strings.GbkPtrToUtf8(j_value))
	}
	return
}

// GetCookie
//
//	@Description: 获取 Cookie
//	@param ConnID 连接 ID
func (t *HttpServer) GetCookie(ConnID uintptr, Name string) string {
	if !t.isValid() {
		return ""
	}
	var j_value uintptr
	if !HP_HttpServer_GetCookie.CallBool(t.hServer, ConnID,
		yu_sys.S{yu_sys.Gbk, Name}, &j_value,
	) {
		return ""
	}
	return yu_strings.GbkPtrToUtf8(j_value)
}

// GetAllCookies
//
//	@Description: 获取所有 Cookie
//	@param ConnID 连接 ID
func (t *HttpServer) GetAllCookies(ConnID uintptr) (values []TNVPair) {
	if !t.isValid() {
		return
	}
	var j_count int
	HP_HttpServer_GetAllCookies.Call(t.hServer, ConnID, 0, &j_count)
	if j_count == 0 {
		return
	}
	j_values := make([][2]uintptr, j_count)
	if !HP_HttpServer_GetAllCookies.CallBool(t.hServer, ConnID, unsafe.Pointer(&j_values[0]), &j_count) {
		return
	}
	values = make([]TNVPair, j_count)
	for j_i := 0; j_i < j_count; j_i++ {
		values[j_i].Name = yu_strings.GbkPtrToUtf8(j_values[j_i][0])
		values[j_i].Value = yu_strings.GbkPtrToUtf8(j_values[j_i][1])
	}
	return
}

// GetWSMessageState
//
//	@Description: 获取当前 WebSocket 消息状态
//	@param ConnID 连接 ID
func (t *HttpServer) GetWSMessageState(ConnID uintptr) (Final bool, Reserved, OperationCode byte, Mask [4]byte, BodyLen int64, BodyRemain int64) {
	var j_Mask uintptr
	if !HP_HttpServer_GetWSMessageState.CallBool(t.hServer, ConnID, &Final, &Reserved, &OperationCode, &j_Mask, &BodyLen, &BodyRemain) {
		return
	}
	*((*uintptr)(unsafe.Pointer(&Mask))) = j_Mask
	return
}

// OnMessageBegin
//
//	@Description: 【可选】绑定开始解析事件
//	@param call->return HPR_ 开头常量
func (t *HttpServer) OnMessageBegin(call func(t *HttpServer, Sender, ConnID uintptr) HPR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnMessageBegin.Call(t.hListener, 0)
	} else {
		t.onMessageBegin = call
		HP_Set_FN_HttpServer_OnMessageBegin.Call(t.hListener, server_http_callback_OnMessageBegin_ptr)
	}
	return t
}

// OnRequestLine
//
//	@Description: 【可选】绑定请求行解析完成事件
//	@param call->return HPR_ 开头常量
func (t *HttpServer) OnRequestLine(call func(t *HttpServer, Sender, ConnID uintptr, Method, Url string) HPR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnRequestLine.Call(t.hListener, 0)
	} else {
		t.onRequestLine = call
		HP_Set_FN_HttpServer_OnRequestLine.Call(t.hListener, server_http_callback_OnRequestLine_ptr)
	}
	return t
}

// _OnHeader
//
//	@Description: 【可选】绑定请求头事件
//	@param call->return HPR_ 开头常量
func (t *HttpServer) _OnHeader(call func(t *HttpServer, Sender, ConnID uintptr, Name, Value string) HPR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnHeader.Call(t.hListener, 0)
	} else {
		t.onHeader = call
		HP_Set_FN_HttpServer_OnHeader.Call(t.hListener, server_http_callback_OnHeader_ptr)
	}
	return t
}

// OnHeadersComplete
//
//	@Description: 【必选】绑定请求头完成事件
//	@param call->return HPR_ 开头常量
func (t *HttpServer) OnHeadersComplete(call func(t *HttpServer, Sender, ConnID uintptr) HPR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnHeadersComplete.Call(t.hListener, 0)
	} else {
		t.onHeadersComplete = call
		HP_Set_FN_HttpServer_OnHeadersComplete.Call(t.hListener, server_http_callback_OnHeadersComplete_ptr)
	}
	return t
}

// OnBody
//
//	@Description: 【必选】绑定请求体报文事件
//	@param call->return HPR_ 开头常量
func (t *HttpServer) OnBody(call func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HPR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnBody.Call(t.hListener, 0)
	} else {
		t.onBody = call
		HP_Set_FN_HttpServer_OnBody.Call(t.hListener, server_http_callback_OnBody_ptr)
	}
	return t
}

// OnChunkHeader
//
//	@Description: 【可选】绑定Chunked 报文头事件
//	@param call->return HPR_ 开头常量
func (t *HttpServer) OnChunkHeader(call func(t *HttpServer, Sender, ConnID uintptr, Length uintptr) HPR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnChunkHeader.Call(t.hListener, 0)
	} else {
		t.onChunkHeader = call
		HP_Set_FN_HttpServer_OnChunkHeader.Call(t.hListener, server_http_callback_OnChunkHeader_ptr)
	}
	return t
}

// OnChunkComplete
//
//	@Description: 【可选】绑定Chunked 报文完成事件
//	@param call->return HPR_ 开头常量
func (t *HttpServer) OnChunkComplete(call func(t *HttpServer, Sender, ConnID uintptr) HPR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnChunkComplete.Call(t.hListener, 0)
	} else {
		t.onChunkComplete = call
		HP_Set_FN_HttpServer_OnChunkComplete.Call(t.hListener, server_http_callback_OnChunkComplete_ptr)
	}
	return t
}

// OnMessageComplete
//
//	@Description: 【必选】绑定完成解析事件
//	@param call->return HPR_ 开头常量
func (t *HttpServer) OnMessageComplete(call func(t *HttpServer, Sender, ConnID uintptr) HPR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnMessageComplete.Call(t.hListener, 0)
	} else {
		t.onMessageComplete = call
		HP_Set_FN_HttpServer_OnMessageComplete.Call(t.hListener, server_http_callback_OnMessageComplete_ptr)
	}
	return t
}

// OnUpgrade
//
//	@Description: 【可选】绑定升级协议事件
//	@param call->return HPR_ 开头常量
func (t *HttpServer) OnUpgrade(call func(t *HttpServer, Sender, ConnID uintptr, UpgradeType HUT) HPR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnUpgrade.Call(t.hListener, 0)
	} else {
		t.onUpgrade = call
		HP_Set_FN_HttpServer_OnUpgrade.Call(t.hListener, server_http_callback_OnUpgrade_ptr)
	}
	return t
}

// OnParseError
//
//	@Description: 【必选】绑定解析错误事件
//	@param call->return HPR_ 开头常量
func (t *HttpServer) OnParseError(call func(t *HttpServer, Sender, ConnID uintptr, ErrorCode int, ErrorDesc string) HPR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnParseError.Call(t.hListener, 0)
	} else {
		t.onParseError = call
		HP_Set_FN_HttpServer_OnParseError.Call(t.hListener, server_http_callback_OnParseError_ptr)
	}
	return t
}

// OnWSMessageHeader
//
//	@Description: 【可选】绑定WebSocket 数据包头事件
//	@param call->return HR_ 开头常量
func (t *HttpServer) OnWSMessageHeader(call func(t *HttpServer, Sender, ConnID uintptr, Final bool, Reserved, OperationCode byte, Mask []byte, BodyLen int64) HR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnWSMessageHeader.Call(t.hListener, 0)
	} else {
		t.onWSMessageHeader = call
		HP_Set_FN_HttpServer_OnWSMessageHeader.Call(t.hListener, server_http_callback_OnWSMessageHeader_ptr)
	}
	return t
}

// OnWSMessageBody
//
//	@Description: 【可选】绑定WebSocket 数据包体事件
//	@param call->return HR_ 开头常量
func (t *HttpServer) OnWSMessageBody(call func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnWSMessageBody.Call(t.hListener, 0)
	} else {
		t.onWSMessageBody = call
		HP_Set_FN_HttpServer_OnWSMessageBody.Call(t.hListener, server_http_callback_OnWSMessageBody_ptr)
	}
	return t
}

// OnWSMessageComplete
//
//	@Description: 【可选】绑定WebSocket 数据包完成事件
//	@param call->return HR_ 开头常量
func (t *HttpServer) OnWSMessageComplete(call func(t *HttpServer, Sender, ConnID uintptr) HR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnWSMessageComplete.Call(t.hListener, 0)
	} else {
		t.onWSMessageComplete = call
		HP_Set_FN_HttpServer_OnWSMessageComplete.Call(t.hListener, server_http_callback_OnWSMessageComplete_ptr)
	}
	return t
}

// OnPrepareListen
//
//	@Description: 【可选】绑定准备监听事件
//	@param call->return HR_ 开头常量
func (t *HttpServer) OnPrepareListen(call func(t *HttpServer, Sender, Listen uintptr) HR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnPrepareListen.Call(t.hListener, 0)
	} else {
		t.onPrepareListen = call
		HP_Set_FN_HttpServer_OnPrepareListen.Call(t.hListener, server_callback_OnPrepareListen_ptr)
	}
	return t
}

// OnAccept
//
//	@Description: 【可选】绑定接受事件
//	@param call->return HR_ 开头常量
func (t *HttpServer) OnAccept(call func(t *HttpServer, Sender, ConnID, Client uintptr) HR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnAccept.Call(t.hListener, 0)
	} else {
		t.onAccept = call
		HP_Set_FN_HttpServer_OnAccept.Call(t.hListener, server_callback_OnAccept_ptr)
	}
	return t
}

// OnHandShake
//
//	@Description: 【可选】绑定握手事件
//	@param call->return HR_ 开头常量
func (t *HttpServer) OnHandShake(call func(t *HttpServer, Sender, ConnID uintptr) HR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnHandShake.Call(t.hListener, 0)
	} else {
		t.onHandShake = call
		HP_Set_FN_HttpServer_OnHandShake.Call(t.hListener, server_callback_OnHandShake_ptr)
	}
	return t
}

// OnSend
//
//	@Description: 【可选】绑定发送事件
//	@param call->return HR_ 开头常量
func (t *HttpServer) OnSend(call func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnSend.Call(t.hListener, 0)
	} else {
		t.onSend = call
		HP_Set_FN_HttpServer_OnSend.Call(t.hListener, server_callback_OnSend_ptr)
	}
	return t
}

// OnReceive
//
//	@Description: 【必选】绑定数据接收事件
//	@param call->return HR_ 开头常量
func (t *HttpServer) OnReceive(call func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnReceive.Call(t.hListener, 0)
	} else {
		t.onReceive = call
		HP_Set_FN_HttpServer_OnReceive.Call(t.hListener, server_callback_OnReceive_ptr)
	}
	return t
}

// OnClose
//
//	@Description: 【必选】绑定断开事件
//	@param call->Operation 通过该参数标识是哪种操作导致的错误 SO_ 开头常量
//	@param call->return HR_ 开头常量
func (t *HttpServer) OnClose(call func(t *HttpServer, Sender, ConnID uintptr, Operation SO, ErrorCode int) HR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnClose.Call(t.hListener, 0)
	} else {
		t.onClose = call
		HP_Set_FN_HttpServer_OnClose.Call(t.hListener, server_callback_OnClose_ptr)
	}
	return t
}

// OnShutdown
//
//	@Description: 【可选】绑定关闭事件
//	@param call->return HR_ 开头常量
func (t *HttpServer) OnShutdown(call func(t *HttpServer, Sender uintptr) HR) *HttpServer {
	if !t.isValid() {
		return t
	}
	if call == nil {
		HP_Set_FN_HttpServer_OnShutdown.Call(t.hListener, 0)
	} else {
		t.onShutdown = call
		HP_Set_FN_HttpServer_OnShutdown.Call(t.hListener, server_callback_OnShutdown_ptr)
	}
	return t
}

// OnDefault
//
//	@Description: 【可选】绑定所有【必选】事件
func (t *HttpServer) OnDefault() *HttpServer {
	if !t.isValid() {
		return t
	}
	if t.onHeadersComplete == nil {
		t.OnHeadersComplete(func(t *HttpServer, Sender, ConnID uintptr) HPR {
			return HPR_OK
		})
	}
	if t.onBody == nil {
		t.OnBody(func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HPR {
			return HPR_OK
		})
	}
	if t.onMessageComplete == nil {
		t.OnMessageComplete(func(t *HttpServer, Sender, ConnID uintptr) HPR {
			return HPR_OK
		})
	}
	if t.onParseError == nil {
		t.OnParseError(func(t *HttpServer, Sender, ConnID uintptr, ErrorCode int, ErrorDesc string) HPR {
			return HPR_OK
		})
	}
	if t.onReceive == nil {
		t.OnReceive(func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR {
			return HR_OK
		})
	}
	if t.onClose == nil {
		t.OnClose(func(t *HttpServer, Sender, ConnID uintptr, Operation SO, ErrorCode int) HR {
			return HR_OK
		})
	}
	return t
}

// --------------------------------------------------------------------------------------------------------------[TNVPairs]

// TNVPair 描述：字符串名值对结构体
type TNVPair struct {
	Name  string
	Value string
}
