| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package api |
| |
| import ( |
| "context" |
| "fmt" |
| "strings" |
| ) |
| |
| import ( |
| greet "dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto" |
| "dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto/triple_gen/greettriple" |
| triple "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol" |
| ) |
| |
| type GreetTripleServer struct { |
| } |
| |
| func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) { |
| resp := &greet.GreetResponse{Greeting: req.Name} |
| return resp, nil |
| } |
| |
| func (srv *GreetTripleServer) GreetStream(ctx context.Context, stream greettriple.GreetService_GreetStreamServer) error { |
| for { |
| req, err := stream.Recv() |
| if err != nil { |
| if triple.IsEnded(err) { |
| break |
| } |
| return fmt.Errorf("triple BidiStream recv error: %s", err) |
| } |
| if err := stream.Send(&greet.GreetStreamResponse{Greeting: req.Name}); err != nil { |
| return fmt.Errorf("triple BidiStream send error: %s", err) |
| } |
| } |
| return nil |
| } |
| |
| func (srv *GreetTripleServer) GreetClientStream(ctx context.Context, stream greettriple.GreetService_GreetClientStreamServer) (*greet.GreetClientStreamResponse, error) { |
| var reqs []string |
| for stream.Recv() { |
| reqs = append(reqs, stream.Msg().Name) |
| } |
| if stream.Err() != nil && !triple.IsEnded(stream.Err()) { |
| return nil, fmt.Errorf("triple ClientStream recv err: %s", stream.Err()) |
| } |
| resp := &greet.GreetClientStreamResponse{ |
| Greeting: strings.Join(reqs, ","), |
| } |
| |
| return resp, nil |
| } |
| |
| func (srv *GreetTripleServer) GreetServerStream(ctx context.Context, req *greet.GreetServerStreamRequest, stream greettriple.GreetService_GreetServerStreamServer) error { |
| for i := 0; i < 5; i++ { |
| if err := stream.Send(&greet.GreetServerStreamResponse{Greeting: req.Name}); err != nil { |
| return fmt.Errorf("triple ServerStream send err: %s", err) |
| } |
| } |
| return nil |
| } |