air

package module
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

README

air

介绍

TCP框架,内部协议, 支持事件,分组,中间件

使用说明
  1. go get gitee.com/sdxstar/air

`

s, err := air.NewServer(&air.Options{
	Address: address,
})

if err != nil {
	t.Fatal(err)
}

s.On(air.OnConnect, func(ctx air.Context) error {
	t.Logf("%s connected, start to check auth", ctx.RemoteAddr())
	var reply string
	err := ctx.Ack("check_auth", nil, &reply)
	if err != nil {
		t.Error(err)
		ctx.Close()
		return nil
	}
	t.Logf("%s", reply)
	return nil
})

s.On(air.OnHeartbeat, func(ctx air.Context) error {
	return nil
})

s.On(air.OnDisconnect, func(ctx air.Context) error {
	return nil
})

s.On(air.OnClose, func(ctx air.Context) error {
	return nil
})

s.Use(middleware.Recover())

s.On("auth", func(ctx air.Context) error {
	t.Logf("%s auth", ctx.RemoteAddr())
	var auth Auth
	err := ctx.Bind(&auth)
	if err != nil {
		ctx.Close()
		return nil
	}
	ctx.Set("Hostname", auth.Hostname)
	ctx.Join("test")
	return nil
})

authGroup := s.Group(CtxAuth())

    authGroup.On("hardware", func(ctx air.Context) error {

        t.Logf("on hardware: ")
	var hardware Hardware
	if err := ctx.Bind(&hardware); err != nil {
		ctx.Close()
		return nil
	}
	conn.BroadcastTo("test", "", []byte("good"))
	return ctx.Reply("OK")
})

s.Start()

`

Documentation

Index

Constants

View Source
const (
	MaxWorkId = -1 ^ (-1 << numWorkerBits)

	MaxSequence = -1 ^ (-1 << numSequenceBits)
)

Variables

View Source
var (
	ErrorMustBePointer     = errors.New("value must be a pointer")
	ErrorWrongRequestType  = errors.New("value must be a string/[]byte/struct")
	ErrorWrongResponseType = errors.New("value must be a pointer of string/[]byte/struct")
)
View Source
var (
	ErrConnClosing    = errors.New("use of closed network connection")
	ErrMessageTimeout = errors.New("timeout")
)

Functions

This section is empty.

Types

type Codec

type Codec interface {
	Encode(v interface{}) (msg []byte, err error)
	Decode(reply []byte, r interface{}) (err error)
}

func NewDefaultCodec

func NewDefaultCodec() Codec

type Conn

type Conn interface {
	Id() string
	IsClosed() bool
	Close()
	Get(key string) interface{}
	Set(key string, value interface{})
	RemoteAddr() net.Addr

	Emit(event string, v interface{}) error
	Ack(event string, v interface{}, r interface{}) error
	BroadcastToAll(event string, msg []byte)
	BroadcastTo(room, event string, msg []byte)
	Join(room string)
	Leave(room string)
}

type ConsoleLog

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

func NewConsoleLog

func NewConsoleLog() *ConsoleLog

func (ConsoleLog) Debug

func (d ConsoleLog) Debug(v ...interface{})

func (ConsoleLog) Error

func (d ConsoleLog) Error(v ...interface{})

func (ConsoleLog) Fatal

func (d ConsoleLog) Fatal(v ...interface{})

func (ConsoleLog) Info

func (d ConsoleLog) Info(v ...interface{})

func (ConsoleLog) Panic

func (d ConsoleLog) Panic(v ...interface{})

func (ConsoleLog) Warn

func (d ConsoleLog) Warn(v ...interface{})

type Context

type Context interface {
	Id() string
	IsClosed() bool
	Close()
	Get(key string) interface{}
	Set(key string, value interface{})
	RemoteAddr() net.Addr

	Emit(event string, v interface{}) error
	Ack(event string, v interface{}, r interface{}) error
	BroadcastToAll(event string, msg []byte)
	BroadcastTo(room, event string, msg []byte)
	Join(room string)
	Leave(room string)

	Content() []byte
	Packet() Packet
	Bind(v interface{}) error

	Reply(v interface{}) error

	Conn() Conn

	Logger() Logger
}

