本文整理汇总了Golang中hash/crc32.Checksum函数的典型用法代码示例。如果您正苦于以下问题:Golang Checksum函数的具体用法?Golang Checksum怎么用?Golang Checksum使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Checksum函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: readBlock
// readBlock reads a full or partial block into p from rs. len(p) must be smaller than (bs - crc32Len).
// The checksum of the block is calculated and verified.
// ErrBadCRC is returned if the checksum is invalid.
func readBlock(rs io.ReadSeeker, p []byte, index, bs int64) (int64, error) {
payloadLen := int(bs - crc32Len)
if len(p) > payloadLen {
return 0, ErrPayloadSizeTooLarge
}
b := make([]byte, crc32Len)
_, err := rs.Seek(index*bs, os.SEEK_SET)
if err != nil {
return 0, err
}
n, err := rs.Read(b)
// Cannot read full crc
if n > 0 && n < 4 {
return 0, ErrBadCRC
}
if err != nil {
return 0, err
}
crc := binary.BigEndian.Uint32(b)
if len(p) == payloadLen {
// fast path for reading into a buffer of payload size
n, err = rs.Read(p)
if err != nil {
return int64(n), err
}
// Invalid crc
if crc != crc32.Checksum(p[:n], crc32cTable) {
return 0, ErrBadCRC
}
return int64(n), nil
} else {
// p is smaller than payload size
// read into another buffer (so we can compare full block CRC) and copy into p
buf := make([]byte, payloadLen)
n, err = rs.Read(buf)
// If there is an eof returned, two cases may happen
// 1. n < len(p) -- we don't have enough data to
// fill into p, we should return the eof
// 2. n >= len(p) -- we have enough data to fill
// into p, shoudln't return the eof
if err == io.EOF && n >= len(p) {
err = nil
}
if err != nil {
return int64(copy(p, buf[:n])), err
}
if crc != crc32.Checksum(buf[:n], crc32cTable) {
return 0, ErrBadCRC
}
return int64(copy(p, buf[:n])), nil
}
}
开发者ID:missmayo,项目名称:cfs,代码行数:58,代码来源:block.go
示例2: PickPeer
func (p fakePeers) PickPeer(key string) (peer ProtoGetter, ok bool) {
if len(p) == 0 {
return
}
n := crc32.Checksum([]byte(key), crc32.IEEETable) % uint32(len(p))
return p[n], p[n] != nil
}
开发者ID:hefju,项目名称:groupcache,代码行数:7,代码来源:groupcache_test.go
示例3: RecvPackage
//一个packet包括包头和包体,保证在接收到包头后两秒钟内接收到包体,否则线程会一直阻塞
//因此,引入了超时机制
func RecvPackage(conn net.Conn) (packet *GetPacket, e error, isClose bool) {
var data []byte
header := make([]byte, 12)
if conn == nil {
// e = errors.New("")
fmt.Println("连接已经关闭")
isClose = true
return
}
n, err := io.ReadFull(conn, header)
if n == 0 && err == io.EOF {
// fmt.Println("客户端断开连接")
isClose = true
e = err
return
} else if err != nil {
// fmt.Println("接收数据出错:", err)
isClose = true
e = err
return
}
//数据包长度
size := binary.BigEndian.Uint32(header)
//crc值
crc1 := binary.BigEndian.Uint32(header[4:8])
msgID := binary.BigEndian.Uint32(header[8:12])
data = make([]byte, size)
timeout := NewTimeOut(func() {
n, err = io.ReadFull(conn, data)
if uint32(n) != size {
log.Println("数据包长度不正确", n, "!=", size)
e = errors.New(fmt.Sprint("数据包长度不正确:%d!=%d", n, size))
return
}
if err != nil {
log.Println("读取数据出错:", err)
e = err
return
}
crc2 := crc32.Checksum(data, crc32.IEEETable)
if crc1 != crc2 {
log.Println("crc 数据验证不正确: ", crc1, " != ", crc2)
e = errors.New(fmt.Sprint("crc 数据验证不正确:%d!=%d", crc1, crc2))
return
}
packet = new(GetPacket)
packet.Date = data
packet.MsgID = int32(msgID)
packet.Size = uint32(len(data))
})
isTimeOut := timeout.Do(time.Second * 5)
if isTimeOut {
e = errors.New("数据包头和数据包体不完整")
return
}
return
}
开发者ID:cokeboL,项目名称:mandela,代码行数:62,代码来源:package.go
示例4: writeBlock
// writeBlock writes a full or partial block into ws. len(p) must be smaller than (bs - crc32Len).
// Each block contains a crc32Len bytes crc32c of the payload and a (bs-crc32Len)
// bytes payload. Any error encountered is returned.
// It is caller's responsibility to ensure that only the last block in the file has
// len(p) < bs - crc32Len
func writeBlock(ws io.WriteSeeker, index, bs int64, p []byte) error {
if int64(len(p)) > bs-crc32Len {
return ErrPayloadSizeTooLarge
}
// seek to the beginning of the block
_, err := ws.Seek(index*bs, os.SEEK_SET)
if err != nil {
return err
}
// write crc32c
// TODO: reuse buffer
b := make([]byte, crc32Len)
binary.BigEndian.PutUint32(b, crc32.Checksum(p, crc32cTable))
_, err = ws.Write(b)
if err != nil {
return err
}
// write payload
_, err = ws.Write(p)
if err != nil {
return err
}
return nil
}
开发者ID:missmayo,项目名称:cfs,代码行数:32,代码来源:block.go
示例5: get
func get(key int64, circle map[uint32]string) string {
keyStr := strconv.Itoa(int(key))
if len(circle) == 0 {
return ""
} else {
hash := crc32.Checksum([]byte(keyStr), crc32q)
var keys []int
for k := range circle {
keys = append(keys, int(k))
}
sort.Ints(keys)
var serverHash uint32
foundHash := false
for i := 0; i < len(keys); i++ {
if keys[i] >= int(hash) {
serverHash = uint32(keys[i])
foundHash = true
break
}
}
if foundHash == true {
return circle[serverHash]
} else {
return circle[uint32(keys[0])]
}
}
}
开发者ID:PrasannaGajbhiye,项目名称:Consistent-Hashing,代码行数:28,代码来源:client.go
最后编辑: kuteng 文档更新时间: 2021-08-23 19:14 作者:kuteng