core

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LOG_MODULE_SERVER     = "server: "
	LOG_MODULE_START      = "start: "
	LOG_MODULE_WRITELOOP  = "writeLoop: "
	LOG_MODULE_READLOOP   = "readLoop: "
	LOG_MODULE_HANDLELOOP = "handleLoop: "
	LOG_MODULE_CLOSE      = "close: "
)
View Source
const (
	ConnFirstMsgSize  = 14 // Conn type (8) + server port (2) + channel ID (4)
	ConnTypeClient    = "client__"
	ConnTypeHandshake = "handshke"

	HandshakeMessage = "sds_handshake"

	EncryptionHeaderSize = 12 // Nonce (8) + data length (4)
	EncryptionNonceSize  = 8
	EncryptionLengthSize = 4
)

Variables

View Source
var (
	GoroutineMap     = &sync.Map{}
	HandshakeChanMap = &sync.Map{} // map[string]chan []byte    Map that stores channels used during handshake process
	TimeRcv          int64
)
View Source
var (
	TimeoutRegistry = make(map[string]TimeoutHandler)
	TimoutMap       = newTimeoutMap()
)
View Source
var RemoteReqIdMap = utils.NewAutoCleanMap(24 * time.Hour)

Functions

func CreateContextWithMessage

func CreateContextWithMessage(ctx context.Context, message *msg.RelayMsgBuf) context.Context

CreateContextWithMessage

func CreateContextWithNetID

func CreateContextWithNetID(ctx context.Context, netID int64) context.Context

CreateContextWithNetID

func CreateContextWithPacketId added in v0.9.0

func CreateContextWithPacketId(ctx context.Context, packetId int64) context.Context

func CreateContextWithParentReqId added in v0.9.0

func CreateContextWithParentReqId(ctx context.Context, reqId int64) context.Context

func CreateContextWithParentReqIdAsReqId added in v0.9.0

func CreateContextWithParentReqIdAsReqId(ctx context.Context) context.Context

func CreateContextWithRecvStartTime added in v0.9.0

func CreateContextWithRecvStartTime(ctx context.Context, recvStartTime int64) context.Context

func CreateContextWithReqId added in v0.9.0

func CreateContextWithReqId(ctx context.Context, reqId int64) context.Context

func CreateFirstMessage added in v0.8.0

func CreateFirstMessage(connType string, serverPort uint16, channelId uint32) []byte

func EncryptAndPack added in v0.8.0

func EncryptAndPack(privKey, plaintext []byte) ([]byte, error)

func GetPacketIdFromContext added in v0.9.0

func GetPacketIdFromContext(ctx context.Context) int64

func GetParentReqIdFromContext added in v0.9.0

func GetParentReqIdFromContext(ctx context.Context) int64

func GetRecvCostTimeFromContext added in v0.9.0

func GetRecvCostTimeFromContext(ctx context.Context) int64

func GetRecvStartTimeFromContext added in v0.9.0

func GetRecvStartTimeFromContext(ctx context.Context) int64

func GetRemoteReqId added in v0.9.0

func GetRemoteReqId(ctx context.Context) string

func GetReqIdFromContext added in v0.9.0

func GetReqIdFromContext(ctx context.Context) int64

func InheritRemoteReqIdFromParentReqId added in v0.9.0

func InheritRemoteReqIdFromParentReqId(ctx context.Context, reqId int64)

func InheritRpcLoggerFromParentReqId added in v0.9.0

func InheritRpcLoggerFromParentReqId(ctx context.Context, reqId int64)

func MessageFromContext

func MessageFromContext(ctx context.Context) *msg.RelayMsgBuf

MessageFromContext get msg from context

func Mylog

func Mylog(b bool, module string, v ...interface{})

Mylog

func NetIDFromContext

func NetIDFromContext(ctx context.Context) int64

NetIDFromContext

func Pack added in v0.9.0

func Pack(plaintext []byte) ([]byte, error)

func ParseFirstMessage added in v0.8.0

func ParseFirstMessage(data []byte) (string, uint16, uint32, error)

func ReadEncryptedHeaderAndBody added in v0.8.0

func ReadEncryptedHeaderAndBody(c net.Conn, privKey []byte, maxBodySize int) (plaintext []byte, bytesRead int, err error)

