| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 | 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:		tc := new(TCPClient)		tc.reconnect = true		tc.connected = true		tc.conn = conn		go tc.reconnecting()		return tc, 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}
 |