oo

package
v0.0.0-...-356349e Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2019 License: MIT Imports: 37 Imported by: 0

Documentation

Overview

channel.go 中转层,框架层与底层交互的管道,负责连接池管理、打包发送、回包转发

Index

Constants

View Source
const (
	DATE_FMT_STR      = "2006-01-02"
	DATE_TIME_FMT_STR = "2006-01-02 15:04:05"
)
View Source
const EBADGATEWAY = "EBADGATEWAY"
View Source
const EBALANCE = "EBALANCE"
View Source
const EEXISTS = "EEXISTS"
View Source
const EFATAL = "EFATAL"
View Source
const ELIMITED = "ELIMITED"
View Source
const ENEEDCODE = "ENEEDCODE"
View Source
const ENEXISTS = "ENEXISTS"
View Source
const ENODATA = "ENODATA"
View Source
const ENOTALLOW = "ENOTALLOW"
View Source
const ENOTAUTH = "ENOTAUTH"
View Source
const ENOTPERM = "ENOTPERM"
View Source
const EPARAM = "EPARAM"
View Source
const EPASSWD = "EPASSWD"
View Source
const ESERVER = "ESERVER"
View Source
const ESUCC = "ESUCC"

外部错误号

View Source
const ETIMENOTALLOW = "ETIMENOTALLOW"
View Source
const ETIMEOUT = "ETIMEOUT"

Variables

This section is empty.

Functions

func AesDecrypt

func AesDecrypt(passwd, ctx []byte) []byte

func AesEncrypt

func AesEncrypt(passwd, ctx []byte) []byte

aes

func Base58Decode

func Base58Decode(input []byte) []byte

Base58Decode decodes Base58-encoded data

func Base58Encode

func Base58Encode(input []byte) []byte

Base58Encode encodes a byte array to Base58

func Base64Decode

func Base64Decode(str string) ([]byte, error)

base64 解码

func Base64Encode

func Base64Encode(data []byte) string

base64 编码

func Bytes2Str

func Bytes2Str(b []byte) string

func BytesToInt

func BytesToInt(buf []byte) int64

字节转换成整形

func CalBasicAuthValue

func CalBasicAuthValue(username, password string) string

func CheckBasicAuth

func CheckBasicAuth(w http.ResponseWriter, r *http.Request) (uname string, upass string, bRet bool)

func CheckKey

func CheckKey(key string) (sessid string, uid uint64, e error)

func CheckPlainKey

func CheckPlainKey(key string) (sessid string, uid uint64, e error)

func CheckSession

func CheckSession(sess *Session) (sessid string, uid uint64, e error)

func ErrStr

func ErrStr(eno string) string

func EscapeByte

func EscapeByte(w io.Writer, b []byte)

func FileExists

func FileExists(path string) (bool, error)

func Fmt2Time

func Fmt2Time(s string) (ret time.Time, err error)

func Fmt2Ts

func Fmt2Ts(s string) int64

func GenEmailCode

func GenEmailCode() string

func GenImgCode

func GenImgCode() string

func GenKey

func GenKey(sessid, passwd string, uid, expire uint64) string

用于下发给客户端

func GenSessid

func GenSessid(uid uint64) string

用于Redis存储的session

func GetRealIP

func GetRealIP(r *http.Request) string

func GetSvrmark

func GetSvrmark(svrname string) string

func HashTime33

func HashTime33(str string) int

hash_time33 算法

func HmacSha1

func HmacSha1(data []byte, key []byte) []byte

hmac_sha1 算法:返回结果的原始buffer

func HmacSha1Str

func HmacSha1Str(data []byte, key []byte) string

hmac_sha1 算法:返回结果的16进制字符串

func HmacSha256

func HmacSha256(data []byte, key []byte) []byte

hmac_sha256 算法:返回结果的原始buffer

func HmacSha256Str

func HmacSha256Str(data []byte, key []byte) string

hmac_sha256 算法:返回结果的16进制字符串

func HttpRequest

func HttpRequest(method, url string, header map[string]string, body []byte) (buf []byte, err error)

func HttpRequestAndParse

func HttpRequestAndParse(method, url string, header map[string]string, body []byte, val interface{}) (err error)

func HttpRequestTimeoutAndParse

func HttpRequestTimeoutAndParse(method, url string, header map[string]string, body []byte, timeout int, val interface{}) (err error)

func IP2Uint32

func IP2Uint32(ipStr string) uint32

IP格式转为uint32

func If

func If(condition bool, trueVal, falseVal interface{}) interface{}

If : ternary operator (三元运算) condition:比较运算 trueVal:运算结果为真时的值 falseVal:运算结果为假时的值 return: 由于不知道传入值的类型, 所有, 必须在接收结果时, 指定对应的值类型

func InArray

