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

  • 相关阅读:
    golang官网可以打开了 go语言
    TinyMCE添加图片 路径自动处理成相对路径
    谷歌浏览器下载地址 chrome最新版本 百度云地址
    对过度自信的矫正
    一个公司需要解决的7个问题
    git mv 命令 移动或重命名
    工信部备案查询验证码输入错误的原因
    Linux下安装jmeter
    jmeter-请求参数化
    更改jmeter发送邮件样式(转)
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7462115.html
Copyright © 2011-2022 走看看