Skip to content

Commit

Permalink
xtcp add net.Listener arg
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyuesong committed Oct 21, 2022
1 parent c7a8a29 commit a0adf1d
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions xtcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,29 @@ type TCP struct {
// Parser(string, []byte) ([][]byte, error),
// })
func New(v interface{}) *TCP {
srv := &TCP{
session: map[string]*Conn{},
receive: make(chan *reqMessage, 50),
}
var conf *Config

if _conf, ok := v.(*Config); ok {
conf = _conf
} else if addr, ok := v.(string); ok {
conf = &Config{
Addr: addr,
Network: "tcp",
}
} else if listener, ok := v.(net.Listener); ok {
conf = &Config{}
srv.listener = listener
}
if conf.MsgPkg == nil {
conf.MsgPkg = &DefaultPkgProto{}
}
srv.Config = conf

return &TCP{
Config: conf,
session: map[string]*Conn{},
receive: make(chan *reqMessage, 50),
}
return srv
}

// Srv 使用该适配器创建命令消息服务
Expand Down Expand Up @@ -117,6 +122,9 @@ func (t *TCP) Run() error {
}

func (t *TCP) listen() error {
if t.listener != nil {
return nil
}
listener, err := net.Listen(t.Config.Network, t.Config.Addr)
t.listener = listener
return err
Expand Down

0 comments on commit a0adf1d

Please sign in to comment.