Skip to content

Instantly share code, notes, and snippets.

@kai5263499
Last active March 13, 2018 23:38
Show Gist options
  • Save kai5263499/fb6fce5665cdd53922c4050759796c68 to your computer and use it in GitHub Desktop.
Save kai5263499/fb6fce5665cdd53922c4050759796c68 to your computer and use it in GitHub Desktop.
Packing two events into one binary blob and parsing them back out again
#include <stdio.h>
#include <stdlib.h>
#include "test.pb-c.h"
int main(int argc, const char * argv[]) {
Myproto *msg;
// the bytes from the combined tracer and msg proto generated from go
char msg_bytes[] = { 0xA8, 0x38, 0x01, 0xB2, 0x38, 0x0D, 0x6D, 0x79, 0x20, 0x74, 0x72, 0x61, 0x63, 0x65, 0x20, 0x64, 0x61, 0x74, 0x61, 0xBA, 0x38, 0x0F, 0x6D, 0x6F, 0x72, 0x65, 0x20, 0x74, 0x72, 0x61, 0x63, 0x65, 0x20, 0x64, 0x61, 0x74, 0x61, 0x08, 0x02, 0x12, 0x07, 0x6d, 0x79, 0x20, 0x74, 0x65, 0x73, 0x74 };
size_t msg_len = sizeof(msg_bytes);
int n=msg_len, i =0;
printf(" [ ");
while (i < n) {
printf(" %02X ",(unsigned)msg_bytes[i]);
i++;
}
printf(" ]\n ");
msg = myproto__unpack(NULL, msg_len, (const uint8_t *)msg_bytes);
if (msg == NULL) {
printf("error unpacking msg\n");
exit(1);
}
printf("unpacked msg nonce=%d data=%s\n", msg->nonce, msg->data);
myproto__free_unpacked(msg, NULL);
Mytracer *trace;
trace = mytracer__unpack(NULL, msg_len, (const uint8_t *)msg_bytes);
if (trace == NULL) {
printf("error unpacking trace\n");
exit(1);
}
printf("unpacked trace msg nonce=%d tracedata=%s anothermsg=%s\n", trace->nonce, trace->tracedata, trace->anothermsg);
mytracer__free_unpacked(trace, NULL);
return 0;
}
package main
import (
"bytes"
"log"
"github.com/davecgh/go-spew/spew"
"github.com/golang/protobuf/proto"
)
func main() {
traceMsg := &Mytracer{
Nonce: 1,
Tracedata: "my trace data",
AnotherMsg: "more trace data",
}
spew.Dump(traceMsg)
traceData, err := proto.Marshal(traceMsg)
if err != nil {
log.Fatal("marshaling error: ", err)
}
spew.Dump(traceData)
msg := &Myproto{
Nonce: 2,
Data: "my test",
}
spew.Dump(msg)
data, err := proto.Marshal(msg)
if err != nil {
log.Fatal("marshaling error: ", err)
}
var b bytes.Buffer
b.Write(traceData)
b.Write(data)
newData := b.Bytes()
spew.Dump(newData)
newMsg := &Myproto{}
err = proto.Unmarshal(newData, newMsg)
if err != nil {
log.Fatal("unmarshaling error: ", err)
}
spew.Dump(newMsg)
newTraceMsg := &Mytracer{}
err = proto.Unmarshal(newData, newTraceMsg)
if err != nil {
log.Fatal("unmarshaling error: ", err)
}
spew.Dump(newTraceMsg)
}
# from golang
(*main.Mytracer)(0xc420020100)(nonce:1 tracedata:"my trace data" anotherMsg:"more trace data" )
([]uint8) (len=37 cap=64) {
00000000 a8 38 01 b2 38 0d 6d 79 20 74 72 61 63 65 20 64 |.8..8.my trace d|
00000010 61 74 61 ba 38 0f 6d 6f 72 65 20 74 72 61 63 65 |ata.8.more trace|
00000020 20 64 61 74 61 | data|
}
(*main.Myproto)(0xc42000a220)(nonce:2 data:"my test" )
([]uint8) (len=48 cap=64) {
00000000 a8 38 01 b2 38 0d 6d 79 20 74 72 61 63 65 20 64 |.8..8.my trace d|
00000010 61 74 61 ba 38 0f 6d 6f 72 65 20 74 72 61 63 65 |ata.8.more trace|
00000020 20 64 61 74 61 08 02 12 07 6d 79 20 74 65 73 74 | data....my test|
}
(*main.Myproto)(0xc42000a2c0)(nonce:2 data:"my test" )
(*main.Mytracer)(0xc420020300)(nonce:1 tracedata:"my trace data" anotherMsg:"more trace data" )
# and from c
[ FFFFFFA8 38 01 FFFFFFB2 38 0D 6D 79 20 74 72 61 63 65 20 64 61 74 61 FFFFFFBA 38 0F 6D 6F 72 65 20 74 72 61 63 65 20 64 61 74 61 08 02 12 07 6D 79 20 74 65 73 74 ]
unpacked msg nonce=2 data=my test
unpacked msg nonce=1 tracedata=my trace data anothermsg=more trace data
syntax = "proto3";
package main;
message myproto {
int64 nonce = 1;
string data = 2;
}
message mytracer {
int64 nonce = 901;
string tracedata = 902;
string anotherMsg = 903;
bytes bagOfBytes = 904;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment