gotcp

package
v0.0.0-...-f683eec Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WriteAsync       = network.WriteAsync
	WriteImmediately = network.WriteImmediately
)

import const value

Variables

View Source
var GoClientContextPool process.ContextPool = &goClientContextPool{
	Pool: sync.Pool{
		New: func() interface{} {
			return &clientCtx{}
		},
	},
}
View Source
var GoServerContextPool process.ContextPool = &goServerContextPool{
	Pool: sync.Pool{
		New: func() interface{} {
			return &sessionCtx{}
		},
	},
}

Functions

func InstallClientOptionsWatchDog

func InstallClientOptionsWatchDog(dog func(cc *ClientOptions))

InstallClientOptionsWatchDog install watch dog

func InstallServerOptionsWatchDog

func InstallServerOptionsWatchDog(dog func(cc *ServerOptions))

InstallServerOptionsWatchDog install watch dog

func NewService

func NewService(name string, opt ...ServerOption) app.Service

Types

type Client

type Client = network.Client

import type

func NewClient

func NewClient(opts ...ClientOption) (_ Client, err error)

func NewClientForProxy

func NewClientForProxy(net, addr string, inner *process.InnerOptions) (Client, error)

NewClientForProxy new client for client proxy. NOTE: you should rewrite this function for custom set option

type ClientContext

type ClientContext = network.ClientContext

import type

type ClientOption

type ClientOption func(cc *ClientOptions) ClientOption

ClientOption option define

func WithClientOptionAddr

func WithClientOptionAddr(v string) ClientOption

Addr Server Addr

func WithClientOptionAutoReconnectTime

func WithClientOptionAutoReconnectTime(v int) ClientOption

AutoReconnect auto reconnect server. zero means not reconnect! -1 means always reconnect, >0 : reconnect times

func WithClientOptionAutoReconnectWait

func WithClientOptionAutoReconnectWait(v time.Duration) ClientOption

AutoReconnectWait reconnect wait time

func WithClientOptionBlockConnect

func WithClientOptionBlockConnect(v bool) ClientOption

BlockConnect 创建客户端时候,是否阻塞等待链接服务器

func WithClientOptionDialer

func WithClientOptionDialer(v func(network, addr string) (conn net.Conn, err error)) ClientOption

Dialer config net dialer

func WithClientOptionFrameLogger

func WithClientOptionFrameLogger(v *zaplog.Logger) ClientOption

frame log

func WithClientOptionHeartbeat

func WithClientOptionHeartbeat(v time.Duration) ClientOption

Heartbeat use websocket ping/pong.

func WithClientOptionMaxMessageSizeLimit

func WithClientOptionMaxMessageSizeLimit(v int) ClientOption

MaxMessageSizeLimit limit message size

func WithClientOptionNetwork

func WithClientOptionNetwork(v string) ClientOption

Network tcp/tcp4/tcp6/unix

func WithClientOptionPacketHeadBuf

func WithClientOptionPacketHeadBuf(v func() []byte) ClientOption

tcp packet head

func WithClientOptionProcessOptions

func WithClientOptionProcessOptions(v ...process.ProcessOption) ClientOption

Process Options

func WithClientOptionReadBufferSize

func WithClientOptionReadBufferSize(v int) ClientOption

ReadBufferSize 一定要大于最大消息的大小.每个链接一个缓冲区。

func WithClientOptionReadSize

func WithClientOptionReadSize(v func(head []byte) (size int)) ClientOption

read tcp packet head size

func WithClientOptionReadTimeout

func WithClientOptionReadTimeout(v time.Duration) ClientOption

ReadTimeout read timeout

func WithClientOptionReuseReadBuffer

func WithClientOptionReuseReadBuffer(v bool) ClientOption

ReuseReadBuffer 复用read缓存区。影响Process.DispatchFilter. 如果此选项设置为true,在DispatchFilter内如果开启协程,需要手动复制内存。 如果在DispatchFilter内不开启协程,设置为true可以减少内存分配。

func WithClientOptionRouter

func WithClientOptionRouter(v Router) ClientOption

process router

func WithClientOptionSendQueueSize

func WithClientOptionSendQueueSize(v int) ClientOption

SendQueueSize async send queue size

func WithClientOptionStopImmediately

