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

  • 相关阅读:
    centos7 安装 nginx
    centos7 安装 mysql
    centos7 安装 python3.7
    nginx添加到系统命令中
    Java多线程6-线程让步
    Java多线程5-线程等待与唤醒
    Java多线程4-synchronized关键字
    Java多线程3-Thread中start和run方法的区别
    Java多线程-2-常用的实现多线程的两种方式
    java多线程1-基础概念
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7462115.html
Copyright © 2011-2022 走看看