Ejemplo n.º 1
0
        /// <summary>
        /// Uses default TwinoModelWriter and WebSocketWriter classes.
        /// Creates websocket protocol byte array data of serialized model string.
        /// </summary>
        public static async Task <byte[]> ToWebSocketUTF8DataAsync(this ISerializableModel model)
        {
            WebSocketMessage message = new WebSocketMessage
            {
                OpCode  = SocketOpCode.UTF8,
                Content = new MemoryStream(Encoding.UTF8.GetBytes(_twriter.Serialize(model)))
            };

            return(await _writer.Create(message));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Uses default TwinoModelWriter and WebSocketWriter classes.
        /// Creates websocket protocol byte array data of serialized model string.
        /// </summary>
        public static byte[] ToWebSocketUTF8Data(this ISerializableModel model)
        {
            WebSocketMessage message = new WebSocketMessage
            {
                OpCode  = SocketOpCode.UTF8,
                Content = new MemoryStream(Encoding.UTF8.GetBytes(_twriter.Serialize(model)))
            };

            return(_writer.Create(message).Result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates serialized string message from T model
        /// </summary>
        public string Serialize(ISerializableModel model)
        {
            string body;

            if (model is IPerformanceCriticalModel critical)
            {
                LightJsonWriter writer = new LightJsonWriter();
                body = writer.Serialize(critical);
            }
            else
            {
                body = JsonConvert.SerializeObject(model);
            }

            return($"[{model.Type},{body}]");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Reads the string message and if any method is subscribed the model event with On method, they will be called.
        /// </summary>
        public virtual void Read(SocketBase client, string message)
        {
            int type = Reader.ReadType(message);

            if (!_descriptors.ContainsKey(type))
            {
                return;
            }

            PackageDescriptor descriptor = _descriptors[type];

            ISerializableModel model = Reader.Read(descriptor.Type, message, true);

            if (model == null)
            {
                return;
            }

            foreach (var action in descriptor.Actions)
            {
                action.DynamicInvoke(client, model);
            }
        }
Ejemplo n.º 5
0
 public byte[] Prepare(ISerializableModel model)
 {
     return(_writer.Create(Serialize(model)).Result);
 }
Ejemplo n.º 6
0
 public string Serialize(ISerializableModel model)
 {
     return(model.Type + "=" + Newtonsoft.Json.JsonConvert.SerializeObject(model));
 }