10 Star 36 Fork 12

Gitee 极速下载 / go-sniffer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/40t/go-sniffer
克隆/下载
util.go 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
Wonder 提交于 2018-09-30 01:10 . add dependency file
package build
import (
"encoding/binary"
"encoding/json"
"fmt"
"time"
"io"
"github.com/40t/go-sniffer/plugSrc/mongodb/build/bson"
)
func GetNowStr(isClient bool) string {
var msg string
layout := "01/02 15:04:05.000000"
msg += time.Now().Format(layout)
if isClient {
msg += "| cli -> ser |"
}else{
msg += "| ser -> cli |"
}
return msg
}
func ReadInt32(r io.Reader) (n int32) {
binary.Read(r, binary.LittleEndian, &n)
return
}
func ReadInt64(r io.Reader) int64 {
var n int64
binary.Read(r, binary.LittleEndian, &n)
return n
}
func ReadString(r io.Reader) string {
var result []byte
var b = make([]byte, 1)
for {
_, err := r.Read(b)
if err != nil {
panic(err)
}
if b[0] == '\x00' {
break
}
result = append(result, b[0])
}
return string(result)
}
func ReadBson2Json(r io.Reader) (string) {
//read len
docLen := ReadInt32(r)
if docLen == 0 {
return ""
}
//document []byte
docBytes := make([]byte, int(docLen))
binary.LittleEndian.PutUint32(docBytes, uint32(docLen))
if _, err := io.ReadFull(r, docBytes[4:]); err != nil {
panic(err)
}
//resolve document
var bsn bson.M
err := bson.Unmarshal(docBytes, &bsn)
if err != nil {
panic(err)
}
//format to Json
jsonStr, err := json.Marshal(bsn)
if err != nil {
return fmt.Sprintf("{\"error\":%s}", err.Error())
}
return string(jsonStr)
}
1
https://gitee.com/mirrors/go-sniffer.git
git@gitee.com:mirrors/go-sniffer.git
mirrors
go-sniffer
go-sniffer
add66245ba50

搜索帮助