ConnectionManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (C) 2012-2016 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.Text;
  10. using Utilities;
  11. namespace ISCSI.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. return RemoveConnection(connection.Session.ISID, connection.Session.TSIH, connection.ConnectionParameters.CID);
  26. }
  27. public bool RemoveConnection(ulong isid, ushort tsih, ushort cid)
  28. {
  29. lock (m_activeConnections)
  30. {
  31. int connectionIndex = GetConnectionStateIndex(isid, tsih, cid);
  32. if (connectionIndex >= 0)
  33. {
  34. m_activeConnections.RemoveAt(connectionIndex);
  35. return true;
  36. }
  37. return false;
  38. }
  39. }
  40. public void ReleaseConnection(ConnectionState connection)
  41. {
  42. // Wait for pending I/O to complete.
  43. connection.RunningSCSICommands.WaitUntilZero();
  44. connection.SendQueue.Stop();
  45. SocketUtils.ReleaseSocket(connection.ClientSocket);
  46. if (connection.Session != null)
  47. {
  48. RemoveConnection(connection);
  49. }
  50. }
  51. public ConnectionState FindConnection(ConnectionState connection)
  52. {
  53. return FindConnection(connection.Session.ISID, connection.Session.TSIH, connection.ConnectionParameters.CID);
  54. }
  55. public ConnectionState FindConnection(ulong isid, ushort tsih, ushort cid)
  56. {
  57. lock (m_activeConnections)
  58. {
  59. int index = GetConnectionStateIndex(isid, tsih, cid);
  60. if (index >= 0)
  61. {
  62. return m_activeConnections[index];
  63. }
  64. return null;
  65. }
  66. }
  67. public List<ConnectionState> GetSessionConnections(ISCSISession session)
  68. {
  69. return GetSessionConnections(session.ISID, session.TSIH);
  70. }
  71. public List<ConnectionState> GetSessionConnections(ulong isid, ushort tsih)
  72. {
  73. List<ConnectionState> result = new List<ConnectionState>();
  74. lock (m_activeConnections)
  75. {
  76. for (int index = 0; index < m_activeConnections.Count; index++)
  77. {
  78. if (m_activeConnections[index].Session.ISID == isid &&
  79. m_activeConnections[index].Session.TSIH == tsih)
  80. {
  81. result.Add(m_activeConnections[index]);
  82. }
  83. }
  84. }
  85. return result;
  86. }
  87. private int GetConnectionStateIndex(ulong isid, ushort tsih, ushort cid)
  88. {
  89. for (int index = 0; index < m_activeConnections.Count; index++)
  90. {
  91. if (m_activeConnections[index].Session.ISID == isid &&
  92. m_activeConnections[index].Session.TSIH == tsih &&
  93. m_activeConnections[index].ConnectionParameters.CID == cid)
  94. {
  95. return index;
  96. }
  97. }
  98. return -1;
  99. }
  100. }
  101. }