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.
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
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
|
|
}
|
|
|
|
err = ngin.pool.Submit(func() {
|
|
ngin.handle(rc)
|
|
})
|
|
if err != nil {
|
|
nlog.Errorf("%s submit conn pool err: %ng", ngin.LogPrefix(), err.Error())
|
|
continue
|
|
}
|
|
}
|
|
}
|