1
0
mirror of https://github.com/chai2010/advanced-go-programming-book.git synced 2025-05-24 12:32:21 +00:00

Merge branch 'master' of github.com:chai2010/advanced-go-programming-book

This commit is contained in:
Xargin 2018-08-15 19:49:06 +08:00
commit 9fe09c7ac3
15 changed files with 6 additions and 928 deletions

View File

@ -1,6 +1,6 @@
# 2.2 CGO基础
要使用CGO特性需要安装CC++构建工具链在macOS和Linux下是要安装GCC在windows下是需要安装MinGW工具。同时需要保证环境变量`CGO_ENABLED`被设置为1这表示CGO是被启用的状态。在本地构建时`CGO_ENABLED`默认是启用的当交叉构建时CGO默认是禁止的。比如要交叉构建ARM环境运行的Go程序需要手工设置好C/C++交叉构建的工具链,同时开启`CGO_ENABLED`环境变量。然后通过`import "C"`语句启用CGO特性。
要使用CGO特性需要安装C/C++构建工具链在macOS和Linux下是要安装GCC在windows下是需要安装MinGW工具。同时需要保证环境变量`CGO_ENABLED`被设置为1这表示CGO是被启用的状态。在本地构建时`CGO_ENABLED`默认是启用的当交叉构建时CGO默认是禁止的。比如要交叉构建ARM环境运行的Go程序需要手工设置好C/C++交叉构建的工具链,同时开启`CGO_ENABLED`环境变量。然后通过`import "C"`语句启用CGO特性。
## 2.2.1 `import "C"`语句
@ -37,6 +37,7 @@ cgo将当前包引用的C语言符号都放到了虚拟的C包中同时当前
```go
package cgo_helper
//#include <stdio.h>
import "C"
type CChar C.char
@ -46,7 +47,7 @@ func (p *CChar) GoString() string {
}
func PrintCString(cs *C.char) {
print(((*CChar)(cs.GoString()))
C.puts(cs)
}
```
@ -55,7 +56,7 @@ func PrintCString(cs *C.char) {
```go
package main
// extern char* cs = "hello"
//char* cs = "hello";
import "C"
import "./cgo_helper"

View File

@ -1,24 +0,0 @@
package main
import (
hs "ch4-04/01/helloservice"
"context"
"fmt"
"google.golang.org/grpc"
"log"
)
func main() {
conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
client := hs.NewHelloServiceClient(conn)
reply, err := client.Hello(context.Background(), &hs.String{Value: "hello"})
if err != nil {
log.Fatal(err)
}
fmt.Println(reply.GetValue())
}

View File

@ -1,151 +0,0 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: hello.proto
package helloservice
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// 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 String struct {
Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *String) Reset() { *m = String{} }
func (m *String) String() string { return proto.CompactTextString(m) }
func (*String) ProtoMessage() {}
func (*String) Descriptor() ([]byte, []int) {
return fileDescriptor_hello_c1f637c850550ef7, []int{0}
}
func (m *String) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_String.Unmarshal(m, b)
}
func (m *String) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_String.Marshal(b, m, deterministic)
}
func (dst *String) XXX_Merge(src proto.Message) {
xxx_messageInfo_String.Merge(dst, src)
}
func (m *String) XXX_Size() int {
return xxx_messageInfo_String.Size(m)
}
func (m *String) XXX_DiscardUnknown() {
xxx_messageInfo_String.DiscardUnknown(m)
}
var xxx_messageInfo_String proto.InternalMessageInfo
func (m *String) GetValue() string {
if m != nil {
return m.Value
}
return ""
}
func init() {
proto.RegisterType((*String)(nil), "helloservice.String")
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion4
// HelloServiceClient is the client API for HelloService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type HelloServiceClient interface {
Hello(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error)
}
type helloServiceClient struct {
cc *grpc.ClientConn
}
func NewHelloServiceClient(cc *grpc.ClientConn) HelloServiceClient {
return &helloServiceClient{cc}
}
func (c *helloServiceClient) Hello(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error) {
out := new(String)
err := c.cc.Invoke(ctx, "/helloservice.HelloService/Hello", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// HelloServiceServer is the server API for HelloService service.
type HelloServiceServer interface {
Hello(context.Context, *String) (*String, error)
}
func RegisterHelloServiceServer(s *grpc.Server, srv HelloServiceServer) {
s.RegisterService(&_HelloService_serviceDesc, srv)
}
func _HelloService_Hello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(String)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(HelloServiceServer).Hello(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/helloservice.HelloService/Hello",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(HelloServiceServer).Hello(ctx, req.(*String))
}
return interceptor(ctx, in, info, handler)
}
var _HelloService_serviceDesc = grpc.ServiceDesc{
ServiceName: "helloservice.HelloService",
HandlerType: (*HelloServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Hello",
Handler: _HelloService_Hello_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "hello.proto",
}
func init() { proto.RegisterFile("hello.proto", fileDescriptor_hello_c1f637c850550ef7) }
var fileDescriptor_hello_c1f637c850550ef7 = []byte{
// 108 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0xce, 0x48, 0xcd, 0xc9,
0xc9, 0xd7, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x01, 0x73, 0x8a, 0x53, 0x8b, 0xca, 0x32,
0x93, 0x53, 0x95, 0xe4, 0xb8, 0xd8, 0x82, 0x4b, 0x8a, 0x32, 0xf3, 0xd2, 0x85, 0x44, 0xb8, 0x58,
0xcb, 0x12, 0x73, 0x4a, 0x53, 0x25, 0x18, 0x15, 0x18, 0x35, 0x38, 0x83, 0x20, 0x1c, 0x23, 0x67,
0x2e, 0x1e, 0x0f, 0x90, 0xfa, 0x60, 0x88, 0x7a, 0x21, 0x63, 0x2e, 0x56, 0x30, 0x5f, 0x48, 0x44,
0x0f, 0xd9, 0x1c, 0x3d, 0x88, 0x21, 0x52, 0x58, 0x45, 0x93, 0xd8, 0xc0, 0x36, 0x1b, 0x03, 0x02,
0x00, 0x00, 0xff, 0xff, 0xf7, 0x6b, 0x07, 0xff, 0x88, 0x00, 0x00, 0x00,
}

View File

@ -1,13 +0,0 @@
syntax = "proto3";
package helloservice;
message String {
string value = 1;
}
service HelloService {
rpc Hello (String) returns (String);
}
//protoc --go_out=plugins=grpc:. hello.proto

View File

@ -1,31 +0,0 @@
package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
hs "ch4-04/01/helloservice"
)
type HelloServiceImpl struct{}
func (p *HelloServiceImpl) Hello(
ctx context.Context, args *hs.String,
) (*hs.String, error) {
reply := &hs.String{Value: "hello:" + args.GetValue()}
return reply, nil
}
func main() {
grpcServer := grpc.NewServer()
hs.RegisterHelloServiceServer(grpcServer, new(HelloServiceImpl))
lis, err := net.Listen("tcp", ":1234")
if err != nil {
log.Fatal(err)
}
grpcServer.Serve(lis)
}

View File

@ -1,218 +0,0 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: hello.proto
package HelloService
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// 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 String struct {
Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *String) Reset() { *m = String{} }
func (m *String) String() string { return proto.CompactTextString(m) }
func (*String) ProtoMessage() {}
func (*String) Descriptor() ([]byte, []int) {
return fileDescriptor_hello_89b12a75f1fdbbb0, []int{0}
}
func (m *String) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_String.Unmarshal(m, b)
}
func (m *String) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_String.Marshal(b, m, deterministic)
}
func (dst *String) XXX_Merge(src proto.Message) {
xxx_messageInfo_String.Merge(dst, src)
}
func (m *String) XXX_Size() int {
return xxx_messageInfo_String.Size(m)
}
func (m *String) XXX_DiscardUnknown() {
xxx_messageInfo_String.DiscardUnknown(m)
}
var xxx_messageInfo_String proto.InternalMessageInfo
func (m *String) GetValue() string {
if m != nil {
return m.Value
}
return ""
}
func init() {
proto.RegisterType((*String)(nil), "HelloService.String")
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion4
// HelloServiceClient is the client API for HelloService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type HelloServiceClient interface {
Hello(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error)
Channel(ctx context.Context, opts ...grpc.CallOption) (HelloService_ChannelClient, error)
}
type helloServiceClient struct {
cc *grpc.ClientConn
}
func NewHelloServiceClient(cc *grpc.ClientConn) HelloServiceClient {
return &helloServiceClient{cc}
}
func (c *helloServiceClient) Hello(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error) {
out := new(String)
err := c.cc.Invoke(ctx, "/HelloService.HelloService/Hello", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *helloServiceClient) Channel(ctx context.Context, opts ...grpc.CallOption) (HelloService_ChannelClient, error) {
stream, err := c.cc.NewStream(ctx, &_HelloService_serviceDesc.Streams[0], "/HelloService.HelloService/Channel", opts...)
if err != nil {
return nil, err
}
x := &helloServiceChannelClient{stream}
return x, nil
}
type HelloService_ChannelClient interface {
Send(*String) error
Recv() (*String, error)
grpc.ClientStream
}
type helloServiceChannelClient struct {
grpc.ClientStream
}
func (x *helloServiceChannelClient) Send(m *String) error {
return x.ClientStream.SendMsg(m)
}
func (x *helloServiceChannelClient) Recv() (*String, error) {
m := new(String)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
// HelloServiceServer is the server API for HelloService service.
type HelloServiceServer interface {
Hello(context.Context, *String) (*String, error)
Channel(HelloService_ChannelServer) error
}
func RegisterHelloServiceServer(s *grpc.Server, srv HelloServiceServer) {
s.RegisterService(&_HelloService_serviceDesc, srv)
}
func _HelloService_Hello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(String)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(HelloServiceServer).Hello(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/HelloService.HelloService/Hello",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(HelloServiceServer).Hello(ctx, req.(*String))
}
return interceptor(ctx, in, info, handler)
}
func _HelloService_Channel_Handler(srv interface{}, stream grpc.ServerStream) error {
return srv.(HelloServiceServer).Channel(&helloServiceChannelServer{stream})
}
type HelloService_ChannelServer interface {
Send(*String) error
Recv() (*String, error)
grpc.ServerStream
}
type helloServiceChannelServer struct {
grpc.ServerStream
}
func (x *helloServiceChannelServer) Send(m *String) error {
return x.ServerStream.SendMsg(m)
}
func (x *helloServiceChannelServer) Recv() (*String, error) {
m := new(String)
if err := x.ServerStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
var _HelloService_serviceDesc = grpc.ServiceDesc{
ServiceName: "HelloService.HelloService",
HandlerType: (*HelloServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Hello",
Handler: _HelloService_Hello_Handler,
},
},
Streams: []grpc.StreamDesc{
{
StreamName: "Channel",
Handler: _HelloService_Channel_Handler,
ServerStreams: true,
ClientStreams: true,
},
},
Metadata: "hello.proto",
}
func init() { proto.RegisterFile("hello.proto", fileDescriptor_hello_89b12a75f1fdbbb0) }
var fileDescriptor_hello_89b12a75f1fdbbb0 = []byte{
// 121 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0xce, 0x48, 0xcd, 0xc9,
0xc9, 0xd7, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0xf1, 0x00, 0x71, 0x82, 0x53, 0x8b, 0xca,
0x32, 0x93, 0x53, 0x95, 0xe4, 0xb8, 0xd8, 0x82, 0x4b, 0x8a, 0x32, 0xf3, 0xd2, 0x85, 0x44, 0xb8,
0x58, 0xcb, 0x12, 0x73, 0x4a, 0x53, 0x25, 0x18, 0x15, 0x18, 0x35, 0x38, 0x83, 0x20, 0x1c, 0xa3,
0x3a, 0x2e, 0x14, 0xf5, 0x42, 0xc6, 0x5c, 0xac, 0x60, 0xbe, 0x90, 0x88, 0x1e, 0xb2, 0xb8, 0x1e,
0xc4, 0x10, 0x29, 0xac, 0xa2, 0x42, 0x96, 0x5c, 0xec, 0xce, 0x19, 0x89, 0x79, 0x79, 0xa9, 0x39,
0xa4, 0x68, 0xd3, 0x60, 0x34, 0x60, 0x4c, 0x62, 0x03, 0x3b, 0xda, 0x18, 0x10, 0x00, 0x00, 0xff,
0xff, 0x9a, 0x51, 0x94, 0xb1, 0xc3, 0x00, 0x00, 0x00,
}

View File

@ -1,14 +0,0 @@
syntax = "proto3";
package HelloService;
message String {
string value = 1;
}
service HelloService {
rpc Hello (String) returns (String);
rpc Channel (stream String) returns (stream String);
}
//protoc --go_out=plugins=grpc:. hello.proto

View File

@ -1,54 +0,0 @@
package main
import (
"context"
"fmt"
"io"
"log"
"time"
"google.golang.org/grpc"
hs "ch4-04/02/HelloService"
)
func main() {
conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
client := hs.NewHelloServiceClient(conn)
reply, err := client.Hello(context.Background(), &hs.String{Value: "hello"})
if err != nil {
log.Fatal(err)
}
fmt.Println(reply.GetValue())
//grpc stream
stream, err := client.Channel(context.Background())
if err != nil {
log.Fatal(err)
}
go func() {
for {
if err := stream.Send(&hs.String{Value: "hi"}); err != nil {
log.Fatal(err)
}
time.Sleep(time.Second)
}
}()
for {
reply, err := stream.Recv()
if err != nil {
if err == io.EOF {
break
}
log.Fatal(err)
}
fmt.Println(reply.GetValue())
}
}

View File

@ -1,53 +0,0 @@
package main
import (
"context"
"io"
"log"
"net"
"google.golang.org/grpc"
hs "ch4-04/02/HelloService"
)
type HelloServiceImpl struct{}
func (p *HelloServiceImpl) Hello(
ctx context.Context, args *hs.String,
) (*hs.String, error) {
reply := &hs.String{Value: "hello:" + args.GetValue()}
return reply, nil
}
//grpc stream
func (p *HelloServiceImpl) Channel(stream hs.HelloService_ChannelServer) error {
for {
args, err := stream.Recv()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
reply := &hs.String{Value: "hello:" + args.GetValue()}
err = stream.Send(reply)
if err != nil {
return err
}
}
}
func main() {
grpcServer := grpc.NewServer()
hs.RegisterHelloServiceServer(grpcServer, new(HelloServiceImpl))
lis, err := net.Listen("tcp", ":1234")
if err != nil {
log.Fatal(err)
}
grpcServer.Serve(lis)
}

View File

@ -1,29 +0,0 @@
package main
import (
"context"
"log"
"google.golang.org/grpc"
pb "ch4-04/03/pubsubservice"
)
func main() {
conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
client := pb.NewPubsubServiceClient(conn)
_, err = client.Publish(context.Background(), &pb.String{Value: "golang: hello Go"})
if err != nil {
log.Fatal(err)
}
_, err = client.Publish(context.Background(), &pb.String{Value: "docker: hello Docker"})
if err != nil {
log.Fatal(err)
}
}

View File

@ -1,38 +0,0 @@
package main
import (
"context"
"fmt"
"io"
"log"
"google.golang.org/grpc"
pb "ch4-04/03/pubsubservice"
)
func main() {
conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
client := pb.NewPubsubServiceClient(conn)
stream, err := client.Subscribe(context.Background(), &pb.String{Value: "golang:"})
if err != nil {
log.Fatal(err)
}
for {
reply, err := stream.Recv()
if err != nil {
if err == io.EOF {
break
}
log.Fatal(err)
}
fmt.Println(reply.GetValue())
}
}

View File

@ -1,214 +0,0 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: pubsubservice.proto
package pubsubservice
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// 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 String struct {
Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *String) Reset() { *m = String{} }
func (m *String) String() string { return proto.CompactTextString(m) }
func (*String) ProtoMessage() {}
func (*String) Descriptor() ([]byte, []int) {
return fileDescriptor_pubsubservice_f5055945d20b225a, []int{0}
}
func (m *String) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_String.Unmarshal(m, b)
}
func (m *String) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_String.Marshal(b, m, deterministic)
}
func (dst *String) XXX_Merge(src proto.Message) {
xxx_messageInfo_String.Merge(dst, src)
}
func (m *String) XXX_Size() int {
return xxx_messageInfo_String.Size(m)
}
func (m *String) XXX_DiscardUnknown() {
xxx_messageInfo_String.DiscardUnknown(m)
}
var xxx_messageInfo_String proto.InternalMessageInfo
func (m *String) GetValue() string {
if m != nil {
return m.Value
}
return ""
}
func init() {
proto.RegisterType((*String)(nil), "pubsubservice.String")
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion4
// PubsubServiceClient is the client API for PubsubService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type PubsubServiceClient interface {
Publish(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error)
Subscribe(ctx context.Context, in *String, opts ...grpc.CallOption) (PubsubService_SubscribeClient, error)
}
type pubsubServiceClient struct {
cc *grpc.ClientConn
}
func NewPubsubServiceClient(cc *grpc.ClientConn) PubsubServiceClient {
return &pubsubServiceClient{cc}
}
func (c *pubsubServiceClient) Publish(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error) {
out := new(String)
err := c.cc.Invoke(ctx, "/pubsubservice.PubsubService/Publish", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *pubsubServiceClient) Subscribe(ctx context.Context, in *String, opts ...grpc.CallOption) (PubsubService_SubscribeClient, error) {
stream, err := c.cc.NewStream(ctx, &_PubsubService_serviceDesc.Streams[0], "/pubsubservice.PubsubService/Subscribe", opts...)
if err != nil {
return nil, err
}
x := &pubsubServiceSubscribeClient{stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
type PubsubService_SubscribeClient interface {
Recv() (*String, error)
grpc.ClientStream
}
type pubsubServiceSubscribeClient struct {
grpc.ClientStream
}
func (x *pubsubServiceSubscribeClient) Recv() (*String, error) {
m := new(String)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
// PubsubServiceServer is the server API for PubsubService service.
type PubsubServiceServer interface {
Publish(context.Context, *String) (*String, error)
Subscribe(*String, PubsubService_SubscribeServer) error
}
func RegisterPubsubServiceServer(s *grpc.Server, srv PubsubServiceServer) {
s.RegisterService(&_PubsubService_serviceDesc, srv)
}
func _PubsubService_Publish_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(String)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(PubsubServiceServer).Publish(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/pubsubservice.PubsubService/Publish",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(PubsubServiceServer).Publish(ctx, req.(*String))
}
return interceptor(ctx, in, info, handler)
}
func _PubsubService_Subscribe_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(String)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(PubsubServiceServer).Subscribe(m, &pubsubServiceSubscribeServer{stream})
}
type PubsubService_SubscribeServer interface {
Send(*String) error
grpc.ServerStream
}
type pubsubServiceSubscribeServer struct {
grpc.ServerStream
}
func (x *pubsubServiceSubscribeServer) Send(m *String) error {
return x.ServerStream.SendMsg(m)
}
var _PubsubService_serviceDesc = grpc.ServiceDesc{
ServiceName: "pubsubservice.PubsubService",
HandlerType: (*PubsubServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Publish",
Handler: _PubsubService_Publish_Handler,
},
},
Streams: []grpc.StreamDesc{
{
StreamName: "Subscribe",
Handler: _PubsubService_Subscribe_Handler,
ServerStreams: true,
},
},
Metadata: "pubsubservice.proto",
}
func init() { proto.RegisterFile("pubsubservice.proto", fileDescriptor_pubsubservice_f5055945d20b225a) }
var fileDescriptor_pubsubservice_f5055945d20b225a = []byte{
// 132 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2e, 0x28, 0x4d, 0x2a,
0x2e, 0x4d, 0x2a, 0x4e, 0x2d, 0x2a, 0xcb, 0x4c, 0x4e, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17,
0xe2, 0x45, 0x11, 0x54, 0x92, 0xe3, 0x62, 0x0b, 0x2e, 0x29, 0xca, 0xcc, 0x4b, 0x17, 0x12, 0xe1,
0x62, 0x2d, 0x4b, 0xcc, 0x29, 0x4d, 0x95, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x82, 0x70, 0x8c,
0x5a, 0x19, 0xb9, 0x78, 0x03, 0xc0, 0x3a, 0x82, 0x21, 0x3a, 0x84, 0xcc, 0xb9, 0xd8, 0x03, 0x4a,
0x93, 0x72, 0x32, 0x8b, 0x33, 0x84, 0x44, 0xf5, 0x50, 0x6d, 0x80, 0x98, 0x24, 0x85, 0x5d, 0x58,
0xc8, 0x9a, 0x8b, 0x33, 0xb8, 0x34, 0xa9, 0x38, 0xb9, 0x28, 0x33, 0x29, 0x95, 0x34, 0xad, 0x06,
0x8c, 0x49, 0x6c, 0x60, 0xd7, 0x1b, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x25, 0x3b, 0x39, 0x24,
0xd4, 0x00, 0x00, 0x00,
}

View File

@ -1,14 +0,0 @@
syntax = "proto3";
package pubsubservice;
message String {
string value = 1;
}
service PubsubService {
rpc Publish (String) returns (String);
rpc Subscribe (String) returns (stream String);
}
//protoc --go_out=plugins=grpc:. pubsubservice.proto

View File

@ -1,70 +0,0 @@
package main
import (
"context"
"log"
"net"
"strings"
"time"
"github.com/docker/docker/pkg/pubsub"
"google.golang.org/grpc"
pb "ch4-04/03/pubsubservice"
)
type PubsubService struct {
pub *pubsub.Publisher
}
func NewPubsubService() *PubsubService {
return &PubsubService{
pub: pubsub.NewPublisher(100*time.Millisecond, 10),
}
}
func (p *PubsubService) Publish(
ctx context.Context, arg *pb.String,
) (*pb.String, error) {
p.pub.Publish(arg.GetValue())
//debug
//reply := &String{Value: "<Publish> " + arg.GetValue()}
//fmt.Println(reply.GetValue())
return &pb.String{}, nil
}
func (p *PubsubService) Subscribe(
arg *pb.String, stream pb.PubsubService_SubscribeServer,
) error {
ch := p.pub.SubscribeTopic(func(v interface{}) bool {
if key, ok := v.(string); ok {
//debug
//fmt.Printf("<debug> %t %s %s %t\n",
// ok,arg.GetValue(),key,strings.HasPrefix(key,arg.GetValue()))
if strings.HasPrefix(key, arg.GetValue()) {
return true
}
}
return false
})
for v := range ch {
if err := stream.Send(&pb.String{Value: v.(string)}); err != nil {
return err
}
}
return nil
}
func main() {
grpcServer := grpc.NewServer()
pb.RegisterPubsubServiceServer(grpcServer, NewPubsubService())
lis, err := net.Listen("tcp", ":1234")
if err != nil {
log.Fatal(err)
}
grpcServer.Serve(lis)
}

Binary file not shown.