Skip to content

Commit

Permalink
重构:移除一些代码至wsutil和bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Jun 30, 2023
1 parent ab6b5fa commit c6b1971
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 1,064 deletions.
8 changes: 5 additions & 3 deletions autobahn/autobahn-client-testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
// https://github.com/snapview/tokio-tungstenite/blob/master/examples/autobahn-client.rs

const (
// host = "ws://192.168.128.44:9003"
host = "ws://127.0.0.1:9003"
host = "ws://192.168.128.44:9003"
// host = "ws://127.0.0.1:9003"
agent = "quickws"
)

Expand All @@ -25,6 +25,7 @@ func (e *echoHandler) OnOpen(c *quickws.Conn) {
}

func (e *echoHandler) OnMessage(c *quickws.Conn, op quickws.Opcode, msg []byte) {
fmt.Println("OnMessage:", c, op, msg)
if op == quickws.Text || op == quickws.Binary {
// os.WriteFile("./debug.dat", msg, 0o644)
if err := c.WriteTimeout(op, msg, 1*time.Minute); err != nil {
Expand Down Expand Up @@ -81,7 +82,8 @@ func runTest(caseNo int) {
done := make(chan struct{})
c, err := quickws.Dial(fmt.Sprintf("%s/runCase?case=%d&agent=%s", host, caseNo, agent),
quickws.WithClientReplyPing(),
quickws.WithClientDecompression(),
// quickws.WithClientCompression(),
quickws.WithClientDecompressAndCompress(),
quickws.WithClientCallback(&echoHandler{done: done}),
)
if err != nil {
Expand Down
26 changes: 16 additions & 10 deletions benchmark_read_write_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ import (
"net"
"testing"
"time"

"github.com/antlabs/wsutil/enum"
"github.com/antlabs/wsutil/frame"
"github.com/antlabs/wsutil/opcode"
)

var noMaskData = []byte{0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f}

// Read reads data from the connection.
// Read can be made to time out and return an error after a fixed
// time limit; see SetDeadline and SetReadDeadline.
Expand Down Expand Up @@ -105,13 +111,13 @@ func Benchmark_WriteFrame(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
//
var f frame
f.fin = true
var f frame.Frame
f.Fin = true

f.opcode = Binary
f.payload = buf.Bytes()
f.payloadLen = int64(buf.Len())
writeFrame(&buf, f)
f.Opcode = opcode.Binary
f.Payload = buf.Bytes()
f.PayloadLen = int64(buf.Len())
frame.WriteFrame(&buf, f)
buf.Reset()
}
}
Expand All @@ -127,7 +133,7 @@ func Benchmark_WriteMessage(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
c.WriteMessage(Binary, buf)
c.WriteMessage(opcode.Binary, buf)
buf2.Reset()
}
}
Expand All @@ -145,19 +151,19 @@ func Benchmark_ReadMessage(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
c.WriteMessage(Binary, wbuf)
c.WriteMessage(opcode.Binary, wbuf)
c.ReadLoop()
buf2.Reset()
}
}

func Benchmark_ReadFrame(b *testing.B) {
r := bytes.NewReader(noMaskData)
var headArray [maxFrameHeaderSize]byte
var headArray [enum.MaxFrameHeaderSize]byte
for i := 0; i < b.N; i++ {

r.Reset(noMaskData)
_, _, err := readHeader(r, &headArray)
_, _, err := frame.ReadHeader(r, &headArray)
if err != nil {
b.Fatal(err)
}
Expand Down
70 changes: 0 additions & 70 deletions bytes_pool.go

This file was deleted.

43 changes: 0 additions & 43 deletions bytes_pool_test.go

This file was deleted.

12 changes: 7 additions & 5 deletions callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
// limitations under the License.
package quickws

type Callback interface {
OnOpen(*Conn)
OnMessage(*Conn, Opcode, []byte)
OnClose(*Conn, error)
}
type (
Callback interface {
OnOpen(*Conn)
OnMessage(*Conn, Opcode, []byte)
OnClose(*Conn, error)
}
)

type DefCallback struct{}

Expand Down
12 changes: 8 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"net/url"
"strings"
"time"

"github.com/antlabs/wsutil/bytespool"
"github.com/antlabs/wsutil/enum"
"github.com/antlabs/wsutil/fixedreader"
)

var (
Expand Down Expand Up @@ -205,16 +209,16 @@ func (d *DialOption) Dial() (c *Conn, err error) {
}

// 处理下已经在bufio里面的数据,后面都是直接操作net.Conn,所以需要取出bufio里面已读取的数据
var fr *fixedReader
var fr *fixedreader.FixedReader
if brw.Reader.Buffered() > 0 {
b, err := brw.Reader.Peek(brw.Reader.Buffered())
if err != nil {
return nil, err
}
b2 := getBytes(len(b) + maxFrameHeaderSize)
b2 := bytespool.GetBytes(len(b) + enum.MaxFrameHeaderSize)
copy(*b2, b)
fr = newBuffer(conn, b2)
fr.w = len(b)
fr = fixedreader.NewFixedReader(conn, b2)
fr.W = len(b)
}
// fmt.Println(brw.Reader.Buffered())
return newConn(conn, true, d.config, fr), nil
Expand Down
Loading

0 comments on commit c6b1971

Please sign in to comment.