func InArray(needle interface{}, hystack interface{}) bool

InArray :给定元素值 是否在 指定的数组中

func InitLog

func InitLog(logdir string, svrname string, warnstr string, fn func(s string), intervals ...int64)

func IntToBytes

func IntToBytes(num interface{}) []byte

整形转换成字节

func JoinArray

func JoinArray(arr interface{}, sep string) string

func JsonData

func JsonData(reqjson interface{}) []byte

为了链式表达,以及不再次引入jsoniter库

func JsonEncode

func JsonEncode(s string) string

func JsonMarshal

func JsonMarshal(v interface{}) (data []byte, err error)

func JsonUnmarshal

func JsonUnmarshal(data []byte, v interface{}) (err error)

func ListenWsServer

func ListenWsServer(addr string, webdir string, chmgr *ChannelManager,
	open_fn func(http.ResponseWriter, *http.Request, *WebSock) error)

func LogBool

func LogBool(cond bool, args ...interface{})

func LogD

func LogD(format string, v ...interface{})

func LogError

func LogError(err error, args ...interface{})

func LogSetCallback

func LogSetCallback(fn func(s string))

func LogUid

func LogUid(uid uint64, format string, v ...interface{})

func LogUpdateLevel

func LogUpdateLevel(level int, uids []uint64)

func LogW

func LogW(format string, v ...interface{})

func LogWrite

func LogWrite(w io.Writer, level string, format string, v ...interface{})

func LowerCaseWithUnderscores

func LowerCaseWithUnderscores(name string) string

映射函数:首字母转小写,大写转成下划线加小写 反向则是:首字母大写,下划线去掉,其后字母转大写

func Md5

func Md5(data []byte) []byte

md5 算法:返回结果的原始buffer

func Md5Str

func Md5Str(data []byte) string

md5 算法:返回结果的16进制字符串

func NewError

func NewError(format string, v ...interface{}) error

func Sha1

func Sha1(data []byte) []byte

sha1 算法:返回结果的原始buffer

func Sha1Str

func Sha1Str(data []byte) string

sha1 算法:返回结果的16进制字符串

func StatChg

func StatChg(key string, val int64)

func StatKeepMax

func StatKeepMax(key string, val int64)

func StatKeepMin

func StatKeepMin(key string, val int64)

func StatSet

func StatSet(key string, val int64)

func Str2Bytes

func Str2Bytes(s string) []byte

func Str2Time

func Str2Time(str, format string) (ret time.Time, err error)

func StringSlice

func StringSlice(ss interface{}) []string

func Struct2Map

func Struct2Map(obj interface{}, args ...interface{}) map[string]interface{}

func TimeNowUnix

func TimeNowUnix() int64

func ToSlice

func ToSlice(arr interface{}) ([]interface{}, bool)

通用转换为数组

func ToStr

func ToStr(data interface{}) string

func Ts2Fmt

func Ts2Fmt(ts int64) string

func Uint32ToIP

func Uint32ToIP(ip uint32) string

uint32转为IP格式

func UploadHandler

func UploadHandler(w http.ResponseWriter, r *http.Request, up_path string)

func UpperCaseWithNoUnderscores

func UpperCaseWithNoUnderscores(name string) string

Types

type Channel

type Channel struct {
	Data interface{}

	Chmgr *ChannelManager
	// contains filtered or unexported fields
}

func (*Channel) Close

func (ch *Channel) Close()

func (*Channel) GetSeq

func (ch *Channel) GetSeq() uint64

func (*Channel) IsClosed

func (ch *Channel) IsClosed() <-chan struct{}

func (*Channel) PushMsg

func (ch *Channel) PushMsg(m interface{})

func (*Channel) RecvChan

func (ch *Channel) RecvChan() <-chan interface{}

type ChannelManager

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

func NewChannelManager

func NewChannelManager(chlen uint64) *ChannelManager

func (*ChannelManager) DelChannel

func (cm *ChannelManager) DelChannel(ch *Channel)

func (*ChannelManager) GetChannel

func (cm *ChannelManager) GetChannel(seq uint64) (*Channel, error)

func (*ChannelManager) NewChannel

func (cm *ChannelManager) NewChannel() *Channel

func (*ChannelManager) PushAllChannelMsg

func (cm *ChannelManager) PushAllChannelMsg(m interface{}, fn func(Data interface{}) bool) error

func (*ChannelManager) PushChannelMsg

func (cm *ChannelManager) PushChannelMsg(seq uint64, m interface{}) error

func (*ChannelManager) PushOneChannelMsg

func (cm *ChannelManager) PushOneChannelMsg(m interface{}, fn func(Data interface{}) bool) error

type Error

