예제 #1
0
        /// <summary>
        /// Convert model to GeneralMessage.
        /// </summary>
        /// <param name="model">The model to convert.</param>
        /// <returns>The converted result in type of GeneralMessage.</returns>
        public GeneralMessage ToMessage(object model)
        {
            Type   modelType = model.GetType();
            string typeName  = null;

            if (!_mapper.TryGetRegisterName(modelType, out typeName))
            {
                //typeName = modelType.FullName
                throw new Exception($"The message type: {modelType.Name} has not registered.");
            }
            GeneralMessage message = new GeneralMessage()
            {
                MessageType = typeName,
                Body        = _serializer.Serialize(model)
            };

            return(message);
        }
예제 #2
0
        /// <summary>
        /// Deserialize byte array into GeneralMessage.
        /// </summary>
        /// <param name="messageData">The message data to deserialize.</param>
        /// <param name="offset">The offset of the message data.</param>
        /// <param name="count">The count of the message data to deserialize.</param>
        /// <param name="isDetach">True if detach the outside data; false to use outside buffer.</param>
        /// <returns>The instance of GeneralMessage class.</returns>
        public static GeneralMessage Deserialize(byte[] messageData, int offset, int count, bool isDetach = true)
        {
            BinaryReader reader = null;

            try
            {
                MemoryStream ms = new MemoryStream(messageData, offset, count);
                reader = new BinaryReader(ms, Encoding.UTF8);
                GeneralMessage msg = new GeneralMessage();
                msg.Header      = reader.ReadInt32();
                msg.MessageType = reader.ReadString();
                msg.Body        = reader.ReadString();
                if (ms.Position < count)
                {
                    int payloadLen    = reader.ReadInt32();
                    int remainDataLen = messageData.Length - (int)ms.Position - offset;
                    if (remainDataLen < payloadLen)
                    {
                        return(null);
                    }
                    if (isDetach)
                    {
                        msg._innerBuffer = reader.ReadBytes(payloadLen);
                        msg.Payload      = new ArraySegment <byte>(msg._innerBuffer);
                    }
                    else
                    {
                        msg.Payload = new ArraySegment <byte>(messageData, offset + (int)ms.Position, payloadLen);
                    }
                }
                return(msg);
            }
            catch
            {
                return(null);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
예제 #3
0
        protected override bool ReceivedMessageFilter(TcpRawMessageReceivedEventArgs tcpRawMessageArgs)
        {
            GeneralMessage serverMessage = GeneralMessage.Deserialize(tcpRawMessageArgs.Message.MessageRawData, false);

            if (serverMessage == null)
            {
                return(false);
            }
            //if the server message is model
            if (serverMessage.Header == _modelHeaderIndicator)
            {
                //get model from general message
                object model = TcpModelConverter.Default.ToModel(serverMessage);
                //if model is response message
                if (model is ResponseMessageBase)
                {
                    _reqResponseAgent.SetRequestResult(model as ResponseMessageBase);
                    return(false);
                }
            }
            return(false);
        }
예제 #4
0
 /// <summary>
 /// Convert "GeneralMessage" into model.
 /// </summary>
 /// <param name="generalMessage">An instance int the type of GeneralMessage.</param>
 /// <returns>The converted model.</returns>
 public object ConvertToModel(GeneralMessage generalMessage)
 {
     return(TcpModelConverter.Default.ToModel(generalMessage));
 }
예제 #5
0
 /// <summary>
 /// Convert message to model in specific type.
 /// </summary>
 /// <typeparam name="T">The model type to convert.</typeparam>
 /// <param name="message">An instance in type of GeneralMessage.</param>
 /// <returns>The converted model in specific type.</returns>
 public T ToModel <T>(GeneralMessage message) where T : ModelMessageBase
 {
     return((T)ToModel(message));
 }
예제 #6
0
 /// <summary>
 /// Convert "GeneralMessage" into model.
 /// </summary>
 /// <param name="generalMessage">An instance int the type of GeneralMessage.</param>
 /// <returns>The converted model.</returns>
 public object ConvertToModel(GeneralMessage generalMessage)
 {
     return(_modelConvert.ToModel(generalMessage));
 }