private bool Serialize <T>(T msg, IActorMessage toSerialize, ushort version, byte[] messageBytes, byte[] resultBytes) where T : IActorMessage { // <verstion-short> (always Zero for this type of serializedMessage) // 16byte.message.guid // <size.of.type.in.Bytes-ushort> // <bytes.of.type.string-bytearray> // <size.of.message-uint> // <bytes.of.message-bytearray> // <size.of.result-uint> // <bytes.of.result-bytearray> // <status.flags-uint> uint serializedByteCount = IActorMessageSerializationActor.MinSizeOfSerializedMessage; var messageID = Guid.NewGuid().ToByteArray(); byte[] typeBytes = null; uint sizeOfType = 0; IFormatter formatter = new MessageTypeFormatter(); MemoryStream typeMs = new MemoryStream(); formatter.Serialize(typeMs, toSerialize.GetType().AssemblyQualifiedName.ToString()); typeBytes = typeMs.GetBuffer(); typeMs = null; // typeBytes = Encoding.UTF8.GetBytes(toSerialize.GetType().AssemblyQualifiedName); sizeOfType = (uint)typeBytes.Length; uint statusFlags = 0; //TODO: byte[] serializedMsg = CreateSerializationPackage(version, typeBytes, messageID, messageBytes, resultBytes, statusFlags); if (null != msg.Status) { if (!(msg.GetType() == typeof(SerializeMessage <IActorMessage>))) { msg.Status.SetResult(new SerializeMessage <IActorMessage>(toSerialize, serializedMsg)); } else { msg.ProcessingResult = serializedMsg; msg.Status.SetResult(msg); } } else { msg.ProcessingResult = serializedMsg; } return(true); }
private bool OnNonSerlializableMessageRecieved <T>(T msg, byte[] toDeSerialize) where T : IActorMessage { var messageIDBytes = new byte[16]; byte[] msgByteArray = null; byte[] resultByteArray = null; uint sizeOfMessage, sizeOfResult; IFormatter formatter = new MessageTypeFormatter(); IFormatter msgFormatter = new BinaryFormatter(); Type messageType = ParseOutHeader(msg, messageIDBytes, formatter, out msgByteArray, out resultByteArray, out sizeOfMessage, out sizeOfResult); IActorMessage resultMsg = null; if (msg is AnonAskMessage <DeserializeMessage <IActorMessage> > ) { (msg as AnonAskMessage <DeserializeMessage <IActorMessage> >).OriginalMessage.Type = messageType; resultMsg = (msg as AnonAskMessage <DeserializeMessage <IActorMessage> >).OriginalMessage; } else { (msg as DeserializeMessage <IActorMessage>).Type = messageType; resultMsg = msg; } object message = null; if (0 != sizeOfMessage) { message = msgFormatter.Deserialize(new MemoryStream(msgByteArray)); } object result = null; if (0 != sizeOfResult) { result = msgFormatter.Deserialize(new MemoryStream(resultByteArray)); } resultMsg.Message = message; resultMsg.ProcessingResult = result; if (null != msg.Status) { msg.Status.SetResult(resultMsg); } return(true); }
private bool OnSerializableMsgRecieved <T>(T msg, byte[] toDeSerialize) where T : IActorMessage { var messageIDBytes = new byte[16]; byte[] msgByteArray = null; byte[] resultByteArray = null; uint sizeOfMessage, sizeOfResult; IFormatter formatter = new MessageTypeFormatter(); IFormatter msgFormatter = new BinaryFormatter(); Type messageType = ParseOutHeader(msg, messageIDBytes, formatter, out msgByteArray, out resultByteArray, out sizeOfMessage, out sizeOfResult); object msgObject = msgFormatter.Deserialize(new MemoryStream(msgByteArray)); IActorMessage invokedMessage = null; if (!_msgTypeToInstatiation.ContainsKey(messageType)) { if (msgObject is IActorMessage) { invokedMessage = (msgObject as IActorMessage); } else { throw new FailedToRegisterActorInvocationForTypeDeserializationException(messageType.FullName); } } else { ActorMessageInvocationBase invoker = null; if (!_msgTypeToInstatiation.TryGetValue(messageType, out invoker)) { throw new FailedToInvokeMessageTypeException(messageType.FullName); } invokedMessage = invoker.Invoke(msgObject); } if (null != msg.Status) { msg.Status.SetResult(invokedMessage); } msg.ProcessingResult = invokedMessage; return(true); }