Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into main
Browse files Browse the repository at this point in the history
# Conflicts:
#	io_server.go
  • Loading branch information
injoyai committed Jun 15, 2023
2 parents 9592d3e + 6a15f24 commit 39d91c0
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 62 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

/output/
/.idea/
/testdata/main/output/
7 changes: 3 additions & 4 deletions dial/dial_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@ func RedialWebsocket(url string, header http.Header, options ...io.OptionClient)
}
return url
}())
for _, v := range options {
v(c)
}
c.SetOptions(options...)
})
}

Expand Down Expand Up @@ -369,7 +367,8 @@ func NewSSH(cfg *SSHConfig, options ...io.OptionClient) (*io.Client, error) {

func RedialSSH(cfg *SSHConfig, options ...io.OptionClient) *io.Client {
return io.Redial(SSHFunc(cfg), func(c *io.Client) {
c.SetKey(cfg.Addr).SetOptions(options...)
c.SetKey(cfg.Addr)
c.SetOptions(options...)
})
}

Expand Down
19 changes: 9 additions & 10 deletions dial/dial_dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@ package dial
import (
"fmt"
"github.com/injoyai/io"
"github.com/injoyai/logs"
"testing"
"time"
)

func TestRedialWebsocket(t *testing.T) {
url := "ws://192.168.10.24:10001/api/user/notice/ws"
url := "ws://127.0.0.1:10001/api/user/notice/ws"
//"ws://192.168.10.3:1880/node-red/comms"
url = "ws://192.168.10.24:10001/api/ai/info/runtime/ws?id=83"
//url = "ws://192.168.10.24:10001/api/ai/info/runtime/ws?id=83"
//url = "ws://192.168.10.38:80/api/ai/photo/ws?key=0.0"
//url := "ws://192.168.10.24:10001/api/user/notice/ws"
//url += "?token=jbYKl72cbOGvbVRwIqM4r6eoirw8f1JRD44+4D5E/URRY4L6TTZYYb/9yhedvd2Ii2GtLo9MieBy5FBeUhugK5jHvppFjExz3B5DVFPqsomF5wezKDFc8a2hZSQ9IDHTS/C+j/3ESSRdbkVHPFxbzQ=="
//url = strings.ReplaceAll(url, "+", "%2B")
t.Log(url)
RedialWebsocket(url, nil, io.WithClientDebug(), func(c *io.Client) {
c.GoTimerWriter(time.Second*10, func(w *io.IWriter) error {
_, err := w.WriteString("83")
return err
})
logs.Debug(url)
c := RedialWebsocket(url, nil, func(c *io.Client) {
c.Debug()
//c.GoTimerWriteASCII(time.Second, "666")
})
select {}
<-c.DoneAll()
}

func TestRedialTCP(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion dial/dial_pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Client
func RedialPipe(addr string, options ...io.OptionClient) *io.Client {
return RedialTCP(addr, func(c *io.Client) {
c.SetReadWriteWithPkg()
c.SetKeepAlive(io.DefaultTimeout)
c.SetKeepAlive(io.DefaultKeepAlive)
c.SetPrintFunc(func(msg io.Message, tag ...string) {
io.PrintWithASCII(msg, append([]string{"PI|C"}, tag...)...)
})
Expand Down
7 changes: 5 additions & 2 deletions dial/dial_serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dial

import (
"github.com/goburrow/serial"
"github.com/injoyai/base/oss"
"github.com/injoyai/io"
"github.com/injoyai/logs"
serial2 "go.bug.st/serial"
Expand Down Expand Up @@ -54,16 +55,18 @@ func SerialFunc(cfg *SerialConfig) func() (io.ReadWriteCloser, error) {
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 * 100) //半双工
//c.SetHalfDuplex(time.Millisecond * 100) //半双工
c.SetOptions(options...)
oss.ListenExit(func() { c.CloseAll() })
})
}

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

Expand Down
60 changes: 40 additions & 20 deletions dial/dial_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,56 @@ func TestScanSerialWithTimeout(t *testing.T) {

func TestNewSerial(t *testing.T) {
c, err := NewSerial(&SerialConfig{
Address: "COM9",
Address: "COM6",
BaudRate: 115200,
DataBits: 8,
StopBits: 1,
Parity: SerialParityNone,
Timeout: time.Second * 10,
Timeout: 0,
})
if err != nil {
t.Error(err)
return
}
defer c.Close()

c.Debug()
//go func() {
// for {
// _, err = c.Write([]byte("+++"))
// logs.PrintErr(err)
// <-time.After(time.Second)
// }
//}()
c.SetPrintFunc(func(msg io.Message, tag ...string) {
logs.Debug(tag, msg)
})
c.SetReadWithAll()
t.Log(c.Run())

_, err = c.Write([]byte("+++"))
select {}
}

func TestNewSerial2(t *testing.T) {
c, err := NewSerial(&SerialConfig{
Address: "COM7",
BaudRate: 115200,
})
if err != nil {
t.Error(err)
return
}
defer c.Close()
c.Debug()
c.SetPrintFunc(func(msg io.Message, tag ...string) {
logs.Debug(tag, msg)
})
c.SetReadWithAll()
go c.Run()
logs.PrintErr(err)

//_, err = c.Write([]byte("+++"))

//logs.PrintErr(err)
go func() {
<-time.After(time.Second * 2)
_, err = c.Write([]byte("++++"))
logs.PrintErr(err)
for {
<-time.After(time.Second * 5)
_, err = c.Write([]byte("777"))
logs.PrintErr(err)
}
}()

<-time.After(time.Second * 20)
//<-time.After(time.Second * 20)

select {}
}

func TestRedialSerialF8L10T(t *testing.T) {
Expand All @@ -70,10 +88,12 @@ func TestRedialSerialF8L10T(t *testing.T) {
t.Log(c.WriteRead([]byte("+++")))
t.Log(c.WriteRead([]byte("+++")))
t.Log(c.WriteRead([]byte("AT+VER?\n")))
t.Log(c.WriteRead([]byte("123")))
t.Log(c.Write([]byte("+++")))
}()
})
defer c.CloseAll()
<-time.After(time.Second * 100)
select {}
}

func TestRedialSerialSL101(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion io_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (this *Client) WriteReadWithTimeout(request []byte, timeout time.Duration)

// WriteRead 同步写读,不超时
func (this *Client) WriteRead(request []byte, timeout ...time.Duration) (response []byte, err error) {
return this.WriteReadWithTimeout(request, conv.GetDefaultDuration(0, timeout...))
return this.WriteReadWithTimeout(request, conv.GetDefaultDuration(DefaultResponseTimeout, timeout...))
}

// GoTimerWriter 协程,定时写入数据,生命周期(一次链接,单次连接断开)
Expand Down
1 change: 1 addition & 0 deletions io_i_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func PrintWithHEX(msg Message, tag ...string) {
log.Print(PrintfWithHEX(msg, tag...))
}

// PrintWithASCII todo [13 10 69 82 82 79 82 13 10]用log.Print打印不出来
func PrintWithASCII(msg Message, tag ...string) {
log.Print(PrintfWithASCII(msg, tag...))
}
Expand Down
3 changes: 2 additions & 1 deletion io_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const (
Ping = "ping"
Pong = "pong"

DefaultTimeout = time.Minute * 10
DefaultKeepAlive = time.Minute * 10 //默认保持连接时间
DefaultResponseTimeout = time.Second * 10 //默认响应超时时间
)

func NewMessageFormat(format string, v ...interface{}) Message {
Expand Down
34 changes: 11 additions & 23 deletions testdata/main/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bufio"
"fmt"
"github.com/injoyai/conv"
"github.com/injoyai/conv/cfg"
Expand All @@ -10,35 +9,24 @@ import (
"github.com/injoyai/io/dial/proxy"
"github.com/injoyai/io/testdata"
"github.com/injoyai/logs"
"os"
"runtime"
)

func main() {
reader := bufio.NewReader(os.Stdin)
dial.RedialWebsocket("ws://192.168.10.24:8300/v1/chat/ws", nil, func(c *io.Client) {
url := "ws://127.0.0.1:10001/api/user/notice/ws"
//"ws://192.168.10.3:1880/node-red/comms"
//url = "ws://192.168.10.24:10001/api/ai/info/runtime/ws?id=83"
//url = "ws://192.168.10.38:80/api/ai/photo/ws?key=0.0"
//url := "ws://192.168.10.24:10001/api/user/notice/ws"
//url += "?token=jbYKl72cbOGvbVRwIqM4r6eoirw8f1JRD44+4D5E/URRY4L6TTZYYb/9yhedvd2Ii2GtLo9MieBy5FBeUhugK5jHvppFjExz3B5DVFPqsomF5wezKDFc8a2hZSQ9IDHTS/C+j/3ESSRdbkVHPFxbzQ=="
//url = strings.ReplaceAll(url, "+", "%2B")
logs.Debug(url)
c := dial.RedialWebsocket(url, nil, func(c *io.Client) {
c.Debug()
c.SetPrintFunc(func(msg io.Message, tag ...string) {
if len(tag) > 0 {
switch tag[0] {
case io.TagRead:
fmt.Printf("[客服] %s\n", conv.NewMap(msg).GetString("data"))
case io.TagWrite:
default:
fmt.Printf("[%s] %s\n", tag[0], msg)
}
}
})
go func() {
for {
msg, _ := reader.ReadString('\n')
c.WriteString(msg)
}
}()
//c.GoTimerWriteASCII(time.Second, "666")
})
select {}
<-c.DoneAll()

logs.PrintErr(NewPortForwardingServer())
return
NewPortForwardingClient()
return
Expand Down

0 comments on commit 39d91c0

Please sign in to comment.