func WithClientOptionStopImmediately(v bool) ClientOption

StopImmediately when session finish,business finish immediately.

func WithClientOptionWriteMethods

func WithClientOptionWriteMethods(v WriteMethod) ClientOption

Write network data method.

func WithClientOptionWriteSize

func WithClientOptionWriteSize(v func(head []byte, size int) (err error)) ClientOption

write tcp packet head size

func WithClientOptionWriteTimeout

func WithClientOptionWriteTimeout(v time.Duration) ClientOption

WriteTimeout write timeout

type ClientOptions

type ClientOptions struct {
	// Network tcp/tcp4/tcp6/unix
	Network string
	// Addr Server Addr
	Addr string
	// Dialer config net dialer
	Dialer func(network, addr string) (conn net.Conn, err error)
	// Process Options
	ProcessOptions []process.ProcessOption
	// process router
	Router Router
	// frame log
	FrameLogger *zaplog.Logger
	// AutoReconnect auto reconnect server. zero means not reconnect! -1 means always reconnect, >0 : reconnect times
	AutoReconnectTime int
	// AutoReconnectWait reconnect wait time
	AutoReconnectWait time.Duration
	// StopImmediately when session finish,business finish immediately.
	StopImmediately bool
	// ReadTimeout read timeout
	ReadTimeout time.Duration
	// WriteTimeout write timeout
	WriteTimeout time.Duration
	// Write network data method.
	WriteMethods WriteMethod
	// SendQueueSize async send queue size
	SendQueueSize int
	// Heartbeat use websocket ping/pong.
	Heartbeat time.Duration
	// tcp packet head
	PacketHeadBuf func() []byte
	// read tcp packet head size
	ReadSize func(head []byte) (size int)
	// write tcp packet head size
	WriteSize func(head []byte, size int) (err error)
	// ReadBufferSize 一定要大于最大消息的大小.每个链接一个缓冲区。
	ReadBufferSize int
	// ReuseReadBuffer 复用read缓存区。影响Process.DispatchFilter.
	// 如果此选项设置为true,在DispatchFilter内如果开启协程,需要手动复制内存。
	// 如果在DispatchFilter内不开启协程,设置为true可以减少内存分配。
	ReuseReadBuffer bool
	// MaxMessageSizeLimit limit message size
	MaxMessageSizeLimit int
	// BlockConnect 创建客户端时候,是否阻塞等待链接服务器
	BlockConnect bool
}

ClientOption

func NewClientOptions

func NewClientOptions(opts ...ClientOption) *ClientOptions

NewClientOptions create options instance.

func (*ClientOptions) ApplyOption

func (cc *ClientOptions) ApplyOption(opts ...ClientOption)

ApplyOption modify options

func (*ClientOptions) GetSetOption

func (cc *ClientOptions) GetSetOption(opt ClientOption) ClientOption

GetSetOption modify and get last option

func (*ClientOptions) SetOption

func (cc *ClientOptions) SetOption(opt ClientOption)

SetOption modify options

type GoClient

type GoClient struct {
	// process
	*rpc.RPCProcess
	// contains filtered or unexported fields
}

go client

func NewClientEx

func NewClientEx(inner *process.InnerOptions, copts *ClientOptions) (cli *GoClient, err error)

NewClientEx 创建客户端 inner *process.InnerOptions 选项应该由上层ClientProxy去决定如何设置。 copts 内部应该设置链接相关的参数。比如读写超时,如何发送数据 opts 业务方决定

func (*GoClient) AddCloseClientFunc

func (sess *GoClient) AddCloseClientFunc(f func(sess Client))

func (*GoClient) ClientValid

func (sess *GoClient) ClientValid() bool

func (*GoClient) Close

func (sess *GoClient) Close() (err error)

func (*GoClient) GetConn

func (sess *GoClient) GetConn() interface{}

GetConn get raw conn(net.Conn,websocket.Conn...)

func (*GoClient) Run

func (sess *GoClient) Run()

Run run client

func (*GoClient) SessionValue

func (sess *GoClient) SessionValue(key interface{}) interface{}

Value wrap context.Context.Value

func (*GoClient) WithSessionValue

func (sess *GoClient) WithSessionValue(key, value interface{})

WithValue wrap context.WithValue

func (*GoClient) Write

