//go:build windows

package yu_hpsocket

import yu_proxy "gogs.qqck.cn/s/tools/proxy"

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

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

// Create
//
//	@Description: 创建 TcpClient 环境
func (t *TcpClient) Create(SSL bool) bool {
	t.Close()
	if t.hListener, _, _ = Create_HP_TcpClientListener.Call(); t.hListener == 0 {
		return false
	}
	if t.SSL = SSL; t.SSL {
		t.hClient, _, _ = Create_HP_SSLClient.Call(t.hListener)
	} else {
		t.hClient, _, _ = Create_HP_TcpClient.Call(t.hListener)
	}
	if t.hClient == 0 {
		Destroy_HP_TcpClientListener.Call(t.hListener)
		t.hListener = 0
		return false
	}
	_client_on_set(t.hClient, &_client_item{
		_type: _client_type_TcpClient,
		t:     t,
	})
	return true
}

// Close
//
//	@Description: 销毁 TcpClient 环境
func (t *TcpClient) Close() {
	t.Stop()
	t.Wait(-1)
	if t.hClient != 0 {
		if t.SSL {
			Destroy_HP_SSLClient.Call(t.hClient)
		} else {
			Destroy_HP_TcpClient.Call(t.hClient)
		}
		_client_on_del(t.hClient)
		t.hClient = 0
	}
	if t.hListener != 0 {
		Destroy_HP_TcpClientListener.Call(t.hListener)
		t.hListener = 0
	}
}

// SetSocketBufferSize
//
//	@Description: 设置通信数据缓冲区大小（根据平均通信数据包大小调整设置，通常设置为：(N * 1024) - sizeof(TBufferObj)）
func (t *TcpClient) SetSocketBufferSize(SocketBufferSize int) *TcpClient {
	if !t.isValid() {
		return t
	}
	HP_TcpClient_SetSocketBufferSize.Call(t.hClient, SocketBufferSize)
	return t
}

// SetKeepAliveTime
//
//	@Description: 设置正常心跳包间隔（毫秒，0 则不发送心跳包，默认：60 * 1000）
func (t *TcpClient) SetKeepAliveTime(KeepAliveTime int) *TcpClient {
	if !t.isValid() {
		return t
	}
	HP_TcpClient_SetKeepAliveTime.Call(t.hClient, KeepAliveTime)
	return t
}

// SetKeepAliveInterval
//
//	@Description: 设置异常心跳包间隔（毫秒，0 不发送心跳包，，默认：20 * 1000，如果超过若干次 [默认：WinXP 5 次, Win7 10 次] 检测不到心跳确认包则认为已断线）
func (t *TcpClient) SetKeepAliveInterval(KeepAliveInterval int) *TcpClient {
	if !t.isValid() {
		return t
	}
	HP_TcpClient_SetKeepAliveInterval.Call(t.hClient, KeepAliveInterval)
	return t
}

// SetNoDelay
//
//	@Description: 设置是否开启 nodelay 模式（默认：false，不开启）
func (t *TcpClient) SetNoDelay(NoDelay bool) *TcpClient {
	if !t.isValid() {
		return t
	}
	HP_TcpClient_SetNoDelay.Call(t.hClient, NoDelay)
	return t
}

// GetSocketBufferSize
//
//	@Description: 获取通信数据缓冲区大小
func (t *TcpClient) GetSocketBufferSize() int {
	if !t.isValid() {
		return 0
	}
	return HP_TcpClient_GetSocketBufferSize.CallInt(t.hClient)
}

// GetKeepAliveTime
//
//	@Description: 获取正常心跳包间隔
func (t *TcpClient) GetKeepAliveTime() int {
	if !t.isValid() {
		return 0
	}
	return HP_TcpClient_GetKeepAliveTime.CallInt(t.hClient)
}

// GetKeepAliveInterval
//
//	@Description: 获取异常心跳包间隔
func (t *TcpClient) GetKeepAliveInterval() int {
	if !t.isValid() {
		return 0
	}
	return HP_TcpClient_GetKeepAliveInterval.CallInt(t.hClient)
}

// IsNoDelay
//
//	@Description: 检查是否开启 nodelay 模式
func (t *TcpClient) IsNoDelay() bool {
	if !t.isValid() {
		return false
	}
	return HP_TcpClient_IsNoDelay.CallBool(t.hClient)
}
