udpate the examples
This commit is contained in:
parent
3b295b16e7
commit
310a84d059
@ -21,14 +21,14 @@ func call(i int) {
|
|||||||
fmt.Println("Call:", i, "rsp:", rsp.Msg)
|
fmt.Println("Call:", i, "rsp:", rsp.Msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stream() {
|
func stream(i int) {
|
||||||
stream, err := cl.Stream(context.Background(), &example.StreamingRequest{Count: int64(10)})
|
stream, err := cl.Stream(context.Background(), &example.StreamingRequest{Count: int64(i)})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("err:", err)
|
fmt.Println("err:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for i := 0; i < 10; i++ {
|
for j := 0; j < i; j++ {
|
||||||
rsp, err := stream.Next()
|
rsp, err := stream.RecvR()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("err:", err)
|
fmt.Println("err:", err)
|
||||||
break
|
break
|
||||||
@ -44,6 +44,34 @@ func stream() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func pingPong(i int) {
|
||||||
|
stream, err := cl.PingPong(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("err:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for j := 0; j < i; j++ {
|
||||||
|
if err := stream.SendR(&example.Ping{Stroke: int64(j)}); err != nil {
|
||||||
|
fmt.Println("err:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rsp, err := stream.RecvR()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("recv err", err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
fmt.Printf("Sent ping %v got pong %v\n", j, rsp.Stroke)
|
||||||
|
}
|
||||||
|
if stream.Error() != nil {
|
||||||
|
fmt.Println("stream err:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := stream.Close(); err != nil {
|
||||||
|
fmt.Println("stream close err:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd.Init()
|
cmd.Init()
|
||||||
|
|
||||||
@ -51,6 +79,10 @@ func main() {
|
|||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
call(i)
|
call(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("\n--- Streamer example ---\n")
|
fmt.Println("\n--- Streamer example ---\n")
|
||||||
stream()
|
stream(10)
|
||||||
|
|
||||||
|
fmt.Println("\n--- Ping Pong example ---\n")
|
||||||
|
pingPong(10)
|
||||||
}
|
}
|
||||||
|
@ -125,17 +125,17 @@ func pingPong(i int) {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd.Init()
|
cmd.Init()
|
||||||
// fmt.Println("\n--- Call example ---\n")
|
fmt.Println("\n--- Call example ---\n")
|
||||||
// for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
// call(i)
|
call(i)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// fmt.Println("\n--- Streamer example ---\n")
|
fmt.Println("\n--- Streamer example ---\n")
|
||||||
// stream(10)
|
stream(10)
|
||||||
|
|
||||||
fmt.Println("\n--- Ping Pong example ---\n")
|
fmt.Println("\n--- Ping Pong example ---\n")
|
||||||
pingPong(10)
|
pingPong(10)
|
||||||
|
|
||||||
// fmt.Println("\n--- Publisher example ---\n")
|
fmt.Println("\n--- Publisher example ---\n")
|
||||||
// pub()
|
pub()
|
||||||
}
|
}
|
||||||
|
@ -117,7 +117,7 @@ var _ server.Option
|
|||||||
type ExampleClient interface {
|
type ExampleClient interface {
|
||||||
Call(ctx context.Context, in *Request, opts ...client.CallOption) (*Response, error)
|
Call(ctx context.Context, in *Request, opts ...client.CallOption) (*Response, error)
|
||||||
Stream(ctx context.Context, in *StreamingRequest, opts ...client.CallOption) (Example_StreamClient, error)
|
Stream(ctx context.Context, in *StreamingRequest, opts ...client.CallOption) (Example_StreamClient, error)
|
||||||
PingPong(ctx context.Context, in *Ping, opts ...client.CallOption) (Example_PingPongClient, error)
|
PingPong(ctx context.Context, opts ...client.CallOption) (Example_PingPongClient, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type exampleClient struct {
|
type exampleClient struct {
|
||||||
@ -149,16 +149,19 @@ func (c *exampleClient) Call(ctx context.Context, in *Request, opts ...client.Ca
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *exampleClient) Stream(ctx context.Context, in *StreamingRequest, opts ...client.CallOption) (Example_StreamClient, error) {
|
func (c *exampleClient) Stream(ctx context.Context, in *StreamingRequest, opts ...client.CallOption) (Example_StreamClient, error) {
|
||||||
req := c.c.NewRequest(c.serviceName, "Example.Stream", in)
|
req := c.c.NewRequest(c.serviceName, "Example.Stream", &StreamingRequest{})
|
||||||
stream, err := c.c.Stream(ctx, req, opts...)
|
stream, err := c.c.Stream(ctx, req, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := stream.Send(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &exampleStreamClient{stream}, nil
|
return &exampleStreamClient{stream}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Example_StreamClient interface {
|
type Example_StreamClient interface {
|
||||||
RecvMsg() (*StreamingResponse, error)
|
RecvR() (*StreamingResponse, error)
|
||||||
client.Streamer
|
client.Streamer
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,7 +169,7 @@ type exampleStreamClient struct {
|
|||||||
client.Streamer
|
client.Streamer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *exampleStreamClient) RecvMsg() (*StreamingResponse, error) {
|
func (x *exampleStreamClient) RecvR() (*StreamingResponse, error) {
|
||||||
m := new(StreamingResponse)
|
m := new(StreamingResponse)
|
||||||
err := x.Recv(m)
|
err := x.Recv(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -175,8 +178,8 @@ func (x *exampleStreamClient) RecvMsg() (*StreamingResponse, error) {
|
|||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *exampleClient) PingPong(ctx context.Context, in *Ping, opts ...client.CallOption) (Example_PingPongClient, error) {
|
func (c *exampleClient) PingPong(ctx context.Context, opts ...client.CallOption) (Example_PingPongClient, error) {
|
||||||
req := c.c.NewRequest(c.serviceName, "Example.PingPong", in)
|
req := c.c.NewRequest(c.serviceName, "Example.PingPong", &Ping{})
|
||||||
stream, err := c.c.Stream(ctx, req, opts...)
|
stream, err := c.c.Stream(ctx, req, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -185,8 +188,8 @@ func (c *exampleClient) PingPong(ctx context.Context, in *Ping, opts ...client.C
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Example_PingPongClient interface {
|
type Example_PingPongClient interface {
|
||||||
SendMsg(*Ping) error
|
SendR(*Ping) error
|
||||||
RecvMsg() (*Pong, error)
|
RecvR() (*Pong, error)
|
||||||
client.Streamer
|
client.Streamer
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,11 +197,11 @@ type examplePingPongClient struct {
|
|||||||
client.Streamer
|
client.Streamer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *examplePingPongClient) SendMsg(m *Ping) error {
|
func (x *examplePingPongClient) SendR(m *Ping) error {
|
||||||
return x.Send(m)
|
return x.Send(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *examplePingPongClient) RecvMsg() (*Pong, error) {
|
func (x *examplePingPongClient) RecvR() (*Pong, error) {
|
||||||
m := new(Pong)
|
m := new(Pong)
|
||||||
err := x.Recv(m)
|
err := x.Recv(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -211,12 +214,67 @@ func (x *examplePingPongClient) RecvMsg() (*Pong, error) {
|
|||||||
|
|
||||||
type ExampleHandler interface {
|
type ExampleHandler interface {
|
||||||
Call(context.Context, *Request, *Response) error
|
Call(context.Context, *Request, *Response) error
|
||||||
Stream(context.Context, func(*StreamingResponse) error) error
|
Stream(context.Context, *StreamingRequest, Example_StreamStream) error
|
||||||
PingPong(context.Context, func(*Pong) error) error
|
PingPong(context.Context, Example_PingPongStream) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterExampleHandler(s server.Server, hdlr ExampleHandler) {
|
func RegisterExampleHandler(s server.Server, hdlr ExampleHandler) {
|
||||||
s.Handle(s.NewHandler(hdlr))
|
s.Handle(s.NewHandler(&exampleHandler{hdlr}))
|
||||||
|
}
|
||||||
|
|
||||||
|
type exampleHandler struct {
|
||||||
|
ExampleHandler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *exampleHandler) Call(ctx context.Context, in *Request, out *Response) error {
|
||||||
|
return h.ExampleHandler.Call(ctx, in, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *exampleHandler) Stream(ctx context.Context, stream server.Streamer) error {
|
||||||
|
m := new(StreamingRequest)
|
||||||
|
if err := stream.Recv(m); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return h.ExampleHandler.Stream(ctx, m, &exampleStreamStream{stream})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Example_StreamStream interface {
|
||||||
|
SendR(*StreamingResponse) error
|
||||||
|
server.Streamer
|
||||||
|
}
|
||||||
|
|
||||||
|
type exampleStreamStream struct {
|
||||||
|
server.Streamer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *exampleStreamStream) SendR(m *StreamingResponse) error {
|
||||||
|
return x.Streamer.Send(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *exampleHandler) PingPong(ctx context.Context, stream server.Streamer) error {
|
||||||
|
return h.ExampleHandler.PingPong(ctx, &examplePingPongStream{stream})
|
||||||
|
}
|
||||||
|
|
||||||
|
type Example_PingPongStream interface {
|
||||||
|
SendR(*Pong) error
|
||||||
|
RecvR() (*Ping, error)
|
||||||
|
server.Streamer
|
||||||
|
}
|
||||||
|
|
||||||
|
type examplePingPongStream struct {
|
||||||
|
server.Streamer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *examplePingPongStream) SendR(m *Pong) error {
|
||||||
|
return x.Streamer.Send(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *examplePingPongStream) RecvR() (*Ping, error) {
|
||||||
|
m := new(Ping)
|
||||||
|
if err := x.Streamer.Recv(m); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var fileDescriptor0 = []byte{
|
var fileDescriptor0 = []byte{
|
||||||
|
Loading…
Reference in New Issue
Block a user