func (sess *GoClient) Write(in []byte) (n int, err error)

type GoServer

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

GoServer websocket server

func NewServer

func NewServer(opts ...ServerOption) *GoServer

func (*GoServer) Broadcast

func (s *GoServer) Broadcast(uri interface{}, msg interface{}, md metadata.MD) error

func (*GoServer) BroadcastFilter

func (s *GoServer) BroadcastFilter(filter func(Session) bool, uri interface{}, msg interface{}, md metadata.MD) error

func (*GoServer) ForEach

func (s *GoServer) ForEach(f func(Session))

func (*GoServer) Listen

func (s *GoServer) Listen(addr string) (err error)

func (*GoServer) Run

func (s *GoServer) Run(addr string) (err error)

func (*GoServer) Serve

func (s *GoServer) Serve(ln net.Listener) (err error)

func (*GoServer) Shutdown

func (s *GoServer) Shutdown(ctx context.Context) (err error)

type GoSession

type GoSession struct {
	// process
	*rpc.RPCProcess
	// contains filtered or unexported fields
}

server session

func (*GoSession) AddCloseSessionFunc

func (sess *GoSession) AddCloseSessionFunc(f func(sess Session))

func (*GoSession) Close

func (sess *GoSession) Close() (err error)

func (*GoSession) GetConn

func (sess *GoSession) GetConn() interface{}

GetConn get raw conn(net.Conn,websocket.Conn...)

func (*GoSession) GetServer

func (sess *GoSession) GetServer() Server

GetServer get raw server(*WsServer,*TcpServer...)

func (*GoSession) Run

func (sess *GoSession) Run()

Run run client

func (*GoSession) SessionValue

func (sess *GoSession) SessionValue(key interface{}) interface{}

Value wrap context.Context.Value

func (*GoSession) WithSessionValue

func (sess *GoSession) WithSessionValue(key, value interface{})

WithValue wrap context.WithValue

func (*GoSession) Write

func (sess *GoSession) Write(in []byte) (n int, err error)

type GoTcpService

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

GoTcpService implement app.Service interface

func (*GoTcpService) Finish

func (svc *GoTcpService) Finish()

func (*GoTcpService) Init

func (svc *GoTcpService) Init(s app.Stoper) (err error)

func (*GoTcpService) Name

func (svc *GoTcpService) Name() string

func (*GoTcpService) Start

func (svc *GoTcpService) Start(s app.Stoper) (err error)

func (*GoTcpService) Stop

func (svc *GoTcpService) Stop()

type Router

type Router = process.Router

import type

type Server

type Server = network.Server

import type

type ServerOption

type ServerOption func(cc *ServerOptions) ServerOption

ServerOption option define

func WithAcceptLoadLimit

func WithAcceptLoadLimit(v func(sess Session, cnt int64) bool) ServerOption

accepted load limit

func WithAddr

func WithAddr(v string) ServerOption

Addr Server Addr

func WithFrameLogger

func WithFrameLogger(v *zaplog.Logger) ServerOption

frame log

func WithHeartbeat

func WithHeartbeat(v time.Duration) ServerOption

Heartbeat use websocket ping/pong.

func WithListen

func WithListen(v func(addr string) (ln net.Listener, err error)) ServerOption

Listen option. can replace kcp wrap

func WithMaxMessageSizeLimit

func WithMaxMessageSizeLimit(v int) ServerOption

MaxMessageSizeLimit limit message size

func WithNetConnOption

func WithNetConnOption(v func(net.Conn)) ServerOption

NetOption modify raw options

func WithNewSession

func WithNewSession(v func(in Session) (Session, error)) ServerOption

NewSession custom session

func WithPacketHeadBuf

func WithPacketHeadBuf(v func() []byte) ServerOption

tcp packet head

func WithProcessOptions

func WithProcessOptions(v ...process.ProcessOption) ServerOption

Process Options

func WithReadBufferSize

func WithReadBufferSize(v int) ServerOption

ReadBufferSize 一定要大于最大消息的大小.每个链接一个缓冲区。

func WithReadSize

func WithReadSize(v func(head []byte) (size int)) ServerOption

read tcp packet head size

func WithReadTimeout

func WithReadTimeout(v time.Duration) ServerOption

ReadTimeout read timetou

func WithRegistry

