getty

package module
v0.3.14 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2016 License: Apache-2.0 Imports: 11 Imported by: 0

README

getty


a netty like asynchronous network I/O library

introdction


DESC : a asynchronous network I/O library in golang. In getty there are two goroutines in one connection(session), one handle network read buffer tcp 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):OnMessage. Getty is based on "ngo" whose author is sanbit(https://github.com/sanbit).

LICENCE : Apache License 2.0

Documentation

Index

Constants

View Source
const (
	Version     = "0.3.14"
	GETTY_MAJOR = 0
	GETTY_MINOR = 3
	GETTY_BUILD = 14
)

Variables

View Source
var (
	ErrSessionClosed  = errors.New("Session Already Closed")
	ErrSessionBlocked = errors.New("Session full blocked")
)
View Source
var (
	ErrInvalidConnection = errors.New("connection has been closed.")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// net
	sync.Mutex

	sync.Once
	// contains filtered or unexported fields
}

func NewClient added in v0.3.14

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

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

func (*Client) Close added in v0.3.14

func (this *Client) Close()

func (*Client) IsClosed added in v0.3.14

func (this *Client) IsClosed() bool

func (*Client) RunEventLoop added in v0.3.14

func (this *Client) RunEventLoop(newSession NewSessionCallback)

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. when @Session got error, session will not invoke @OnClose but invoke @OnError.
	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.
	// Y'd better set the package's maximum length. If the message's length is greater than it, u should
	// should return err and getty will close this connection soon.
	OnMessage(*Session, interface{})
}

EventListener is used to process pkg that recved 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
}

packet handler interface

type Reader

type Reader interface {
	// Parse 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 NewServer added in v0.3.14

func NewServer() *Server

func (*Server) Accept added in v0.3.14

func (this *Server) Accept(newSession NewSessionCallback) (*Session, error)

func (*Server) Bind added in v0.3.14

func (this *Server) Bind(network string, host string, port int) error

(Server)Bind's functionality is equal to (Server)Listen.

func (*Server) Close added in v0.3.14

func (this *Server) Close()

func (*Server) IsClosed added in v0.3.14

func (this *Server) IsClosed() bool

func (*Server) Listen added in v0.3.14

func (this *Server) Listen(network string, addr string) error

net.ipv4.tcp_max_syn_backlog net.ipv4.tcp_timestamps net.ipv4.tcp_tw_recycle

func (*Server) Listener added in v0.3.14

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

func (*Server) RunEventloop added in v0.3.14

func (this *Server) RunEventloop(newSession NewSessionCallback)

type Session

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

getty base session

func NewSession added in v0.3.14

func NewSession(conn net.Conn) *Session

func (*Session) Close

func (this *Session) Close()

this function will be invoked by NewSessionCallback(if return error is not nil) or (Session)handleLoop automatically. It is goroutine-safe to be invoked many times.

func (*Session) Conn

func (this *Session) Conn() net.Conn

func (*Session) GetAttribute

func (this *Session) GetAttribute(key string) interface{}

func (*Session) IsClosed

func (this *Session) IsClosed() bool

check whether the session has been closed.

func (*Session) RemoveAttribute

func (this *Session) RemoveAttribute(key string)

func (*Session) Reset

func (this *Session) Reset()

func (*Session) RunEventLoop added in v0.3.14

func (this *Session) RunEventLoop()

func (*Session) SetAttribute

func (this *Session) SetAttribute(key string, value interface{})

func (*Session) SetConn added in v0.3.14

func (this *Session) SetConn(conn net.Conn)

func (*Session) SetCronPeriod

func (this *Session) SetCronPeriod(period int)

period is in millisecond

func (*Session) SetEventListener

func (this *Session) SetEventListener(listener EventListener)

func (*Session) SetName

func (this *Session) SetName(name string)

func (*Session) SetPkgHandler

func (this *Session) SetPkgHandler(handler ReadWriter)

set package handler

func (*Session) SetRQLen added in v0.3.14

func (this *Session) SetRQLen(readQLen int)

set @Session's read queue size

func (*Session) SetReadDeadline added in v0.3.14

func (this *Session) SetReadDeadline(rDeadline time.Duration)

SetReadDeadline sets deadline for the future read calls.

func (*Session) SetWQLen added in v0.3.14

func (this *Session) SetWQLen(writeQLen int)

set @Session's write queue size

func (*Session) SetWaitTime

func (this *Session) SetWaitTime(waitTime time.Duration)

set maximum wait time when session got error or got exit signal

func (*Session) SetWriteDeadline added in v0.3.14

func (this *Session) SetWriteDeadline(wDeadline time.Duration)

SetWriteDeadlile sets deadline for the future read calls.

func (*Session) Stat

func (this *Session) Stat() string

return the connect statistic data

func (*Session) WriteBytes

func (this *Session) WriteBytes(pkg []byte) error

for codecs

func (*Session) WritePkg

func (this *Session) WritePkg(pkg interface{}) error

Queued write, for handler

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