maeqtt/main.go
2021-09-28 12:30:32 +02:00

57 lines
996 B
Go

package main
import (
"bufio"
"log"
"net"
"runtime/debug"
"badat.dev/maeqtt/v2/mqtt/packets"
)
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 handlePanic()
reader := bufio.NewReader(con)
packet, err := packets.ReadPacket(reader)
if err != nil {
log.Println("Error reading packet ", err)
return
}
connect, isConn := (*packet).(packets.ConnectPacket)
if !isConn {
log.Println("Didn't recieve a connet packet")
panic("TODO: Send a disconnect packet")
}
conn := NewConnection(connect, con)
sess := NewSession(&conn, connect)
sess.HandlerLoop()
}
func handlePanic() {
if r := recover(); r != nil {
log.Println("Recovering from panic:", r)
log.Println("Stack Trace:")
debug.PrintStack()
}
}