Skip to content
Permalink
Browse files

feat: add PINGREQ <-> PINGRESP

  • Loading branch information...
bati11 committed Mar 14, 2019
1 parent 062dc27 commit 868eaf7701ca11bbc08a9075ae9239807219819f
Showing with 62 additions and 0 deletions.
  1. +21 −0 mqtt/handler/pingreq_handler.go
  2. +13 −0 mqtt/packet/pingreq.go
  3. +19 −0 mqtt/packet/pingresp.go
  4. +9 −0 mqtt/server.go
@@ -0,0 +1,21 @@
package handler

import (
"fmt"

"github.com/bati11/oreno-mqtt/mqtt/packet"
)

func HandlePingreq(reader *packet.MQTTReader) (*packet.Pingresp, error) {
fmt.Printf(" HandlePingreq\n")
pingreq, err := reader.ReadPingreq()
if err != nil {
return nil, err
}
fmt.Printf(" %+v\n", pingreq)
pingresp := packet.NewPingresp()
fmt.Printf(" ---\n")
fmt.Printf(" %+v\n", pingresp)
fmt.Printf(" %+v\n", pingresp.ToBytes())
return &pingresp, nil
}
@@ -0,0 +1,13 @@
package packet

type Pingreq struct {
FixedHeader *FixedHeader
}

func (reader *MQTTReader) ReadPingreq() (*Pingreq, error) {
fixedHeader, err := reader.readFixedHeader()
if err != nil {
return nil, err
}
return &Pingreq{fixedHeader}, nil
}
@@ -0,0 +1,19 @@
package packet

type Pingresp struct {
FixedHeader
}

func NewPingresp() Pingresp {
fixedHeader := FixedHeader{
PacketType: PINGRESP,
RemainingLength: 0,
}
return Pingresp{fixedHeader}
}

func (p *Pingresp) ToBytes() []byte {
var result []byte
result = append(result, p.FixedHeader.ToBytes()...)
return result
}
@@ -68,6 +68,15 @@ func handle(conn net.Conn) error {
if err != nil {
return err
}
case packet.PINGREQ:
pingresp, err := handler.HandlePingreq(mqttReader)
if err != nil {
return err
}
_, err = conn.Write(pingresp.ToBytes())
if err != nil {
return err
}
case packet.DISCONNECT:
return nil
}

0 comments on commit 868eaf7

Please sign in to comment.
You can’t perform that action at this time.