SCSITarget.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 System.Threading;
  11. using Utilities;
  12. namespace SCSI
  13. {
  14. public delegate void OnCommandCompleted(SCSIStatusCodeName status, byte[] responseBytes, object task);
  15. public abstract class SCSITarget
  16. {
  17. private class SCSICommand
  18. {
  19. public byte[] CommandBytes;
  20. public LUNStructure LUN;
  21. public byte[] Data;
  22. public object Task;
  23. public OnCommandCompleted OnCommandCompleted;
  24. }
  25. private BlockingQueue<SCSICommand> m_commandQueue = new BlockingQueue<SCSICommand>();
  26. public event EventHandler<StandardInquiryEventArgs> OnStandardInquiry;
  27. public event EventHandler<DeviceIdentificationInquiryEventArgs> OnDeviceIdentificationInquiry;
  28. public SCSITarget()
  29. {
  30. Thread workerThread = new Thread(ProcessCommandQueue);
  31. workerThread.IsBackground = true;
  32. workerThread.Start();
  33. }
  34. public void ProcessCommandQueue()
  35. {
  36. while (true)
  37. {
  38. SCSICommand command;
  39. bool stopping = !m_commandQueue.TryDequeue(out command);
  40. if (stopping)
  41. {
  42. return;
  43. }
  44. byte[] responseBytes;
  45. SCSIStatusCodeName status = ExecuteCommand(command.CommandBytes, command.LUN, command.Data, out responseBytes);
  46. command.OnCommandCompleted(status, responseBytes, command.Task);
  47. }
  48. }
  49. public void QueueCommand(byte[] commandBytes, LUNStructure lun, byte[] data, object task, OnCommandCompleted OnCommandCompleted)
  50. {
  51. SCSICommand command = new SCSICommand();
  52. command.CommandBytes = commandBytes;
  53. command.LUN = lun;
  54. command.Data = data;
  55. command.OnCommandCompleted = OnCommandCompleted;
  56. command.Task = task;
  57. m_commandQueue.Enqueue(command);
  58. }
  59. public abstract SCSIStatusCodeName ExecuteCommand(byte[] commandBytes, LUNStructure lun, byte[] data, out byte[] response);
  60. public void NotifyStandardInquiry(object sender, StandardInquiryEventArgs args)
  61. {
  62. // To be thread-safe we must capture the delegate reference first
  63. EventHandler<StandardInquiryEventArgs> handler = OnStandardInquiry;
  64. if (handler != null)
  65. {
  66. handler(sender, args);
  67. }
  68. }
  69. public void NotifyDeviceIdentificationInquiry(object sender, DeviceIdentificationInquiryEventArgs args)
  70. {
  71. // To be thread-safe we must capture the delegate reference first
  72. EventHandler<DeviceIdentificationInquiryEventArgs> handler = OnDeviceIdentificationInquiry;
  73. if (handler != null)
  74. {
  75. handler(sender, args);
  76. }
  77. }
  78. }
  79. }