2017-06-12 20:19:18 +00:00
|
|
|
using System;
|
2017-06-13 07:03:01 +00:00
|
|
|
using System.Runtime.Serialization;
|
2017-06-13 03:31:47 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Converters;
|
2017-06-12 20:19:18 +00:00
|
|
|
|
|
|
|
namespace Ooui
|
|
|
|
{
|
|
|
|
public class Message
|
|
|
|
{
|
2017-06-13 07:03:01 +00:00
|
|
|
[JsonProperty("m")]
|
2017-06-12 20:19:18 +00:00
|
|
|
public MessageType MessageType = MessageType.Nop;
|
2017-06-13 03:31:47 +00:00
|
|
|
|
|
|
|
[JsonProperty("id")]
|
2017-06-12 20:19:18 +00:00
|
|
|
public string TargetId = "";
|
2017-06-13 03:31:47 +00:00
|
|
|
|
|
|
|
[JsonProperty("k")]
|
|
|
|
public string Key = "";
|
|
|
|
|
2017-06-19 06:42:13 +00:00
|
|
|
[JsonProperty("v", NullValueHandling = NullValueHandling.Ignore)]
|
2017-06-15 09:39:19 +00:00
|
|
|
public object Value = null;
|
2017-06-13 07:51:24 +00:00
|
|
|
|
2017-06-19 07:08:33 +00:00
|
|
|
[JsonProperty("rid", NullValueHandling = NullValueHandling.Ignore)]
|
|
|
|
public string ResultId = null;
|
|
|
|
|
2017-06-16 04:27:15 +00:00
|
|
|
public static Message Call (string targetId, string method, params object[] args) => new Message {
|
|
|
|
MessageType = MessageType.Call,
|
|
|
|
TargetId = targetId,
|
|
|
|
Key = method,
|
|
|
|
Value = args,
|
|
|
|
};
|
|
|
|
|
2018-04-16 05:55:23 +00:00
|
|
|
public static Message Set (string targetId, string property, object value) => new Message {
|
|
|
|
MessageType = MessageType.Set,
|
|
|
|
TargetId = targetId,
|
|
|
|
Key = property,
|
|
|
|
Value = value,
|
|
|
|
};
|
|
|
|
|
2017-06-24 20:34:47 +00:00
|
|
|
public static Message Event (string targetId, string eventType, object value = null) => new Message {
|
2017-06-15 06:10:58 +00:00
|
|
|
MessageType = MessageType.Event,
|
|
|
|
TargetId = targetId,
|
|
|
|
Key = eventType,
|
2017-06-24 20:34:47 +00:00
|
|
|
Value = value,
|
2017-06-15 06:10:58 +00:00
|
|
|
};
|
2018-03-03 07:44:28 +00:00
|
|
|
|
2018-04-16 05:55:23 +00:00
|
|
|
public class PropertyReference
|
|
|
|
{
|
|
|
|
[JsonProperty ("id")]
|
|
|
|
public string TargetId = "";
|
|
|
|
|
|
|
|
[JsonProperty ("k")]
|
|
|
|
public string Key = "";
|
|
|
|
}
|
|
|
|
|
2018-03-03 07:44:28 +00:00
|
|
|
public void WriteJson (System.IO.TextWriter w)
|
|
|
|
{
|
|
|
|
w.Write ('{');
|
|
|
|
switch (MessageType) {
|
|
|
|
case MessageType.Call: w.Write ("\"m\":\"call\",\"id\":\""); break;
|
|
|
|
case MessageType.Create: w.Write ("\"m\":\"create\",\"id\":\""); break;
|
|
|
|
case MessageType.Event: w.Write ("\"m\":\"event\",\"id\":\""); break;
|
|
|
|
case MessageType.Listen: w.Write ("\"m\":\"listen\",\"id\":\""); break;
|
|
|
|
case MessageType.Nop: w.Write ("\"m\":\"nop\",\"id\":\""); break;
|
|
|
|
case MessageType.RemoveAttribute: w.Write ("\"m\":\"remAttr\",\"id\":\""); break;
|
|
|
|
case MessageType.Set: w.Write ("\"m\":\"set\",\"id\":\""); break;
|
|
|
|
case MessageType.SetAttribute: w.Write ("\"m\":\"setAttr\",\"id\":\""); break;
|
|
|
|
}
|
|
|
|
w.Write (TargetId);
|
|
|
|
w.Write ("\",\"k\":\"");
|
|
|
|
w.Write (Key);
|
2018-03-23 23:13:10 +00:00
|
|
|
w.Write ('\"');
|
2018-03-03 07:44:28 +00:00
|
|
|
if (Value != null) {
|
2018-03-23 23:13:10 +00:00
|
|
|
w.Write (",\"v\":");
|
2018-03-08 21:28:28 +00:00
|
|
|
JsonConvert.WriteJsonValue (w, Value);
|
2018-03-03 07:44:28 +00:00
|
|
|
}
|
2018-03-23 23:13:10 +00:00
|
|
|
if (ResultId != null) {
|
|
|
|
w.Write (",\"rid\":");
|
|
|
|
JsonConvert.WriteJsonString (w, ResultId);
|
2018-03-03 07:44:28 +00:00
|
|
|
}
|
2018-03-23 23:13:10 +00:00
|
|
|
w.Write ('}');
|
2018-03-03 07:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public string ToJson ()
|
|
|
|
{
|
|
|
|
using (var sw = new System.IO.StringWriter ()) {
|
|
|
|
WriteJson (sw);
|
|
|
|
return sw.ToString ();
|
|
|
|
}
|
|
|
|
}
|
2018-03-08 21:28:28 +00:00
|
|
|
|
|
|
|
public static Message FromJson (string json)
|
|
|
|
{
|
|
|
|
var m = new Message ();
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
var e = json.Length;
|
|
|
|
|
|
|
|
while (i < e) {
|
|
|
|
while (i < e && (json[i]==',' || json[i]=='{' || char.IsWhiteSpace (json[i])))
|
|
|
|
i++;
|
|
|
|
if (i >= e)
|
|
|
|
throw new Exception ("JSON Unexpected end");
|
|
|
|
var n = e - i;
|
|
|
|
if (json[i] == '}')
|
|
|
|
break;
|
|
|
|
if (n > 4 && json[i] == '\"' && json[i+2] == '\"' && json[i+3] == ':') {
|
|
|
|
switch (json[i + 1]) {
|
|
|
|
case 'm':
|
|
|
|
if (json[i + 4] == '\"' && json[i + 5] == 'e') {
|
|
|
|
m.MessageType = MessageType.Event;
|
|
|
|
}
|
|
|
|
i += 5;
|
|
|
|
while (i < e && json[i] != '\"') i++;
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
case 'k': {
|
|
|
|
i += 5;
|
|
|
|
var se = i;
|
|
|
|
while (se < e && json[se] != '\"')
|
|
|
|
se++;
|
|
|
|
m.Key = json.Substring (i, se - i);
|
|
|
|
i = se + 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
m.Value = JsonConvert.ReadJsonValue (json, i + 4);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (n > 5 && json[i] == '\"' && json[i + 3] == '\"' && json[i + 4] == ':' && json[i+5] == '\"') {
|
|
|
|
switch (json[i + 1]) {
|
|
|
|
case 'i': {
|
|
|
|
i += 6;
|
|
|
|
var se = i;
|
|
|
|
while (se < e && json[se] != '\"')
|
|
|
|
se++;
|
|
|
|
m.TargetId = json.Substring (i, se - i);
|
|
|
|
i = se + 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Exception ("JSON Expected property");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString ()
|
|
|
|
{
|
|
|
|
return ToJson ();
|
|
|
|
}
|
2017-06-12 20:19:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 07:03:01 +00:00
|
|
|
[JsonConverter (typeof (StringEnumConverter))]
|
2017-06-12 20:19:18 +00:00
|
|
|
public enum MessageType
|
|
|
|
{
|
2017-06-13 07:03:01 +00:00
|
|
|
[EnumMember(Value = "nop")]
|
2017-06-12 20:19:18 +00:00
|
|
|
Nop,
|
2017-06-13 07:03:01 +00:00
|
|
|
[EnumMember(Value = "create")]
|
2017-06-12 20:19:18 +00:00
|
|
|
Create,
|
2017-06-13 07:03:01 +00:00
|
|
|
[EnumMember(Value = "set")]
|
2017-06-12 20:45:27 +00:00
|
|
|
Set,
|
2017-11-26 17:28:06 +00:00
|
|
|
[EnumMember (Value = "setAttr")]
|
|
|
|
SetAttribute,
|
2018-02-02 02:43:23 +00:00
|
|
|
[EnumMember(Value = "remAttr")]
|
|
|
|
RemoveAttribute,
|
2017-06-13 07:03:01 +00:00
|
|
|
[EnumMember(Value = "call")]
|
2017-06-12 20:45:27 +00:00
|
|
|
Call,
|
2017-06-15 06:10:58 +00:00
|
|
|
[EnumMember(Value = "listen")]
|
|
|
|
Listen,
|
|
|
|
[EnumMember(Value = "event")]
|
|
|
|
Event,
|
2017-06-12 20:19:18 +00:00
|
|
|
}
|
|
|
|
}
|