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() } }