本文整理匯總了Golang中code/google/com/p/goprotobuf/proto.NewBuffer函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewBuffer函數的具體用法?Golang NewBuffer怎麽用?Golang NewBuffer使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewBuffer函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: newLog
// Creates a new log.
func newLog() *Log {
return &Log{
entries: make([]*LogEntry, 0),
pBuffer: proto.NewBuffer(nil),
pLogEntry: &protobuf.ProtoLogEntry{},
}
}
示例2: struct_pack
func struct_pack(L *lua.LState) int {
format := byte(L.CheckInt(2))
l_value := L.CheckNumber(3)
b := proto.NewBuffer(nil)
b.Reset()
L.SetTop(1)
switch format {
case 'i':
b.EncodeFixed32(uint64(int32(l_value)))
L.Push(lua.LString(string(b.Bytes())))
case 'q':
b.EncodeFixed64(uint64(int64(l_value)))
L.Push(lua.LString(string(b.Bytes())))
case 'f':
b.EncodeFixed32(uint64(float32(l_value)))
L.Push(lua.LString(string(b.Bytes())))
case 'd':
b.EncodeFixed64(uint64(float64(l_value)))
L.Push(lua.LString(string(b.Bytes())))
case 'I':
b.EncodeFixed32(uint64(uint32(l_value)))
L.Push(lua.LString(string(b.Bytes())))
case 'Q':
b.EncodeFixed64(uint64(l_value))
L.Push(lua.LString(string(b.Bytes())))
default:
L.RaiseError("Unknown, format")
}
L.Call(1, 0)
return 0
}
示例3: receiveMsg
func receiveMsg(conn net.Conn) {
buf := make([]byte, 128)
n, err := conn.Read(buf)
if checkerr(err) {
return
}
fmt.Printf("\nread (%d) byte from %v :\n%v\n", n, conn.RemoteAddr(), buf[:n])
protobuf := proto.NewBuffer(buf[:n])
var msg OSMsg.OSMsg
pumerr := protobuf.Unmarshal(&msg)
if checkerr(pumerr) {
return
}
fmt.Printf("[Message]----->%v\n\n", msg.String())
//
// umerr := proto.Unmarshal(buf[:n], &msg)
// if checkerr(umerr) {
// return
// }
// fmt.Printf("[MyMessage] %v\n", msg)
// readStr := string(buf[:n])
// fmt.Printf("read string (%d):\n%s\n", n, readStr)
// umterr := proto.UnmarshalText(readStr, &msg)
// if checkerr(umterr) {
// return
// }
// fmt.Printf("[MyMessage] %v\n", msg)
}
示例4: NewServerCodec
func NewServerCodec(conn zmq.Socket) rpc.ServerCodec {
req := proto.NewBuffer(nil)
packetIds := make(idMap)
clientIds := make(map[uint64]uint64)
return &serverCodec{conn, req, packetIds, clientIds}
}
示例5: Get
func (l *bufferList) Get() (*proto.Buffer, bool) {
if v, ok := l.l.Get(); ok {
return v.(*proto.Buffer), ok
}
return proto.NewBuffer(make([]byte, 0, 4096)), false
}
示例6: BenchmarkUnmarshal
func BenchmarkUnmarshal(b *testing.B) {
b.StopTimer()
// BenchmarkUnmarshal is called multiple times.
once.Do(func() {
go http.ListenAndServe("localhost:9090", nil)
})
raw := prepareBuf(b).Bytes()
buf := proto.NewBuffer(make([]byte, 0, 4096))
for i := 0; i < b.N; i++ {
buf.SetBuf(raw)
b.StartTimer()
v := &SampleValueSeries{Value: make([]*SampleValueSeries_Value, 0, numSamples)}
if err := buf.Unmarshal(v); err != nil {
b.Fatal(err)
}
b.StopTimer()
if len(v.Value) != numSamples {
b.Fatal(len(v.Value))
}
}
}
示例7: BuildJsonFromProto
func BuildJsonFromProto(cmdname string, cmddata []byte) string {
recvbuf := proto.NewBuffer(cmddata)
recv := &AccountTokenVerifyLoginUserPmd_CS{} //難點,這裏這個結構是不確定的,隻能動態描述
recvbuf.Unmarshal(recv)
recv_json, _ := json.Marshal(recv)
return string(recv_json)
}
示例8: struct_unpack
func struct_unpack(L *lua.LState) int {
format := byte(L.CheckInt(1))
l_value := L.CheckString(2)
pos := L.CheckInt(3)
b_value := []byte(l_value)
b := proto.NewBuffer(b_value[pos:])
switch format {
case 'i':
value, _ := b.DecodeFixed32()
L.Push(lua.LNumber(int32(value)))
case 'q':
value, _ := b.DecodeFixed64()
L.Push(lua.LNumber(int64(value)))
case 'f':
value, _ := b.DecodeFixed32()
L.Push(lua.LNumber(float32(value)))
case 'd':
value, _ := b.DecodeFixed64()
L.Push(lua.LNumber(float64(value)))
case 'I':
value, _ := b.DecodeFixed32()
L.Push(lua.LNumber(uint32(value)))
case 'Q':
value, _ := b.DecodeFixed64()
L.Push(lua.LNumber(uint64(value)))
default:
L.RaiseError("Unknown, format")
}
return 1
}
示例9: NewBuffer
func NewBuffer(rw io.ReadWriter) *Buffer {
return &Buffer{
rw: rw,
protoBuffer: proto.NewBuffer(nil),
index: 0,
}
}
示例10: BuildProtoFromJson
func BuildProtoFromJson(typ reflect.Type, cmdjson string) []byte {
proto_cmd := reflect.New(typ).Interface().(proto.Message)
rawdata := []byte(cmdjson)
json.Unmarshal(rawdata, proto_cmd)
sendbuf := proto.NewBuffer(nil)
sendbuf.Marshal(proto_cmd)
return sendbuf.Bytes()
}
示例11: Write
func (self *LevelDbShard) Write(database string, series []*protocol.Series) error {
wb := levigo.NewWriteBatch()
defer wb.Close()
for _, s := range series {
if len(s.Points) == 0 {
return errors.New("Unable to write no data. Series was nil or had no points.")
}
count := 0
for fieldIndex, field := range s.Fields {
temp := field
id, err := self.createIdForDbSeriesColumn(&database, s.Name, &temp)
if err != nil {
return err
}
keyBuffer := bytes.NewBuffer(make([]byte, 0, 24))
dataBuffer := proto.NewBuffer(nil)
for _, point := range s.Points {
keyBuffer.Reset()
dataBuffer.Reset()
keyBuffer.Write(id)
timestamp := self.convertTimestampToUint(point.GetTimestampInMicroseconds())
// pass the uint64 by reference so binary.Write() doesn't create a new buffer
// see the source code for intDataSize() in binary.go
binary.Write(keyBuffer, binary.BigEndian, ×tamp)
binary.Write(keyBuffer, binary.BigEndian, point.SequenceNumber)
pointKey := keyBuffer.Bytes()
if point.Values[fieldIndex].GetIsNull() {
wb.Delete(pointKey)
goto check
}
err = dataBuffer.Marshal(point.Values[fieldIndex])
if err != nil {
return err
}
wb.Put(pointKey, dataBuffer.Bytes())
check:
count++
if count >= self.writeBatchSize {
err = self.db.Write(self.writeOptions, wb)
if err != nil {
return err
}
count = 0
wb.Clear()
}
}
}
}
return self.db.Write(self.writeOptions, wb)
}
示例12: Write
func (self *Shard) Write(database string, series []*protocol.Series) error {
wb := make([]storage.Write, 0)
for _, s := range series {
if len(s.Points) == 0 {
return errors.New("Unable to write no data. Series was nil or had no points.")
}
if len(s.FieldIds) == 0 {
return errors.New("Unable to write points without fields")
}
count := 0
for fieldIndex, id := range s.FieldIds {
for _, point := range s.Points {
// keyBuffer and dataBuffer have to be recreated since we are
// batching the writes, otherwise new writes will override the
// old writes that are still in memory
keyBuffer := bytes.NewBuffer(make([]byte, 0, 24))
dataBuffer := proto.NewBuffer(nil)
var err error
binary.Write(keyBuffer, binary.BigEndian, &id)
timestamp := self.convertTimestampToUint(point.GetTimestampInMicroseconds())
// pass the uint64 by reference so binary.Write() doesn't create a new buffer
// see the source code for intDataSize() in binary.go
binary.Write(keyBuffer, binary.BigEndian, ×tamp)
binary.Write(keyBuffer, binary.BigEndian, point.SequenceNumber)
pointKey := keyBuffer.Bytes()
if point.Values[fieldIndex].GetIsNull() {
wb = append(wb, storage.Write{Key: pointKey, Value: nil})
goto check
}
err = dataBuffer.Marshal(point.Values[fieldIndex])
if err != nil {
return err
}
wb = append(wb, storage.Write{Key: pointKey, Value: dataBuffer.Bytes()})
check:
count++
if count >= self.writeBatchSize {
err = self.db.BatchPut(wb)
if err != nil {
return err
}
count = 0
wb = make([]storage.Write, 0, self.writeBatchSize)
}
}
}
}
return self.db.BatchPut(wb)
}
示例13: zig_zag_encode64
func zig_zag_encode64(L *lua.LState) int {
b := proto.NewBuffer(nil)
b.Reset()
err := b.EncodeZigzag64(uint64(L.CheckNumber(1)))
if err == nil {
x, _ := binary.ReadUvarint(bytes.NewBuffer(b.Bytes()))
L.Push(lua.LNumber(x))
return 1
}
return 0
}
示例14: Write
func (self *Shard) Write(database string, series []*protocol.Series) error {
self.closeLock.RLock()
defer self.closeLock.RUnlock()
if self.closed {
return fmt.Errorf("Shard is closed")
}
wb := make([]storage.Write, 0)
for _, s := range series {
if len(s.Points) == 0 {
return errors.New("Unable to write no data. Series was nil or had no points.")
}
if len(s.FieldIds) == 0 {
return errors.New("Unable to write points without fields")
}
count := 0
for fieldIndex, id := range s.FieldIds {
for _, point := range s.Points {
// keyBuffer and dataBuffer have to be recreated since we are
// batching the writes, otherwise new writes will override the
// old writes that are still in memory
dataBuffer := proto.NewBuffer(nil)
var err error
sk := newStorageKey(id, point.GetTimestamp(), point.GetSequenceNumber())
if point.Values[fieldIndex].GetIsNull() {
wb = append(wb, storage.Write{Key: sk.bytes(), Value: nil})
goto check
}
err = dataBuffer.Marshal(point.Values[fieldIndex])
if err != nil {
return err
}
wb = append(wb, storage.Write{Key: sk.bytes(), Value: dataBuffer.Bytes()})
check:
count++
if count >= self.writeBatchSize {
err = self.db.BatchPut(wb)
if err != nil {
return err
}
count = 0
wb = make([]storage.Write, 0, self.writeBatchSize)
}
}
}
}
return self.db.BatchPut(wb)
}
示例15: zig_zag_decode64
func zig_zag_decode64(L *lua.LState) int {
n := L.CheckNumber(1)
b_buf := bytes.NewBuffer([]byte{})
binary.Write(b_buf, binary.LittleEndian, n)
b := proto.NewBuffer(b_buf.Bytes())
value, err := b.DecodeZigzag64()
if err == nil {
L.Push(lua.LNumber(value))
return 1
}
return 0
}