zoukankan      html  css  js  c++  java
  • procotol.go 源码阅读

    import (
        "bytes"
        "encoding/binary"
    )

    const (
        // 支持数据最大长度为 2 << 61
        // DataLengthOfLenth = 8
        // 支持数据最大长度为 2 << 30
        DataLengthOfLenth = 4
    )

    //通讯协议处理,主要处理封包和解包的过程
    type Protocol struct {
        // 包头
        header string
        // 包头长度
        headerLen int
    }

    func NewProtocol(packetHeader string) *Protocol {
        return &Protocol{
            header:    packetHeader,
            headerLen: len([]byte(packetHeader)),
        }
    }

    func (self *Protocol) ReSet(header string) {
        self.header = header
        self.headerLen = len([]byte(header))
    }

    //封包
    func (self *Protocol) Packet(message []byte) []byte {
        return append(append([]byte(self.header), IntToBytes(len(message))...), message...)
    }

    //解包
    func (self *Protocol) Unpack(buffer []byte) (readerSlice [][]byte, bufferOver []byte) {
        length := len(buffer)

        var i int
        for i = 0; i < length; i = i + 1 {
            if length < i+self.headerLen+DataLengthOfLenth {
                break
            }
            if string(buffer[i:i+self.headerLen]) == self.header {
                messageLength := BytesToInt(buffer[i+self.headerLen : i+self.headerLen+DataLengthOfLenth])
                if length < i+self.headerLen+DataLengthOfLenth+messageLength {
                    break
                }
                data := buffer[i+self.headerLen+DataLengthOfLenth : i+self.headerLen+DataLengthOfLenth+messageLength]

                readerSlice = append(readerSlice, data)

                i += self.headerLen + DataLengthOfLenth + messageLength - 1
            }
        }

        if i == length {
            bufferOver = make([]byte, 0)
            return
        }
        bufferOver = buffer[i:]
        return
    }

    //整形转换成字节
    // func IntToBytes(n int) []byte {
    //     x := int64(n)

    //     bytesBuffer := bytes.NewBuffer([]byte{})
    //     binary.Write(bytesBuffer, binary.BigEndian, x)
    //     return bytesBuffer.Bytes()
    // }

    // //字节转换成整形
    // func BytesToInt(b []byte) int {
    //     bytesBuffer := bytes.NewBuffer(b)

    //     var x int64
    //     binary.Read(bytesBuffer, binary.BigEndian, &x)

    //     return int(x)
    // }

    //整形转换成字节
    func IntToBytes(n int) []byte {
        x := int32(n)

        bytesBuffer := bytes.NewBuffer([]byte{})
        binary.Write(bytesBuffer, binary.LittleEndian, x)
        return bytesBuffer.Bytes()
    }

    //字节转换成整形
    func BytesToInt(b []byte) int {
        bytesBuffer := bytes.NewBuffer(b)

        var x int32
        binary.Read(bytesBuffer, binary.LittleEndian, &x)

        return int(x)
    }

  • 相关阅读:
    突然想写一篇有关欧拉函数的博客
    洛谷P1199 三国游戏——题解
    洛谷P1310 表达式的值——题解
    洛谷P1309 瑞士轮——题解
    洛谷P1077 摆花——题解
    size_t是什么?
    c++ bitset——一个有趣的类型
    有关文件操作的总结
    一本通&&洛谷 ——靶型数独——题解
    一本通【例题4】Addition Chains——题解
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7462115.html
Copyright © 2011-2022 走看看