ConnectionManager.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. namespace ISCSI.Server
  11. {
  12. internal class ConnectionManager
  13. {
  14. private List<ConnectionState> m_activeConnections = new List<ConnectionState>();
  15. public void AddConnection(ConnectionState connection)
  16. {
  17. lock (m_activeConnections)
  18. {
  19. m_activeConnections.Add(connection);
  20. }
  21. }
  22. public bool RemoveConnection(ConnectionState connection)
  23. {
  24. return RemoveConnection(connection.Session.ISID, connection.Session.TSIH, connection.ConnectionParameters.CID);
  25. }
  26. public bool RemoveConnection(ulong isid, ushort tsih, ushort cid)
  27. {
  28. lock (m_activeConnections)
  29. {
  30. int connectionIndex = GetConnectionStateIndex(isid, tsih, cid);
  31. if (connectionIndex >= 0)
  32. {
  33. m_activeConnections.RemoveAt(connectionIndex);
  34. return true;
  35. }
  36. return false;
  37. }
  38. }
  39. public ConnectionState FindConnection(ConnectionState connection)
  40. {
  41. return FindConnection(connection.Session.ISID, connection.Session.TSIH, connection.ConnectionParameters.CID);
  42. }
  43. public ConnectionState FindConnection(ulong isid, ushort tsih, ushort cid)
  44. {
  45. lock (m_activeConnections)
  46. {
  47. int index = GetConnectionStateIndex(isid, tsih, cid);
  48. if (index >= 0)
  49. {
  50. return m_activeConnections[index];
  51. }
  52. return null;
  53. }
  54. }
  55. public List<ConnectionState> GetSessionConnections(ulong isid, ushort tsih)
  56. {
  57. List<ConnectionState> result = new List<ConnectionState>();
  58. lock (m_activeConnections)
  59. {
  60. for (int index = 0; index < m_activeConnections.Count; index++)
  61. {
  62. if (m_activeConnections[index].Session.ISID == isid &&
  63. m_activeConnections[index].Session.TSIH == tsih)
  64. {
  65. result.Add(m_activeConnections[index]);
  66. }
  67. }
  68. }
  69. return result;
  70. }
  71. private int GetConnectionStateIndex(ulong isid, ushort tsih, ushort cid)
  72. {
  73. for (int index = 0; index < m_activeConnections.Count; index++)
  74. {
  75. if (m_activeConnections[index].Session.ISID == isid &&
  76. m_activeConnections[index].Session.TSIH == tsih &&
  77. m_activeConnections[index].ConnectionParameters.CID == cid)
  78. {
  79. return index;
  80. }
  81. }
  82. return -1;
  83. }
  84. }
  85. }