You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nnet/server_tcp.go

41 lines
954 B
Go

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