예제 #1
0
        private static void CallAsyncResult(Task task, IMessageSource source, UInt64 session)
        {
            switch (task.Status)
            {
            case TaskStatus.Canceled:
            {
                source.Respond(new ExceptionMessage(session, new TaskCanceledException()));
            }
            break;

            case TaskStatus.Faulted:
            {
                source.Respond(new ExceptionMessage(session, task.Exception.InnerException));
            }
            break;

            case TaskStatus.RanToCompletion:
            {
                Type taskType = task.GetType();
                if (taskType.IsGenericType && TypeFormatter.CanSerialize(taskType.GetGenericArguments()[0]))
                {
                    object result = taskType.GetProperty("Result", BindingFlags.Public | BindingFlags.Instance).GetValue(task, null);
                    source.Respond(new ResultMessage(session, result));
                }
                else
                {
                    source.Respond(new ResultMessage(session, null));
                }
            }
            break;
            }
        }
예제 #2
0
 private static void Call(IMessageSource source, IMessage message, Func <object[], object> target, bool extend)
 {
     object[] args; message.TryGetInvocationArgs(out args);
     if (extend)
     {
         args = args.Insert(0, source);
     }
     try
     {
         object result = target(args);
         if (result != null && !TypeFormatter.CanSerialize(result.GetType()))
         {
             result = null;
         }
         source.Respond(new ResultMessage(message, result));
     }
     catch (Exception er)
     {
         try
         {
             source.Respond(new ExceptionMessage(message, er));
         }
         catch (Exception fatal)
         {
             Application.Error(fatal);
         }
     }
 }
예제 #3
0
        public void Serialize(Stream serializationStream, object graph)
        {
            IMessage message = (graph as IMessage);

            serializationStream.EncodeVariableInt(message.Id);

            PropertyManager.ReadLock();
            try
            {
                Dictionary <UInt32, PropertyContainer> .Enumerator iterator = PropertyManager.GetEnumerator();
                if (iterator.MoveNext())
                {
                    MemoryStream components = MemoryPool <MemoryStream> .Get();

                    try
                    {
                        do
                        {
                            object value; if (iterator.Current.Value.TryGet(message.Template, out value) && value != null && TypeFormatter.CanSerialize(value.GetType()))
                            {
                                components.Encode(iterator.Current.Key);
                                TypeFormatter.Serialize(components, value);

                                serializationStream.Encode(components.Length);
                                components.Seek(0, SeekOrigin.Begin);
                                components.CopyTo(serializationStream);
                                components.SetLength(0);
                            }
                        }while (iterator.MoveNext());
                    }
                    finally
                    {
                        MemoryPool <MemoryStream> .Return(components);
                    }
                }
            }
            finally
            {
                PropertyManager.ReadRelease();
            }
            serializationStream.Encode((long)0);
        }