maeqtt/main.go
2021-09-01 22:36:43 +02:00

64 lines
1.2 KiB
Go

package main
import (
"bufio"
"log"
"net"
"badat.dev/maeqtt/v2/mqtt/packets"
"badat.dev/maeqtt/v2/mqtt/properties"
"github.com/gdexlab/go-render/render" // For testing
)
func main() {
listen_addr := ":1883"
listener, err := net.Listen("tcp", listen_addr)
if err != nil {
log.Fatal(err)
}
for {
conn, err := listener.Accept()
if err != nil {
log.Println("Failed accepting connection ", err)
}
go handleConnection(conn)
}
}
func handleConnection(con net.Conn) {
defer closeConnection(con)
for {
reader := bufio.NewReader(con)
packet, err := packets.ReadPacket(reader)
if err != nil {
log.Println("Error reading packet ", err)
break
}
log.Println(render.AsCode(packet))
clientId := "aa"
resp := packets.ConnackPacket{
ResonCode: packets.ConnectReasonCodeSuccess,
SessionPresent: false,
Properties: properties.ConnackPacketProperties{
AssignedClientIdentifier: properties.AssignedClientIdentifier{Value: &clientId},
},
}
err = resp.Write(con)
log.Println("Wrote response")
if err != nil {
log.Println("Error writing response ", err)
break
}
}
}
func closeConnection(con net.Conn) {
err := con.Close()
if err != nil {
log.Println("Failed to close connection", err)
}
}