You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
10 KiB

4 years ago
4 years ago
  1. #if MYSQL_6_9
  2. // Copyright © 2004, 2013, Oracle and/or its affiliates. All rights reserved.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. namespace Externals.MySql.Data.MySqlClient.Memcached
  24. {
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Text;
  28. using System.IO;
  29. /// <summary>
  30. /// Implementation of the Memcached text client protocol.
  31. /// </summary>
  32. internal class TextClient : Client
  33. {
  34. private Encoding encoding;
  35. private static readonly string PROTOCOL_ADD = "add";
  36. private static readonly string PROTOCOL_APPEND = "append";
  37. private static readonly string PROTOCOL_CAS = "cas";
  38. private static readonly string PROTOCOL_DECREMENT = "decr";
  39. private static readonly string PROTOCOL_DELETE = "delete";
  40. private static readonly string PROTOCOL_FLUSHALL = "flush_all";
  41. // private static readonly string PROTOCOL_GET = "get";
  42. private static readonly string PROTOCOL_GETS = "gets";
  43. private static readonly string PROTOCOL_INCREMENT = "incr";
  44. private static readonly string PROTOCOL_PREPEND = "prepend";
  45. private static readonly string PROTOCOL_REPLACE = "replace";
  46. private static readonly string PROTOCOL_SET = "set";
  47. private static readonly string VALUE = "VALUE";
  48. private static readonly string END = "END";
  49. // Errors
  50. private static readonly string ERR_ERROR = "ERROR";
  51. private static readonly string ERR_CLIENT_ERROR = "CLIENT_ERROR";
  52. private static readonly string ERR_SERVER_ERROR = "SERVER_ERROR";
  53. internal protected TextClient( string server, uint port ) : base( server, port )
  54. {
  55. encoding = Encoding.UTF8;
  56. }
  57. #region Memcached protocol interface
  58. public override void Add(string key, object data, TimeSpan expiration)
  59. {
  60. SendCommand(PROTOCOL_ADD, key, data, expiration);
  61. }
  62. public override void Append(string key, object data )
  63. {
  64. SendCommand(PROTOCOL_APPEND, key, data);
  65. }
  66. public override void Cas(string key, object data, TimeSpan expiration, ulong casUnique)
  67. {
  68. SendCommand(PROTOCOL_CAS, key, data, expiration, casUnique);
  69. }
  70. public override void Decrement(string key, int amount)
  71. {
  72. SendCommand(PROTOCOL_DECREMENT, key, amount);
  73. }
  74. public override void Delete(string key)
  75. {
  76. SendCommand(PROTOCOL_DELETE, key);
  77. }
  78. public override void FlushAll(TimeSpan delay)
  79. {
  80. SendCommand(PROTOCOL_FLUSHALL, delay);
  81. }
  82. public override KeyValuePair<string, object> Get(string key)
  83. {
  84. KeyValuePair<string, object>[] kvp = Gets(key);
  85. if (kvp.Length == 0)
  86. throw new MemcachedException("Item does not exists.");
  87. else
  88. return kvp[0];
  89. }
  90. private KeyValuePair<string, object>[] Gets(params string[] keys)
  91. {
  92. StringBuilder sb = new StringBuilder();
  93. sb.Append(string.Format("{0}", PROTOCOL_GETS));
  94. for (int i = 0; i < keys.Length; i++)
  95. {
  96. sb.Append(string.Format(" {0}", keys[i]));
  97. }
  98. sb.Append("\r\n");
  99. SendData(sb.ToString());
  100. byte[] res = GetResponse();
  101. return ParseGetResponse(res);
  102. }
  103. public override void Increment(string key, int amount)
  104. {
  105. SendCommand(PROTOCOL_INCREMENT, key, amount);
  106. }
  107. public override void Prepend(string key, object data)
  108. {
  109. SendCommand(PROTOCOL_PREPEND, key, data);
  110. }
  111. public override void Replace(string key, object data, TimeSpan expiration)
  112. {
  113. SendCommand(PROTOCOL_REPLACE, key, data, expiration);
  114. }
  115. public override void Set(string key, object data, TimeSpan expiration)
  116. {
  117. SendCommand(PROTOCOL_SET, key, data, expiration);
  118. }
  119. #endregion
  120. #region Support methods
  121. /// <summary>
  122. /// Sends a command to the memcached server.
  123. /// </summary>
  124. /// <remarks>This version is for commands that take a key, data, expiration and casUnique.</remarks>
  125. private void SendCommand(string cmd, string key, object data, TimeSpan expiration, ulong casUnique)
  126. {
  127. StringBuilder sb = new StringBuilder();
  128. // set key flags exptime
  129. sb.Append(string.Format("{0} {1} 0 {2} ", cmd, key, (int)(expiration.TotalSeconds)));
  130. byte[] buf = encoding.GetBytes(data.ToString());
  131. string s = encoding.GetString(buf, 0, buf.Length);
  132. sb.Append(s.Length.ToString());
  133. sb.AppendFormat(" {0}", casUnique);
  134. sb.Append("\r\n");
  135. sb.Append(s);
  136. sb.Append("\r\n");
  137. SendData(sb.ToString());
  138. GetResponse();
  139. }
  140. /// <summary>
  141. /// Sends a command to the memcached server.
  142. /// </summary>
  143. /// <param name="cmd"></param>
  144. /// <param name="key"></param>
  145. /// <param name="data"></param>
  146. /// <param name="expiration"></param>
  147. /// <remarks>This version is for commands that take a key, data and expiration</remarks>
  148. private void SendCommand(string cmd, string key, object data, TimeSpan expiration)
  149. {
  150. StringBuilder sb = new StringBuilder();
  151. // set key flags exptime
  152. sb.Append(string.Format("{0} {1} 0 {2} ", cmd, key, (int)(expiration.TotalSeconds)));
  153. byte[] buf = encoding.GetBytes(data.ToString());
  154. string s = encoding.GetString(buf, 0, buf.Length);
  155. sb.Append(s.Length.ToString());
  156. sb.Append("\r\n");
  157. sb.Append(s);
  158. sb.Append("\r\n");
  159. SendData(sb.ToString());
  160. GetResponse();
  161. }
  162. /// <summary>
  163. /// Send a command to memcached server.
  164. /// </summary>
  165. /// <remarks>This version is for commands that don't need flags neither expiration fields.</remarks>
  166. private void SendCommand(string cmd, string key, object data )
  167. {
  168. StringBuilder sb = new StringBuilder();
  169. // set key
  170. sb.Append(string.Format("{0} {1} ", cmd, key ));
  171. byte[] buf = encoding.GetBytes(data.ToString());
  172. string s = encoding.GetString(buf, 0, buf.Length);
  173. if ((cmd == PROTOCOL_APPEND) || (cmd == PROTOCOL_PREPEND))
  174. {
  175. sb.Append("0 0 ");
  176. }
  177. sb.Append(s.Length.ToString());
  178. sb.Append("\r\n");
  179. sb.Append(s);
  180. sb.Append("\r\n");
  181. SendData(sb.ToString());
  182. GetResponse();
  183. }
  184. /// <summary>
  185. /// Sends a command to the server.
  186. /// </summary>
  187. /// <param name="cmd"></param>
  188. /// <param name="key"></param>
  189. /// <remarks>This version is for commands that only require a key</remarks>
  190. private void SendCommand(string cmd, string key )
  191. {
  192. StringBuilder sb = new StringBuilder();
  193. // set key
  194. sb.Append(string.Format("{0} {1} ", cmd, key ));
  195. sb.Append("\r\n");
  196. SendData(sb.ToString());
  197. GetResponse();
  198. }
  199. /// <summary>
  200. /// Sends a command to the server.
  201. /// </summary>
  202. /// <remarks>This version is for commands that only require a key and an integer value.</remarks>
  203. private void SendCommand(string cmd, string key, int amount )
  204. {
  205. StringBuilder sb = new StringBuilder();
  206. // set key
  207. sb.Append(string.Format("{0} {1} {2}", cmd, key, amount));
  208. sb.Append("\r\n");
  209. SendData(sb.ToString());
  210. GetResponse();
  211. }
  212. /// <summary>
  213. /// Sends a command to the server.
  214. /// </summary>
  215. /// <remarks>This version is for commands that only require a key and expiration.</remarks>
  216. private void SendCommand(string cmd, TimeSpan expiration)
  217. {
  218. StringBuilder sb = new StringBuilder();
  219. sb.Append(string.Format("{0} {1}\r\n", PROTOCOL_FLUSHALL, expiration.TotalSeconds));
  220. SendData(sb.ToString());
  221. GetResponse();
  222. }
  223. private void ValidateErrorResponse(byte[] res)
  224. {
  225. string s = encoding.GetString(res, 0, res.Length);
  226. if ((s.StartsWith(ERR_ERROR, StringComparison.OrdinalIgnoreCase)) ||
  227. (s.StartsWith(ERR_CLIENT_ERROR, StringComparison.OrdinalIgnoreCase)) ||
  228. (s.StartsWith(ERR_SERVER_ERROR, StringComparison.OrdinalIgnoreCase)))
  229. {
  230. throw new MemcachedException(s);
  231. }
  232. }
  233. private void SendData(string sData)
  234. {
  235. byte[] data = encoding.GetBytes(sData);
  236. stream.Write(data, 0, data.Length);
  237. }
  238. private KeyValuePair<string, object>[] ParseGetResponse(byte[] input)
  239. {
  240. // VALUE key2 10 9 2\r\n111222333\r\nEND\r\n
  241. string[] sInput = encoding.GetString(input, 0, input.Length).Split(new string[] { "\r\n" }, StringSplitOptions.None);
  242. List<KeyValuePair<string, object>> l = new List<KeyValuePair<string, object>>();
  243. int i = 0;
  244. string key = "";
  245. KeyValuePair<string, object> kvp;
  246. while ((sInput[i] != END) && (i < sInput.Length))
  247. {
  248. if (sInput[i].StartsWith(VALUE, StringComparison.OrdinalIgnoreCase))
  249. {
  250. key = sInput[i].Split(' ')[1];
  251. }
  252. else
  253. {
  254. kvp = new KeyValuePair<string, object>(key, sInput[i]);
  255. l.Add(kvp);
  256. }
  257. i++;
  258. }
  259. return l.ToArray();
  260. }
  261. private byte[] GetResponse()
  262. {
  263. byte[] res = new byte[1024];
  264. MemoryStream ms = new MemoryStream();
  265. int cnt = stream.Read(res, 0, 1024);
  266. while (cnt > 0)
  267. {
  268. ms.Write(res, 0, cnt);
  269. if (cnt < 1024) break;
  270. cnt = stream.Read(res, 0, 1024);
  271. }
  272. byte[] res2 = ms.ToArray();
  273. ValidateErrorResponse(res2);
  274. return res2;
  275. }
  276. #endregion
  277. }
  278. }
  279. #endif