comm

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTLVRead is returned when there is an error reading a TLV object.
	ErrTLVRead = fmt.Errorf("TLV %s", "read error")
	// ErrTLVWrite is returned when  there is an error writing a TLV object.
	ErrTLVWrite = fmt.Errorf("TLV %s", "write error")
	// ErrTypeNotFound is returned when a request for a TLV type is made and none can be found.
	ErrTypeNotFound = fmt.Errorf("TLV %s", "type not found")
)

Functions

func BcdToString

func BcdToString(bcd []byte) string

func CopyByte

func CopyByte(dest []byte, src byte, index int) int

func CopyStr

func CopyStr(dest []byte, src string, index int, len int) int

func DiceCheck

func DiceCheck(prob float64) bool

DiceCheck 投概率骰子,得到结果比给定数字大则返回true,否则返回false

func Equal

func Equal(tlv1, tlv2 TLV) bool

Equal returns true if a pair of TLV objects are the same.

func FormatTime

func FormatTime(time time.Time) string

func IntToFixStr

func IntToFixStr(i int64, l int) string

func LogHex

func LogHex(level logging.Level, model string, bts []byte)

func RandNum

func RandNum(min, max int32) int

func SavePid

func SavePid(f string) string

SavePid 在程序执行的当前目录生成pid文件

func StartMonitor

func StartMonitor(port int)

StartMonitor 开启pprof,监听请求

func StoBcd

func StoBcd(w string) []byte

func TakeBytes

func TakeBytes(c gnet.Conn, bytes int) []byte

TakeBytes 消费一定字节数的数据

func ToBytes

func ToBytes(tlv TLV) ([]byte, error)

ToBytes returns bytes from a TLV object

func ToTPUDHISlices

func ToTPUDHISlices(content []byte, pkgLen int) (rt [][]byte)

ToTPUDHISlices 拆分为长短信切片 纯ASCII内容的拆分 pkgLen = 160 含中文内容的拆分 pkgLen = 140

func TrimStr

func TrimStr(bts []byte) string

func Ucs2Decode

func Ucs2Decode(ucs2 []byte) string

Ucs2Decode Decode from UCS2.

func Ucs2Encode

func Ucs2Encode(s string) []byte

Ucs2Encode Encode to UCS2.

func WriteObject

func WriteObject(tlv TLV, w io.Writer) error

WriteObject writes a TLV object to io.Writer

Types

type BcdSequence

type BcdSequence struct {
	sync.Mutex // 锁
	// contains filtered or unexported fields
}

BcdSequence 电信MsgId序号生成器,支持每分钟产生100万个不重复序号,超出后序号会重复 BCD 4bit编码,用4bit表示0-9的数字

func NewBcdSequence

func NewBcdSequence(w string) *BcdSequence

func (*BcdSequence) NextVal

func (tf *BcdSequence) NextVal() []byte

type CycleSequence

type CycleSequence struct {
	sync.Mutex // 锁
	// contains filtered or unexported fields
}

CycleSequence 生成可循环使用的序号 构成为: 0 | datacenter 2 bit | worker 3 bit| sequence 26 bit 最大支持32个节点,单节点2^26以内不会重复(67,108,864)

func NewCycleSequence

func NewCycleSequence(d int32, w int32) *CycleSequence

NewCycleSequence d for datacenter-id, w for worker-id

func (*CycleSequence) NextVal

func (s *CycleSequence) NextVal() int32

func (*CycleSequence) String

func (s *CycleSequence) String() string

type TLV

type TLV interface {
	Type() uint16
	Length() uint16
	Value() []byte
}

TLV represents a Type-Length-Value object.

func FromBytes

func FromBytes(data []byte) (TLV, error)

FromBytes returns a TLV object from bytes

func New

func New(typ uint16, val []byte) TLV

New returns a TLV object from the args

func ReadObject

func ReadObject(r io.Reader) (TLV, error)

ReadObject returns a TLV object from io.Reader

type TlvList

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

TlvList is ad double-linked list containing TLV objects.

func NewTlvList

func NewTlvList() *TlvList

NewTlvList returns a new, empty TLVList.

func Read

func Read(r io.Reader) (*TlvList, error)

Read takes an io.Reader and builds a TLVList from that.

func (*TlvList) Add

func (tl *TlvList) Add(typ uint16, value []byte)

Add pushes a new TLV object onto the TLVList. It builds the object from its args

func (*TlvList) AddObject

func (tl *TlvList) AddObject(obj TLV)

AddObject adds a TLV object onto the TLVList

func (*TlvList) Get

func (tl *TlvList) Get(typ uint16) (TLV, error)

Get checks the TLVList for any object matching the type, It returns the first one found. If the type could not be found, Get returns ErrTypeNotFound.

func (*TlvList) GetAll

func (tl *TlvList) GetAll(typ uint16) []TLV

GetAll checks the TLVList for all objects matching the type, returning a slice containing all matching objects. If no object has the requested type, an empty slice is returned.

func (*TlvList) Length

func (tl *TlvList) Length() int32

Length returns the number of objects int the TLVList.

func (*TlvList) Remove

func (tl *TlvList) Remove(typ uint16) int

Remove removes all objects with the requested type. It returns a count of the number of removed objects.

func (*TlvList) RemoveObject

func (tl *TlvList) RemoveObject(obj TLV) int

RemoveObject takes an TLV object as an argument, and removes all matching objects. It matches on not just type, but also the value contained in the object.

func (*TlvList) String

func (tl *TlvList) String() string

func (*TlvList) Write

func (tl *TlvList) Write(w io.Writer) error

Write writes out the TLVList to an io.Writer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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