Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed May 16, 2023
1 parent 479b858 commit 49a67bf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
44 changes: 24 additions & 20 deletions dial/dial_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ func TCPFunc(addr string) func() (io.ReadWriteCloser, error) {
}

// NewTCP 新建TCP连接
func NewTCP(addr string) (*io.Client, error) {
c, err := io.NewDial(TCPFunc(addr))
if err == nil {
func NewTCP(addr string, options ...io.OptionClient) (*io.Client, error) {
return io.NewDial(TCPFunc(addr), func(c *io.Client) {
c.SetKey(addr)
}
return c, err
c.SetOptions(options...)
})
}

// RedialTCP 一直连接TCP服务端,并重连
func RedialTCP(addr string, options ...io.OptionClient) *io.Client {
return io.Redial(TCPFunc(addr), func(c *io.Client) {
c.SetKey(addr).SetOptions(options...)
c.SetKey(addr)
c.SetOptions(options...)
})
}

Expand All @@ -54,18 +54,18 @@ func UDPFunc(addr string) func() (io.ReadWriteCloser, error) {
return func() (io.ReadWriteCloser, error) { return UDP(addr) }
}

func NewUDP(addr string) (*io.Client, error) {
c, err := io.NewDial(UDPFunc(addr))
if err == nil {
func NewUDP(addr string, options ...io.OptionClient) (*io.Client, error) {
return io.NewDial(UDPFunc(addr), func(c *io.Client) {
c.SetKey(addr)
}
return c, err
c.SetOptions(options...)
})
}

// RedialUDP 一直连接UDP服务端,并重连
func RedialUDP(addr string, options ...io.OptionClient) *io.Client {
return io.Redial(UDPFunc(addr), func(c *io.Client) {
c.SetKey(addr).SetOptions(options...)
c.SetKey(addr)
c.SetOptions(options...)
})
}

Expand All @@ -83,8 +83,11 @@ func FileFunc(path string) func() (io.ReadWriteCloser, error) {
}
}

func NewFile(path string) (*io.Client, error) {
return io.NewDial(FileFunc(path))
func NewFile(path string, options ...io.OptionClient) (*io.Client, error) {
return io.NewDial(FileFunc(path), func(c *io.Client) {
c.SetKey(path)
c.SetOptions(options...)
})
}

//================================WebsocketDial================================
Expand All @@ -94,8 +97,11 @@ func Memory() (io.ReadWriteCloser, error) {
return &_memory{Buffer: bytes.NewBuffer(nil)}, nil
}

func NewMemory() (*io.Client, error) {
return io.NewDial(Memory)
func NewMemory(options ...io.OptionClient) (*io.Client, error) {
return io.NewDial(Memory, func(c *io.Client) {
c.SetKey("memory")
c.SetOptions(options...)
})
}

type _memory struct {
Expand Down Expand Up @@ -147,12 +153,10 @@ func NewMQTT(clientID, topic string, qos byte, cfg *MQTTConfig) (*io.Client, err
return c, err
}

func RedialMQTT(clientID, topic string, qos byte, cfg *MQTTConfig, fn ...io.OptionClient) *io.Client {
func RedialMQTT(clientID, topic string, qos byte, cfg *MQTTConfig, options ...io.OptionClient) *io.Client {
return io.Redial(MQTTFunc(clientID, topic, qos, cfg.SetAutoReconnect(true)), func(c *io.Client) {
c.SetKey(topic)
for _, v := range fn {
v(c)
}
c.SetOptions(options...)
})
}

Expand Down
16 changes: 8 additions & 8 deletions dial/dial_serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ func SerialFunc(cfg *SerialConfig) func() (io.ReadWriteCloser, error) {
}
}

func NewSerial(cfg *SerialConfig) (*io.Client, error) {
c, err := io.NewDial(SerialFunc(cfg))
if err == nil {
func NewSerial(cfg *SerialConfig, options ...io.OptionClient) (*io.Client, error) {
return io.NewDial(SerialFunc(cfg), func(c *io.Client) {
c.SetKey(cfg.Address)
c.SetHalfDuplex(time.Millisecond * 30) //半双工
}
return c, err
c.SetHalfDuplex(time.Millisecond * 100) //半双工
c.SetOptions(options...)
})
}

func RedialSerial(cfg *SerialConfig, options ...io.OptionClient) *io.Client {
return io.Redial(SerialFunc(cfg), func(c *io.Client) {
c.SetKey(cfg.Address).SetOptions(options...)
c.SetHalfDuplex(time.Millisecond * 30) //半双工
c.SetKey(cfg.Address)
c.SetHalfDuplex(time.Millisecond * 100) //半双工
c.SetOptions(options...)
})
}

Expand Down
6 changes: 5 additions & 1 deletion dial/dial_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ func TestRedialSerial(t *testing.T) {
}, func(c *io.Client) {
c.Debug()
c.SetPrintWithASCII()
c.GoTimerWriteASCII(time.Second*1, "+++")
go func() {
t.Log(c.WriteRead([]byte("+++")))
t.Log(c.WriteRead([]byte("+++")))
t.Log(c.WriteRead([]byte("AT+VER?\n")))
}()
})
defer c.CloseAll()
<-time.After(time.Second * 100)
Expand Down

0 comments on commit 49a67bf

Please sign in to comment.