Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MySql.Data.MySqlClient;
  7. using GTANetworkAPI;
  8.  
  9. namespace DatabaseAPI
  10. {
  11. public class API : Script
  12. {
  13. #region Settings
  14. public static readonly string HOST = "localhost"; //altis.liveyourlife.cc
  15. public static readonly int PORT = 3306; //3306
  16. public static readonly string DATABASE = "gta_dev"; //gta_dev
  17. public static readonly string USERNAME = "root"; //gta_dev
  18. public static readonly string PASSWORD = ""; //X7x25a4iLo266arA4E7aHuH8feJaji
  19. public static readonly string SSLMODE = "none";
  20.  
  21. // Die Anzahl an Verbindungen, die gleichzeitig offen sind.
  22. public static readonly int POOL_SIZE = 4;
  23.  
  24. // Der Interval (in Minuten) in dem die Verbindungen erneuert werden.
  25. public static readonly int RECONNECT_INTERVAL = 180;
  26. #endregion
  27.  
  28. private static API instance;
  29.  
  30. private List<MySqlConnection> availableConnections;
  31.  
  32. public API()
  33. {
  34. this.availableConnections = new List<MySqlConnection>();
  35.  
  36. PopulatePool();
  37. StartReconnectThread();
  38.  
  39. instance = this;
  40. }
  41.  
  42. public static API GetInstance()
  43. {
  44. return instance;
  45. }
  46.  
  47. public MySqlConnection GetConnection()
  48. {
  49. MySqlConnection connection = null;
  50.  
  51. do
  52. {
  53. Monitor.Enter(availableConnections);
  54.  
  55. bool sleep = false;
  56.  
  57. if (availableConnections.Count > 0)
  58. {
  59. connection = availableConnections[0];
  60.  
  61. availableConnections.RemoveAt(0);
  62. }
  63. else
  64. {
  65. sleep = true;
  66. }
  67.  
  68. Monitor.Exit(availableConnections);
  69.  
  70. if (sleep)
  71. {
  72. Task.Delay(1).Wait();
  73. }
  74. } while (connection == null);
  75.  
  76. return connection;
  77. }
  78.  
  79. public void FreeConnection(MySqlConnection connection)
  80. {
  81. Monitor.Enter(availableConnections);
  82.  
  83. availableConnections.Add(connection);
  84.  
  85. Monitor.Exit(availableConnections);
  86. }
  87.  
  88. private bool PopulatePool()
  89. {
  90. string connectionDescription = "Server=" + HOST + ";Database=" + DATABASE + ";Port=" + PORT + ";User Id=" + USERNAME + ";Password=" + PASSWORD + ";SSLMODE=" + SSLMODE;
  91.  
  92. /*for (int i = 0; i < POOL_SIZE; i++)
  93. {
  94. MySqlConnection connection = new MySqlConnection(connectionDescription);
  95.  
  96. connection.Open();
  97.  
  98. availableConnections.Add(connection);
  99. }*/
  100.  
  101. MySqlConnection conn = null;
  102. try
  103. {
  104. conn = new MySqlConnection(connectionDescription);
  105. conn.Open();
  106. availableConnections.Add(conn);
  107. Console.WriteLine("_______________________________________\n");
  108. Console.ForegroundColor = ConsoleColor.Green;
  109. Console.WriteLine("\n[MySQL] Verbindung hergestellt!");
  110. Console.ForegroundColor = ConsoleColor.White;
  111. Console.WriteLine("\n[MySQL] Host: {0}", DatabaseAPI.API.HOST);
  112. Console.WriteLine("_______________________________________\n");
  113. return true;
  114. }
  115. catch (MySqlException ex)
  116. {
  117. switch (ex.Number)
  118. {
  119. case 0: //Zugriff verweigert (Überprüfe DATABASE, USERNAME, PASSWORD)
  120. MySQLError("Zugriff verweigert! überprüfe: DATABASE, USERNAME und PASSWORD!");
  121. break;
  122. case 1042: //Verbindung zum Server kann nicht hergestellt werden
  123. MySQLError("Keine Verbindung zum Server");
  124. break;
  125. case 1045: //USERNAME oder PASWORD falsch
  126. MySQLError("USERNAME oder PASSWORD falsch!");
  127. break;
  128. default: //Ein unbekannter Fehler
  129. MySQLError("Irgendetwas ist schief gelaufen!");
  130. break;
  131. }
  132. }
  133. finally
  134. {
  135. if (conn != null)
  136. {
  137. conn.Close();
  138. }
  139. }
  140. return false;
  141. }
  142.  
  143. public static void MySQLError(string fehler)
  144. {
  145. Console.WriteLine("_______________________________________\n");
  146. Console.ForegroundColor = ConsoleColor.Red;
  147. Console.WriteLine("\n[MySQL] Verbindung fehlgeschlagen!");
  148. Console.ForegroundColor = ConsoleColor.White;
  149. Console.WriteLine("\nFehler: {0}", fehler);
  150. Console.WriteLine("_______________________________________\n");
  151. }
  152.  
  153. private void StartReconnectThread()
  154. {
  155. Task.Run(() =>
  156. {
  157. while (true)
  158. {
  159. Task.Delay(1000 * 60 * RECONNECT_INTERVAL).Wait();
  160.  
  161. int connectionsClosed = 0;
  162.  
  163. do
  164. {
  165. Monitor.Enter(availableConnections);
  166.  
  167. bool sleep = false;
  168.  
  169. if (availableConnections.Count > 0)
  170. {
  171. MySqlConnection connection = availableConnections[0];
  172.  
  173. availableConnections.RemoveAt(0);
  174.  
  175. connection.Close();
  176. connectionsClosed++;
  177. }
  178. else
  179. {
  180. sleep = true;
  181. }
  182.  
  183. Monitor.Exit(availableConnections);
  184.  
  185. if (sleep)
  186. {
  187. Task.Delay(1).Wait();
  188. }
  189. } while (connectionsClosed < POOL_SIZE);
  190.  
  191. Monitor.Enter(availableConnections);
  192.  
  193. PopulatePool();
  194.  
  195. Monitor.Exit(availableConnections);
  196. }
  197. });
  198. }
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement