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)
    }

  • 相关阅读:
    httprunner 3.x--参数化parameters(参数和值多对一,多对多,一对一等),csv文件
    httprunner 3.x--variables变量和声明(变量和值一一对应)
    httprunner 3.x 带参数接口测试(get方法)
    appium安装(windows7)
    httprunner 3运行报错AttributeError: module 'allure' has no attribute 'severity_level'
    jmespath用法
    httprunner3.x 测试用例应用/变量传递(测试用例编写)
    Python框架之Tornado(一)
    Python之Web框架
    Python 装饰器(进阶篇)
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7462115.html
Copyright © 2011-2022 走看看