//go:build windows

package yu_hpsocket

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

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

// Create
//
//	@Description: 创建 TcpServer 环境
func (t *TcpServer) Create(SSL bool) bool {
	t.Close()
	if t.hListener, _, _ = Create_HP_TcpServerListener.Call(); t.hListener == 0 {
		return false
	}
	if t.SSL = SSL; t.SSL {
		t.hServer, _, _ = Create_HP_SSLServer.Call(t.hListener)
	} else {
		t.hServer, _, _ = Create_HP_TcpServer.Call(t.hListener)
	}
	if t.hServer == 0 {
		Destroy_HP_TcpServerListener.Call(t.hListener)
		t.hListener = 0
		return false
	}
	_server_on_set(t.hServer, &_server_item{
		_type: _server_type_TcpServer,
		t:     t,
	})
	return true
}

// Close
//
//	@Description: 销毁 TcpServer 环境
func (t *TcpServer) Close() {
	t.Stop()
	t.Wait(-1)
	if t.hServer != 0 {
		if t.SSL {
			Destroy_HP_SSLServer.Call(t.hServer)
		} else {
			Destroy_HP_TcpServer.Call(t.hServer)
		}
		_server_on_del(t.hServer)
		t.hServer = 0
	}
	if t.hListener != 0 {
		Destroy_HP_TcpServerListener.Call(t.hListener)
		t.hListener = 0
	}
}

// SetSocketListenQueue
//
//	@Description: 设置监听 Socket 的等候队列大小（根据并发连接数量调整设置）
func (t *TcpServer) SetSocketListenQueue(SocketListenQueue int) *TcpServer {
	if !t.isValid() {
		return t
	}
	HP_TcpServer_SetSocketListenQueue.Call(t.hServer, SocketListenQueue)
	return t
}

// SetAcceptSocketCount
//
//	@Description: 设置 Accept 预投递数量（根据负载调整设置，Accept 预投递数量越大则支持的并发连接请求越多
func (t *TcpServer) SetAcceptSocketCount(AcceptSocketCount int) *TcpServer {
	if !t.isValid() {
		return t
	}
	HP_TcpServer_SetAcceptSocketCount.Call(t.hServer, AcceptSocketCount)
	return t
}

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

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

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

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

// GetSocketListenQueue
//
//	@Description: 获取监听 Socket 的等候队列大小
func (t *TcpServer) GetSocketListenQueue() int {
	if !t.isValid() {
		return 0
	}
	return HP_TcpServer_GetSocketListenQueue.CallInt(t.hServer)
}

// GetAcceptSocketCount
//
//	@Description: 获取 Accept 预投递数量
func (t *TcpServer) GetAcceptSocketCount() int {
	if !t.isValid() {
		return 0
	}
	return HP_TcpServer_GetAcceptSocketCount.CallInt(t.hServer)
}

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

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

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

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