type DefaultCodec

type DefaultCodec struct {
}

func (*DefaultCodec) Decode

func (c *DefaultCodec) Decode(reply []byte, r interface{}) (err error)

func (*DefaultCodec) Encode

func (c *DefaultCodec) Encode(v interface{}) (msg []byte, err error)

type Event

type Event = string
const (
	OnConnect    Event = "connect"
	OnJoin       Event = "join"
	OnLeave      Event = "leave"
	OnDisconnect Event = "disconnect"
	OnClose      Event = "close"
	OnHeartbeat  Event = "heartbeat"
)

type EventHandler

type EventHandler func(ctx Context) error

type Group

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

func NewGroup

func NewGroup(s *Server) *Group

func (*Group) Group

func (g *Group) Group(middleware ...MiddlewareFunc) (sg *Group)

func (*Group) Handler

func (g *Group) Handler(e string) EventHandler

func (*Group) On

func (g *Group) On(e string, fn EventHandler)

func (*Group) Use

func (g *Group) Use(middleware ...MiddlewareFunc)

type IdGenerator

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

func NewSnowFlake

func NewSnowFlake(workerId uint16) (*IdGenerator, error)

New returns a new snowflake node that can be used to generate snowflake

func (*IdGenerator) Generate

func (sf *IdGenerator) Generate() uint64

Next creates and returns a unique snowflake ID

type Logger

type Logger interface {
	Info(v ...interface{})
	Debug(v ...interface{})
	Warn(v ...interface{})
	Error(v ...interface{})
	Fatal(v ...interface{})
	Panic(v ...interface{})
}

type MessageKey

type MessageKey = uint16
const (
	MessageKeyHeartbeat   MessageKey = 0xffff // MessageKeyHeartbeat heartbeat message
	MessageKeyEmit        MessageKey = iota   // MessageKeyEmit Emit request, no response
	MessageKeyAckRequest                      // MessageKeyAckRequest Emit request, wait for response (ack)
	MessageKeyAckResponse                     // MessageKeyAckResponse ack response
)

type MiddlewareFunc

type MiddlewareFunc func(EventHandler) EventHandler

type Options

type Options struct {
	// common
	WorkId                 uint16
	Address                string
	PacketSendChanLimit    uint32
	PacketReceiveChanLimit uint32
	MessageTimeout         time.Duration
	Codec                  Codec
	Logger                 Logger

	// only for server
	HeartbeatTimeout time.Duration
}

func DefaultOptions

func DefaultOptions() *Options

type Packet

type Packet interface {
	Serialize() []byte
	Key() uint16
	Id() uint64
	Event() string
	Body() []byte
	NewReplyPacket(data []byte) Packet
}

func NewHeartbeatPacket

func NewHeartbeatPacket() Packet

func NewPacket

func NewPacket(key MessageKey, id uint64, event string, data []byte) Packet

func ReadPacket

func ReadPacket(r io.Reader) (Packet, error)

type Router

type Router interface {
	Use(args ...MiddlewareFunc)
	On(e string, fn EventHandler)
	Handler(e string) EventHandler
	// contains filtered or unexported methods
}

type Server

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

func NewServer

func NewServer(opts *Options) (srv *Server, err error)

func (*Server) Close

func (s *Server) Close()

func (*Server) Group

func (s *Server) Group(m ...MiddlewareFunc) (g *Group)

func (*Server) Handler

func (s *Server) Handler(e string) EventHandler

func (*Server) On

func (s *Server) On(e string, fn EventHandler)

func (*Server) SetLog

func (s *Server) SetLog(log Logger)

func (*Server) SetRouter

func (e *Server) SetRouter(event string, router Router)

func (*Server) Start

func (s *Server) Start()

Start starts service

func (*Server) Use

func (s *Server) Use(middleware ...MiddlewareFunc)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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