Пример #1
0
 private void OnMessage(byte[] bytes, int length)
 {
     try {
         GenericCommand command = GenericCommand.Parser.ParseFrom(bytes, 0, length);
         LCLogger.Debug($"{id} <= {FormatCommand(command)}");
         if (command.HasI)
         {
             // 应答
             int requestIndex = command.I;
             if (requestToResponses.TryGetValue(command, out TaskCompletionSource <GenericCommand> tcs))
             {
                 requestToResponses.Remove(command);
                 if (command.HasErrorMessage)
                 {
                     // 错误
                     ErrorCommand error  = command.ErrorMessage;
                     int          code   = error.Code;
                     string       detail = error.Detail;
                     // 包装成异常抛出
                     LCException exception = new LCException(code, detail);
                     tcs.TrySetException(exception);
                 }
                 else
                 {
                     tcs.TrySetResult(command);
                 }
             }
             else
             {
                 LCLogger.Error($"No request for {requestIndex}");
             }
         }
         else
         {
             if (command.Cmd == CommandType.Echo)
             {
                 // 心跳应答
                 heartBeat.Pong();
             }
             else if (command.Cmd == CommandType.Goaway)
             {
                 // 针对连接的消息
                 Reset();
             }
             else
             {
                 // 通知
                 string peerId = command.HasPeerId ? command.PeerId : defaultClientId;
                 if (idToClients.TryGetValue(peerId, out LCIMClient client))
                 {
                     // 通知具体客户端
                     client.HandleNotification(command);
                 }
             }
         }
     } catch (Exception e) {
         LCLogger.Error(e);
     }
 }
Пример #2
0
 public static void ShowToast(Component component, LCException e)
 {
     component.SendMessageUpwards("ShowToast", $"{e.Code} : {e.Message}", SendMessageOptions.RequireReceiver);
 }
Пример #3
0
 public static void LogException(LCException e)
 {
     Debug.LogError($"{e.Code} : {e.Message}");
 }