forked from x/icebergs
92 lines
2.2 KiB
Go
92 lines
2.2 KiB
Go
package tcp
|
|
|
|
import (
|
|
"net"
|
|
|
|
ice "shylinux.com/x/icebergs"
|
|
"shylinux.com/x/icebergs/base/mdb"
|
|
kit "shylinux.com/x/toolkits"
|
|
"shylinux.com/x/toolkits/logs"
|
|
)
|
|
|
|
func _broad_addr(m *ice.Message, host, port string) *net.UDPAddr {
|
|
if addr, e := net.ResolveUDPAddr("udp4", kit.Format("%s:%s", host, port)); !m.Warn(e, ice.ErrNotValid, host, port, logs.FileLineMeta(2)) {
|
|
return addr
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Listener struct {
|
|
m *ice.Message
|
|
h string
|
|
s *Stat
|
|
|
|
net.Listener
|
|
}
|
|
|
|
func (l Listener) Accept() (net.Conn, error) {
|
|
c, e := l.Listener.Accept()
|
|
l.s.nc += 1
|
|
return &Conn{m: l.m, s: &Stat{}, Conn: c}, e
|
|
}
|
|
func (l Listener) Close() error {
|
|
l.m.Cmd(mdb.MODIFY, SERVER, "", mdb.HASH, mdb.HASH, l.h, STATUS, CLOSE)
|
|
return l.Listener.Close()
|
|
}
|
|
|
|
func _server_udp(m *ice.Message, arg ...string) {
|
|
l, e := net.ListenUDP("udp4", _broad_addr(m, "0.0.0.0", m.Option(PORT)))
|
|
defer l.Close()
|
|
mdb.HashCreate(m, arg, kit.Dict(mdb.TARGET, l), STATUS, kit.Select(ERROR, OPEN, e == nil), ERROR, kit.Format(e))
|
|
switch cb := m.OptionCB("").(type) {
|
|
case func(*net.UDPConn):
|
|
m.Assert(e)
|
|
cb(l)
|
|
}
|
|
}
|
|
func _server_listen(m *ice.Message, arg ...string) {
|
|
if m.Option("type") == "udp4" {
|
|
_server_udp(m, arg...)
|
|
return
|
|
}
|
|
l, e := net.Listen(TCP, m.Option(HOST)+":"+m.Option(PORT))
|
|
l = &Listener{m: m, h: mdb.HashCreate(m, arg, kit.Dict(mdb.TARGET, l), STATUS, kit.Select(ERROR, OPEN, e == nil), ERROR, kit.Format(e)), s: &Stat{}, Listener: l}
|
|
if e == nil {
|
|
defer l.Close()
|
|
}
|
|
switch cb := m.OptionCB("").(type) {
|
|
case func(net.Listener):
|
|
m.Assert(e)
|
|
cb(l)
|
|
case func(net.Conn):
|
|
for {
|
|
if c, e := l.Accept(); e == nil {
|
|
cb(c)
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
default:
|
|
m.ErrorNotImplement(cb)
|
|
}
|
|
}
|
|
|
|
const (
|
|
PROTOCOL = "protocol"
|
|
HOSTPORT = "hostport"
|
|
HOSTNAME = "hostname"
|
|
NODENAME = "nodename"
|
|
)
|
|
const (
|
|
LISTEN = "listen"
|
|
)
|
|
const SERVER = "server"
|
|
|
|
func init() {
|
|
Index.MergeCommands(ice.Commands{
|
|
SERVER: {Name: "server hash auto prunes", Help: "服务器", Actions: ice.MergeActions(ice.Actions{
|
|
LISTEN: {Name: "listen type name port=9030 host=", Hand: func(m *ice.Message, arg ...string) { _server_listen(m, arg...) }},
|
|
}, mdb.StatusHashAction(mdb.FIELD, "time,hash,status,type,name,host,port,error,nconn"), mdb.ClearOnExitHashAction())},
|
|
})
|
|
}
|