func orderToStr(order OrderMessage) string { str := string(order.Type) + "0" + strconv.Itoa(int(order.SenderID)) + strconv.Itoa(int(order.Floor)) + strconv.Itoa(int(order.Direction)) crc := crc16.Crc16([]byte(str)) // HAXHAX bitshift and convert to byte slice -> string str += string([]byte{byte((crc >> 8) & 0xff), byte(crc & 0xff)}) return str }
func strToOrder(str string) OrderMessage { // Check for CRC mismatch. Not tamper-proof, but should be corruption-proof. calculatedCrc := crc16.Crc16([]byte(str[:6])) receivedCrc := (uint16(str[6]) << 8) + uint16(str[7]) if calculatedCrc != receivedCrc { log.Error("CRC mismatch in " + str[:4] + " message!") return OrderMessage{Type: InvalidOrder} // Probably corrupted } senderID, _ := strconv.Atoi(string(str[3])) floorNum, _ := strconv.Atoi(string(str[4])) dirNum, _ := strconv.Atoi(string(str[5])) return OrderMessage{Type: OrderType(str[:2]), SenderID: uint(senderID), Floor: driver.Floor(floorNum), Direction: driver.Direction(dirNum)} }
// redis cluster uses CRC16 + modulo to determine a key's slot func keySlot(key string) uint16 { cs := crc16.Crc16([]byte(key)) return cs % redisSlotMax }