/// <summary> /// 设置结果 /// </summary> /// <param name="value">数据</param> /// <param name="serializer">序列化工具</param> private void SetResult(object value, IDynamicJsonSerializer serializer) { try { var result = (T)serializer.Convert(value, typeof(T)); this.taskSource.TrySetResult(result); } catch (SerializerException ex) { this.taskSource.TrySetException(ex); } catch (Exception ex) { this.taskSource.TrySetException(new SerializerException(ex)); } }
/// <summary> /// 设置远程异常 /// </summary> /// <param name="serializer">序列化工具</param> /// <param name="exceptionContext">上下文</param> /// <returns></returns> public static bool SetRemoteException(IDynamicJsonSerializer serializer, ExceptionContext exceptionContext) { try { var packet = exceptionContext.Packet; packet.state = false; packet.body = exceptionContext.Exception.Message; var packetJson = serializer.Serialize(packet); exceptionContext.Session.SendText(packetJson); return(true); } catch (Exception) { return(false); } }
/// <summary> /// 设置行为 /// </summary> /// <param name="setType">行为类型</param> /// <param name="value">数据值</param> /// <param name="serializer">序列化工具</param> public void SetAction(SetTypes setType, object value, IDynamicJsonSerializer serializer) { switch (setType) { case SetTypes.SetReturnReult: this.SetResult(value, serializer); break; case SetTypes.SetReturnException: var remoteException = new RemoteException((string)value); this.taskSource.TrySetException(remoteException); break; case SetTypes.SetTimeoutException: var timeoutException = new TimeoutException(); this.taskSource.TrySetException(timeoutException); break; case SetTypes.SetShutdownException: var shutdownException = new SocketException((int)SocketError.Shutdown); this.taskSource.TrySetException(shutdownException); break; } }
/// <summary> /// 设置远程异常 /// </summary> /// <param name="serializer">序列化工具</param> /// <param name="exceptionContext">上下文</param> /// <returns></returns> public static bool SetRemoteException(IDynamicJsonSerializer serializer, ExceptionContext exceptionContext) { try { var packet = exceptionContext.Packet; packet.state = false; packet.body = exceptionContext.Exception.Message; var packetJson = serializer.Serialize(packet); exceptionContext.Session.SendText(packetJson); return true; } catch (Exception) { return false; } }