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.

258 lines
7.3 KiB

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. using System;
  24. using System.IO;
  25. using System.Diagnostics;
  26. using System.Text;
  27. using Externals.MySql.Data.Common;
  28. using Externals.MySql.Data.MySqlClient.Properties;
  29. namespace Externals.MySql.Data.MySqlClient
  30. {
  31. /// <summary>
  32. /// Summary description for MySqlStream.
  33. /// </summary>
  34. internal class MySqlStream
  35. {
  36. private byte sequenceByte;
  37. private int maxBlockSize;
  38. private ulong maxPacketSize;
  39. private byte[] packetHeader = new byte[4];
  40. MySqlPacket packet;
  41. TimedStream timedStream;
  42. Stream inStream;
  43. Stream outStream;
  44. internal Stream BaseStream
  45. {
  46. get
  47. {
  48. return timedStream;
  49. }
  50. }
  51. public MySqlStream(Encoding encoding)
  52. {
  53. // we have no idea what the real value is so we start off with the max value
  54. // The real value will be set in NativeDriver.Configure()
  55. maxPacketSize = ulong.MaxValue;
  56. // we default maxBlockSize to MaxValue since we will get the 'real' value in
  57. // the authentication handshake and we know that value will not exceed
  58. // true maxBlockSize prior to that.
  59. maxBlockSize = Int32.MaxValue;
  60. packet = new MySqlPacket(encoding);
  61. }
  62. public MySqlStream(Stream baseStream, Encoding encoding, bool compress)
  63. : this(encoding)
  64. {
  65. timedStream = new TimedStream(baseStream);
  66. Stream stream;
  67. if (compress)
  68. stream = new CompressedStream(timedStream);
  69. else
  70. stream = timedStream;
  71. inStream = new BufferedStream(stream);
  72. outStream = stream;
  73. }
  74. public void Close()
  75. {
  76. outStream.Close();
  77. inStream.Close();
  78. timedStream.Close();
  79. }
  80. #region Properties
  81. public Encoding Encoding
  82. {
  83. get { return packet.Encoding; }
  84. set { packet.Encoding = value; }
  85. }
  86. public void ResetTimeout(int timeout)
  87. {
  88. timedStream.ResetTimeout(timeout);
  89. }
  90. public byte SequenceByte
  91. {
  92. get { return sequenceByte; }
  93. set { sequenceByte = value; }
  94. }
  95. public int MaxBlockSize
  96. {
  97. get { return maxBlockSize; }
  98. set { maxBlockSize = value; }
  99. }
  100. public ulong MaxPacketSize
  101. {
  102. get { return maxPacketSize; }
  103. set { maxPacketSize = value; }
  104. }
  105. #endregion
  106. #region Packet methods
  107. /// <summary>
  108. /// ReadPacket is called by NativeDriver to start reading the next
  109. /// packet on the stream.
  110. /// </summary>
  111. public MySqlPacket ReadPacket()
  112. {
  113. //Debug.Assert(packet.Position == packet.Length);
  114. // make sure we have read all the data from the previous packet
  115. //Debug.Assert(HasMoreData == false, "HasMoreData is true in OpenPacket");
  116. LoadPacket();
  117. // now we check if this packet is a server error
  118. if (packet.Buffer[0] == 0xff)
  119. {
  120. packet.ReadByte(); // read off the 0xff
  121. int code = packet.ReadInteger(2);
  122. string msg = String.Empty;
  123. if (packet.Version.isAtLeast(5, 5, 0))
  124. msg = packet.ReadString(Encoding.UTF8);
  125. else
  126. msg = packet.ReadString();
  127. if (msg.StartsWith("#", StringComparison.Ordinal))
  128. {
  129. msg.Substring(1, 5); /* state code */
  130. msg = msg.Substring(6);
  131. }
  132. throw new MySqlException(msg, code);
  133. }
  134. return packet;
  135. }
  136. /// <summary>
  137. /// Reads the specified number of bytes from the stream and stores them at given
  138. /// offset in the buffer.
  139. /// Throws EndOfStreamException if not all bytes can be read.
  140. /// </summary>
  141. /// <param name="stream">Stream to read from</param>
  142. /// <param name="buffer"> Array to store bytes read from the stream </param>
  143. /// <param name="offset">The offset in buffer at which to begin storing the data read from the current stream. </param>
  144. /// <param name="count">Number of bytes to read</param>
  145. internal static void ReadFully(Stream stream, byte[] buffer, int offset, int count)
  146. {
  147. int numRead = 0;
  148. int numToRead = count;
  149. while (numToRead > 0)
  150. {
  151. int read = stream.Read(buffer, offset + numRead, numToRead);
  152. if (read == 0)
  153. {
  154. throw new EndOfStreamException();
  155. }
  156. numRead += read;
  157. numToRead -= read;
  158. }
  159. }
  160. /// <summary>
  161. /// LoadPacket loads up and decodes the header of the incoming packet.
  162. /// </summary>
  163. public void LoadPacket()
  164. {
  165. try
  166. {
  167. packet.Length = 0;
  168. int offset = 0;
  169. while (true)
  170. {
  171. ReadFully(inStream, packetHeader, 0, 4);
  172. sequenceByte = (byte)(packetHeader[3] + 1);
  173. int length = (int)(packetHeader[0] + (packetHeader[1] << 8) +
  174. (packetHeader[2] << 16));
  175. // make roo for the next block
  176. packet.Length += length;
  177. ReadFully(inStream, packet.Buffer, offset, length);
  178. offset += length;
  179. // if this block was < maxBlock then it's last one in a multipacket series
  180. if (length < maxBlockSize) break;
  181. }
  182. packet.Position = 0;
  183. }
  184. catch (IOException ioex)
  185. {
  186. throw new MySqlException(Resources.ReadFromStreamFailed, true, ioex);
  187. }
  188. }
  189. public void SendPacket(MySqlPacket packet)
  190. {
  191. byte[] buffer = packet.Buffer;
  192. int length = packet.Position - 4;
  193. if ((ulong)length > maxPacketSize)
  194. throw new MySqlException(Resources.QueryTooLarge, (int)MySqlErrorCode.PacketTooLarge);
  195. int offset = 0;
  196. while (length > 0)
  197. {
  198. int lenToSend = length > maxBlockSize ? maxBlockSize : length;
  199. buffer[offset] = (byte)(lenToSend & 0xff);
  200. buffer[offset + 1] = (byte)((lenToSend >> 8) & 0xff);
  201. buffer[offset + 2] = (byte)((lenToSend >> 16) & 0xff);
  202. buffer[offset + 3] = sequenceByte++;
  203. outStream.Write(buffer, offset, lenToSend + 4);
  204. outStream.Flush();
  205. length -= lenToSend;
  206. offset += lenToSend;
  207. }
  208. }
  209. public void SendEntirePacketDirectly(byte[] buffer, int count)
  210. {
  211. buffer[0] = (byte)(count & 0xff);
  212. buffer[1] = (byte)((count >> 8) & 0xff);
  213. buffer[2] = (byte)((count >> 16) & 0xff);
  214. buffer[3] = sequenceByte++;
  215. outStream.Write(buffer, 0, count + 4);
  216. outStream.Flush();
  217. }
  218. #endregion
  219. }
  220. }
  221. #endif