package nnet import ( "errors" "git.noahlan.cn/noahlan/nnet/config" "git.noahlan.cn/noahlan/ntool/nlog" "net" ) func (ngin *Engine) ListenTCP(conf config.TCPServerConf) error { err := ngin.setup() if err != nil { nlog.Errorf("%s failed to setup server, err:%v", ngin.LogPrefix(), err) return err } listener, err := net.Listen(conf.Protocol, conf.Addr) if err != nil { nlog.Errorf("%s failed to listening at [%s %s] %v", ngin.LogPrefix(), conf.Protocol, conf.Addr, err) return err } nlog.Infof("%s now listening %s at %s...", ngin.LogPrefix(), conf.Protocol, conf.Addr) defer func() { _ = listener.Close() ngin.Stop() }() for { rc, err := listener.Accept() if err != nil { if errors.Is(err, net.ErrClosed) { nlog.Errorf("%s connection closed, err:%v", ngin.LogPrefix(), err) return err } nlog.Errorf("%s accept connection failed, err:%v", ngin.LogPrefix(), err) continue } go ngin.handle(rc) } }