tcp

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2019 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
	Caches: map[string]*ctx.Cache{
		"nlisten": &ctx.Cache{Name: "nlisten", Value: "0", Help: "监听数量"},
		"nclient": &ctx.Cache{Name: "nclient", Value: "0", Help: "连接数量"},
	},
	Configs: map[string]*ctx.Config{
		"":         &ctx.Config{Name: "address", Value: ":9090", Help: "网络地址"},
		"address":  &ctx.Config{Name: "address", Value: ":9090", Help: "网络地址"},
		"security": &ctx.Config{Name: "security(true/false)", Value: "false", Help: "加密通信"},
		"protocol": &ctx.Config{Name: "protocol(tcp/tcp4/tcp6)", Value: "tcp4", Help: "网络协议"},
		"retry": &ctx.Config{Name: "retry", Value: map[string]interface{}{
			"interval": "3s", "counts": 3,
		}, Help: "网络协议"},
	},
	Commands: map[string]*ctx.Command{
		"listen": &ctx.Command{Name: "listen address [security [protocol]]", Help: "网络监听", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
			m.Start(fmt.Sprintf("pub%d", m.Capi("nlisten", 1)), "网络监听", m.Meta["detail"]...)
			return
		}},
		"accept": &ctx.Command{Name: "accept address [security [protocol]]", Help: "网络连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
			m.Start(fmt.Sprintf("com%d", m.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)
			return
		}},
		"dial": &ctx.Command{Name: "dial address [security [protocol]]", Help: "网络连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
			m.Start(fmt.Sprintf("com%d", m.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)
			return
		}},
		"send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
			if tcp, ok := m.Target().Server.(*TCP); m.Assert(ok) {
				tcp.Write([]byte(arg[0]))
			}
			return
		}},
		"recv": &ctx.Command{Name: "recv size", Help: "接收消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
			if tcp, ok := m.Target().Server.(*TCP); m.Assert(ok) {
				n, e := strconv.Atoi(arg[0])
				m.Assert(e)
				buf := make([]byte, n)

				n, _ = tcp.Read(buf)
				m.Echo(string(buf[:n]))
			}
			return
		}},
		"ifconfig": &ctx.Command{Name: "ifconfig [name]", Help: "网络配置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
			if ifs, e := net.Interfaces(); m.Assert(e) {
				for _, v := range ifs {
					if len(arg) > 0 && !strings.Contains(v.Name, arg[0]) {
						continue
					}
					if ips, e := v.Addrs(); m.Assert(e) {
						for _, x := range ips {
							ip := strings.Split(x.String(), "/")
							if strings.Contains(ip[0], ":") || len(ip) == 0 {
								continue
							}
							if len(v.HardwareAddr.String()) == 0 {
								continue
							}

							m.Add("append", "index", v.Index)
							m.Add("append", "name", v.Name)
							m.Add("append", "ip", ip[0])
							m.Add("append", "mask", ip[1])
							m.Add("append", "hard", v.HardwareAddr.String())
						}
					}
				}
				m.Table()
			}
			return
		}},
		"probe": &ctx.Command{Name: "probe [port]", Help: "端口检测", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) {
			if t, e := net.DialTimeout("tcp", arg[0], 10*time.Second); e == nil {
				m.Echo("active")
				t.Close()
			}
			return
		}},
	},
}

Functions

This section is empty.

Types

type TCP

type TCP struct {
	net.Conn
	net.Listener
	*ctx.Context
}

func (*TCP) Begin

func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server

func (*TCP) Close

func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool

func (*TCP) Fuck added in v1.1.1

func (tcp *TCP) Fuck(address []string, action func(address string) (net.Conn, error))

func (*TCP) Read added in v1.1.0

func (tcp *TCP) Read(b []byte) (n int, e error)

func (*TCP) Spawn

func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server

func (*TCP) Start

func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool

func (*TCP) Write added in v1.1.0

func (tcp *TCP) Write(b []byte) (n int, e error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL