3 Star 5 Fork 4

Gitee 极速下载/Tao

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/leesper/tao
克隆/下载
defs.go 3.19 KB
一键复制 编辑 原始数据 按行查看 历史
leesper 提交于 2017-10-16 18:04 . delete flag.Parse() in server.go
package tao
import (
"context"
"errors"
"fmt"
"hash/fnv"
"os"
"reflect"
"runtime"
"time"
"unsafe"
)
// ErrUndefined for undefined message type.
type ErrUndefined int32
func (e ErrUndefined) Error() string {
return fmt.Sprintf("undefined message type %d", e)
}
// Error codes returned by failures dealing with server or connection.
var (
ErrParameter = errors.New("parameter error")
ErrNilKey = errors.New("nil key")
ErrNilValue = errors.New("nil value")
ErrWouldBlock = errors.New("would block")
ErrNotHashable = errors.New("not hashable")
ErrNilData = errors.New("nil data")
ErrBadData = errors.New("more than 8M data")
ErrNotRegistered = errors.New("handler not registered")
ErrServerClosed = errors.New("server has been closed")
)
// definitions about some constants.
const (
MaxConnections = 1000
BufferSize128 = 128
BufferSize256 = 256
BufferSize512 = 512
BufferSize1024 = 1024
defaultWorkersNum = 20
)
type onConnectFunc func(WriteCloser) bool
type onMessageFunc func(Message, WriteCloser)
type onCloseFunc func(WriteCloser)
type onErrorFunc func(WriteCloser)
type workerFunc func()
type onScheduleFunc func(time.Time, WriteCloser)
// OnTimeOut represents a timed task.
type OnTimeOut struct {
Callback func(time.Time, WriteCloser)
Ctx context.Context
}
// NewOnTimeOut returns OnTimeOut.
func NewOnTimeOut(ctx context.Context, cb func(time.Time, WriteCloser)) *OnTimeOut {
return &OnTimeOut{
Callback: cb,
Ctx: ctx,
}
}
// Hashable is a interface for hashable object.
type Hashable interface {
HashCode() int32
}
const intSize = unsafe.Sizeof(1)
func hashCode(k interface{}) uint32 {
var code uint32
h := fnv.New32a()
switch v := k.(type) {
case bool:
h.Write((*((*[1]byte)(unsafe.Pointer(&v))))[:])
code = h.Sum32()
case int:
h.Write((*((*[intSize]byte)(unsafe.Pointer(&v))))[:])
code = h.Sum32()
case int8:
h.Write((*((*[1]byte)(unsafe.Pointer(&v))))[:])
code = h.Sum32()
case int16:
h.Write((*((*[2]byte)(unsafe.Pointer(&v))))[:])
code = h.Sum32()
case int32:
h.Write((*((*[4]byte)(unsafe.Pointer(&v))))[:])
code = h.Sum32()
case int64:
h.Write((*((*[8]byte)(unsafe.Pointer(&v))))[:])
code = h.Sum32()
case uint:
h.Write((*((*[intSize]byte)(unsafe.Pointer(&v))))[:])
code = h.Sum32()
case uint8:
h.Write((*((*[1]byte)(unsafe.Pointer(&v))))[:])
code = h.Sum32()
case uint16:
h.Write((*((*[2]byte)(unsafe.Pointer(&v))))[:])
code = h.Sum32()
case uint32:
h.Write((*((*[4]byte)(unsafe.Pointer(&v))))[:])
code = h.Sum32()
case uint64:
h.Write((*((*[8]byte)(unsafe.Pointer(&v))))[:])
code = h.Sum32()
case string:
h.Write([]byte(v))
code = h.Sum32()
case Hashable:
c := v.HashCode()
h.Write((*((*[4]byte)(unsafe.Pointer(&c))))[:])
code = h.Sum32()
default:
panic("key not hashable")
}
return code
}
func isNil(v interface{}) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
kd := rv.Type().Kind()
switch kd {
case reflect.Ptr, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Slice:
return rv.IsNil()
default:
return false
}
}
func printStack() {
var buf [4096]byte
n := runtime.Stack(buf[:], false)
os.Stderr.Write(buf[:n])
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mirrors/Tao.git
git@gitee.com:mirrors/Tao.git
mirrors
Tao
Tao
master

搜索帮助