Documentation ¶
Overview ¶
Teleport is a versatile, high-performance and flexible TCP socket framework. It can be used for peer-peer, rpc, gateway, micro services, push services, game services and so on.
Copyright 2015-2017 HenryLee. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
- Constants
- Variables
- func Criticalf(format string, args ...interface{})
- func Debugf(format string, args ...interface{})
- func Errorf(format string, args ...interface{})
- func Fatalf(format string, args ...interface{})
- func Go(fn func())
- func GraceSignal()
- func Infof(format string, args ...interface{})
- func Noticef(format string, args ...interface{})
- func Panicf(format string, args ...interface{})
- func Printf(format string, args ...interface{})
- func Reboot(timeout ...time.Duration)
- func SetGlobalBodyCodec(codecName string)
- func SetGlobalHeaderCodec(codecName string)
- func SetGopool(maxGoroutinesAmount int, maxGoroutineIdleDuration time.Duration)
- func SetLogger(logger Logger)
- func SetRawlogLevel(level string)
- func SetShutdown(timeout time.Duration, firstSweep, beforeExiting func() error)
- func Shutdown(timeout ...time.Duration)
- func StatusText(code int) string
- func Tracef(format string, args ...interface{})
- func Warnf(format string, args ...interface{})
- type ForeSession
- type Handler
- type HandlersMaker
- type Logger
- type Peer
- func (p *Peer) Close() (err error)
- func (p *Peer) Dial(addr string, id ...string) (Session, error)
- func (p *Peer) DialContext(ctx context.Context, addr string, id ...string) (Session, error)
- func (p *Peer) GetSession(sessionId string) (Session, bool)
- func (p *Peer) Listen() error
- func (p *Peer) ServeConn(conn net.Conn, id ...string) Session
- type PeerConfig
- type Plugin
- type PluginContainer
- type PostAcceptPlugin
- type PostDialPlugin
- type PostReadBodyPlugin
- type PostReadHeaderPlugin
- type PostRegPlugin
- type PostWritePullPlugin
- type PostWritePushPlugin
- type PostWriteReplyPlugin
- type PreReadBodyPlugin
- type PreReadHeaderPlugin
- type PreWritePullPlugin
- type PreWritePushPlugin
- type PreWriteReplyPlugin
- type PullCmd
- type PullCtx
- type PushCtx
- type ReadCtx
- type Router
- type Session
- type SessionHub
- type UnknownPullCtx
- type UnknownPushCtx
- type WriteCtx
- type Xerror
Constants ¶
const ( TypeUndefined int32 = 0 TypePull int32 = 1 TypeReply int32 = 2 // reply to pull TypePush int32 = 4 )
Packet Header types
const ( StatusWriteFailed = 100 StatusConnClosed = 101 StatusOK = 200 StatusBadUri = 400 StatusNotFound = 404 StatusUnsupportedTx = 410 StatusUnsupportedCodecType = 415 StatusFailedPlugin = 424 StatusInternalServerError = 500 StatusNotImplemented = 501 StatusBadGateway = 502 StatusGatewayTimeout = 504 StatusVariantAlsoNegotiates = 506 StatusInsufficientStorage = 507 StatusLoopDetected = 508 StatusNotExtended = 510 StatusNetworkAuthenticationRequired = 511 )
Response Header status codes as registered with IANA.
Variables ¶
var ErrConnClosed = errors.New("connection is closed")
ErrConnClosed connection is closed error.
var ErrListenClosed = errors.New("listener is closed")
ErrListenClosed listener is closed error.
var FirstSweep, BeforeExiting func() error
FirstSweep is first executed. BeforeExiting is executed before process exiting. Usage: share github.com/henrylee2cn/goutil/graceful with other project.
var GetReceiverPacket = socket.GetReceiverPacket
GetReceiverPacket returns a packet for sending.
func GetReceiverPacket(bodyGetting func(*socket.Header) interface{}) *socket.Packet
var GetSenderPacket = socket.GetSenderPacket
GetSenderPacket returns a packet for sending.
func GetSenderPacket(typ int32, uri string, body interface{}, setting ...socket.PacketSetting) *socket.Packet
var PutPacket = socket.PutPacket
PutPacket puts a *socket.Packet to packet stack.
func PutPacket(p *socket.Packet)
Functions ¶
func Criticalf ¶
func Criticalf(format string, args ...interface{})
Criticalf logs a message using CRITICAL as log level.
func Debugf ¶
func Debugf(format string, args ...interface{})
Debugf logs a message using DEBUG as log level.
func Errorf ¶
func Errorf(format string, args ...interface{})
Errorf logs a message using ERROR as log level.
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf is equivalent to l.Criticalf followed by a call to os.Exit(1).
func Infof ¶
func Infof(format string, args ...interface{})
Infof logs a message using INFO as log level.
func Noticef ¶
func Noticef(format string, args ...interface{})
Noticef logs a message using NOTICE as log level.
func Panicf ¶
func Panicf(format string, args ...interface{})
Panicf is equivalent to l.Criticalf followed by a call to panic().
func Printf ¶
func Printf(format string, args ...interface{})
Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.
func SetGlobalBodyCodec ¶
func SetGlobalBodyCodec(codecName string)
SetGlobalBodyCodec sets global default body codec. Note:
If the codec.Codec named 'codecName' is not registered, it will panic; It is not safe to call it concurrently.
func SetGlobalHeaderCodec ¶
func SetGlobalHeaderCodec(codecName string)
SetGlobalHeaderCodec sets global default header codec. Note:
If the codec.Codec named 'codecName' is not registered, it will panic; It is not safe to call it concurrently.
func SetGopool ¶
SetGopool set or reset go pool config. Note: Make sure to call it before calling NewPeer() and Go()
func SetLogger ¶
func SetLogger(logger Logger)
SetLogger sets global logger. Note: Concurrent is not safe!
func SetRawlogLevel ¶
func SetRawlogLevel(level string)
SetRawlogLevel sets the default logger's level. Note: Concurrent is not safe!
func SetShutdown ¶
SetShutdown sets the function which is called after the process shutdown, and the time-out period for the process shutdown. If 0<=timeout<5s, automatically use 'MinShutdownTimeout'(5s). If timeout<0, indefinite period. 'firstSweep' is first executed. 'beforeExiting' is executed before process exiting.
func Shutdown ¶
Shutdown closes all the frame process gracefully. Parameter timeout is used to reset time-out period for the process shutdown.
func StatusText ¶
StatusText returns a text for the Response Header status code. It returns the empty string if the code is unknown.
Types ¶
type ForeSession ¶
type ForeSession interface { ChangeId(newId string) Close() error Id() string IsOk() bool Peer() *Peer RemoteIp() string SetReadTimeout(duration time.Duration) SetWriteTimeout(duration time.Duration) Public() goutil.Map PublicLen() int Send(packet *socket.Packet) error Receive(packet *socket.Packet) error }
Session a connection session.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler pull or push handler type info
func (*Handler) ArgElemType ¶
ArgElemType returns the handler arg elem type.
type HandlersMaker ¶
type HandlersMaker func(string, interface{}, PluginContainer) ([]*Handler, error)
HandlersMaker makes []*Handler
type Logger ¶
type Logger interface { // Printf formats according to a format specifier and writes to standard output. // It returns the number of bytes written and any write error encountered. Printf(format string, args ...interface{}) // Fatalf is equivalent to Criticalf followed by a call to os.Exit(1). Fatalf(format string, args ...interface{}) // Panicf is equivalent to Criticalf followed by a call to panic(). Panicf(format string, args ...interface{}) // Criticalf logs a message using CRITICAL as log level. Criticalf(format string, args ...interface{}) // Errorf logs a message using ERROR as log level. Errorf(format string, args ...interface{}) // Warnf logs a message using WARNING as log level. Warnf(format string, args ...interface{}) // Noticef logs a message using NOTICE as log level. Noticef(format string, args ...interface{}) // Infof logs a message using INFO as log level. Infof(format string, args ...interface{}) // Debugf logs a message using DEBUG as log level. Debugf(format string, args ...interface{}) // Tracef logs a message using TRACE as log level. Tracef(format string, args ...interface{}) }
Logger interface
type Peer ¶
type Peer struct { PullRouter *Router PushRouter *Router // contains filtered or unexported fields }
Peer peer which is server or client.
func (*Peer) DialContext ¶
DialContext connects with the peer of the destination address, using the provided context.
func (*Peer) GetSession ¶
GetSession gets the session by id.
type PeerConfig ¶
type PeerConfig struct { TlsCertFile string `yaml:"tls_cert_file" ini:"tls_cert_file" comment:"TLS certificate file path"` TlsKeyFile string `yaml:"tls_key_file" ini:"tls_key_file" comment:"TLS key file path"` DefaultReadTimeout time.Duration `yaml:"default_read_timeout" ini:"default_read_timeout" comment:"Default maximum duration for reading; ns,µs,ms,s,m,h"` DefaultWriteTimeout time.Duration `yaml:"default_write_timeout" ini:"default_write_timeout" comment:"Default maximum duration for writing; ns,µs,ms,s,m,h"` SlowCometDuration time.Duration `yaml:"slow_comet_duration" ini:"slow_comet_duration" comment:"Slow operation alarm threshold; ns,µs,ms,s ..."` DefaultHeaderCodec string `yaml:"default_header_codec" ini:"default_header_codec" comment:"Default header codec"` DefaultBodyCodec string `yaml:"default_body_codec" ini:"default_body_codec" comment:"Default body codec"` DefaultBodyGzipLevel int32 `yaml:"default_body_gzip_level" ini:"default_body_gzip_level" comment:"Default body gzip level"` PrintBody bool `yaml:"print_body" ini:"print_body" comment:"Is print body or not"` DefaultDialTimeout time.Duration `` /* 141-byte string literal not displayed */ ListenAddrs []string `yaml:"listen_addrs" ini:"listen_addrs" comment:"Listen addresses; for server role"` }
PeerConfig peer config Note:
yaml tag is used for github.com/henrylee2cn/cfgo ini tag is used for github.com/henrylee2cn/ini
type PluginContainer ¶
type PluginContainer interface { Add(plugins ...Plugin) error Remove(pluginName string) error GetByName(pluginName string) Plugin GetAll() []Plugin PostDialPlugin PostAcceptPlugin PreWritePullPlugin PostWritePullPlugin PreWritePushPlugin PostWritePushPlugin PreReadHeaderPlugin PostReadHeaderPlugin PostRegPlugin PreWriteReplyPlugin PostWriteReplyPlugin PreReadBodyPlugin PostReadBodyPlugin // contains filtered or unexported methods }
PluginContainer plugin container that defines base methods to manage plugins.
type PostAcceptPlugin ¶
type PostAcceptPlugin interface {
PostAccept(ForeSession) error
}
Interfaces about plugin.
type PostDialPlugin ¶
type PostDialPlugin interface {
PostDial(ForeSession) error
}
Interfaces about plugin.
type PostReadBodyPlugin ¶
Interfaces about plugin.
type PostReadHeaderPlugin ¶
Interfaces about plugin.
type PostRegPlugin ¶
Interfaces about plugin.
type PostWritePullPlugin ¶
Interfaces about plugin.
type PostWritePushPlugin ¶
Interfaces about plugin.
type PostWriteReplyPlugin ¶
Interfaces about plugin.
type PreReadBodyPlugin ¶
Interfaces about plugin.
type PreReadHeaderPlugin ¶
Interfaces about plugin.
type PreWritePullPlugin ¶
Interfaces about plugin.
type PreWritePushPlugin ¶
Interfaces about plugin.
type PreWriteReplyPlugin ¶
Interfaces about plugin.
type PullCmd ¶
type PullCmd struct { Xerror Xerror // contains filtered or unexported fields }
PullCmd the command of the pulling operation's response.
type PushCtx ¶
type PushCtx interface { Uri() string Path() string RawQuery() string Query() url.Values Public() goutil.Map PublicLen() int Ip() string Peer() *Peer Session() Session }
PushCtx push handler context. For example:
type HomePush struct{ PushCtx }
type ReadCtx ¶
type ReadCtx interface { Input() *socket.Packet Public() goutil.Map PublicLen() int Ip() string Peer() *Peer Session() Session }
ReadCtx for reading packet.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router the router of pull or push.
func (*Router) SetUnknown ¶
SetUnknown sets the default handler, which is called when no handler for pull or push is found.
type Session ¶
type Session interface { ChangeId(newId string) Close() error GoPull(uri string, args interface{}, reply interface{}, done chan *PullCmd, setting ...socket.PacketSetting) Id() string IsOk() bool Peer() *Peer Pull(uri string, args interface{}, reply interface{}, setting ...socket.PacketSetting) *PullCmd Push(uri string, args interface{}, setting ...socket.PacketSetting) error ReadTimeout() time.Duration RemoteIp() string SetReadTimeout(duration time.Duration) SetWriteTimeout(duration time.Duration) Socket() socket.Socket WriteTimeout() time.Duration Public() goutil.Map PublicLen() int }
Session a connection session.
type SessionHub ¶
type SessionHub struct {
// contains filtered or unexported fields
}
SessionHub sessions hub
func (*SessionHub) Delete ¶
func (sh *SessionHub) Delete(id string)
Delete deletes the *session for a id.
func (*SessionHub) Get ¶
func (sh *SessionHub) Get(id string) (*session, bool)
Get gets *session by id. If second returned arg is false, mean the *session is not found.
func (*SessionHub) Len ¶
func (sh *SessionHub) Len() int
Len returns the length of the session hub. Note: the count implemented using sync.Map may be inaccurate.
func (*SessionHub) Random ¶
func (sh *SessionHub) Random() (*session, bool)
Random gets a *session randomly. If third returned arg is false, mean no *session is exist.
func (*SessionHub) Range ¶
func (sh *SessionHub) Range(f func(*session) bool)
Range calls f sequentially for each id and *session present in the session hub. If f returns false, range stops the iteration.
type UnknownPullCtx ¶
type UnknownPushCtx ¶
type WriteCtx ¶
type WriteCtx interface { Output() *socket.Packet Public() goutil.Map PublicLen() int Ip() string Peer() *Peer Session() Session }
WriteCtx for writing packet.