func WithRegistry(v discovery.Registry) ServerOption

Registry

func WithReuseReadBuffer

func WithReuseReadBuffer(v bool) ServerOption

ReuseReadBuffer 复用read缓存区。影响Process.DispatchFilter. 如果此选项设置为true,在DispatchFilter内如果开启协程,需要手动复制内存。 如果在DispatchFilter内不开启协程,设置为true可以减少内存分配。 默认为false,是为了防止错误的配置导致bug。

func WithRouter

func WithRouter(v Router) ServerOption

process router

func WithSendQueueSize

func WithSendQueueSize(v int) ServerOption

SendQueueSize async send queue size

func WithSessionLogger

func WithSessionLogger(v func(sess Session, global *zaplog.Logger) (r *zaplog.Logger)) ServerOption

SessionLogger custom session logger

func WithSessionRouter

func WithSessionRouter(v func(sess Session, global Router) (r Router)) ServerOption

SessionRouter custom session router

func WithStopImmediately

func WithStopImmediately(v bool) ServerOption

StopImmediately when session finish,business finish immediately.

func WithWriteMethods

func WithWriteMethods(v WriteMethod) ServerOption

Write network data method.

func WithWriteSize

func WithWriteSize(v func(head []byte, size int) (err error)) ServerOption

write tcp packet head size

func WithWriteTimeout

func WithWriteTimeout(v time.Duration) ServerOption

WriteTimeout write timeout

type ServerOptions

type ServerOptions struct {
	// Addr Server Addr
	Addr string
	// Listen option. can replace kcp wrap
	Listen func(addr string) (ln net.Listener, err error)
	// NetOption modify raw options
	NetConnOption func(net.Conn)
	// accepted load limit
	AcceptLoadLimit func(sess Session, cnt int64) bool
	// Process Options
	ProcessOptions []process.ProcessOption
	// process router
	Router Router
	// SessionRouter custom session router
	SessionRouter func(sess Session, global Router) (r Router)
	// frame log
	FrameLogger *zaplog.Logger
	// SessionLogger custom session logger
	SessionLogger func(sess Session, global *zaplog.Logger) (r *zaplog.Logger)
	// NewSession custom session
	NewSession func(in Session) (Session, error)
	// StopImmediately when session finish,business finish immediately.
	StopImmediately bool
	// ReadTimeout read timetou
	ReadTimeout time.Duration
	// WriteTimeout write timeout
	WriteTimeout time.Duration
	// Write network data method.
	WriteMethods WriteMethod
	// SendQueueSize async send queue size
	SendQueueSize int
	// Heartbeat use websocket ping/pong.
	Heartbeat time.Duration
	// tcp packet head
	PacketHeadBuf func() []byte
	// read tcp packet head size
	ReadSize func(head []byte) (size int)
	// write tcp packet head size
	WriteSize func(head []byte, size int) (err error)
	// ReadBufferSize 一定要大于最大消息的大小.每个链接一个缓冲区。
	ReadBufferSize int
	// ReuseReadBuffer 复用read缓存区。影响Process.DispatchFilter.
	// 如果此选项设置为true,在DispatchFilter内如果开启协程,需要手动复制内存。
	// 如果在DispatchFilter内不开启协程,设置为true可以减少内存分配。
	// 默认为false,是为了防止错误的配置导致bug。
	ReuseReadBuffer bool
	// MaxMessageSizeLimit limit message size
	MaxMessageSizeLimit int
	// Registry
	Registry discovery.Registry
}

ServerOption

func NewServerOptions

func NewServerOptions(opts ...ServerOption) *ServerOptions

NewServerOptions create options instance.

func (*ServerOptions) ApplyOption

func (cc *ServerOptions) ApplyOption(opts ...ServerOption)

ApplyOption modify options

func (*ServerOptions) GetSetOption

func (cc *ServerOptions) GetSetOption(opt ServerOption) ServerOption

GetSetOption modify and get last option

func (*ServerOptions) SetOption

func (cc *ServerOptions) SetOption(opt ServerOption)

SetOption modify options

type Session

type Session = network.Session

import type

type SessionContext

type SessionContext = network.SessionContext

import type

type WriteMethod

type WriteMethod = network.WriteMethod

import type

Jump to

Keyboard shortcuts

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