DefaultJsonParser.cs 19 KB

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