Documentation ¶
Index ¶
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 ( Version = "0.8.3" DATE = "2018/03/18" GETTY_MAJOR = 0 GETTY_MINOR = 8 GETTY_BUILD = 3 )
Variables ¶
var ( ErrSessionClosed = errors.New("session Already Closed") ErrSessionBlocked = errors.New("session Full Blocked") ErrMsgTooLong = errors.New("Message Too Long") ErrNullPeerAddr = errors.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 ¶
This section is empty.
Types ¶
type Client ¶
type Client interface { EndPoint }
func NewTCPClient ¶ added in v0.8.3
func NewTCPClient(opts ...ClientOption) Client
NewTcpClient function builds a tcp client.
func NewUDPClient ¶ added in v0.8.3
func NewUDPClient(opts ...ClientOption) Client
NewUdpClient function builds a connected udp client
func NewWSClient ¶ added in v0.8.3
func NewWSClient(opts ...ClientOption) Client
NewWsClient function builds a ws client.
func NewWSSClient ¶ added in v0.8.3
func NewWSSClient(opts ...ClientOption) Client
NewWSSClient function builds a wss client.
type ClientOption ¶ added in v0.8.3
type ClientOption func(*ClientOptions)
func WithConnectionNumber ¶ added in v0.8.3
func WithConnectionNumber(num int) ClientOption
@num is connection number.
func WithRootCertificateFile ¶ added in v0.8.3
func WithRootCertificateFile(cert string) ClientOption
@cert is client certificate file. it can be empty.
func WithServerAddress ¶ added in v0.8.3
func WithServerAddress(addr string) ClientOption
@addr is server address.
type ClientOptions ¶ added in v0.8.3
type ClientOptions struct {
// contains filtered or unexported fields
}
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 EndPoint ¶ added in v0.8.3
type EndPoint interface { // 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 EndPointType ¶ added in v0.8.3
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 ¶ added in v0.8.3
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 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. // // 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 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 // 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 ¶
func NewTCPServer ¶ added in v0.8.3
func NewTCPServer(opts ...ServerOption) Server
NewTCServer builds a tcp server.
func NewUDPPEndPoint ¶ added in v0.8.3
func NewUDPPEndPoint(opts ...ServerOption) Server
NewUDPEndPoint builds a unconnected udp server.
func NewWSSServer ¶ added in v0.8.3
func NewWSSServer(opts ...ServerOption) Server
NewWSSServer builds a secure websocket server.
func NewWSServer ¶ added in v0.8.3
func NewWSServer(opts ...ServerOption) Server
NewWSServer builds a websocket server.
type ServerOption ¶ added in v0.8.3
type ServerOption func(*ServerOptions)
func WithLocalAddress ¶ added in v0.8.3
func WithLocalAddress(addr string) ServerOption
@addr server listen address.
func WithWebsocketServerCert ¶ added in v0.8.3
func WithWebsocketServerCert(cert string) ServerOption
@cert: server certificate file
func WithWebsocketServerPath ¶ added in v0.8.3
func WithWebsocketServerPath(path string) ServerOption
@path: websocket request url path
func WithWebsocketServerPrivateKey ¶ added in v0.8.3
func WithWebsocketServerPrivateKey(key string) ServerOption
@key: server private key(contains its public key)
func WithWebsocketServerRootCert ¶ added in v0.8.3
func WithWebsocketServerRootCert(cert string) ServerOption
@cert is the root certificate file to verify the legitimacy of server
type ServerOptions ¶ added in v0.8.3
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) SetRQLen(int) SetWQLen(int) SetWaitTime(time.Duration) 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 ¶ added in v0.8.3
func (UDPContext) String ¶ added in v0.8.3
func (c UDPContext) String() string