Exemplo n.º 1
0
        /// <summary>
        /// 创建新的SetAction
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="taskSource">任务源</param>
        /// <returns></returns>
        private Action <SetTypes, object> NewSetAction <T>(TaskCompletionSource <T> taskSource)
        {
            Action <SetTypes, object> setAction = (setType, value) =>
            {
                if (setType == SetTypes.SetReturnReult)
                {
                    try
                    {
                        var result = JObject.Cast <T>(value);
                        taskSource.TrySetResult(result);
                    }
                    catch (SerializerException ex)
                    {
                        taskSource.TrySetException(ex);
                    }
                    catch (Exception ex)
                    {
                        taskSource.TrySetException(new SerializerException(ex));
                    }
                }
                else if (setType == SetTypes.SetReturnException)
                {
                    var exception = new RemoteException((string)value);
                    taskSource.TrySetException(exception);
                }
                else if (setType == SetTypes.SetTimeoutException)
                {
                    var exception = new TimeoutException();
                    taskSource.TrySetException(exception);
                }
                else if (setType == SetTypes.SetShutdownException)
                {
                    var exception = new SocketException((int)SocketError.Shutdown);
                    taskSource.TrySetException(exception);
                }
            };

            return(setAction);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 设置行为
        /// </summary>
        /// <param name="setType">行为类型</param>
        /// <param name="value">数据值</param>
        public void SetAction(SetTypes setType, object value)
        {
            switch (setType)
            {
            case SetTypes.SetReturnReult:
                this.SetResult(value);
                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;
            }
        }