tcp

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2018 License: MIT Imports: 7 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{
		"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: "网络协议"},
	},
	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) {
			m.Start(fmt.Sprintf("pub%d", m.Capi("nlisten", 1)), "网络监听", m.Meta["detail"]...)
		}},
		"accept": &ctx.Command{Name: "accept address [security [protocol]]", Help: "网络连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			m.Start(fmt.Sprintf("com%d", m.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)
		}},
		"dial": &ctx.Command{Name: "dial address [security [protocol]]", Help: "网络连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			m.Start(fmt.Sprintf("com%d", m.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)
		}},
		"send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			if tcp, ok := m.Target().Server.(*TCP); m.Assert(ok) {
				tcp.Write([]byte(arg[0]))
			}
		}},
		"recv": &ctx.Command{Name: "recv size", Help: "接收消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			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]))
			}
		}},
		"ifconfig": &ctx.Command{Name: "ifconfig", Help: "网络配置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			if ifs, e := net.Interfaces(); m.Assert(e) {
				for _, v := range ifs {
					if ips, e := v.Addrs(); m.Assert(e) {
						for _, x := range ips {
							ip := x.String()
							if !strings.Contains(ip, ":") && len(ip) > 0 && len(v.HardwareAddr) > 0 {
								m.Add("append", "index", v.Index)
								m.Add("append", "name", v.Name)
								m.Add("append", "hard", v.HardwareAddr)
								m.Add("append", "ip", ip)
							}
						}
					}
				}
				m.Table()
			}
		}},
	},
}

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) 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