DefaultJsonParser.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. #if !BESTHTTP_DISABLE_SOCKETIO
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using BestHTTP.PlatformSupport.Memory;
  7. using BestHTTP.SocketIO3.Events;
  8. namespace BestHTTP.SocketIO3.Parsers
  9. {
  10. public sealed class Placeholder
  11. {
  12. public bool _placeholder;
  13. public int num;
  14. }
  15. [PlatformSupport.IL2CPP.Il2CppEagerStaticClassConstructionAttribute]
  16. public sealed class DefaultJsonParser : IParser
  17. {
  18. static DefaultJsonParser()
  19. {
  20. BestHTTP.JSON.LitJson.JsonMapper.RegisterImporter<string, byte[]>(str => Convert.FromBase64String(str));
  21. }
  22. private IncomingPacket PacketWithAttachment = IncomingPacket.Empty;
  23. private int ToInt(char ch)
  24. {
  25. int charValue = Convert.ToInt32(ch);
  26. int num = charValue - '0';
  27. if (num < 0 || num > 9)
  28. return -1;
  29. return num;
  30. }
  31. public IncomingPacket Parse(SocketManager manager, string from)
  32. {
  33. int idx = 0;
  34. var transportEvent = (TransportEventTypes)ToInt(from[idx++]);
  35. var socketIOEvent = SocketIOEventTypes.Unknown;
  36. var nsp = string.Empty;
  37. var id = -1;
  38. var payload = string.Empty;
  39. int attachments = 0;
  40. if (from.Length > idx && ToInt(from[idx]) >= 0)
  41. socketIOEvent = (SocketIOEventTypes)ToInt(from[idx++]);
  42. else
  43. socketIOEvent = SocketIOEventTypes.Unknown;
  44. // Parse Attachment
  45. if (socketIOEvent == SocketIOEventTypes.BinaryEvent || socketIOEvent == SocketIOEventTypes.BinaryAck)
  46. {
  47. int endIdx = from.IndexOf('-', idx);
  48. if (endIdx == -1)
  49. endIdx = from.Length;
  50. int.TryParse(from.Substring(idx, endIdx - idx), out attachments);
  51. idx = endIdx + 1;
  52. }
  53. // Parse Namespace
  54. if (from.Length > idx && from[idx] == '/')
  55. {
  56. int endIdx = from.IndexOf(',', idx);
  57. if (endIdx == -1)
  58. endIdx = from.Length;
  59. nsp = from.Substring(idx, endIdx - idx);
  60. idx = endIdx + 1;
  61. }
  62. else
  63. nsp = "/";
  64. // Parse Id
  65. if (from.Length > idx && ToInt(from[idx]) >= 0)
  66. {
  67. int startIdx = idx++;
  68. while (from.Length > idx && ToInt(from[idx]) >= 0)
  69. idx++;
  70. int.TryParse(from.Substring(startIdx, idx - startIdx), out id);
  71. }
  72. // What left is the payload data
  73. if (from.Length > idx)
  74. payload = from.Substring(idx);
  75. else
  76. payload = string.Empty;
  77. var packet = new IncomingPacket(transportEvent, socketIOEvent, nsp, id);
  78. packet.AttachementCount = attachments;
  79. string eventName = packet.EventName;
  80. object[] args = null;
  81. switch (socketIOEvent)
  82. {
  83. case SocketIOEventTypes.Unknown:
  84. packet.DecodedArg = payload;
  85. break;
  86. case SocketIOEventTypes.Connect:
  87. // No Data | Object
  88. if (!string.IsNullOrEmpty(payload))
  89. (eventName, args) = ReadData(manager, packet, payload);
  90. break;
  91. case SocketIOEventTypes.Disconnect:
  92. // No Data
  93. break;
  94. case SocketIOEventTypes.Error:
  95. // String | Object
  96. (eventName, args) = ReadData(manager, packet, payload);
  97. break;
  98. default:
  99. // Array
  100. (eventName, args) = ReadData(manager, packet, payload);
  101. // Save payload until all attachments arrive
  102. if (packet.AttachementCount > 0)
  103. packet.DecodedArg = payload;
  104. break;
  105. }
  106. packet.EventName = eventName;
  107. if (args != null)
  108. {
  109. if (args.Length == 1)
  110. packet.DecodedArg = args[0];
  111. else
  112. packet.DecodedArgs = args;
  113. }
  114. if (packet.AttachementCount > 0)
  115. {
  116. PacketWithAttachment = packet;
  117. return IncomingPacket.Empty;
  118. }
  119. return packet;
  120. }
  121. public IncomingPacket MergeAttachements(SocketManager manager, IncomingPacket packet)
  122. {
  123. string payload = packet.DecodedArg as string;
  124. packet.DecodedArg = null;
  125. string placeholderFormat = "{{\"_placeholder\":true,\"num\":{0}}}";
  126. for (int i = 0; i < packet.Attachements.Count; ++i)
  127. {
  128. string placeholder = string.Format(placeholderFormat, i);
  129. BufferSegment data = packet.Attachements[i];
  130. payload = payload.Replace(placeholder, "\"" + Convert.ToBase64String(data.Data, data.Offset, data.Count) + "\"");
  131. }
  132. (string eventName, object[] args) = ReadData(manager, packet, payload);
  133. packet.EventName = eventName;
  134. if (args != null)
  135. {
  136. if (args.Length == 1)
  137. packet.DecodedArg = args[0];
  138. else
  139. packet.DecodedArgs = args;
  140. }
  141. return packet;
  142. }
  143. private (string, object[]) ReadData(SocketManager manager, IncomingPacket packet, string payload)
  144. {
  145. Socket socket = manager.GetSocket(packet.Namespace);
  146. string eventName = packet.EventName;
  147. Subscription subscription = socket.GetSubscription(eventName);
  148. object[] args = null;
  149. switch (packet.SocketIOEvent)
  150. {
  151. case SocketIOEventTypes.Unknown:
  152. // TODO: Error?
  153. break;
  154. case SocketIOEventTypes.Connect:
  155. // No Data | Object
  156. using (var strReader = new System.IO.StringReader(payload))
  157. args = ReadParameters(socket, subscription, strReader);
  158. break;
  159. case SocketIOEventTypes.Disconnect:
  160. // No Data
  161. break;
  162. case SocketIOEventTypes.Error:
  163. // String | Object
  164. switch (payload[0])
  165. {
  166. case '{':
  167. using (var strReader = new System.IO.StringReader(payload))
  168. args = ReadParameters(socket, subscription, strReader);
  169. break;
  170. default:
  171. args = new object[] { new Error(payload) };
  172. break;
  173. }
  174. break;
  175. case SocketIOEventTypes.Ack:
  176. eventName = IncomingPacket.GenerateAcknowledgementNameFromId(packet.Id);
  177. subscription = socket.GetSubscription(eventName);
  178. args = ReadParameters(socket, subscription, JSON.LitJson.JsonMapper.ToObject<List<object>>(payload), 0);
  179. break;
  180. default:
  181. // Array
  182. List<object> array = null;
  183. using (var reader = new System.IO.StringReader(payload))
  184. array = JSON.LitJson.JsonMapper.ToObject<List<object>>(new JSON.LitJson.JsonReader(reader));
  185. if (array.Count > 0)
  186. {
  187. eventName = array[0].ToString();
  188. subscription = socket.GetSubscription(eventName);
  189. }
  190. if (packet.AttachementCount == 0 || packet.Attachements != null)
  191. {
  192. try
  193. {
  194. args = ReadParameters(socket, subscription, array, 1);
  195. }
  196. catch(Exception ex)
  197. {
  198. HTTPManager.Logger.Exception("DefaultJsonParser", string.Format("ReadParameters with eventName: {0}", eventName), ex);
  199. }
  200. }
  201. break;
  202. }
  203. return (eventName, args);
  204. }
  205. private object[] ReadParameters(Socket socket, Subscription subscription, List<object> array, int startIdx)
  206. {
  207. object[] args = null;
  208. if (array.Count > startIdx)
  209. {
  210. var desc = subscription != null ? subscription.callbacks.FirstOrDefault() : default(CallbackDescriptor);
  211. int paramCount = desc.ParamTypes != null ? desc.ParamTypes.Length : 0;
  212. int arrayIdx = startIdx;
  213. if (paramCount > 0)
  214. {
  215. args = new object[paramCount];
  216. for (int i = 0; i < desc.ParamTypes.Length; ++i)
  217. {
  218. Type type = desc.ParamTypes[i];
  219. if (type == typeof(Socket))
  220. args[i] = socket;
  221. else if (type == typeof(SocketManager))
  222. args[i] = socket.Manager;
  223. else if (type == typeof(Placeholder))
  224. args[i] = new Placeholder();
  225. else
  226. args[i] = ConvertTo(desc.ParamTypes[i], array[arrayIdx++]);
  227. }
  228. }
  229. }
  230. return args;
  231. }
  232. public object ConvertTo(Type toType, object obj)
  233. {
  234. if (obj == null)
  235. return null;
  236. #if NETFX_CORE
  237. TypeInfo objType = obj.GetType().GetTypeInfo();
  238. #else
  239. Type objType = obj.GetType();
  240. #endif
  241. #if NETFX_CORE
  242. TypeInfo typeInfo = toType.GetTypeInfo();
  243. #endif
  244. #if NETFX_CORE
  245. if (typeInfo.IsEnum)
  246. #else
  247. if (toType.IsEnum)
  248. #endif
  249. return Enum.Parse(toType, obj.ToString(), true);
  250. #if NETFX_CORE
  251. if (typeInfo.IsPrimitive)
  252. #else
  253. if (toType.IsPrimitive)
  254. #endif
  255. return Convert.ChangeType(obj, toType);
  256. if (toType == typeof(string))
  257. return obj.ToString();
  258. #if NETFX_CORE
  259. if (typeInfo.IsGenericType && toType.Name == "Nullable`1")
  260. return Convert.ChangeType(obj, toType.GenericTypeArguments[0]);
  261. #else
  262. if (toType.IsGenericType && toType.Name == "Nullable`1")
  263. return Convert.ChangeType(obj, toType.GetGenericArguments()[0]);
  264. #endif
  265. #if NETFX_CORE
  266. if (objType.Equals(typeInfo))
  267. #else
  268. if (objType.Equals(toType))
  269. #endif
  270. return obj;
  271. if (toType == typeof(byte[]) && objType == typeof(string))
  272. return Convert.FromBase64String(obj.ToString());
  273. return JSON.LitJson.JsonMapper.ToObject(toType, JSON.LitJson.JsonMapper.ToJson(obj));
  274. }
  275. private object[] ReadParameters(Socket socket, Subscription subscription, System.IO.TextReader reader)
  276. {
  277. var desc = subscription != null ? subscription.callbacks.FirstOrDefault() : default(CallbackDescriptor);
  278. int paramCount = desc.ParamTypes != null ? desc.ParamTypes.Length : 0;
  279. object[] args = null;
  280. if (paramCount > 0)
  281. {
  282. args = new object[paramCount];
  283. for (int i = 0; i < desc.ParamTypes.Length; ++i)
  284. {
  285. Type type = desc.ParamTypes[i];
  286. if (type == typeof(Socket))
  287. args[i] = socket;
  288. else if (type == typeof(SocketManager))
  289. args[i] = socket.Manager;
  290. else {
  291. BestHTTP.JSON.LitJson.JsonReader jr = new JSON.LitJson.JsonReader(reader);
  292. args[i] = JSON.LitJson.JsonMapper.ToObject(desc.ParamTypes[i], jr);
  293. reader.Read();
  294. }
  295. }
  296. }
  297. return args;
  298. }
  299. public IncomingPacket Parse(SocketManager manager, BufferSegment data, TransportEventTypes transportEvent = TransportEventTypes.Unknown)
  300. {
  301. IncomingPacket packet = IncomingPacket.Empty;
  302. if (PacketWithAttachment.Attachements == null)
  303. PacketWithAttachment.Attachements = new List<BufferSegment>(PacketWithAttachment.AttachementCount);
  304. PacketWithAttachment.Attachements.Add(data);
  305. if (PacketWithAttachment.Attachements.Count == PacketWithAttachment.AttachementCount)
  306. {
  307. packet = manager.Parser.MergeAttachements(manager, PacketWithAttachment);
  308. PacketWithAttachment = IncomingPacket.Empty;
  309. }
  310. return packet;
  311. }
  312. public OutgoingPacket CreateOutgoing(TransportEventTypes transportEvent, string payload)
  313. {
  314. return new OutgoingPacket { Payload = "" + (char)('0' + (byte)transportEvent) + payload };
  315. }
  316. private StringBuilder builder = new StringBuilder();
  317. public OutgoingPacket CreateOutgoing(Socket socket, SocketIOEventTypes socketIOEvent, int id, string name, object arg)
  318. {
  319. return CreateOutgoing(socket, socketIOEvent, id, name, arg != null ? new object[] { arg } : null);
  320. }
  321. private int GetBinaryCount(object[] args)
  322. {
  323. if (args == null || args.Length == 0)
  324. return 0;
  325. int count = 0;
  326. for (int i = 0; i < args.Length; ++i)
  327. if (args[i] is byte[])
  328. count++;
  329. return count;
  330. }
  331. public OutgoingPacket CreateOutgoing(Socket socket, SocketIOEventTypes socketIOEvent, int id, string name, object[] args)
  332. {
  333. builder.Length = 0;
  334. List<byte[]> attachements = null;
  335. switch(socketIOEvent)
  336. {
  337. case SocketIOEventTypes.Ack:
  338. if (GetBinaryCount(args) > 0)
  339. {
  340. attachements = CreatePlaceholders(args);
  341. socketIOEvent = SocketIOEventTypes.BinaryAck;
  342. }
  343. break;
  344. case SocketIOEventTypes.Event:
  345. if (GetBinaryCount(args) > 0)
  346. {
  347. attachements = CreatePlaceholders(args);
  348. socketIOEvent = SocketIOEventTypes.BinaryEvent;
  349. }
  350. break;
  351. }
  352. builder.Append(((int)TransportEventTypes.Message).ToString());
  353. builder.Append(((int)socketIOEvent).ToString());
  354. if (socketIOEvent == SocketIOEventTypes.BinaryEvent || socketIOEvent == SocketIOEventTypes.BinaryAck)
  355. {
  356. builder.Append(attachements.Count.ToString());
  357. builder.Append('-');
  358. }
  359. // Add the namespace. If there is any other then the root nsp ("/")
  360. // then we have to add a trailing "," if we have more data.
  361. bool nspAdded = false;
  362. if (socket.Namespace != "/")
  363. {
  364. builder.Append(socket.Namespace);
  365. nspAdded = true;
  366. }
  367. // ack id, if any
  368. if (id >= 0)
  369. {
  370. if (nspAdded)
  371. {
  372. builder.Append(',');
  373. nspAdded = false;
  374. }
  375. builder.Append(id.ToString());
  376. }
  377. // payload
  378. switch (socketIOEvent)
  379. {
  380. case SocketIOEventTypes.Connect:
  381. // No Data | Object
  382. if (args != null && args.Length > 0)
  383. {
  384. if (nspAdded) builder.Append(',');
  385. builder.Append(BestHTTP.JSON.LitJson.JsonMapper.ToJson(args[0]));
  386. }
  387. break;
  388. case SocketIOEventTypes.Disconnect:
  389. // No Data
  390. break;
  391. case SocketIOEventTypes.Error:
  392. // String | Object
  393. if (args != null && args.Length > 0)
  394. {
  395. if (nspAdded) builder.Append(',');
  396. builder.Append(BestHTTP.JSON.LitJson.JsonMapper.ToJson(args[0]));
  397. }
  398. break;
  399. case SocketIOEventTypes.Ack:
  400. case SocketIOEventTypes.BinaryAck:
  401. if (nspAdded) builder.Append(',');
  402. if (args != null && args.Length > 0)
  403. {
  404. var argsJson = JSON.LitJson.JsonMapper.ToJson(args);
  405. builder.Append(argsJson);
  406. }
  407. else
  408. builder.Append("[]");
  409. break;
  410. default:
  411. if (nspAdded) builder.Append(',');
  412. // Array
  413. builder.Append('[');
  414. if (!string.IsNullOrEmpty(name))
  415. {
  416. builder.Append('\"');
  417. builder.Append(name);
  418. builder.Append('\"');
  419. }
  420. if (args != null && args.Length > 0)
  421. {
  422. builder.Append(',');
  423. var argsJson = JSON.LitJson.JsonMapper.ToJson(args);
  424. builder.Append(argsJson, 1, argsJson.Length - 2);
  425. }
  426. builder.Append(']');
  427. break;
  428. }
  429. return new OutgoingPacket { Payload = builder.ToString(), Attachements = attachements };
  430. }
  431. private List<byte[]> CreatePlaceholders(object[] args)
  432. {
  433. List<byte[]> attachements = null;
  434. for (int i = 0; i < args.Length; ++i)
  435. {
  436. var binary = args[i] as byte[];
  437. if (binary != null)
  438. {
  439. if (attachements == null)
  440. attachements = new List<byte[]>();
  441. attachements.Add(binary);
  442. args[i] = new Placeholder { _placeholder = true, num = attachements.Count - 1 };
  443. }
  444. }
  445. return attachements;
  446. }
  447. }
  448. }
  449. #endif