1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-26 01:24:05 +08:00
icebergs/base/tcp/client.go
2024-02-09 11:30:20 +08:00

63 lines
1.4 KiB
Go

package tcp
import (
"net"
"time"
ice "shylinux.com/x/icebergs"
"shylinux.com/x/icebergs/base/mdb"
"shylinux.com/x/icebergs/base/nfs"
kit "shylinux.com/x/toolkits"
)
type Conn struct {
net.Conn
m *ice.Message
h string
s *Stat
}
func (c *Conn) Read(b []byte) (int, error) {
n, e := c.Conn.Read(b)
c.s.nr += n
return n, e
}
func (c *Conn) Write(b []byte) (int, error) {
n, e := c.Conn.Write(b)
c.s.nw += n
return n, e
}
func (c *Conn) Close() error { return c.Conn.Close() }
func _client_dial(m *ice.Message, arg ...string) {
c, e := net.DialTimeout(TCP, m.Option(HOST)+nfs.DF+m.Option(PORT), 3*time.Second)
c = &Conn{Conn: c, m: m, s: &Stat{}}
defer kit.If(e == nil, func() { c.Close() })
switch cb := m.OptionCB("").(type) {
case func(net.Conn):
kit.If(!m.WarnNotValid(e), func() { cb(c) })
default:
m.ErrorNotImplement(cb)
}
}
const (
DIAL = "dial"
)
const CLIENT = "client"
func init() {
Index.MergeCommands(ice.Commands{
CLIENT: {Help: "客户端", Actions: ice.MergeActions(ice.Actions{
DIAL: {Name: "dial type name port=9010 host=", Help: "连接", Hand: func(m *ice.Message, arg ...string) {
switch m.Option(mdb.TYPE) {
case UDP4:
_client_dial_udp4(m, arg...)
default:
_client_dial(m, arg...)
}
}},
}, mdb.StatusHashAction(mdb.FIELD, "time,hash,status,type,name,host,port,error"), mdb.ClearOnExitHashAction())},
})
}