123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Text;
- namespace ISCSI.Client
- {
-
-
-
- public class ISCSIDisk : Disk
- {
- private ISCSIClient m_client;
- private int m_bytesPerSector;
- private long m_size;
- private bool m_isLoggedIn;
- private ushort m_lun;
- public ISCSIDisk()
- {
- string initiatorName = "iqn.1991-05.com.microsoft:" + Environment.MachineName;
- m_client = new ISCSIClient(initiatorName);
- }
- public ISCSIDisk(string initiatorName)
- {
- m_client = new ISCSIClient(initiatorName);
- }
- public bool Connect(IPAddress targetAddress, int targetPort, string targetName, ushort lun)
- {
- bool isConnected = m_client.Connect(targetAddress, targetPort);
- if (isConnected)
- {
- m_isLoggedIn = m_client.Login(targetName);
- if (m_isLoggedIn)
- {
- List<ushort> luns = m_client.GetLUNList();
- if (luns.Contains(lun))
- {
- m_lun = lun;
- m_size = (long)m_client.ReadCapacity(lun, out m_bytesPerSector);
- return true;
- }
- m_client.Logout();
- m_isLoggedIn = false;
- }
- }
- return false;
- }
- public void Disconnect()
- {
- if (m_isLoggedIn)
- {
- m_client.Logout();
- }
- m_client.Disconnect();
- }
- public override byte[] ReadSectors(long sectorIndex, int sectorCount)
- {
- if (!m_isLoggedIn)
- {
- throw new InvalidOperationException("Not connected");
- }
- return m_client.Read(m_lun, (ulong)sectorIndex, (uint)sectorCount, m_bytesPerSector);
- }
- public override void WriteSectors(long sectorIndex, byte[] data)
- {
- if (!m_isLoggedIn)
- {
- throw new InvalidOperationException("Not connected");
- }
- if (IsReadOnly)
- {
- throw new UnauthorizedAccessException("Attempted to perform write on a readonly disk");
- }
- bool success = m_client.Write(m_lun, (ulong)sectorIndex, data, m_bytesPerSector);
- if (!success)
- {
- string message = String.Format("Failed to write to sector {0}", sectorIndex);
- }
- }
- public override int BytesPerSector
- {
- get
- {
- return m_bytesPerSector;
- }
- }
- public override long Size
- {
- get
- {
- return m_size;
- }
- }
- }
- }
|