type Error = struct {
	Ret string `json:"ret,omitempty"` // 错误号
	Msg string `json:"msg,omitempty"` // 错误信息
	Eid string `json:"eid,omitempty"` // 出错时相关的ID,例如订单号
}

func PackErr

func PackErr(err string) *Error

type ParaNull

type ParaNull = struct {
}

type ReqCtx

type ReqCtx = struct {
	Ctx    interface{} // 所在环境,例如websock, ns, ...
	Cmd    string      // 原请求命令,例如logind.registe
	Sess   *Session    // 请求环境
	CoSche bool        // 是否在新起协程环境?方便判断堵塞情况
}

func MakeReqCtx

func MakeReqCtx(c interface{}, cmd string, sess *Session) (ctx *ReqCtx)

type ReqCtxHandler

type ReqCtxHandler = func(*ReqCtx, *RpcMsg) (*RpcMsg, error)

type RpcError

type RpcError struct {
	Eno string
	Err error
}

func NewRpcError

func NewRpcError(eno string, format ...interface{}) (e *RpcError)

func (*RpcError) Errno

func (e *RpcError) Errno() string

func (*RpcError) Error

func (e *RpcError) Error() string

type RpcMsg

type RpcMsg = struct {
	Cmd  string          `json:"cmd,omitempty"`
	Mark string          `json:"mark,omitempty"` // 标志,通常用于细化转发
	Chk  string          `json:"chk,omitempty"`
	Sess *Session        `json:"sess,omitempty"`
	Err  *Error          `json:"err,omitempty"`
	Para json.RawMessage `json:"para,omitempty"`
}

func PackError

func PackError(cmd string, err string, msg string) *RpcMsg

func PackFatal

func PackFatal(msg string) *RpcMsg

func PackNull

func PackNull(cmd string) *RpcMsg

func PackPara

func PackPara(cmd string, para interface{}) *RpcMsg

func PackRpcMsg

func PackRpcMsg(cmd string, para interface{}, sess *Session) *RpcMsg

func PackRspMsg

func PackRspMsg(reqmsg *RpcMsg, eret string, rsp interface{}, eid ...string) (rspmsg *RpcMsg)

func RpcReturn

func RpcReturn(reqmsg *RpcMsg, eret_rpcerr interface{}, rsp ...interface{}) (rspmsg *RpcMsg, err error)

转化rpcerr, 清空err

type Session

type Session = struct {
	Uid    uint64 `json:"uid,omitempty"`
	Key    string `json:"key,omitempty"`
	Gwid   string `json:"gwid,omitempty"`   // gw的svrmark
	Connid uint64 `json:"connid,omitempty"` // 连接ch
	Ipv4   uint   `json:"ipv4,omitempty"`
}

func PackSess

func PackSess(uid uint64, key string, gwid string, connid uint64) *Session

type Sqler

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

Sqler is data mapper struct

func NewSqler

func NewSqler(args ...interface{}) *Sqler

func (*Sqler) Avg

func (dba *Sqler) Avg(avg string) string

Avg : select avg field

func (*Sqler) BuildExecut

func (dba *Sqler) BuildExecut() (string, error)

BuildExecut : build execute query string

func (*Sqler) BuildQuery

func (dba *Sqler) BuildQuery() (string, error)

BuildQuery : build query string

func (*Sqler) Count

func (dba *Sqler) Count(args ...interface{}) string

Count : select count rows

func (*Sqler) Delete

func (dba *Sqler) Delete() string

Delete : delete data

func (*Sqler) Distinct

func (dba *Sqler) Distinct() *Sqler

Distinct : select distinct

func (*Sqler) Group

func (dba *Sqler) Group(group string) *Sqler

Group : select group by

func (*Sqler) Having

func (dba *Sqler) Having(having string) *Sqler

Having : select having

func (*Sqler) Ignore

func (dba *Sqler) Ignore() *Sqler

func (*Sqler) Insert

func (dba *Sqler) Insert(data interface{}) string

Insert : insert data

func (*Sqler) InsertBatch

func (dba *Sqler) InsertBatch(data interface{}) string

func (*Sqler) InsertDuplicate

func (dba *Sqler) InsertDuplicate(data interface{}, key_fields []string, args ...interface{}) string

func (*Sqler) Join

func (dba *Sqler) Join(args ...interface{}) *Sqler

Join : select join query

func (*Sqler) LeftJoin

func (dba *Sqler) LeftJoin(args ...interface{}) *Sqler

LeftJoin : like join , the relation is left

func (*Sqler) Limit

func (dba *Sqler) Limit(limit int) *Sqler

Limit : select limit

func (*Sqler) Max

func (dba *Sqler) Max(max string) string

Max : select max field

func (*Sqler) Min

func (dba *Sqler) Min(min string) string

Min : select min field

func (*Sqler) Offset

