func (kvserver *KvServer) PingStreamCS(stream BfKvService_PingStreamCSServer) error { log.Printf("===PingStreamCS===,clientid=(%s)", getClientId(stream.Context())) pingResp := &BfPingData{Message: message} anyResp, err := ptypes.MarshalAny(pingResp) if err != nil { log.Fatalf("MarshalAny fail,%v", err) return err } for { anyReq, err := stream.Recv() if err == io.EOF { return nil } if err != nil { return err } pingReq := &BfPingData{} if ptypes.Is(anyReq, pingReq) { ptypes.UnmarshalAny(anyReq, pingReq) log.Printf("recv,%s", pingReq.Message) } else { log.Fatalf("PingStreamS,%v", anyReq) return err } if err := stream.Send(anyResp); err != nil { return err } } }
func (kvserver *KvServer) PingStreamS(anyReq *Any, stream BfKvService_PingStreamSServer) error { log.Printf("===PingStreamS===,clientid=(%s)", getClientId(stream.Context())) pingReq := &BfPingData{} if ptypes.Is(anyReq, pingReq) { ptypes.UnmarshalAny(anyReq, pingReq) log.Printf("recv,%s", pingReq.Message) } else { log.Fatalf("PingStreamS,%v", anyReq) return nil } pingResp := &BfPingData{Message: message} anyResp, err := ptypes.MarshalAny(pingResp) if err != nil { log.Fatalf("MarshalAny fail,%v", err) return err } rd := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < 10; i++ { log.Printf("send,%d", i) if err := stream.Send(anyResp); err != nil { return err } s := 500 + rd.Int31n(500) - 1 time.Sleep(time.Duration(s) * time.Millisecond) } return nil }
//===internal api=== func (client *BfTrderClient) DispatchPush(anyResp *Any) { if ptypes.Is(anyResp, tickType_) { tickResp := &BfTickData{} ptypes.UnmarshalAny(anyResp, tickResp) spi_.OnTick(tickResp) } else if ptypes.Is(anyResp, pingType_) { pingResp := &BfPingData{} ptypes.UnmarshalAny(anyResp, pingResp) spi_.OnPing(pingResp) } else if ptypes.Is(anyResp, accountType_) { accountResp := &BfAccountData{} ptypes.UnmarshalAny(anyResp, accountResp) spi_.OnAccount(accountResp) } else if ptypes.Is(anyResp, positionType_) { positionResp := &BfPositionData{} ptypes.UnmarshalAny(anyResp, positionResp) spi_.OnPosition(positionResp) } else if ptypes.Is(anyResp, orderType_) { orderResp := &BfOrderData{} ptypes.UnmarshalAny(anyResp, orderResp) spi_.OnOrder(orderResp) } else if ptypes.Is(anyResp, tradeType_) { tradeResp := &BfTradeData{} ptypes.UnmarshalAny(anyResp, tradeResp) spi_.OnTrade(tradeResp) } else if ptypes.Is(anyResp, logType_) { logResp := &BfLogData{} ptypes.UnmarshalAny(anyResp, logResp) spi_.OnLog(logResp) } else if ptypes.Is(anyResp, errorType_) { errorResp := &BfErrorData{} ptypes.UnmarshalAny(anyResp, errorResp) spi_.OnError(errorResp) } else if ptypes.Is(anyResp, notificationType_) { notificationResp := &BfNotificationData{} ptypes.UnmarshalAny(anyResp, notificationResp) spi_.OnNotification(notificationResp) } else { log.Printf("invalid type message,%v", anyResp) } }