ConnectionManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Net;
  10. using Utilities;
  11. namespace SMBLibrary.Server
  12. {
  13. internal class ConnectionManager
  14. {
  15. private List<ConnectionState> m_activeConnections = new List<ConnectionState>();
  16. public void AddConnection(ConnectionState connection)
  17. {
  18. lock (m_activeConnections)
  19. {
  20. m_activeConnections.Add(connection);
  21. }
  22. }
  23. public bool RemoveConnection(ConnectionState connection)
  24. {
  25. lock (m_activeConnections)
  26. {
  27. int connectionIndex = m_activeConnections.IndexOf(connection);
  28. if (connectionIndex >= 0)
  29. {
  30. m_activeConnections.RemoveAt(connectionIndex);
  31. return true;
  32. }
  33. return false;
  34. }
  35. }
  36. public void ReleaseConnection(ConnectionState connection)
  37. {
  38. connection.SendQueue.Stop();
  39. SocketUtils.ReleaseSocket(connection.ClientSocket);
  40. connection.CloseSessions();
  41. RemoveConnection(connection);
  42. }
  43. public void ReleaseConnection(IPEndPoint clientEndPoint)
  44. {
  45. ConnectionState connection = FindConnection(clientEndPoint);
  46. if (connection != null)
  47. {
  48. ReleaseConnection(connection);
  49. }
  50. }
  51. public void ReleaseInactiveConnections(TimeSpan inactivityDuration)
  52. {
  53. List<ConnectionState> connections = new List<ConnectionState>(m_activeConnections);
  54. foreach (ConnectionState connection in connections)
  55. {
  56. if (connection.LastReceiveDT.Add(inactivityDuration) < DateTime.UtcNow)
  57. {
  58. ReleaseConnection(connection);
  59. }
  60. }
  61. }
  62. public void ReleaseAllConnections()
  63. {
  64. List<ConnectionState> connections = new List<ConnectionState>(m_activeConnections);
  65. foreach (ConnectionState connection in connections)
  66. {
  67. ReleaseConnection(connection);
  68. }
  69. }
  70. private ConnectionState FindConnection(IPEndPoint clientEndPoint)
  71. {
  72. lock (m_activeConnections)
  73. {
  74. for (int index = 0; index < m_activeConnections.Count; index++)
  75. {
  76. if (m_activeConnections[index].ClientEndPoint.Equals(clientEndPoint))
  77. {
  78. return m_activeConnections[index];
  79. }
  80. }
  81. }
  82. return null;
  83. }
  84. public List<SessionInformation> GetSessionsInformation()
  85. {
  86. List<SessionInformation> result = new List<SessionInformation>();
  87. lock (m_activeConnections)
  88. {
  89. foreach (ConnectionState connection in m_activeConnections)
  90. {
  91. List<SessionInformation> sessions = connection.GetSessionsInformation();
  92. result.AddRange(sessions);
  93. }
  94. }
  95. return result;
  96. }
  97. }
  98. }