Skip to content

Commit

Permalink
添加body 缓冲池
Browse files Browse the repository at this point in the history
  • Loading branch information
biless committed Jun 30, 2016
1 parent 49cd4d5 commit b81a8e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
21 changes: 18 additions & 3 deletions src/mqtt/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ import (
"sync"
)

//字节池
var bytePool = sync.Pool{
New:func() interface{} {
buf := make([]byte, 1)
return &buf
},
}

//缓冲层
var bufPool = sync.Pool{
New:func() interface{} {
buf := make([]byte,127)
return &buf
},
}

// 解析MQTT协议
func GetMqtt(conn net.TCPConn) (*MqttBuffer,*error) {
mqttBuffer := new(MqttBuffer)
Expand All @@ -22,9 +31,15 @@ func GetMqtt(conn net.TCPConn) (*MqttBuffer,*error) {
mqttBuffer.MqttControl = controlHeader
len,err := DeCodeLenFormTCPConn(conn)
mqttBuffer.Len = *len
mqttBuffer.body = DeCodeBodyFormTCPConn(conn,len)
return mqttBuffer,nil
}

//从TCP链接中获取body
func DeCodeBodyFormTCPConn(conn net.TCPConn,len int)(*[]byte,*error){
return nil,nil
}

// 从TCP连接中获取一个字节的数据进行解码
func DeCodeLenFormTCPConn(conn net.TCPConn) (*uint,*error) {
var len uint = 0;
Expand All @@ -34,14 +49,14 @@ func DeCodeLenFormTCPConn(conn net.TCPConn) (*uint,*error) {
return nil,new(error)
}
lenTemp,err := DeCodeLengthByByte((*byteTemp)[0])
defer bytePool.Put(byteTemp)
if(err != nil){
return nil,new(error)
}
len |= (lenTemp.Data) << (uint(i * 7))
if(lenTemp.IsContinue == 0){
return &len,nil
if(lenTemp.IsContinue == 0) {
return &len, nil
}
defer bytePool.Put(byteTemp)
}
return nil,new(error)
}
Expand Down
10 changes: 5 additions & 5 deletions src/mqtt/mqtt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const size = 10000000

func TestControlHeader_GetByte(t *testing.T) {
buf := make([]byte,122)
buf[0] = 1;
fmt.Print(buf[0]," ");
buf1 := buf[:2];
fmt.Print(buf1[0]," ");
buf1[0] = 2;
buf[0] = 1
fmt.Print(buf[0]," ")
buf1 := buf[:2]
fmt.Print(buf1[0]," ")
buf1[0] = 2
fmt.Print(buf[0],buf1[0])
}

Expand Down

0 comments on commit b81a8e3

Please sign in to comment.