package main import ( "log" "net" "runtime/debug" "badat.dev/maeqtt/v2/mqtt/packets" "badat.dev/maeqtt/v2/session" ) func main() { listen_addr := ":1883" listener, err := net.Listen("tcp", listen_addr) if err != nil { log.Fatal(err) } var sessions map[string]*session.Session = make(map[string]*session.Session) for { conn, err := listener.Accept() if err != nil { log.Println("Failed accepting connection ", err) } handleConnection(conn, sessions) } } func handleConnection(con net.Conn, sessions map[string]*session.Session) { defer handlePanic(con) conReq, err := session.NewConnection(con) if err != nil { // TODO panic(err) } var sess *session.Session if conReq.ConnectPakcet.ClientId != nil { sess, exists := sessions[*conReq.ConnectPakcet.ClientId] if exists { sess.ConnecionChannel <- conReq } } if sess == nil { newSess := session.NewSession(conReq) sess = &newSess go func() { defer handlePanic(con) sess.HandlerLoop() }() } } func handlePanic(con net.Conn) { if r := recover(); r != nil { log.Println("Recovering from panic:", r) log.Println("Stack Trace:") debug.PrintStack() err := packets.DisconnectPacket{ ReasonCode: packets.DisconnectReasonCodeImplErorr, }.Write(con) if err != nil { log.Println("Failed to send a disconnect packet after recovering from panic", err) } } }