Skip to content

Commit

Permalink
dev: add uint16 method for bytebuf
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxu19830126 committed Jan 14, 2019
1 parent 3d2943b commit 583c981
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 11 deletions.
68 changes: 67 additions & 1 deletion buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func Byte2UInt64(data []byte) uint64 {
return binary.BigEndian.Uint64(data)
}

// Byte2UInt16 byte array to uint16 value using big order
func Byte2UInt16(data []byte) uint16 {
return binary.BigEndian.Uint16(data)
}

// Byte2UInt32 byte array to uint32 value using big order
func Byte2UInt32(data []byte) uint32 {
return binary.BigEndian.Uint32(data)
Expand Down Expand Up @@ -92,6 +97,30 @@ func Int64ToBytes(v int64) []byte {
return ret
}

// Uint32ToBytesTo uint32 value to bytes array using big order
func Uint32ToBytesTo(v uint32, ret []byte) {
binary.BigEndian.PutUint32(ret, v)
}

// UInt32ToBytes uint32 value to bytes array using big order
func UInt32ToBytes(v uint32) []byte {
ret := make([]byte, 4)
Uint32ToBytesTo(v, ret)
return ret
}

// Uint16ToBytesTo uint16 value to bytes array using big order
func Uint16ToBytesTo(v uint16, ret []byte) {
binary.BigEndian.PutUint16(ret, v)
}

// UInt16ToBytes uint16 value to bytes array using big order
func UInt16ToBytes(v uint16) []byte {
ret := make([]byte, 2)
Uint16ToBytesTo(v, ret)
return ret
}

// ByteBuf a buf with byte arrays
//
// | discardable bytes | readable bytes | writeable bytes |
Expand Down Expand Up @@ -307,9 +336,19 @@ func (b *ByteBuf) ReadInt() (int, error) {
return Byte2Int(b.buf[b.readerIndex-4 : b.readerIndex]), nil
}

// ReadUInt16 get uint16 value from buf
func (b *ByteBuf) ReadUInt16() (uint16, error) {
if b.Readable() < 2 {
return 0, io.ErrShortBuffer
}

b.readerIndex += 2
return Byte2UInt16(b.buf[b.readerIndex-2 : b.readerIndex]), nil
}

// ReadUInt32 get uint32 value from buf
func (b *ByteBuf) ReadUInt32() (uint32, error) {
if b.Readable() < 8 {
if b.Readable() < 4 {
return 0, io.ErrShortBuffer
}

Expand Down Expand Up @@ -417,6 +456,33 @@ func (b *ByteBuf) WriteInt(v int) (n int, err error) {
return 4, nil
}

// WriteUInt16 write uint16 value to buf using big order
// return write bytes count, error
func (b *ByteBuf) WriteUInt16(v uint16) (n int, err error) {
b.Expansion(2)
Uint16ToBytesTo(v, b.buf[b.writerIndex:b.writerIndex+2])
b.writerIndex += 2
return 2, nil
}

// WriteUInt32 write uint32 value to buf using big order
// return write bytes count, error
func (b *ByteBuf) WriteUInt32(v uint32) (n int, err error) {
b.Expansion(4)
Uint32ToBytesTo(v, b.buf[b.writerIndex:b.writerIndex+4])
b.writerIndex += 4
return 4, nil
}

// WriteUInt64 write uint64 value to buf using big order
// return write bytes count, error
func (b *ByteBuf) WriteUInt64(v uint64) (n int, err error) {
b.Expansion(8)
Uint64ToBytesTo(v, b.buf[b.writerIndex:b.writerIndex+8])
b.writerIndex += 8
return 8, nil
}

// WriteInt64 write int64 value to buf using big order
// return write bytes count, error
func (b *ByteBuf) WriteInt64(v int64) (n int, err error) {
Expand Down
1 change: 1 addition & 0 deletions conn_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func (pool *AddressBasedPool) createConn(addr string) IOSession {
return conn
}

// ForEach do foreach session
func (pool *AddressBasedPool) ForEach(visitor func(addr string, conn IOSession)) {
pool.Lock()
for addr, conn := range pool.conns {
Expand Down
20 changes: 10 additions & 10 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func check(t *testing.T, condition bool, tp string, f func(), args ...interface{
buf.WriteByte('\n')
}
}
log(2, fmt.Sprintf(buf.String(), args...))
doLog(2, fmt.Sprintf(buf.String(), args...))
f()
return false
}
Expand All @@ -76,16 +76,16 @@ func isNil(t *testing.T, v interface{}, f func()) bool {
}
switch vv := v.(type) {
case toString:
log(2, fmt.Sprintf(`not nil
doLog(2, fmt.Sprintf(`not nil
v = %#v`, vv.String()))
case error:
log(2, fmt.Sprintf(`not nil
doLog(2, fmt.Sprintf(`not nil
v = %#v`, vv.Error()))
case []byte:
log(2, fmt.Sprintf(`not nil
doLog(2, fmt.Sprintf(`not nil
v = %#v`, vv))
default:
log(2, fmt.Sprintf(`not nil
doLog(2, fmt.Sprintf(`not nil
v = %v`, vv))
}
f()
Expand All @@ -104,7 +104,7 @@ func notNil(t *testing.T, v interface{}, f func()) bool {
if v != nil {
return true
}
log(2, fmt.Sprintf(`is nil`))
doLog(2, fmt.Sprintf(`is nil`))
f()
return false
}
Expand All @@ -121,7 +121,7 @@ func deepEqual(t *testing.T, a, b interface{}, f func()) bool {
if reflect.DeepEqual(a, b) {
return true
}
log(2, fmt.Sprintf(`not deep equal
doLog(2, fmt.Sprintf(`not deep equal
a = %#v
b = %#v`, a, b))
f()
Expand Down Expand Up @@ -204,11 +204,11 @@ func equal(t *testing.T, a, b interface{}, f func()) bool {
return true
}
if printable {
log(2, fmt.Sprintf(`not equal
doLog(2, fmt.Sprintf(`not equal
a = '%c' = %#v
b = '%c' = %#v`, a, a, b, b))
} else {
log(2, fmt.Sprintf(`not equal
doLog(2, fmt.Sprintf(`not equal
a = %#v
b = %#v`, a, b))
}
Expand Down Expand Up @@ -361,7 +361,7 @@ func unsafeEqual(ia, ib interface{}, size int) bool {
)
}

func log(depth int, val string) {
func doLog(depth int, val string) {
if _, file, line, ok := runtime.Caller(1 + depth); ok {
// Truncate file name at last file name separator.
if index := strings.LastIndex(file, "/"); index >= 0 {
Expand Down

0 comments on commit 583c981

Please sign in to comment.