//go:build windows

package yu_hpsocket

import (
	yu_proxy "gogs.qqck.cn/s/tools/proxy"
	"time"
)

// TcpPackClient
// @Description: TcpPackClientListener 对象，请自己创建和销毁，目的是保证安全
type TcpPackClient struct {
	Client[TcpPackClient]
	TcpClient
}

// NewTcpPackClient
//
//	@Description:  创建 TcpPackClient 对象
func NewTcpPackClient() (t *TcpPackClient) {
	t = &TcpPackClient{}
	t.proxy_info = yu_proxy.NewInfo()
	return
}

// Create
//
//	@Description: 创建 TcpPackClient 环境
func (t *TcpPackClient) Create(SSL bool) bool {
	t.Close()
	if t.hListener, _, _ = Create_HP_TcpPackClientListener.Call(); t.hListener == 0 {
		return false
	}
	if t.SSL = SSL; t.SSL {
		t.hClient, _, _ = Create_HP_SSLPackClient.Call(t.hListener)
	} else {
		t.hClient, _, _ = Create_HP_TcpPackClient.Call(t.hListener)
	}
	if t.hClient == 0 {
		Destroy_HP_TcpPackClientListener.Call(t.hListener)
		t.hListener = 0
		return false
	}
	_client_on_set(t.hClient, &_client_item{
		_type: _client_type_TcpPackClient,
		t:     t,
	})
	return true
}

// Close
//
//	@Description: 销毁 TcpPackClient 环境
func (t *TcpPackClient) Close() {
	for t.GetPendingDataLength() > 0 {
		time.Sleep(time.Microsecond)
	}
	t.Stop()
	t.Wait(-1)
	if t.hClient != 0 {
		if t.SSL {
			Destroy_HP_SSLPackClient.Call(t.hClient)
		} else {
			Destroy_HP_TcpPackClient.Call(t.hClient)
		}
		_client_on_del(t.hClient)
		t.hClient = 0
	}
	if t.hListener != 0 {
		Destroy_HP_TcpPackClientListener.Call(t.hListener)
		t.hListener = 0
	}
}

// SetMaxPackSize
//
//	@Description: 设置数据包最大长度（有效数据包最大长度不能超过 4194303/0x3FFFFF 字节，默认：262144/0x40000）
func (t *TcpPackClient) SetMaxPackSize(MaxPackSize int) *TcpPackClient {
	if !t.isValid() {
		return t
	}
	HP_TcpPackClient_SetMaxPackSize.Call(t.hClient, MaxPackSize)
	return t
}

// SetPackHeaderFlag
//
//	@Description: 设置包头标识（有效包头标识取值范围 0 ~ 1023/0x3FF，当包头标识为 0 时不校验包头，默认：0）
func (t *TcpPackClient) SetPackHeaderFlag(PackHeaderFlag int) *TcpPackClient {
	if !t.isValid() {
		return t
	}
	HP_TcpPackClient_SetPackHeaderFlag.Call(t.hClient, PackHeaderFlag)
	return t
}

// GetMaxPackSize
//
//	@Description: 获取数据包最大长度
func (t *TcpPackClient) GetMaxPackSize() int {
	if !t.isValid() {
		return 0
	}
	return HP_TcpPackClient_GetMaxPackSize.CallInt(t.hClient)
}

// GetPackHeaderFlag
//
//	@Description: 获取包头标识
func (t *TcpPackClient) GetPackHeaderFlag() int {
	if !t.isValid() {
		return 0
	}
	return HP_TcpPackClient_GetPackHeaderFlag.CallInt(t.hClient)
}
