1package main 2 3import ( 4 "context" 5 "flag" 6 "fmt" 7 "io" 8 "log" 9 "time" 10 11 flatbuffers "github.com/google/flatbuffers/go" 12 models "github.com/google/flatbuffers/grpc/examples/go/greeter/models" 13 "google.golang.org/grpc" 14) 15 16var ( 17 addr = "3000" 18 name = flag.String("name", "Flatbuffers", "name to be sent to server :D") 19) 20 21func printSayHello(client models.GreeterClient, name string) { 22 log.Printf("Name to be sent (%s)", name) 23 b := flatbuffers.NewBuilder(0) 24 i := b.CreateString(name) 25 models.HelloRequestStart(b) 26 models.HelloRequestAddName(b, i) 27 b.Finish(models.HelloRequestEnd(b)) 28 29 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 30 defer cancel() 31 request, err := client.SayHello(ctx, b, grpc.CallContentSubtype("flatbuffers")) 32 if err != nil { 33 log.Fatalf("%v.SayHello(_) = _, %v: ", client, err) 34 } 35 log.Printf("server said %q", request.Message()) 36} 37 38func printSayManyHello(client models.GreeterClient, name string) { 39 log.Printf("Name to be sent (%s)", name) 40 b := flatbuffers.NewBuilder(0) 41 i := b.CreateString(name) 42 models.HelloRequestStart(b) 43 models.HelloRequestAddName(b, i) 44 b.Finish(models.HelloRequestEnd(b)) 45 46 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 47 defer cancel() 48 stream, err := client.SayManyHellos(ctx, b, grpc.CallContentSubtype("flatbuffers")) 49 if err != nil { 50 log.Fatalf("%v.SayManyHellos(_) = _, %v", client, err) 51 } 52 for { 53 request, err := stream.Recv() 54 if err == io.EOF { 55 break 56 } 57 if err != nil { 58 log.Fatalf("%v.SayManyHellos(_) = _, %v", client, err) 59 } 60 log.Printf("server said %q", request.Message()) 61 } 62} 63 64func main() { 65 flag.Parse() 66 conn, err := grpc.Dial(fmt.Sprintf("localhost:%d", 3000), grpc.WithInsecure(), grpc.WithCodec(flatbuffers.FlatbuffersCodec{})) 67 if err != nil { 68 log.Fatalf("fail to dial: %v", err) 69 } 70 defer conn.Close() 71 client := models.NewGreeterClient(conn) 72 printSayHello(client, *name) 73 printSayManyHello(client, *name) 74} 75