func ReadEncryptionHeader added in v0.9.0

func ReadEncryptionHeader(c net.Conn) (nonce uint64, dataLen uint32, bytesRead int, err error)

func ReadNonEncryptedHeaderAndBody added in v0.9.0

func ReadNonEncryptedHeaderAndBody(c net.Conn, maxBodySize int) (plaintext []byte, bytesRead int, err error)

func Register

func Register(cmd string, handler func(context.Context, WriteCloser))

Register

func RegisterRemoteReqId added in v0.9.0

func RegisterRemoteReqId(ctx context.Context, remoteReqId string) context.Context

func RegisterReqId added in v0.9.0

func RegisterReqId(ctx context.Context, rootReqId string)

func RegisterTimeoutHandler added in v0.7.0

func RegisterTimeoutHandler(cmd string, handler TimeoutHandler)

func StoreRemoteReqId added in v0.9.0

func StoreRemoteReqId(reqId int64, rootReqId string)

func UnpackEncryptionHeader added in v0.9.0

func UnpackEncryptionHeader(data []byte) (uint64, uint32)

func WriteFull added in v0.8.0

func WriteFull(c net.Conn, data []byte) error

Types

type ContextKV added in v0.9.0

type ContextKV struct {
	Key   interface{}
	Value interface{}
}

type Handler

type Handler interface {
	Handle(context.Context, interface{})
}

Handler

type HandlerFunc

type HandlerFunc func(context.Context, WriteCloser)

HandlerFunc

func GetHandlerFunc

func GetHandlerFunc(msgType string) HandlerFunc

GetHandlerFunc

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(ctx context.Context, c WriteCloser)

Handle

type MsgHandler

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

MsgHandler

type MyValue added in v0.7.0

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

type Server

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

Server

func CreateServer

func CreateServer(opt ...ServerOption) *Server

CreateServer

func ServerFromCtx

func ServerFromCtx(ctx context.Context) (*Server, bool)

ServerFromCtx

func (*Server) AddVolumeLogJob added in v0.8.0

func (s *Server) AddVolumeLogJob(logAll bool, logRead bool, logWrite bool, logInbound bool, logOutbound bool)

func (*Server) Broadcast

func (s *Server) Broadcast(msg *msg.RelayMsgBuf)

Broadcast

func (*Server) ConnsSize

func (s *Server) ConnsSize() int

ConnsSize

func (*Server) GetInboundAndReset added in v0.5.0

func (s *Server) GetInboundAndReset() int64

func (*Server) GetOutboundAndReset added in v0.5.0

func (s *Server) GetOutboundAndReset() int64

func (*Server) GetReadFlow

func (s *Server) GetReadFlow() int64

unused

func (*Server) GetSecondReadFlow

func (s *Server) GetSecondReadFlow() int64

GetSecondReadFlow

func (*Server) GetSecondWriteFlow

func (s *Server) GetSecondWriteFlow() int64

GetSecondWriteFlow

func (*Server) GetWriteFlow

func (s *Server) GetWriteFlow() int64

unused

func (*Server) SetVolRecOptions added in v0.8.0

func (s *Server) SetVolRecOptions(opt ...ServerVolRecOption)

func (*Server) Start

func (s *Server) Start(l net.Listener) error

Start

func (*Server) Stop

func (s *Server) Stop()

Stop

func (*Server) Unicast

func (s *Server) Unicast(ctx context.Context, netid int64, msg *msg.RelayMsgBuf) error

Unicast

type ServerConn

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

ServerConn

func CreateServerConn

func CreateServerConn(id int64, s *Server, c net.Conn) *ServerConn

CreateServerConn

func (*ServerConn) Close

func (sc *ServerConn) Close()

Close

func (*ServerConn) GetIP

func (sc *ServerConn) GetIP() string

GetIP

func (*ServerConn) GetLocalAddr added in v0.5.0

func (sc *ServerConn) GetLocalAddr() string

func (*ServerConn) GetLocalP2pAddress added in v0.8.0

func (sc *ServerConn) GetLocalP2pAddress() string

func (*ServerConn) GetName

func (sc *ServerConn) GetName() string

GetName

func (*ServerConn) GetNetID

