一、[]byte与struct互转
type CDNDispatchReq struct{
flag bool
Fromsegid int32
Tosegid int32
};
func handle_DISPATCH(data []byte, datalen int, ip net.IP, port int) int {
req := *(**CDNDispatchReq)(unsafe.Pointer(&data))
flag := req.flag
}
二、求struct的sizeof
unsafe.Sizeof( *req)
size := unsafe.Sizeof( *req)
三、实现 ntohs
uint16 port = 0x8000 //网络字节序
portData := *(*[]byte)(unsafe.Pointer(&port)) //
pt := binary.BigEndian.Uint16(portData)
四、几个字节序转换函数
func Htons(i uint16) uint16 {
return (i<<8)&0xff00 | i>>8
}
func Ntohl(i uint32) uint32 {
return (i<<24)&0xff000000 | (i>>24) | (i<<8)&0x00ff0000 | (i>>8)&0x0000ff00
}
func Ntohs(i uint16) uint16 {
return (i<<8)&0xff00 | i>>8