Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Jul 16, 2023
1 parent fc86b80 commit 6821642
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ autobahn-client-testsuite-linux-amd64
autobahn-server-testsuite-darwin-arm64
autobahn-server-testsuite-linux-amd64
autobahn/autobahn-server-testsuite-darwin-arm64-arena
autobahn-client-darwin-arm64
autobahn-client-linux-amd64
autobahn-server-darwin-arm64
autobahn-server-darwin-arm64-arena
autobahn-server-linux-amd64
cpu.profile
mem.profile
10 changes: 5 additions & 5 deletions autobahn/Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
all:
# mac, arm64
GOOS=darwin GOARCH=arm64 go build -o autobahn-server-testsuite-darwin-arm64 ./autobahn-server-testsuite.go
GOOS=darwin GOARCH=arm64 go build -o autobahn-server-darwin-arm64 ./autobahn-server.go
# mac, arm64
GOOS=darwin GOARCH=arm64 go build -tags=goexperiment.arenas -o autobahn-server-testsuite-darwin-arm64-arena ./autobahn-server-testsuite.go
GOOS=darwin GOARCH=arm64 go build -tags=goexperiment.arenas -o autobahn-server-darwin-arm64-arena ./autobahn-server.go
# linux amd64
GOOS=linux GOARCH=amd64 go build -o autobahn-server-testsuite-linux-amd64 ./autobahn-server-testsuite.go
GOOS=linux GOARCH=amd64 go build -o autobahn-server-linux-amd64 ./autobahn-server.go

# mac, arm64
GOOS=darwin GOARCH=arm64 go build -o autobahn-client-testsuite-darwin-arm64 ./autobahn-client-testsuite.go
GOOS=darwin GOARCH=arm64 go build -o autobahn-client-darwin-arm64 ./autobahn-client.go
# linux amd64
GOOS=linux GOARCH=amd64 go build -o autobahn-client-testsuite-linux-amd64 ./autobahn-client-testsuite.go
GOOS=linux GOARCH=amd64 go build -o autobahn-client-linux-amd64 ./autobahn-client.go

key:
openssl genrsa 2048 > privatekey.pem
Expand Down
5 changes: 4 additions & 1 deletion autobahn/autobahn-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func (e *echoHandler) OnOpen(c *quickws.Conn) {

func (e *echoHandler) OnMessage(c *quickws.Conn, op quickws.Opcode, msg []byte) {
// fmt.Println("OnMessage:", c, msg, op)
if err := c.WriteTimeout(op, msg, 3*time.Second); err != nil {
// if err := c.WriteTimeout(op, msg, 3*time.Second); err != nil {
// fmt.Println("write fail:", err)
// }
if err := c.WriteMessage2(op, msg); err != nil {
fmt.Println("write fail:", err)
}
}
Expand Down
8 changes: 8 additions & 0 deletions benchmark_mask_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package quickws

import (
"math/rand"
"testing"
)

Expand All @@ -11,3 +12,10 @@ func Benchmark_Rand(t *testing.B) {
newMask(maskValue[:])
}
}

func Benchmark_Rand_Uint32(t *testing.B) {
for i := 0; i < t.N; i++ {
// newMask(maskValue[:])
_ = rand.Uint32()
}
}
17 changes: 17 additions & 0 deletions benchmark_read_write_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (testconn *testConn) Read(b []byte) (n int, err error) {
// time limit; see SetDeadline and SetWriteDeadline.
func (testconn *testConn) Write(b []byte) (n int, err error) {
return testconn.buf.Write(b)
return
}

// Close closes the connection.
Expand Down Expand Up @@ -117,6 +118,22 @@ func Benchmark_WriteMessage(b *testing.B) {
}
}

func Benchmark_WriteMessage2(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.WriteMessage2(opcode.Binary, buf)
buf2.Reset()
}
}

// TODO
// func Benchmark_ReadMessage(b *testing.B) {
// var c Conn
Expand Down
37 changes: 34 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/binary"
"fmt"
"io"
"math/rand"
"net"
"sync"
"time"
Expand Down Expand Up @@ -329,8 +330,39 @@ func (w *wrapBuffer) Close() error {
return nil
}

func (c *Conn) WriteMessage2(op Opcode, writeBuf []byte) (err error) {
if op == opcode.Text {
if !c.utf8Check(writeBuf) {
return ErrTextNotUTF8
}
}

rsv1 := c.compression && (op == opcode.Text || op == opcode.Binary)
if 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))
maskValue := uint32(0)
if c.client {
maskValue = rand.Uint32()
}

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

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

if op == opcode.Text {
if !c.utf8Check(writeBuf) {
Expand All @@ -354,14 +386,13 @@ func (c *Conn) WriteMessage(op Opcode, writeBuf []byte) (err error) {
}

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

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

func (c *Conn) SetDeadline(t time.Time) error {
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.20230714141652-f7a6614364f1
require github.com/antlabs/wsutil v0.0.9-0.20230716150812-82d34e757670
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.20230714141652-f7a6614364f1 h1:LHBIJ1e0Rt5mqeGm31dJf+BP9g19WgUxgd1gOPcIkc8=
github.com/antlabs/wsutil v0.0.9-0.20230714141652-f7a6614364f1/go.mod h1:7ec5eUM7nmKW+Oi6F1I58iatOeL9k+yIsfOh1zh910g=
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=

0 comments on commit 6821642

Please sign in to comment.