package network

import (
	"errors"
	"sync"
	"time"
)

const (
	NetTCP = "tcp"
	NetUDP = "udp"
)

const (
	DefaultDialTimout = 10 * time.Second
	// DefaultReadTimout 默认读取超时时间
	DefaultReadTimout          = 5 * time.Second
	DefaultWriteTimout         = 3 * time.Second
	DefaultModbusWriteInterval = 1 * time.Second
)

var (
	// ErrClosed 表示连接已关闭, 此连接不可再重用
	ErrClosed = errors.New("network: connection was closed")
	// ErrTimout 用于特定情况下的超时
	ErrTimout = errors.New("network: timout")
	// ErrReconnect 表示连接已经关闭且正在重连中. 遇到此错误时应重试读取或写入直至成功
	// 此错误仅在 "SetReconnect" 为 true 时开启, 仅适用于 Client 及派生接口
	ErrReconnect = errors.New("network: reconnecting")
	// ErrConnNotFound 连接不存在
	ErrConnNotFound = errors.New("network: connection not found")
)

func IsClosed(err error) bool {
	return err == ErrClosed
}

func IsReconnect(err error) bool {
	return err == ErrReconnect
}

var (
	// defaultPool 分配指定数量大小的 byte 数组
	defaultPool = sync.Pool{New: func() any {
		return make(Bytes, 4096)
	}}
)

// ModbusCreator 创建需要写入的数据
type ModbusCreator interface {
	Create() ([]byte, error)
}
