From: AceVest Date: Sun, 4 Jun 2017 11:43:03 +0000 (+0800) Subject: .... X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/rndc.conf.html?a=commitdiff_plain;h=a1edd241de3d0f21d6aa243ba76ab4bfda0afed4;p=acecode.git .... --- diff --git a/learn/go/protobuf/client.go b/learn/go/protobuf/client.go index 81930d2..68c4e93 100644 --- a/learn/go/protobuf/client.go +++ b/learn/go/protobuf/client.go @@ -11,18 +11,24 @@ import ( "fmt" "log" "net" - "io" - + //"github.com/acecode/learn/go/protobuf/sample" + "./sample" "github.com/golang/protobuf/proto" ) func main() { defer fmt.Println("Program Exited...") - sampleData := sample.SampleData { - Title : "test golang protobuf", - Type : SAMPLE_TYPE_TEST, - Data : [1.414, 2.732, 3.14, 6.18], + dataType := sample.SampleType_SAMPLE_TYPE_TEST + title := "test golang protobuf" + sampleData := &sample.SampleData { + Title : &title, + Type : &dataType, + Data : []float64{1.414, 2.732, 3.14, 6.18}, + Source: &sample.SampleData_Source { + Band: proto.String("Ace"), + Version: proto.String("1.2"), + }, } data, err := proto.Marshal(sampleData) @@ -37,7 +43,8 @@ func main() { defer conn.Close() - if _, err := io.Copy(conn, data) { + fmt.Println("Data Len: ", len(data)) + if _, err := conn.Write(data); err != nil { log.Fatal(err) } } diff --git a/learn/go/protobuf/sample.pb.go b/learn/go/protobuf/sample.pb.go deleted file mode 100644 index 6d868f2..0000000 --- a/learn/go/protobuf/sample.pb.go +++ /dev/null @@ -1,128 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// source: sample.proto - -/* -Package sample is a generated protocol buffer package. - -It is generated from these files: - sample.proto - -It has these top-level messages: - SampleData -*/ -package sample - -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package - -type SampleType int32 - -const ( - SampleType_SAMPLE_TYPE_RAW SampleType = 1 - SampleType_SAMPLE_TYPE_LIGHT SampleType = 2 - SampleType_SAMPLE_TYPE_TEMPERATURE SampleType = 3 - SampleType_SAMPLE_TYPE_HUMIDITY SampleType = 4 - SampleType_SAMPLE_TYPE_TEST SampleType = 5 -) - -var SampleType_name = map[int32]string{ - 1: "SAMPLE_TYPE_RAW", - 2: "SAMPLE_TYPE_LIGHT", - 3: "SAMPLE_TYPE_TEMPERATURE", - 4: "SAMPLE_TYPE_HUMIDITY", - 5: "SAMPLE_TYPE_TEST", -} -var SampleType_value = map[string]int32{ - "SAMPLE_TYPE_RAW": 1, - "SAMPLE_TYPE_LIGHT": 2, - "SAMPLE_TYPE_TEMPERATURE": 3, - "SAMPLE_TYPE_HUMIDITY": 4, - "SAMPLE_TYPE_TEST": 5, -} - -func (x SampleType) Enum() *SampleType { - p := new(SampleType) - *p = x - return p -} -func (x SampleType) String() string { - return proto.EnumName(SampleType_name, int32(x)) -} -func (x *SampleType) UnmarshalJSON(data []byte) error { - value, err := proto.UnmarshalJSONEnum(SampleType_value, data, "SampleType") - if err != nil { - return err - } - *x = SampleType(value) - return nil -} -func (SampleType) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } - -type SampleData struct { - Title *string `protobuf:"bytes,1,opt,name=title" json:"title,omitempty"` - Type *SampleType `protobuf:"varint,2,req,name=type,enum=sample.SampleType" json:"type,omitempty"` - Data []string `protobuf:"bytes,3,rep,name=data" json:"data,omitempty"` - XXX_unrecognized []byte `json:"-"` -} - -func (m *SampleData) Reset() { *m = SampleData{} } -func (m *SampleData) String() string { return proto.CompactTextString(m) } -func (*SampleData) ProtoMessage() {} -func (*SampleData) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } - -func (m *SampleData) GetTitle() string { - if m != nil && m.Title != nil { - return *m.Title - } - return "" -} - -func (m *SampleData) GetType() SampleType { - if m != nil && m.Type != nil { - return *m.Type - } - return SampleType_SAMPLE_TYPE_RAW -} - -func (m *SampleData) GetData() []string { - if m != nil { - return m.Data - } - return nil -} - -func init() { - proto.RegisterType((*SampleData)(nil), "sample.SampleData") - proto.RegisterEnum("sample.SampleType", SampleType_name, SampleType_value) -} - -func init() { proto.RegisterFile("sample.proto", fileDescriptor0) } - -var fileDescriptor0 = []byte{ - // 201 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x29, 0x4e, 0xcc, 0x2d, - 0xc8, 0x49, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x83, 0xf0, 0x94, 0xe2, 0xb8, 0xb8, - 0x82, 0xc1, 0x2c, 0x97, 0xc4, 0x92, 0x44, 0x21, 0x11, 0x2e, 0xd6, 0x92, 0xcc, 0x92, 0x9c, 0x54, - 0x09, 0x46, 0x05, 0x46, 0x0d, 0xce, 0x20, 0x08, 0x47, 0x48, 0x8d, 0x8b, 0xa5, 0xa4, 0xb2, 0x20, - 0x55, 0x82, 0x49, 0x81, 0x49, 0x83, 0xcf, 0x48, 0x48, 0x0f, 0x6a, 0x10, 0x44, 0x5f, 0x48, 0x65, - 0x41, 0x6a, 0x10, 0x58, 0x5e, 0x48, 0x88, 0x8b, 0x25, 0x25, 0xb1, 0x24, 0x51, 0x82, 0x59, 0x81, - 0x59, 0x83, 0x33, 0x08, 0xcc, 0xd6, 0x6a, 0x65, 0x84, 0x59, 0x00, 0x52, 0x28, 0x24, 0xcc, 0xc5, - 0x1f, 0xec, 0xe8, 0x1b, 0xe0, 0xe3, 0x1a, 0x1f, 0x12, 0x19, 0xe0, 0x1a, 0x1f, 0xe4, 0x18, 0x2e, - 0xc0, 0x28, 0x24, 0xca, 0x25, 0x88, 0x2c, 0xe8, 0xe3, 0xe9, 0xee, 0x11, 0x22, 0xc0, 0x24, 0x24, - 0xcd, 0x25, 0x8e, 0x2c, 0x1c, 0xe2, 0xea, 0x1b, 0xe0, 0x1a, 0xe4, 0x18, 0x12, 0x1a, 0xe4, 0x2a, - 0xc0, 0x2c, 0x24, 0xc1, 0x25, 0x82, 0x2c, 0xe9, 0x11, 0xea, 0xeb, 0xe9, 0xe2, 0x19, 0x12, 0x29, - 0xc0, 0x22, 0x24, 0xc2, 0x25, 0x80, 0xaa, 0x2d, 0x38, 0x44, 0x80, 0x15, 0x10, 0x00, 0x00, 0xff, - 0xff, 0x55, 0xab, 0x27, 0x80, 0xfe, 0x00, 0x00, 0x00, -} diff --git a/learn/go/protobuf/install.md b/learn/go/protobuf/sample/install.md similarity index 100% rename from learn/go/protobuf/install.md rename to learn/go/protobuf/sample/install.md diff --git a/learn/go/protobuf/sample/sample.pb.go b/learn/go/protobuf/sample/sample.pb.go new file mode 100644 index 0000000..c0ec6df --- /dev/null +++ b/learn/go/protobuf/sample/sample.pb.go @@ -0,0 +1,166 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: sample.proto + +/* +Package sample is a generated protocol buffer package. + +It is generated from these files: + sample.proto + +It has these top-level messages: + SampleData +*/ +package sample + +import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package + +type SampleType int32 + +const ( + SampleType_SAMPLE_TYPE_RAW SampleType = 1 + SampleType_SAMPLE_TYPE_LIGHT SampleType = 2 + SampleType_SAMPLE_TYPE_TEMPERATURE SampleType = 3 + SampleType_SAMPLE_TYPE_HUMIDITY SampleType = 4 + SampleType_SAMPLE_TYPE_TEST SampleType = 5 +) + +var SampleType_name = map[int32]string{ + 1: "SAMPLE_TYPE_RAW", + 2: "SAMPLE_TYPE_LIGHT", + 3: "SAMPLE_TYPE_TEMPERATURE", + 4: "SAMPLE_TYPE_HUMIDITY", + 5: "SAMPLE_TYPE_TEST", +} +var SampleType_value = map[string]int32{ + "SAMPLE_TYPE_RAW": 1, + "SAMPLE_TYPE_LIGHT": 2, + "SAMPLE_TYPE_TEMPERATURE": 3, + "SAMPLE_TYPE_HUMIDITY": 4, + "SAMPLE_TYPE_TEST": 5, +} + +func (x SampleType) Enum() *SampleType { + p := new(SampleType) + *p = x + return p +} +func (x SampleType) String() string { + return proto.EnumName(SampleType_name, int32(x)) +} +func (x *SampleType) UnmarshalJSON(data []byte) error { + value, err := proto.UnmarshalJSONEnum(SampleType_value, data, "SampleType") + if err != nil { + return err + } + *x = SampleType(value) + return nil +} +func (SampleType) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } + +type SampleData struct { + Title *string `protobuf:"bytes,1,opt,name=title" json:"title,omitempty"` + Type *SampleType `protobuf:"varint,2,opt,name=type,enum=sample.SampleType" json:"type,omitempty"` + Data []float64 `protobuf:"fixed64,3,rep,name=data" json:"data,omitempty"` + Source *SampleData_Source `protobuf:"group,4,opt,name=Source,json=source" json:"source,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *SampleData) Reset() { *m = SampleData{} } +func (m *SampleData) String() string { return proto.CompactTextString(m) } +func (*SampleData) ProtoMessage() {} +func (*SampleData) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } + +func (m *SampleData) GetTitle() string { + if m != nil && m.Title != nil { + return *m.Title + } + return "" +} + +func (m *SampleData) GetType() SampleType { + if m != nil && m.Type != nil { + return *m.Type + } + return SampleType_SAMPLE_TYPE_RAW +} + +func (m *SampleData) GetData() []float64 { + if m != nil { + return m.Data + } + return nil +} + +func (m *SampleData) GetSource() *SampleData_Source { + if m != nil { + return m.Source + } + return nil +} + +type SampleData_Source struct { + Band *string `protobuf:"bytes,5,req,name=band" json:"band,omitempty"` + Version *string `protobuf:"bytes,6,req,name=version" json:"version,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *SampleData_Source) Reset() { *m = SampleData_Source{} } +func (m *SampleData_Source) String() string { return proto.CompactTextString(m) } +func (*SampleData_Source) ProtoMessage() {} +func (*SampleData_Source) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0, 0} } + +func (m *SampleData_Source) GetBand() string { + if m != nil && m.Band != nil { + return *m.Band + } + return "" +} + +func (m *SampleData_Source) GetVersion() string { + if m != nil && m.Version != nil { + return *m.Version + } + return "" +} + +func init() { + proto.RegisterType((*SampleData)(nil), "sample.SampleData") + proto.RegisterType((*SampleData_Source)(nil), "sample.SampleData.Source") + proto.RegisterEnum("sample.SampleType", SampleType_name, SampleType_value) +} + +func init() { proto.RegisterFile("sample.proto", fileDescriptor0) } + +var fileDescriptor0 = []byte{ + // 265 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x54, 0x8f, 0x5f, 0x4b, 0xc3, 0x30, + 0x14, 0xc5, 0xc9, 0xfa, 0x47, 0x76, 0x11, 0x8d, 0xd7, 0x8a, 0x51, 0x5f, 0x8a, 0x0f, 0x12, 0x7c, + 0x28, 0xb8, 0x07, 0xdf, 0x0b, 0x0b, 0xae, 0xb0, 0x42, 0x49, 0x33, 0x64, 0x4f, 0x23, 0xba, 0x3c, + 0x0c, 0xe6, 0x5a, 0xda, 0x28, 0xec, 0x03, 0xf8, 0xe1, 0xfc, 0x56, 0xd2, 0x74, 0x4a, 0xf7, 0x76, + 0xce, 0xef, 0xde, 0x73, 0x92, 0x0b, 0xa7, 0xad, 0xfe, 0xa8, 0xb7, 0x26, 0xa9, 0x9b, 0xca, 0x56, + 0x18, 0xf6, 0xee, 0xfe, 0x87, 0x00, 0x94, 0x4e, 0x4e, 0xb5, 0xd5, 0x18, 0x41, 0x60, 0x37, 0x76, + 0x6b, 0x18, 0x89, 0x09, 0x1f, 0xcb, 0xde, 0xe0, 0x03, 0xf8, 0x76, 0x5f, 0x1b, 0x36, 0x8a, 0x09, + 0x3f, 0x9b, 0x60, 0x72, 0x68, 0xea, 0x73, 0x6a, 0x5f, 0x1b, 0xe9, 0xe6, 0x88, 0xe0, 0xaf, 0xb5, + 0xd5, 0xcc, 0x8b, 0x3d, 0x4e, 0xa4, 0xd3, 0xf8, 0x04, 0x61, 0x5b, 0x7d, 0x36, 0xef, 0x86, 0xf9, + 0x31, 0xe1, 0x30, 0xb9, 0x39, 0x4e, 0x77, 0xaf, 0x26, 0xa5, 0x5b, 0x90, 0x87, 0xc5, 0xdb, 0x67, + 0x08, 0x7b, 0xd2, 0x15, 0xbe, 0xe9, 0xdd, 0x9a, 0x05, 0xf1, 0x88, 0x8f, 0xa5, 0xd3, 0xc8, 0xe0, + 0xe4, 0xcb, 0x34, 0xed, 0xa6, 0xda, 0xb1, 0xd0, 0xe1, 0x3f, 0xfb, 0xf8, 0xfd, 0x7f, 0x4b, 0xf7, + 0x27, 0xbc, 0x84, 0xf3, 0x32, 0xcd, 0x8b, 0xb9, 0x58, 0xa9, 0x65, 0x21, 0x56, 0x32, 0x7d, 0xa5, + 0x04, 0xaf, 0xe0, 0x62, 0x08, 0xe7, 0xd9, 0xcb, 0x4c, 0xd1, 0x11, 0xde, 0xc1, 0xf5, 0x10, 0x2b, + 0x91, 0x17, 0x42, 0xa6, 0x6a, 0x21, 0x05, 0xf5, 0x90, 0x41, 0x34, 0x1c, 0xce, 0x16, 0x79, 0x36, + 0xcd, 0xd4, 0x92, 0xfa, 0x18, 0x01, 0x3d, 0x8e, 0x95, 0x8a, 0x06, 0xbf, 0x01, 0x00, 0x00, 0xff, + 0xff, 0x7c, 0x62, 0x65, 0x58, 0x6a, 0x01, 0x00, 0x00, +} diff --git a/learn/go/protobuf/sample.proto b/learn/go/protobuf/sample/sample.proto similarity index 55% rename from learn/go/protobuf/sample.proto rename to learn/go/protobuf/sample/sample.proto index 308f92e..a7038b7 100644 --- a/learn/go/protobuf/sample.proto +++ b/learn/go/protobuf/sample/sample.proto @@ -10,8 +10,12 @@ enum SampleType { message SampleData { optional string title = 1; - required SampleType type = 2; - repeated string data = 3; + optional SampleType type = 2; // SampleType 必需是optional + repeated double data = 3; + optional group Source = 4 { + required string band = 5; + required string version = 6; + } }; diff --git a/learn/go/protobuf/server.go b/learn/go/protobuf/server.go index 250e73b..fad838b 100644 --- a/learn/go/protobuf/server.go +++ b/learn/go/protobuf/server.go @@ -9,9 +9,55 @@ package main import ( "fmt" - "sample" + "net" + "log" + "github.com/golang/protobuf/proto" + "./sample" ) func main() { defer fmt.Println("Program Exited...") + listener, err := net.Listen("tcp", ":8000") + if err != nil { + log.Fatal(err) + } + + conn, err := listener.Accept() + if err != nil { + log.Fatal(err) + } + + defer conn.Close() + + //var data []byte 这里一定要指定缓冲区大小 + data := make([]byte, 4096) + if n, err := conn.Read(data); err != nil { + log.Fatal("conn.Read: ", err) + } else { + log.Println("Recevied Len: ", n) + data = data[:n] + } + + fmt.Println("Data Len: ", len(data)) + + sampleData := sample.SampleData{ } + err = proto.Unmarshal(data, &sampleData) + if err != nil { + log.Fatal("proto.Unmarshal: ", err) + } + + fmt.Println("Title: ", sampleData.GetTitle()) + fmt.Println("Type: ", sampleData.GetType()) + fmt.Printf("Data: ") + for i, v := range(sampleData.GetData()) { + if i > 0 { + fmt.Printf(" ") + } + + fmt.Printf("%v", v) + } + + fmt.Println("") + fmt.Println("Source.Band: ", sampleData.GetSource().GetBand()) + fmt.Println("Source.Version: ", sampleData.GetSource().GetVersion()) }