Skip to content

Commit

Permalink
变量重命名
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Sep 9, 2022
1 parent e48baba commit 033bbff
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 26 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test:
go test -timeout 30s -run ^Test github.com/lxzan/gws

bench:
go test -benchmem -bench ^Benchmark github.com/lxzan/gws
3 changes: 2 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (c *Conn) emitError(err error) {

code, ok := err.(Code)
if !ok {
c.debugLog(err)
code = CloseGoingAway
}

Expand All @@ -117,7 +118,7 @@ func (c *Conn) Close(code Code, reason []byte) (err error) {
} else {
content = append(content, code.Error()...)
}
_ = c.writeFrame(Opcode_CloseConnection, content, false)
_ = c.writeFrame(OpcodeCloseConnection, content, false)
err = c.netConn.Close()
})
return
Expand Down
6 changes: 3 additions & 3 deletions example/tests/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (c *WebSocketHandler) OnTest(socket *websocket.Conn) {
var size = rand.Intn(1024)
var k = internal.AlphabetNumeric.Generate(size)
socket.Storage.Put(string(k), 1)
socket.Write(websocket.Opcode_Text, k)
socket.Write(websocket.OpcodeText, k)
}
}

Expand All @@ -75,14 +75,14 @@ func (c *WebSocketHandler) OnVerify(socket *websocket.Conn) {
panic("failed")
}

socket.Write(websocket.Opcode_Text, []byte("ok"))
socket.Write(websocket.OpcodeText, []byte("ok"))
}

func (c *WebSocketHandler) OnBench(socket *websocket.Conn) {
const count = 100000
for i := 0; i < count; i++ {
var size = rand.Intn(1024)
var k = internal.AlphabetNumeric.Generate(size)
socket.Write(websocket.Opcode_Text, k)
socket.Write(websocket.OpcodeText, k)
}
}
2 changes: 1 addition & 1 deletion frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func isDataFrame(code Opcode) bool {
return code <= Opcode_Binary
return code <= OpcodeBinary
}

type frameHeader [internal.FrameHeaderSize]byte
Expand Down
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Message struct {
data *bytes.Buffer
}

func (c *Message) Opcode() Opcode {
func (c *Message) MessageType() Opcode {
return c.opcode
}

Expand Down
12 changes: 6 additions & 6 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import "errors"
type Opcode uint8

const (
Opcode_Continuation Opcode = 0x0
Opcode_Text Opcode = 0x1
Opcode_Binary Opcode = 0x2
Opcode_CloseConnection Opcode = 0x8
Opcode_Ping Opcode = 0x9
Opcode_Pong Opcode = 0xA
OpcodeContinuation Opcode = 0x0
OpcodeText Opcode = 0x1
OpcodeBinary Opcode = 0x2
OpcodeCloseConnection Opcode = 0x8
OpcodePing Opcode = 0x9
OpcodePong Opcode = 0xA
)

type EventHandler interface {
Expand Down
14 changes: 7 additions & 7 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func (c *Conn) readControl() (continued bool, retErr error) {
}

switch c.fh.GetOpcode() {
case Opcode_Ping:
case OpcodePing:
c.handler.OnPing(c, payload)
return true, nil
case Opcode_Pong:
case OpcodePong:
c.handler.OnPong(c, payload)
return true, nil
case Opcode_CloseConnection:
case OpcodeCloseConnection:
switch n {
case 0:
c.emitClose(CloseNormalClosure, nil)
Expand Down Expand Up @@ -87,7 +87,7 @@ func (c *Conn) readMessage() (continued bool, retErr error) {
}

// just for continuation opcode
if opcode == Opcode_Text || opcode == Opcode_Binary {
if opcode == OpcodeText || opcode == OpcodeBinary {
c.opcode = opcode
}

Expand Down Expand Up @@ -139,7 +139,7 @@ func (c *Conn) readMessage() (continued bool, retErr error) {
}
}

if !fin || (fin && opcode == Opcode_Continuation) {
if !fin || (fin && opcode == OpcodeContinuation) {
if err := writeN(c.fragmentBuffer, buf.Bytes(), contentLength); err != nil {
return false, err
}
Expand All @@ -150,7 +150,7 @@ func (c *Conn) readMessage() (continued bool, retErr error) {

if fin {
switch opcode {
case Opcode_Continuation:
case OpcodeContinuation:
if err := writeN(buf, c.fragmentBuffer.Bytes(), contentLength); err != nil {
return false, err
}
Expand All @@ -161,7 +161,7 @@ func (c *Conn) readMessage() (continued bool, retErr error) {
if c.fragmentBuffer.Cap() > c.conf.ReadBufferSize {
c.fragmentBuffer = bytes.NewBuffer(nil)
}
case Opcode_Text, Opcode_Binary:
case OpcodeText, OpcodeBinary:
c.mq.Push(&Message{compressed: c.compressEnabled, opcode: opcode, data: buf, index: -1})
c.messageLoop()
default:
Expand Down
4 changes: 2 additions & 2 deletions websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func BenchmarkCompress(b *testing.B) {
for i := 0; i < b.N; i++ {
buf.Reset()
fw.Reset(buf)
fw.Write(*(*[]byte)(unsafe.Pointer(&s)))
fw.Write(s)
fw.Flush()
fw.Close()
}
Expand All @@ -52,7 +52,7 @@ func BenchmarkDeCompress(b *testing.B) {
var s = internal.AlphabetNumeric.Generate(1024)
var buf = bytes.NewBuffer(nil)
fw, _ := flate.NewWriter(buf, -2)
fw.Write(*(*[]byte)(unsafe.Pointer(&s)))
fw.Write(s)
fw.Flush()
fw.Close()
fr := flate.NewReader(nil)
Expand Down
10 changes: 5 additions & 5 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import "bytes"

// send ping frame
func (c *Conn) WritePing(payload []byte) {
c.emitError(c.writeFrame(Opcode_Ping, payload, false))
c.emitError(c.writeFrame(OpcodePing, payload, false))
}

// send pong frame
func (c *Conn) WritePong(payload []byte) {
c.emitError(c.writeFrame(Opcode_Pong, payload, false))
c.emitError(c.writeFrame(OpcodePong, payload, false))
}

// send close frame
func (c *Conn) WriteClose(code Code, reason []byte) {
var content = code.Bytes()
content = append(content, reason...)
c.emitError(c.writeFrame(Opcode_CloseConnection, content, false))
c.emitError(c.writeFrame(OpcodeCloseConnection, content, false))
}

// 发送消息; 此方法会回收内存, 不要用来写控制帧
// send a message; this method reclaims memory and should not be used to write control frames
func (c *Conn) Write(opcode Opcode, content []byte) {
c.emitError(c.writeMessage(opcode, content))
func (c *Conn) Write(messageType Opcode, content []byte) {
c.emitError(c.writeMessage(messageType, content))
_pool.Put(bytes.NewBuffer(content))
}

Expand Down

0 comments on commit 033bbff

Please sign in to comment.