-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
378 lines (361 loc) · 7.97 KB
/
utils.go
File metadata and controls
378 lines (361 loc) · 7.97 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package hbtp
import (
"bytes"
"context"
"errors"
"fmt"
"math"
"net"
"os"
"reflect"
"time"
"unsafe"
)
func PathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func EndContext(ctx context.Context) bool {
if ctx == nil {
return true
}
select {
case <-ctx.Done():
return true
default:
return false
}
}
var Debug = false
func Infof(s string, args ...interface{}) {
tms := time.Now().Format("2006-01-02 15:04:05")
if len(args) > 0 {
fmt.Println(tms + " [info] " + fmt.Sprintf(s, args...))
} else {
fmt.Println(tms + " [info] " + s)
}
}
func Errorf(s string, args ...interface{}) {
tms := time.Now().Format("2006-01-02 15:04:05")
if len(args) > 0 {
println(tms + " [err] " + fmt.Sprintf(s, args...))
} else {
println(tms + " [err] " + s)
}
}
func Debugf(s string, args ...interface{}) {
if !Debug {
return
}
tms := time.Now().Format("2006-01-02 15:04:05")
if len(args) > 0 {
println(tms + " [debug] " + fmt.Sprintf(s, args...))
} else {
println(tms + " [debug] " + s)
}
}
func TcpRead(ctx context.Context, conn net.Conn, ln uint) ([]byte, error) {
if conn == nil || ln <= 0 {
return nil, errors.New("handleRead ln<0")
}
rn := uint(0)
bts := make([]byte, ln)
for {
if EndContext(ctx) {
return nil, errors.New("context dead")
}
n, err := conn.Read(bts[rn:])
if n > 0 {
rn += uint(n)
}
if rn >= ln {
break
}
if err != nil {
return nil, err
}
if n <= 0 {
return nil, errors.New("conn abort")
}
}
return bts, nil
}
func TcpWrite(ctx context.Context, conn net.Conn, bts []byte) error {
ln := len(bts)
if conn == nil || ln <= 0 {
return errors.New("handleRead ln<0")
}
wn := 0
for wn < ln {
if EndContext(ctx) {
return errors.New("context dead")
}
n, err := conn.Write(bts[wn:])
if err != nil {
return err
}
wn += n
}
return nil
}
// BigEndian
func BigByteToInt(data []byte) int64 {
ln := len(data)
rt := int64(0)
for i := 0; i < ln; i++ {
rt |= int64(data[ln-1-i]) << (i * 8)
}
return rt
}
func BigIntToByte(data int64, ln int) []byte {
rt := make([]byte, ln)
for i := 0; i < ln; i++ {
rt[ln-1-i] = byte(data >> (i * 8))
}
return rt
}
// LittleEndian
func LitByteToInt(data []byte) int64 {
ln := len(data)
rt := int64(0)
for i := 0; i < ln; i++ {
rt |= int64(data[i]) << (i * 8)
}
return rt
}
func LitIntToByte(data int64, ln int) []byte {
rt := make([]byte, ln)
for i := 0; i < ln; i++ {
rt[i] = byte(data >> (i * 8))
}
return rt
}
// BigEndian
func BigByteToFloat32(data []byte) float32 {
v := BigByteToInt(data)
return math.Float32frombits(uint32(v))
}
func BigFloatToByte32(data float32) []byte {
v := math.Float32bits(data)
return BigIntToByte(int64(v), 4)
}
func BigByteToFloat64(data []byte) float64 {
v := BigByteToInt(data)
return math.Float64frombits(uint64(v))
}
func BigFloatToByte64(data float64) []byte {
v := math.Float64bits(data)
return BigIntToByte(int64(v), 8)
}
// LittleEndian
func LitByteToFloat32(data []byte) float32 {
v := LitByteToInt(data)
return math.Float32frombits(uint32(v))
}
func LitFloatToByte32(data float32) []byte {
v := math.Float32bits(data)
return LitIntToByte(int64(v), 4)
}
func LitByteToFloat64(data []byte) float64 {
v := LitByteToInt(data)
return math.Float64frombits(uint64(v))
}
func LitFloatToByte64(data float64) []byte {
v := math.Float64bits(data)
return LitIntToByte(int64(v), 8)
}
type SliceMock struct {
addr unsafe.Pointer
len uint
cap uint
}
func FlcStructSizeof(pt interface{}) int {
if pt == nil {
return 0
}
ptv := reflect.ValueOf(pt)
if ptv.Kind() == reflect.Ptr {
if ptv.IsZero() {
return 0
}
ptv = ptv.Elem()
}
ln := ptv.NumField()
num := 0
for i := 0; i < ln; i++ {
fd := ptv.Field(i)
num += int(fd.Type().Size())
}
return num
}
func FlcStruct2Byte(pt interface{}) ([]byte, error) {
if pt == nil {
return nil, errors.New("param is nil")
}
ptv := reflect.ValueOf(pt)
if ptv.Kind() == reflect.Ptr {
if ptv.IsZero() {
return nil, errors.New("pt is null")
}
ptv = ptv.Elem()
}
ln := ptv.NumField()
buf := &bytes.Buffer{}
for i := 0; i < ln; i++ {
fdt := ptv.Type().Field(i)
fd := ptv.Field(i)
sz := int(fd.Type().Size())
val := fd.Interface()
var bts []byte
switch fd.Kind() {
case reflect.Int8:
bts = LitIntToByte(int64(val.(int8)), sz)
case reflect.Uint8:
bts = LitIntToByte(int64(val.(uint8)), sz)
case reflect.Int16:
bts = LitIntToByte(int64(val.(int16)), sz)
case reflect.Uint16:
bts = LitIntToByte(int64(val.(uint16)), sz)
case reflect.Int32:
bts = LitIntToByte(int64(val.(int32)), sz)
case reflect.Uint32:
bts = LitIntToByte(int64(val.(uint32)), sz)
case reflect.Int64:
bts = LitIntToByte(val.(int64), sz)
case reflect.Uint64:
bts = LitIntToByte(int64(val.(uint64)), sz)
case reflect.Int:
bts = LitIntToByte(int64(val.(int)), sz)
case reflect.Uint:
bts = LitIntToByte(int64(val.(uint)), sz)
case reflect.Float32:
bts = LitIntToByte(int64(val.(float32)), sz)
case reflect.Float64:
bts = LitIntToByte(int64(val.(float64)), sz)
default:
println("field not found type:%s", fdt.Name)
continue
}
buf.Write(bts)
}
return buf.Bytes(), nil
}
func FlcByte2Struct(bts []byte, pt interface{}) error {
if pt == nil {
return errors.New("param is nil")
}
ptv := reflect.ValueOf(pt)
if ptv.Kind() != reflect.Ptr || ptv.IsZero() {
return errors.New("pt is not ptr")
}
ptv = ptv.Elem()
ln := ptv.NumField()
buf := bytes.NewBuffer(bts)
for i := 0; i < ln; i++ {
fdt := ptv.Type().Field(i)
fd := ptv.Field(i)
sz := int(fd.Type().Size())
bts := make([]byte, sz)
n, _ := buf.Read(bts)
if n != sz {
println("field set err:%s", fdt.Name)
continue
}
val := LitByteToInt(bts)
switch fd.Kind() {
case reflect.Int8:
fd.Set(reflect.ValueOf(int8(val)))
case reflect.Uint8:
fd.Set(reflect.ValueOf(uint8(val)))
case reflect.Int16:
fd.Set(reflect.ValueOf(int16(val)))
case reflect.Uint16:
fd.Set(reflect.ValueOf(uint16(val)))
case reflect.Int32:
fd.Set(reflect.ValueOf(int32(val)))
case reflect.Uint32:
fd.Set(reflect.ValueOf(uint32(val)))
case reflect.Int64:
fd.Set(reflect.ValueOf(uint64(val)))
case reflect.Uint64:
fd.Set(reflect.ValueOf(uint64(val)))
case reflect.Int:
fd.Set(reflect.ValueOf(int(val)))
case reflect.Uint:
fd.Set(reflect.ValueOf(uint(val)))
case reflect.Float32:
fd.Set(reflect.ValueOf(float32(val)))
case reflect.Float64:
fd.Set(reflect.ValueOf(float64(val)))
default:
println("field not found type:%s", fdt.Name)
continue
}
}
return nil
}
/*func Struct2Byte(pt interface{}) ([]byte, error) {
if pt == nil {
return nil, errors.New("param is nil")
}
ln := SizeOf(pt)
return Struct2ByteLen(pt, ln)
}
func Struct2ByteLen(pt interface{}, ln int) ([]byte, error) {
if pt == nil {
return nil, errors.New("param is nil")
}
ptv := reflect.ValueOf(pt)
if ptv.Kind() != reflect.Ptr && !ptv.IsZero() {
return nil, errors.New("pt is not ptr")
}
return Struct2Bytes(unsafe.Pointer(ptv.Pointer()), ln)
}
func Byte2Struct(data []byte, pt interface{}) error {
if pt == nil {
return errors.New("param is nil")
}
ptv := reflect.ValueOf(pt)
if ptv.Kind() != reflect.Ptr && !ptv.IsZero() {
return errors.New("pt is not ptr")
}
pte := ptv.Elem()
if pte.Kind() != reflect.Struct {
return fmt.Errorf("*pt is not struct:%s", pte.Kind())
}
return Bytes2Struct(data, unsafe.Pointer(ptv.Pointer()))
}*/
func Ptr2Bytes(pt unsafe.Pointer, ln int) ([]byte, error) {
if pt == nil {
return nil, errors.New("param is nil")
}
mock := &SliceMock{
addr: pt,
cap: uint(ln),
len: uint(ln),
}
bts := *(*[]byte)(unsafe.Pointer(mock))
rtbts := make([]byte, mock.len)
copy(rtbts, bts)
return rtbts, nil
}
func Bytes2Ptr(data []byte, pt unsafe.Pointer) error {
ln := len(data)
if pt == nil || ln <= 0 {
return errors.New("param is nil")
}
mock := &SliceMock{
addr: pt,
cap: uint(ln),
len: uint(ln),
}
bts := *(*[]byte)(unsafe.Pointer(mock))
copy(bts, data)
return nil
}