FastMessageEncoder.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. //using System.Linq;
  5. using System.Text;
  6. namespace ECOM.ServiceBus
  7. {
  8. internal class FastMessageEncoder
  9. {
  10. internal class Proxy
  11. {
  12. FastMessageEncoder m_Encoder = new FastMessageEncoder ();
  13. public Proxy Encode<T>(string Key, T Value)
  14. {
  15. m_Encoder [Key] = Value;
  16. return this;
  17. }
  18. public override string ToString()
  19. {
  20. return m_Encoder.ToString ();
  21. }
  22. static public implicit operator string(Proxy proxy)
  23. {
  24. return proxy.ToString ();
  25. }
  26. }
  27. public static Proxy Encode<T>(string Key, T Value)
  28. {
  29. Proxy proxy = new Proxy ();
  30. proxy.Encode (Key, Value);
  31. return proxy;
  32. }
  33. public FastMessageEncoder()
  34. {
  35. m_arMessage = new List<string> ();
  36. }
  37. public void Set(string Key, string strValue)
  38. {
  39. StringBuilder Value = new StringBuilder (strValue);
  40. Value.Replace ("\\", "\\\\");
  41. Value.Replace ("/", "\\/");
  42. Value.Replace ("\"", "\\\"");
  43. StringBuilder build = new StringBuilder ();
  44. build.Append ('\r').Append ('\n').Append (' ').Append (' ');
  45. build.Append ('"').Append (Key).Append ('"');
  46. build.Append (' ').Append (':').Append (' ');
  47. build.Append ('"').Append (Value).Append ('"');
  48. m_arMessage.Add (build.ToString ());
  49. }
  50. public void Set(string Key, int Value)
  51. {
  52. StringBuilder build = new StringBuilder ();
  53. build.Append ('\r').Append ('\n').Append (' ').Append (' ');
  54. build.Append ('"').Append (Key).Append ('"');
  55. build.Append (' ').Append (':').Append (' ');
  56. build.Append (Value);
  57. m_arMessage.Add (build.ToString ());
  58. }
  59. public void Set(string Key, double Value)
  60. {
  61. StringBuilder build = new StringBuilder ();
  62. build.Append ('\r').Append ('\n').Append (' ').Append (' ');
  63. build.Append ('"').Append (Key).Append ('"');
  64. build.Append (' ').Append (':').Append (' ');
  65. build.Append (Value);
  66. m_arMessage.Add (build.ToString ());
  67. }
  68. public void Set(string Key, bool Value)
  69. {
  70. StringBuilder build = new StringBuilder ();
  71. build.Append ('\r').Append ('\n').Append (' ').Append (' ');
  72. build.Append ('"').Append (Key).Append ('"');
  73. build.Append (' ').Append (':').Append (' ');
  74. build.Append (Value.ToString ());
  75. m_arMessage.Add (build.ToString ());
  76. }
  77. public void Set(string Key, string [] arValue)
  78. {
  79. StringBuilder tValue = new StringBuilder ();
  80. foreach (string strValue in arValue)
  81. {
  82. StringBuilder Value = new StringBuilder (strValue);
  83. Value.Replace ("\\", "\\\\");
  84. Value.Replace ("/", "\\/");
  85. Value.Replace ("\"", "\\\"");
  86. if (tValue.Length > 0)
  87. {
  88. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  89. tValue.Append (',').Append (' ');
  90. }
  91. else
  92. {
  93. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  94. tValue.Append (' ').Append (' ');
  95. }
  96. tValue.Append ('\"').Append (Value).Append ('"');
  97. }
  98. StringBuilder build = new StringBuilder ();
  99. build.Append ('\r').Append ('\n').Append (' ').Append (' ');
  100. build.Append ('"').Append (Key).Append ('"');
  101. build.Append (' ').Append (':').Append (' ');
  102. build.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']');
  103. m_arMessage.Add (build.ToString ());
  104. }
  105. public void Set(string Key, List<string> arValue)
  106. {
  107. StringBuilder tValue = new StringBuilder ();
  108. foreach (string strValue in arValue)
  109. {
  110. StringBuilder Value = new StringBuilder (strValue);
  111. Value.Replace ("\\", "\\\\");
  112. Value.Replace ("/", "\\/");
  113. Value.Replace ("\"", "\\\"");
  114. if (tValue.Length > 0)
  115. {
  116. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  117. tValue.Append (',').Append (' ');
  118. }
  119. else
  120. {
  121. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  122. tValue.Append (' ').Append (' ');
  123. }
  124. tValue.Append ('\"').Append (Value).Append ('"');
  125. }
  126. StringBuilder build = new StringBuilder ();
  127. build.Append ('\r').Append ('\n').Append (' ').Append (' ');
  128. build.Append ('"').Append (Key).Append ('"');
  129. build.Append (' ').Append (':').Append (' ');
  130. build.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']');
  131. m_arMessage.Add (build.ToString ());
  132. }
  133. public void Set(string Key, List<FastMessageEncoder> arValue)
  134. {
  135. StringBuilder tValue = new StringBuilder ();
  136. foreach (FastMessageEncoder strValue in arValue)
  137. {
  138. StringBuilder Value = new StringBuilder (strValue.ToString ());
  139. Value.Replace ("\\", "\\\\");
  140. Value.Replace ("/", "\\/");
  141. Value.Replace ("\"", "\\\"");
  142. if (tValue.Length > 0)
  143. {
  144. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  145. tValue.Append (',').Append (' ');
  146. }
  147. else
  148. {
  149. tValue.Append ('\r').Append ('\n').Append (' ').Append (' ');
  150. tValue.Append (' ').Append (' ');
  151. }
  152. tValue.Append ('\"').Append (Value).Append ('"');
  153. }
  154. StringBuilder build = new StringBuilder ();
  155. build.Append ('\r').Append ('\n').Append (' ').Append (' ');
  156. build.Append ('"').Append (Key).Append ('"');
  157. build.Append (' ').Append (':').Append (' ');
  158. build.Append ('[').Append (tValue).Append ('\r').Append ('\n').Append (' ').Append (' ').Append (']');
  159. m_arMessage.Add (build.ToString ());
  160. }
  161. public string ToStringOfElement()
  162. {
  163. StringBuilder build = new StringBuilder ();
  164. for (int Index = 0; Index < m_arMessage.Count; Index++)
  165. {
  166. string msg = m_arMessage [Index];
  167. if (Index > 0)
  168. build.Append (',');
  169. build.Append (msg);
  170. }
  171. return build.ToString ();
  172. }
  173. public override string ToString()
  174. {
  175. StringBuilder build = new StringBuilder ();
  176. build.Append ('\r').Append ('\n').Append ('{');
  177. build.Append (ToStringOfElement ());
  178. build.Append ('\r').Append ('\n').Append ('}').Append ('\r').Append ('\n');
  179. return build.ToString ();
  180. }
  181. static Type TypeOf_int = typeof (int);
  182. static Type TypeOf_uint = typeof (uint);
  183. static Type TypeOf_UInt16 = typeof (UInt16);
  184. static Type TypeOf_long = typeof (long);
  185. static Type TypeOf_ulong = typeof (ulong);
  186. static Type TypeOf_double = typeof (double);
  187. static Type TypeOf_float = typeof (float);
  188. static Type TypeOf_bool = typeof (bool);
  189. static Type TypeOf_string = typeof (string);
  190. static Type TypeOf_list_string = typeof (List<string>);
  191. static Type TypeOf_string_array = typeof (string []);
  192. public object this [string Key]
  193. {
  194. set
  195. {
  196. Type type = value.GetType ();
  197. if (type == TypeOf_int)
  198. {
  199. int toInt = (int) value;
  200. Set (Key, toInt);
  201. return;
  202. }
  203. if (type == TypeOf_uint)
  204. {
  205. uint toUint = (uint) value;
  206. int toInt = (int) toUint;
  207. Set (Key, toInt);
  208. return;
  209. }
  210. if (type == TypeOf_UInt16)
  211. {
  212. UInt16 toUint16 = (UInt16) value;
  213. int toInt = (int) toUint16;
  214. Set (Key, toInt);
  215. return;
  216. }
  217. if (type == TypeOf_long)
  218. {
  219. long toLong = (long) value;
  220. int toInt = (int) toLong;
  221. Set (Key, toInt);
  222. return;
  223. }
  224. if (type == TypeOf_ulong)
  225. {
  226. ulong toUlong = (ulong) value;
  227. int toInt = (int) toUlong;
  228. Set (Key, toInt);
  229. return;
  230. }
  231. if (type == TypeOf_double)
  232. {
  233. double toDouble = (double) value;
  234. Set (Key, toDouble);
  235. return;
  236. }
  237. if (type == TypeOf_float)
  238. {
  239. double toDouble = (float) value;
  240. Set (Key, toDouble);
  241. return;
  242. }
  243. if (type == TypeOf_bool)
  244. {
  245. bool toBool = (bool) value;
  246. Set (Key, toBool);
  247. return;
  248. }
  249. if (type == TypeOf_string)
  250. {
  251. Set (Key, value.ToString ());
  252. return;
  253. }
  254. if (type == TypeOf_list_string)
  255. {
  256. List<string> arString = (List<string>) value;
  257. Set (Key, arString);
  258. return;
  259. }
  260. if (type == TypeOf_string_array)
  261. {
  262. string [] arString = (string []) value;
  263. Set (Key, arString);
  264. return;
  265. }
  266. throw (new ArgumentException ("Unknown data type of " + type.ToString ()));
  267. }
  268. }
  269. protected List<string> m_arMessage;
  270. }
  271. }