util

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2020 License: MIT Imports: 21 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPortRange     = errors.New("invalid port range")
	ErrInvalidAddressFormat = errors.New("invalid address format")
)
View Source
var (
	ErrMaxPacket  = errors.New("packet over size")
	ErrMinPacket  = errors.New("packet short size")
	ErrShortMsgID = errors.New("short msgid")
)

Functions

func BytesMD5 added in v0.0.3

func BytesMD5(data []byte) string

字节计算MD5

func CompressBytes added in v0.0.3

func CompressBytes(data []byte) ([]byte, error)

压缩字节

func DecompressBytes added in v0.0.3

func DecompressBytes(data []byte) ([]byte, error)

解压字节

func DetectPort added in v0.0.3

func DetectPort(addr string, fn func(a *Address, port int) (interface{}, error)) (interface{}, error)

在给定的端口范围内找到一个能用的端口 addr格式参考ParseAddress函数

func FileExists added in v0.0.3

func FileExists(name string) bool

检查文件是否存在

func FileSize added in v0.0.3

func FileSize(name string) int64

获取文件大小

func GetLocalIP added in v0.0.3

func GetLocalIP() string

获取本地IP地址,有多重IP时,默认取第一个

func GetPrivateIPv4 added in v0.0.3

func GetPrivateIPv4() ([]*net.IPAddr, error)

获得本机的IPV4的地址

func GetPublicIPv6 added in v0.0.3

func GetPublicIPv6() ([]*net.IPAddr, error)

获得本机的IPV6地址

func GetRemoteAddrss added in v0.0.3

func GetRemoteAddrss(ses cellnet.Session) (string, bool)

获取session远程的地址

func IsEOFOrNetReadError

func IsEOFOrNetReadError(err error) bool

判断网络错误

func JoinAddress added in v0.0.3

func JoinAddress(host string, port int) string

将host和端口合并为(host:port)格式的地址

func ReadFileLines added in v0.0.3

func ReadFileLines(filename string, callback func(line string) bool) error

读取文本文件的所有行

func ReadKVFile added in v0.0.3

func ReadKVFile(filename string, callback func(k, v string) bool) (ret error)

读取=分割的配置文件

func RecvLTVPacket

func RecvLTVPacket(reader io.Reader, maxPacketSize int) (msg interface{}, err error)

接收Length-Type-Value格式的封包流程

func SendLTVPacket

func SendLTVPacket(writer io.Writer, ctx cellnet.ContextSet, data interface{}) error

发送Length-Type-Value格式的封包流程

func SpliteAddress

func SpliteAddress(addr string) (host string, port int, err error)

将普通地址格式(host:port)拆分

func StackToString added in v0.0.3

func StackToString(count int) string

给定打印层数,一般3~5覆盖你的逻辑及封装代码范围

func StringHash

func StringHash(s string) (hash uint16)

字符串转为16位整形值

func StringMD5 added in v0.0.3

func StringMD5(str string) string

字符串计算MD5

func WriteFull

func WriteFull(writer io.Writer, buf []byte) error

完整发送所有封包

Types

type Address added in v0.0.3

type Address struct {
	Scheme  string
	Host    string
	MinPort int
	MaxPort int
	Path    string
}

支持地址范围的格式

func ParseAddress added in v0.0.3

func ParseAddress(addr string) (addrObj *Address, err error)

cellnet专有的地址格式 scheme://host:minPort~maxPort/path 提供地址范围扩展

func (*Address) HostPortString added in v0.0.3

func (self *Address) HostPortString(port int) string

返回(host:port)格式地址

func (*Address) String added in v0.0.3

func (self *Address) String(port int) string

返回scheme://host:port/path 格式地址

type KVPair added in v0.0.3

type KVPair struct {
	Key   string
	Value string
}

func ReadKVFileValues added in v0.0.3

func ReadKVFileValues(filename string) (ret []KVPair, err error)

将=分割的文件按值读回

type Pipe

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

不限制大小,添加不发生阻塞,接收阻塞等待

func NewPipe

func NewPipe() *Pipe

func (*Pipe) Add

func (self *Pipe) Add(msg interface{})

添加时不会发送阻塞

func (*Pipe) Pick

func (self *Pipe) Pick(retList *[]interface{}) (exit bool)

如果没有数据,发生阻塞

func (*Pipe) Reset

func (self *Pipe) Reset()

type Queue

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

func NewQueue

func NewQueue(size int) *Queue

func (*Queue) Clear added in v0.0.3

func (self *Queue) Clear()

func (*Queue) Count

func (self *Queue) Count() int

func (*Queue) Dequeue

func (self *Queue) Dequeue() (ret interface{})

func (*Queue) Enqueue

func (self *Queue) Enqueue(data interface{})

func (*Queue) Peek

func (self *Queue) Peek() interface{}

type RemoteAddr added in v0.0.3

type RemoteAddr interface {
	RemoteAddr() net.Addr
}

修复ws没有实现所有net.conn方法,导致无法获取客服端地址问题.

type SignalTester

type SignalTester struct {
	*testing.T
	// contains filtered or unexported fields
}

func NewSignalTester

func NewSignalTester(t *testing.T) *SignalTester

func (*SignalTester) Done

func (self *SignalTester) Done(value interface{})

func (*SignalTester) SetTimeout

func (self *SignalTester) SetTimeout(du time.Duration)

func (*SignalTester) WaitAndExpect

func (self *SignalTester) WaitAndExpect(msg string, values ...interface{}) bool

Jump to

Keyboard shortcuts

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