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.

111 lines
3.8 KiB

4 years ago
  1. #if MYSQL_6_9
  2. // Copyright (c) 2004-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
  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 Externals.MySql.Data.MySqlClient;
  24. using Externals.MySql.Data.MySqlClient.Properties;
  25. using System;
  26. using System.IO;
  27. namespace Externals.MySql.Data.Common
  28. {
  29. /// <summary>
  30. /// Summary description for StreamCreator.
  31. /// </summary>
  32. internal class StreamCreator
  33. {
  34. string hostList;
  35. uint port;
  36. string pipeName;
  37. // uint timeOut;
  38. uint keepalive;
  39. DBVersion driverVersion;
  40. public StreamCreator(string hosts, uint port, string pipeName, uint keepalive, DBVersion driverVersion)
  41. {
  42. hostList = hosts;
  43. if (hostList == null || hostList.Length == 0)
  44. hostList = "localhost";
  45. this.port = port;
  46. this.pipeName = pipeName;
  47. this.keepalive = keepalive;
  48. this.driverVersion = driverVersion;
  49. }
  50. public static Stream GetStream(string server, uint port, string pipename, uint keepalive, DBVersion v, uint timeout)
  51. {
  52. MySqlConnectionStringBuilder settings = new MySqlConnectionStringBuilder();
  53. settings.Server = server;
  54. settings.Port = port;
  55. settings.PipeName = pipename;
  56. settings.Keepalive = keepalive;
  57. settings.ConnectionTimeout = timeout;
  58. return GetStream(settings);
  59. }
  60. public static Stream GetStream(MySqlConnectionStringBuilder settings)
  61. {
  62. switch (settings.ConnectionProtocol)
  63. {
  64. case MySqlConnectionProtocol.Tcp: return GetTcpStream(settings);
  65. case MySqlConnectionProtocol.UnixSocket: return GetUnixSocketStream(settings);
  66. case MySqlConnectionProtocol.SharedMemory: return GetSharedMemoryStream(settings);
  67. case MySqlConnectionProtocol.NamedPipe: return GetNamedPipeStream(settings);
  68. }
  69. throw new InvalidOperationException(Resources.UnknownConnectionProtocol);
  70. }
  71. private static Stream GetTcpStream(MySqlConnectionStringBuilder settings)
  72. {
  73. MyNetworkStream s = MyNetworkStream.CreateStream(settings, false);
  74. return s;
  75. }
  76. private static Stream GetUnixSocketStream(MySqlConnectionStringBuilder settings)
  77. {
  78. if (Platform.IsWindows())
  79. throw new InvalidOperationException(Resources.NoUnixSocketsOnWindows);
  80. MyNetworkStream s = MyNetworkStream.CreateStream(settings, true);
  81. return s;
  82. }
  83. private static Stream GetSharedMemoryStream(MySqlConnectionStringBuilder settings)
  84. {
  85. SharedMemoryStream str = new SharedMemoryStream(settings.SharedMemoryName);
  86. str.Open(settings.ConnectionTimeout);
  87. return str;
  88. }
  89. private static Stream GetNamedPipeStream(MySqlConnectionStringBuilder settings)
  90. {
  91. Stream stream = NamedPipeStream.Create(settings.PipeName, settings.Server, settings.ConnectionTimeout);
  92. return stream;
  93. }
  94. }
  95. }
  96. #endif