Help us shape the future of Go! Take the 2026 Go Developer Survey

getty

package module
v0.3.15-0...-43624e3 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2018 License: Apache-2.0 Imports: 26 Imported by: 0

README

getty


a netty like asynchronous network I/O library

introdction


Getty is a asynchronous network I/O library in golang. Getty is based on "ngo" whose author is sanbit(https://github.com/sanbit).

In getty there are two goroutines in one connection(session), one handle network read buffer tcp/websocket stream, the other handle logic process and write response into network write buffer. If your logic process may take a long time, you should start a new logic process goroutine by yourself in codec.go:(Codec)OnMessage.

You can also handle heartbeat logic in codec.go:(Codec):OnCron. If you use tcp, you should send hearbeat package by yourself, and then invoke session.go:(Session)UpdateActive to update its active time. Please check whether the tcp session has been timeout or not in codec.go:(Codec)OnCron by session.go:(Session)GetActive.

Whatever if you use websocket, you do not need to care about hearbeat request/response because Getty do this task in session.go:(Session)handleLoop by sending/received websocket ping/pong frames. You just need to check whether the websocket session has been timeout or not in codec.go:(Codec)OnCron by session.go:(Session)GetActive.

You can get code example in https://github.com/AlexStocks/getty-examples.

LICENCE


Apache License 2.0

Documentation

Index

Constants

View Source
const (
	CONNECTED_UDP_CLIENT   = 1
	UNCONNECTED_UDP_CLIENT = 2
	TCP_CLIENT             = 3
	WS_CLIENT              = 4
	WSS_CLIENT             = 5
)
View Source
const (
	CompressNone            CompressType = flate.NoCompression      // 0
	CompressZip                          = flate.DefaultCompression // -1
	CompressBestSpeed                    = flate.BestSpeed          // 1
	CompressBestCompression              = flate.BestCompression    // 9
	CompressHuffman                      = flate.HuffmanOnly        // -2
	CompressSnappy                       = 10
)
View Source
const (
	UDP_SERVER = 1
	TCP_SERVER = 2
	WS_SERVER  = 3
	WSS_SERVER = 4
)
View Source
const (
	Version     = "0.8.1"
	DATE        = "2018/03/08"
	GETTY_MAJOR = 0
	GETTY_MINOR = 8
	GETTY_BUILD = 1
)

Variables

View Source
var (
	ErrSessionClosed  = errors.New("session Already Closed")
	ErrSessionBlocked = errors.New("session Full Blocked")
	ErrMsgTooLong     = errors.New("Message Too Long")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// net
	sync.Mutex

	sync.Once
	// contains filtered or unexported fields
}

func NewTCPClient added in v0.8.3

func NewTCPClient(connNum int, connInterval time.Duration, serverAddr string) *Client

NewTcpClient function builds a tcp client. @connNum is connection number. @connInterval is reconnect sleep interval when getty fails to connect the server. @serverAddr is server address.

func NewUDPClient added in v0.8.3

func NewUDPClient(connNum int, connInterval time.Duration, serverAddr string) *Client

NewUdpClient function builds a udp client @connNum is connection number. If this value is non-zero, getty will build some connected udp clients.

@connInterval is reconnect sleep interval when getty fails to connect the server. @serverAddr is server address.

func NewWSClient added in v0.8.3

func NewWSClient(connNum int, connInterval time.Duration, serverAddr string) *Client

NewWsClient function builds a ws client. @connNum is connection number. @connInterval is reconnect sleep interval when getty fails to connect the server. @serverAddr is server address. its prefix should be "ws://".

func NewWSSClient added in v0.8.3

func NewWSSClient(connNum int, connInterval time.Duration, serverAddr string, cert string) *Client

NewWSSClient function builds a wss client. @connNum is connection number. @connInterval is reconnect sleep interval when getty fails to connect the server. @serverAddr is server address. @cert is client certificate file. it can be emtpy. @privateKey is client private key(contains its public key). it can be empty. @caCert is the root certificate file to verify the legitimacy of server

func (*Client) Close

func (c *Client) Close()

func (*Client) IsClosed

func (c *Client) IsClosed() bool

func (*Client) RunEventLoop

func (c *Client) RunEventLoop(newSession NewSessionCallback)

type CompressType added in v0.8.3

type CompressType int

type Connection added in v0.8.3

type Connection interface {
	ID() uint32
	SetCompressType(CompressType)
	LocalAddr() string
	RemoteAddr() string

	// update session's active time
	UpdateActive()
	// get session's active time
	GetActive() time.Time

	// SetReadTimeout sets deadline for the future read calls.
	SetReadTimeout(time.Duration)

	// SetWriteTimeout sets deadline for the future read calls.
	SetWriteTimeout(time.Duration)
	Write(interface{}) (int, error)
	// contains filtered or unexported methods
}

type EventListener

type EventListener interface {
	// invoked when session opened
	// If the return error is not nil, @Session will be closed.
	OnOpen(Session) error

	// invoked when session closed.
	OnClose(Session)

	// invoked when got error.
	OnError(Session, error)

	// invoked periodically, its period can be set by (Session)SetCronPeriod
	OnCron(Session)

	// invoked when receive packge. Pls attention that do not handle long time logic processing in this func.
	// You'd better set the package's maximum length. If the message's length is greater than it, u should
	// should return err in Reader{Read} and getty will close this connection soon.
	OnMessage(Session, interface{})
}

EventListener is used to process pkg that received from remote session

type NewSessionCallback

type NewSessionCallback func(Session) error

NewSessionCallback will be invoked when server accepts a new client connection or client connects to server successfully. If there are too many client connections or u do not want to connect a server again, u can return non-nil error. And then getty will close the new session.

type ReadWriter

type ReadWriter interface {
	Reader
	Writer
}

tcp package handler interface

type Reader

type Reader interface {
	// Parse tcp/udp/websocket pkg from buffer and if possible return a complete pkg
	// If length of buf is not long enough, u should return {nil,0, nil}
	// The second return value is the length of the pkg.
	Read(Session, []byte) (interface{}, int, error)
}

Reader is used to unmarshal a complete pkg from buffer

type Server

type Server struct {
	sync.Once
	// contains filtered or unexported fields
}

func NewTCPServer added in v0.8.3

func NewTCPServer(addr string) *Server

NewTCServer builds a tcp server. @addr server listen address.

func NewUDPPServer

func NewUDPPServer(addr string) *Server

NewUDPServer builds a unconnected udp server. @addr server listen address.

func NewWSSServer added in v0.8.3

func NewWSSServer(addr, path, cert, privateKey, caCert string) *Server

NewWSSServer builds a secure websocket server. @addr server listen address. @path: websocket request url path @cert: server certificate file @privateKey: server private key(contains its public key) @caCert: root certificate file. to verify the legitimacy of client. it can be nil.

func NewWSServer added in v0.8.3

func NewWSServer(addr string, path string) *Server

NewWSServer builds a websocket server. @addr server listen address. @path: websocket request url path

func (*Server) Close

func (s *Server) Close()

func (*Server) IsClosed

func (s *Server) IsClosed() bool

func (*Server) Listener

func (s *Server) Listener() net.Listener

func (*Server) RunEventloop

func (s *Server) RunEventloop(newSession NewSessionCallback)

RunEventloop serves client request. @newSession: new connection callback

type Session

type Session interface {
	Connection
	Reset()
	Conn() net.Conn
	Stat() string
	IsClosed() bool

	SetMaxMsgLen(int)
	SetName(string)
	SetEventListener(EventListener)
	SetPkgHandler(ReadWriter)
	SetReader(Reader)
	SetWriter(Writer)
	SetCronPeriod(int)
	SetRQLen(int)
	SetWQLen(int)
	SetWaitTime(time.Duration)

	GetAttribute(interface{}) interface{}
	SetAttribute(interface{}, interface{})
	RemoveAttribute(interface{})

	WritePkg(interface{}, time.Duration) error
	WriteBytes([]byte) error
	WriteBytesArray(...[]byte) error
	Close()
}

func NewSession

func NewSession() Session

func NewTCPSession

func NewTCPSession(conn net.Conn) Session

func NewUDPSession

func NewUDPSession(conn *net.UDPConn, peerAddr *net.UDPAddr) Session

func NewWSSession

func NewWSSession(conn *websocket.Conn) Session

type UDPContext added in v0.8.3

type UDPContext struct {
	Pkg      []byte
	PeerAddr *net.UDPAddr
}

type Writer

type Writer interface {
	Write(Session, interface{}) error
}

Writer is used to marshal pkg and write to session

Jump to

Keyboard shortcuts

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