func (dba *Sqler) Offset(offset int) *Sqler

Offset : select offset

func (*Sqler) OrIgnore

func (dba *Sqler) OrIgnore() *Sqler

func (*Sqler) OrWhere

func (dba *Sqler) OrWhere(args ...interface{}) *Sqler

OrWhere : like where , but the relation is or,

func (*Sqler) Order

func (dba *Sqler) Order(order string) *Sqler

Order : select order by

func (*Sqler) Page

func (dba *Sqler) Page(page int) *Sqler

Page : select page

func (*Sqler) Reset

func (dba *Sqler) Reset()

------------------------------------------------------------------------- Reset : reset union select

func (*Sqler) RightJoin

func (dba *Sqler) RightJoin(args ...interface{}) *Sqler

RightJoin : like join , the relation is right

func (*Sqler) Select

func (dba *Sqler) Select(args ...interface{}) string

为了链式使用,不返回错误号,若需要则自行调用BuildQuery

func (*Sqler) Sum

func (dba *Sqler) Sum(sum string) string

Sum : select sum field

func (*Sqler) Table

func (dba *Sqler) Table(table string) *Sqler

Table : select table

func (*Sqler) Update

func (dba *Sqler) Update(data interface{}, args ...interface{}) string

Update : update data: string, map[string]interface{}, struct

func (*Sqler) UpdateBatch

func (dba *Sqler) UpdateBatch(data []map[string]interface{}, keyarr interface{}, args ...interface{}) string

func (*Sqler) Where

func (dba *Sqler) Where(args ...interface{}) *Sqler

Where : query or execute where condition, the relation is and

type TagOptions

type TagOptions struct {
	Skip      bool // "-"
	Name      string
	Omitempty bool
	Omitzero  bool
}

func GetTagOptions

func GetTagOptions(tag reflect.StructTag, tagname string) TagOptions

type WebSock

type WebSock struct {
	Ws   *websocket.Conn
	Ch   *Channel
	Data interface{} // 用于扩展

	Sess *Session // 一般是在open_fn里设置好这里,方便后面makereqctx
	// contains filtered or unexported fields
}

func InitWebSock

func InitWebSock(w http.ResponseWriter, r *http.Request, chmgr *ChannelManager) (c *WebSock, err error)

func InitWsClient

func InitWsClient(Scheme string, Host string, Path string, chmgr *ChannelManager) (*WebSock, error)

func (*WebSock) Close

func (c *WebSock) Close()

func (*WebSock) ConnInfo

func (c *WebSock) ConnInfo() (s string)

func (*WebSock) DefHandleFunc

func (c *WebSock) DefHandleFunc(fn interface{})

func (*WebSock) HandleFunc

func (c *WebSock) HandleFunc(cmd string, fn interface{})

老接口回调要二次查找

func (*WebSock) HandleFuncMap

func (c *WebSock) HandleFuncMap(mm map[string]ReqCtxHandler)

func (*WebSock) IsReady

func (c *WebSock) IsReady() bool

func (*WebSock) OpenCoroutineFlag

func (c *WebSock) OpenCoroutineFlag()

func (*WebSock) RecvRequest

func (c *WebSock) RecvRequest()

只有服务端需要主动调用

func (*WebSock) SendRpc

func (c *WebSock) SendRpc(rpcmsg *RpcMsg) error

func (*WebSock) SendRpcParseResponse

func (c *WebSock) SendRpcParseResponse(rpcmsg *RpcMsg, sec int64, pret interface{}) (err error)

func (*WebSock) SendRpcSafe

func (c *WebSock) SendRpcSafe(rpcmsg *RpcMsg) error

func (*WebSock) SendRpcWithResponse

func (c *WebSock) SendRpcWithResponse(rpcmsg *RpcMsg, sec int64) (rsp *RpcMsg, err error)

func (*WebSock) SetCloseHandler

func (c *WebSock) SetCloseHandler(handler func(c *WebSock))

func (*WebSock) SetIntervalHandler

func (c *WebSock) SetIntervalHandler(fn func(c *WebSock, ms int64))

func (*WebSock) SetOpenHandler

func (c *WebSock) SetOpenHandler(handler func(c *WebSock))

func (*WebSock) SetReadTimeout

func (c *WebSock) SetReadTimeout(read_timeo int64)

func (*WebSock) SkipHandleFunc

func (c *WebSock) SkipHandleFunc(cmd string)

func (*WebSock) StartDial

func (c *WebSock) StartDial(chk_interval int64, def_ip string)

客户端进入自动重连,若不想堵塞可以用go程

type WsHandler

type WsHandler = func(*WebSock, *RpcMsg) (*RpcMsg, error)

返回的err仅用于日志,若要回发消息,必须明确返回消息部分

Jump to

Keyboard shortcuts

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