-
Notifications
You must be signed in to change notification settings - Fork 151
/
defs.go
142 lines (128 loc) · 3.19 KB
/
defs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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])
}