| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 | package networkimport (	"fmt"	"net"	"time")// Body 通过 defaultPool 分配 byte 数组func Body() (p []byte) {	p = defaultPool.Get().([]byte)	defaultPool.Put(p)	return}// Dial 拨号. network 可选 NetTCP 或 NetUDP 表示使用 TCP 或 UDP 协议, address 为服务器地址// Dial 实现 net.Conn 接口func Dial(network, address string) (net.Conn, error) {	return DialTimout(network, address, DefaultDialTimout)}// DialTimout 拨号并指定超时时间func DialTimout(network, address string, timout time.Duration) (net.Conn, error) {	conn, err := net.DialTimeout(network, address, timout)	if err != nil {		return nil, err	}	switch network {	case NetTCP:		return createTCPClient(conn), nil	case NetUDP:		panic("not implemented")	default:		panic(fmt.Sprintf("unsupported protocol: %s", network))	}}// NewModbusClient 每秒使用 data 创建数据并发送至服务器func NewModbusClient(conn net.Conn, data ModbusCreator) ModbusClient {	ms := new(modbusClient)	ms.connected = true	ms.b = make([]byte, 0)	ms.p = make(chan []byte, 1)	ms.data = data	ms.conn = conn	go ms.async()	return ms}
 |