func (sc *ServerConn) GetNetID() int64

GetNetID

func (*ServerConn) GetPort

func (sc *ServerConn) GetPort() string

GetPort

func (*ServerConn) GetRemoteAddr added in v0.5.0

func (sc *ServerConn) GetRemoteAddr() string

func (*ServerConn) GetRemoteP2pAddress added in v0.8.0

func (sc *ServerConn) GetRemoteP2pAddress() string

func (*ServerConn) SendBadVersionMsg added in v0.7.0

func (sc *ServerConn) SendBadVersionMsg(version uint16, cmd string)

func (*ServerConn) SetConnName

func (sc *ServerConn) SetConnName(name string)

SetConnName

func (*ServerConn) SetWriteHook added in v0.9.0

func (sc *ServerConn) SetWriteHook(h []WriteHook)

func (*ServerConn) Start

func (sc *ServerConn) Start()

Start server starts readLoop, writeLoop, handleLoop

func (*ServerConn) Write

func (sc *ServerConn) Write(message *message.RelayMsgBuf, ctx context.Context) error

Write * error is caught at application layer, if it's utils.ErrWouldBlock,sleep and then continue write

type ServerOption

type ServerOption func(*options)

ServerOption

func BufferSizeOption

func BufferSizeOption(indicator int) ServerOption

BufferSizeOption

func ContextKVOption added in v0.9.0

func ContextKVOption(kv []ContextKV) ServerOption

ContextKVOption

func LogOpenOption

func LogOpenOption(b bool) ServerOption

LogOpenOption

func MaxConnectionsOption

func MaxConnectionsOption(indicator int) ServerOption

MaxConnectionsOption

func MaxFlowOption

func MaxFlowOption(indicator int) ServerOption

MaxFlowOption

func MinAppVersionOption added in v0.7.0

func MinAppVersionOption(minAppVersion uint16) ServerOption

func OnCloseOption

func OnCloseOption(cb func(WriteCloser)) ServerOption

OnCloseOption

func OnConnectOption

func OnConnectOption(cb func(WriteCloser) bool) ServerOption

OnConnectOption

func OnErrorOption

func OnErrorOption(cb func(WriteCloser)) ServerOption

OnErrorOption

func OnMessageOption

func OnMessageOption(cb func(msg.RelayMsgBuf, WriteCloser)) ServerOption

OnMessageOption

func P2pAddressOption added in v0.8.0

func P2pAddressOption(p2pAddress string) ServerOption

type ServerVolRecOption added in v0.8.0

type ServerVolRecOption func(*volRecOpts)

func LogAllOption added in v0.8.0

func LogAllOption(logOpen bool) ServerVolRecOption

LogAllOption

func LogInboundOption added in v0.8.0

func LogInboundOption(logOpen bool) ServerVolRecOption

LogInboundOption

func LogOutboundOption added in v0.8.0

func LogOutboundOption(logOpen bool) ServerVolRecOption

LogOutboundOption

func LogReadOption added in v0.8.0

func LogReadOption(logOpen bool) ServerVolRecOption

LogReadOption

func OnStartLogOption added in v0.8.0

func OnStartLogOption(cb func(*Server)) ServerVolRecOption

func OnWriteOption added in v0.8.0

func OnWriteOption(logOpen bool) ServerVolRecOption

OnWriteOption

type TaskPool

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

TaskPool

var GlobalTaskPool *TaskPool

GlobalTaskPool

func (*TaskPool) Job

func (tp *TaskPool) Job(id int64, fc func()) error

Job: add job to the pool

type TimeoutHandler added in v0.7.0

type TimeoutHandler interface {
	Handle(ctx context.Context, message *msg.RelayMsgBuf)
	GetDuration() time.Duration
	GetTimeoutMsg(reqMessage *msg.RelayMsgBuf) *msg.RelayMsgBuf
	CanDelete(rspMessage *msg.RelayMsgBuf) bool
}

type WriteCloser

type WriteCloser interface {
	Write(*message.RelayMsgBuf, context.Context) error
	Close()
}

WriteCloser

type WriteHook added in v0.9.0

type WriteHook struct {
	Message string
	Fn      func(packetId, costTime int64)
}

Jump to

Keyboard shortcuts

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