network

package
v0.0.0-...-7c60c3b Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2019 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NET_Paylen = 4
	NET_Minlen = 4
	NET_Maxlen = 1024 * 1024 * 5
)

默认数据

View Source
const (
	WEB_LINK = "web"
	TCP_LINK = "tcp"
	UDP_LINK = "udp"
)

Variables

View Source
var (
	DefaultOptions = Options{
		OnClose: func(obj interface{}, err error) {
			fmt.Println("miss close: [ class", reflect.TypeOf(obj).String(), "] err =", err)
		},
	}
)

Functions

func DefaultHandler

func DefaultHandler(conn net.Conn) error

func DialScan

func DialScan(kind string, addr string) (net.Conn, error)

自选类型

func Listen

func Listen(addr string) net.Listener

默认tcp

func OnHandler

func OnHandler(handle Handler, addrs ...string)

func UnHandler

func UnHandler(addrs ...string)

Types

type CloseFunc

type CloseFunc func()

*close web func

func StartTcpServer

func StartTcpServer(addr string, args ...Option) (cancel CloseFunc, err error)

* quick start tcp server

func StartWebServer

func StartWebServer(addr string, args ...Option) (cancel CloseFunc, err error)

* quick start web server

type ConnSet

type ConnSet map[net.Conn]struct{}

type Handler

type Handler func(net.Conn) error

func GetHandler

func GetHandler(addr string) (handle Handler)

type ListenSet

type ListenSet map[net.Listener]struct{}

type Manager

type Manager interface {
	net.Listener
	Dial(string) error
}

连接器管理(目前没啥用,actor里面已经实现)

func NewManager

func NewManager(addr string) Manager

可以看作统一类型的连接管理器

type Option

type Option func(*Options)

func OptionHandler

func OptionHandler(fn func(net.Conn) error) Option

options

func OptionOnClose

func OptionOnClose(fn func(interface{}, error)) Option

type Options

type Options struct {
	Handler
	OnClose func(interface{}, error)
}

func NewOptions

func NewOptions(opts ...Option) *Options

func (Options) Copy

func (this Options) Copy(opts ...Option) *Options

func (*Options) Exchange

func (this *Options) Exchange(addr string) (handle Handler)

func (*Options) Init

func (this *Options) Init(opts ...Option) *Options

type Server

type Server struct {
	// contains filtered or unexported fields
}

* class 用来管理端口(同一监听同一处理方式) * 需要统计

func (*Server) Close

func (this *Server) Close() (err error)

func (*Server) Serve

func (this *Server) Serve(ln net.Listener, handle Handler) (err error)

func (*Server) Start

func (this *Server) Start(addr string) (err error)

快速启动(默认为tcp服务)

func (*Server) Wrap

func (this *Server) Wrap(fn func())

go func

type SocketPacket

type SocketPacket struct {
	bytes.ByteArray
	Topic int
	MsgID int64 //默认异步(0)
}

* 基础网络包 * msgID: 0表示异步, >0表示同步消息,(必须回传, 不回传会导致客户端阻塞)

func ReadBegin

func ReadBegin(b []byte) *SocketPacket

* 读包

func WriteBegin

func WriteBegin(cmd int, args ...interface{}) *SocketPacket

* 写包(这里可以通过规定增长提高效率)

func WriteCopy

func WriteCopy(pack *SocketPacket, args ...interface{}) *SocketPacket

同样返回

func WriteSync

func WriteSync(id int64, cmd int, args ...interface{}) *SocketPacket

同步回复

func (*SocketPacket) Cmd

func (this *SocketPacket) Cmd() int

func (*SocketPacket) Flush

func (this *SocketPacket) Flush() []byte

func (*SocketPacket) ReadBegin

func (this *SocketPacket) ReadBegin()

read

func (*SocketPacket) String

func (this *SocketPacket) String() string

func (*SocketPacket) Sync

func (this *SocketPacket) Sync() bool

func (*SocketPacket) With

func (this *SocketPacket) With(args ...interface{}) *SocketPacket

func (*SocketPacket) WriteBegin

func (this *SocketPacket) WriteBegin() *SocketPacket

write

type Stream

type Stream interface {
	//io
	Read() ([]byte, error)
	Write([]byte) error
	Close() error
	//other
	SetSendTimeout(time.Duration) error
	SetReadTimeout(time.Duration) error
	//addr
	Address() string      //远端地址
	LocalAddress() string //本地的地址
}

通用的socket

func With

func With(conn net.Conn) Stream

new

func WithAddr

func WithAddr(addr string) Stream

type Symbiote

type Symbiote interface {
	Start(string) error
	Serve(net.Listener, Handler) error
	Close() error
}

服务

func NewServer

func NewServer(opts ...Option) Symbiote

*用于管理所有的link *可以开启多个服务, 但是不能单独关闭,如果你想单独关闭,使用join

Jump to

Keyboard shortcuts

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