Documentation ¶
Index ¶
- Constants
- Variables
- func GetTimeWheel() *gxtime.Wheel
- func IsSameAddr(a1, a2 net.Addr) bool
- func SetLogger(logger Logger)
- func SetLoggerCallerDisable() error
- func SetLoggerLevel(level LoggerLevel) error
- type Client
- type ClientOption
- type ClientOptions
- type CompressType
- type Connection
- type EndPoint
- type EndPointID
- type EndPointType
- type EventListener
- type Logger
- type LoggerLevel
- type NewSessionCallback
- type ReadWriter
- type Reader
- type Server
- type ServerOption
- type ServerOptions
- type Session
- type UDPContext
- type Values
- type ValuesContext
- type Writer
Constants ¶
const ( CompressNone CompressType = flate.NoCompression // 0 CompressZip = flate.DefaultCompression // -1 CompressBestSpeed = flate.BestSpeed // 1 CompressBestCompression = flate.BestCompression // 9 CompressHuffman = flate.HuffmanOnly // -2 CompressSnappy = 10 )
const ( // DebugLevel logs are typically voluminous, and are usually disabled in // production. LoggerLevelDebug = LoggerLevel(zapcore.DebugLevel) // InfoLevel is the default logging priority. LoggerLevelInfo = LoggerLevel(zapcore.InfoLevel) // WarnLevel logs are more important than Infof, but don't need individual // human review. LoggerLevelWarn = LoggerLevel(zapcore.WarnLevel) // ErrorLevel logs are high-priority. If an application is running smoothly, // it shouldn't generate any error-level logs. LoggerLevelError = LoggerLevel(zapcore.ErrorLevel) // DPanicLevel logs are particularly important errors. In development the // logger panics after writing the message. LoggerLevelDPanic = LoggerLevel(zapcore.DPanicLevel) // PanicLevel logs a message, then panics. LoggerLevelPanic = LoggerLevel(zapcore.PanicLevel) // FatalLevel logs a message, then calls os.Exit(1). LoggerLevelFatal = LoggerLevel(zapcore.FatalLevel) )
Variables ¶
var ( ErrSessionClosed = perrors.New("session Already Closed") ErrSessionBlocked = perrors.New("session Full Blocked") ErrNullPeerAddr = perrors.New("peer address is nil") )
var EndPointType_name = map[int32]string{
0: "UDP_ENDPOINT",
1: "UDP_CLIENT",
2: "TCP_CLIENT",
3: "WS_CLIENT",
4: "WSS_CLIENT",
7: "TCP_SERVER",
8: "WS_SERVER",
9: "WSS_SERVER",
}
var EndPointType_value = map[string]int32{
"UDP_ENDPOINT": 0,
"UDP_CLIENT": 1,
"TCP_CLIENT": 2,
"WS_CLIENT": 3,
"WSS_CLIENT": 4,
"TCP_SERVER": 7,
"WS_SERVER": 8,
"WSS_SERVER": 9,
}
Functions ¶
func GetTimeWheel ¶
func IsSameAddr ¶
refers from https://github.com/facebookgo/grace/blob/master/gracenet/net.go#L180:6
func SetLoggerCallerDisable ¶
func SetLoggerCallerDisable() error
SetLoggerCallerDisable: disable caller info in production env for performance improve. It is highly recommended that you execute this method in a production environment.
Types ¶
type Client ¶
type Client interface { EndPoint }
func NewTCPClient ¶
func NewTCPClient(opts ...ClientOption) Client
NewTcpClient function builds a tcp client.
func NewUDPClient ¶
func NewUDPClient(opts ...ClientOption) Client
NewUdpClient function builds a connected udp client
func NewWSClient ¶
func NewWSClient(opts ...ClientOption) Client
NewWsClient function builds a ws client.
func NewWSSClient ¶
func NewWSSClient(opts ...ClientOption) Client
NewWSSClient function builds a wss client.
type ClientOption ¶
type ClientOption func(*ClientOptions)
func WithConnectionNumber ¶
func WithConnectionNumber(num int) ClientOption
@num is connection number.
func WithReconnectInterval ¶
func WithReconnectInterval(reconnectInterval int) ClientOption
@reconnectInterval is server address.
func WithRootCertificateFile ¶
func WithRootCertificateFile(cert string) ClientOption
@cert is client certificate file. it can be empty.
type ClientOptions ¶
type ClientOptions struct {
// contains filtered or unexported fields
}
type CompressType ¶
type CompressType int
type Connection ¶
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) // contains filtered or unexported methods }
type EndPoint ¶
type EndPoint interface { // get EndPoint ID ID() EndPointID // get endpoint type EndPointType() EndPointType // run event loop and serves client request. RunEventLoop(newSession NewSessionCallback) // check the endpoint has been closed IsClosed() bool // close the endpoint and free its resource Close() }
type EndPointID ¶
type EndPointID = int32
type EndPointType ¶
type EndPointType int32
const ( UDP_ENDPOINT EndPointType = 0 UDP_CLIENT EndPointType = 1 TCP_CLIENT EndPointType = 2 WS_CLIENT EndPointType = 3 WSS_CLIENT EndPointType = 4 TCP_SERVER EndPointType = 7 WS_SERVER EndPointType = 8 WSS_SERVER EndPointType = 9 )
func (EndPointType) String ¶
func (x EndPointType) String() string
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 getty received a package. 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. // // If ur logic processing in this func will take a long time, u should start a goroutine // pool(like working thread pool in cpp) to handle the processing asynchronously. Or u // can do the logic processing in other asynchronous way. // !!!In short, ur OnMessage callback func should return asap. // // If this is a udp event listener, the second parameter type is UDPContext. OnMessage(Session, interface{}) }
EventListener is used to process pkg that received from remote session
type Logger ¶
type Logger interface { Info(args ...interface{}) Warn(args ...interface{}) Error(args ...interface{}) Debug(args ...interface{}) Infof(fmt string, args ...interface{}) Warnf(fmt string, args ...interface{}) Errorf(fmt string, args ...interface{}) Debugf(fmt string, args ...interface{}) }
Logger for user who want to customize logger of getty
type LoggerLevel ¶
type LoggerLevel int8
type NewSessionCallback ¶
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 Reader ¶
type Reader interface { // Parse tcp/udp/websocket pkg from buffer and if possible return a complete pkg. // When receiving a tcp network streaming segment, there are 4 cases as following: // case 1: a error found in the streaming segment; // case 2: can not unmarshal a pkg header from the streaming segment; // case 3: unmarshal a pkg header but can not unmarshal a pkg from the streaming segment; // case 4: just unmarshal a pkg from the streaming segment; // case 5: unmarshal more than one pkg from the streaming segment; // // The return value is (nil, 0, error) as case 1. // The return value is (nil, 0, nil) as case 2. // The return value is (nil, pkgLen, nil) as case 3. // The return value is (pkg, pkgLen, nil) as case 4. // The handleTcpPackage may invoke func Read many times as case 5. Read(Session, []byte) (interface{}, int, error) }
Reader is used to unmarshal a complete pkg from buffer
type Server ¶
func NewUDPPEndPoint ¶
func NewUDPPEndPoint(opts ...ServerOption) Server
NewUDPEndPoint builds a unconnected udp server.
func NewWSSServer ¶
func NewWSSServer(opts ...ServerOption) Server
NewWSSServer builds a secure websocket server.
func NewWSServer ¶
func NewWSServer(opts ...ServerOption) Server
NewWSServer builds a websocket server.
type ServerOption ¶
type ServerOption func(*ServerOptions)
func WithLocalAddress ¶
func WithLocalAddress(addr string) ServerOption
@addr server listen address.
func WithWebsocketServerCert ¶
func WithWebsocketServerCert(cert string) ServerOption
@cert: server certificate file
func WithWebsocketServerPath ¶
func WithWebsocketServerPath(path string) ServerOption
@path: websocket request url path
func WithWebsocketServerPrivateKey ¶
func WithWebsocketServerPrivateKey(key string) ServerOption
@key: server private key(contains its public key)
func WithWebsocketServerRootCert ¶
func WithWebsocketServerRootCert(cert string) ServerOption
@cert is the root certificate file to verify the legitimacy of server
type ServerOptions ¶
type ServerOptions struct {
// contains filtered or unexported fields
}
type Session ¶
type Session interface { Connection Reset() Conn() net.Conn Stat() string IsClosed() bool // get endpoint type EndPoint() EndPoint SetMaxMsgLen(int) SetName(string) SetEventListener(EventListener) SetPkgHandler(ReadWriter) SetReader(Reader) SetWriter(Writer) SetCronPeriod(int) // Deprecated: don't use read queue. SetRQLen(int) SetWQLen(int) SetWaitTime(time.Duration) SetTaskPool(*gxsync.TaskPool) GetAttribute(interface{}) interface{} SetAttribute(interface{}, interface{}) RemoveAttribute(interface{}) // the Writer will invoke this function. Pls attention that if timeout is less than 0, WritePkg will send @pkg asap. // for udp session, the first parameter should be UDPContext. WritePkg(pkg interface{}, timeout time.Duration) error WriteBytes([]byte) error WriteBytesArray(...[]byte) error Close() }
type UDPContext ¶
func (UDPContext) String ¶
func (c UDPContext) String() string
type ValuesContext ¶
func NewValuesContext ¶
func NewValuesContext(ctx context.Context) *ValuesContext
func (*ValuesContext) Delete ¶
func (c *ValuesContext) Delete(key interface{})
func (*ValuesContext) Get ¶
func (c *ValuesContext) Get(key interface{}) (interface{}, bool)
func (*ValuesContext) Set ¶
func (c *ValuesContext) Set(key interface{}, value interface{})