Skip to content

Commit

Permalink
更新代码
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Jul 17, 2023
1 parent 6821642 commit 32b9ee4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dump:
go build -o /dev/null -gcflags -S *
# dump:
#go build -o /dev/null -gcflags -S *
2 changes: 1 addition & 1 deletion autobahn/autobahn-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (e *echoHandler) OnMessage(c *quickws.Conn, op quickws.Opcode, msg []byte)
// if err := c.WriteTimeout(op, msg, 3*time.Second); err != nil {
// fmt.Println("write fail:", err)
// }
if err := c.WriteMessage2(op, msg); err != nil {
if err := c.WriteMessage(op, msg); err != nil {
fmt.Println("write fail:", err)
}
}
Expand Down
32 changes: 16 additions & 16 deletions benchmark_read_write_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,23 @@ type testConn struct {
buf *bytes.Buffer
}

func Benchmark_WriteMessage(b *testing.B) {
var c Conn
buf2 := bytes.NewBuffer(make([]byte, 0, 1024))
c.c = &testConn{buf: buf2}
buf := make([]byte, 1024)
for i := range buf {
buf[i] = 1
}
// func Benchmark_WriteMessage(b *testing.B) {
// var c Conn
// buf2 := bytes.NewBuffer(make([]byte, 0, 1024))
// c.c = &testConn{buf: buf2}
// buf := make([]byte, 1024)
// for i := range buf {
// buf[i] = 1
// }

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

func Benchmark_WriteMessage2(b *testing.B) {
func Benchmark_WriteMessage(b *testing.B) {
var c Conn
buf2 := bytes.NewBuffer(make([]byte, 0, 1024))
c.c = &testConn{buf: buf2}
Expand All @@ -129,7 +129,7 @@ func Benchmark_WriteMessage2(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
c.WriteMessage2(opcode.Binary, buf)
c.WriteMessage(opcode.Binary, buf)
buf2.Reset()
}
}
Expand Down
70 changes: 35 additions & 35 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (w *wrapBuffer) Close() error {
return nil
}

func (c *Conn) WriteMessage2(op Opcode, writeBuf []byte) (err error) {
func (c *Conn) WriteMessage(op Opcode, writeBuf []byte) (err error) {
if op == opcode.Text {
if !c.utf8Check(writeBuf) {
return ErrTextNotUTF8
Expand Down Expand Up @@ -358,42 +358,42 @@ func (c *Conn) WriteMessage2(op Opcode, writeBuf []byte) (err error) {
maskValue = rand.Uint32()
}

return frame.WriteFrame2(&c.fw, c.c, writeBuf, rsv1, c.client, op, maskValue)
return frame.WriteFrame(&c.fw, c.c, writeBuf, rsv1, c.client, op, maskValue)
}

func (c *Conn) WriteMessage(op Opcode, writeBuf []byte) (err error) {
var f frame.FrameHeader

if op == opcode.Text {
if !c.utf8Check(writeBuf) {
return ErrTextNotUTF8
}
}

f.Fin = true
f.Rsv1 = c.compression && (op == opcode.Text || op == opcode.Binary)
if f.Rsv1 {
var out wrapBuffer
w := compressNoContextTakeover(&out, defaultCompressionLevel)
if _, err = io.Copy(w, bytes.NewReader(writeBuf)); err != nil {
return
}

if err = w.Close(); err != nil {
return
}
writeBuf = out.Bytes()
}

f.Opcode = op
f.PayloadLen = int64(len(writeBuf))
if c.client {
f.Mask = true
newMask(f.MaskValue[:])
}

return frame.WriteFrame(c.c, f, writeBuf, &c.fw)
}
// func (c *Conn) WriteMessageOld(op Opcode, writeBuf []byte) (err error) {
// var f frame.FrameHeader

// if op == opcode.Text {
// if !c.utf8Check(writeBuf) {
// return ErrTextNotUTF8
// }
// }

// f.Fin = true
// f.Rsv1 = c.compression && (op == opcode.Text || op == opcode.Binary)
// if f.Rsv1 {
// var out wrapBuffer
// w := compressNoContextTakeover(&out, defaultCompressionLevel)
// if _, err = io.Copy(w, bytes.NewReader(writeBuf)); err != nil {
// return
// }

// if err = w.Close(); err != nil {
// return
// }
// writeBuf = out.Bytes()
// }

// f.Opcode = op
// f.PayloadLen = int64(len(writeBuf))
// if c.client {
// f.Mask = true
// newMask(f.MaskValue[:])
// }

// return frame.WriteFrameOld(c.c, f, writeBuf, &c.fw)
// }

func (c *Conn) SetDeadline(t time.Time) error {
return c.c.SetDeadline(t)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/antlabs/quickws

go 1.20

require github.com/antlabs/wsutil v0.0.9-0.20230716150812-82d34e757670
require github.com/antlabs/wsutil v0.0.9
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/antlabs/wsutil v0.0.9-0.20230716150812-82d34e757670 h1:ShwLNXS4XdsvPxiHpuxW3Dwahu0kI6muZ2gFuZ0A+gU=
github.com/antlabs/wsutil v0.0.9-0.20230716150812-82d34e757670/go.mod h1:7ec5eUM7nmKW+Oi6F1I58iatOeL9k+yIsfOh1zh910g=
github.com/antlabs/wsutil v0.0.9 h1:CNURXjtBZ3O/5xp+uimBtAoxR309R4QS1uxGJ/QCp+c=
github.com/antlabs/wsutil v0.0.9/go.mod h1:7ec5eUM7nmKW+Oi6F1I58iatOeL9k+yIsfOh1zh910g=

0 comments on commit 32b9ee4

Please sign in to comment.