public void Write_AeWithNonAsciiCharacters_ShouldBeAsciified()
{
var notExpected = "GÖTEBORG";
var request = new AAssociateRQ(new DicomAssociation("MALMÖ", notExpected));
var writePdu = request.Write();
RawPDU readPdu;
using (var stream = new MemoryStream())
{
writePdu.WritePDU(stream);
var length = (int)writePdu.Length;
var buffer = new byte[length];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(buffer, 0, length);
readPdu = new RawPDU(buffer);
}
readPdu.Reset();
readPdu.SkipBytes("Unknown", 10);
var actual = readPdu.ReadString("Called AE", 16);
Assert.NotEqual(notExpected, actual);
}
开发者ID:GMZ,项目名称:fo-dicom,代码行数:25,代码来源:PDUTest.cs
示例2: Save_ToNonExistingDirectory_Succeeds
public void Save_ToNonExistingDirectory_Succeeds()
{
var path = @".\Test Data\PDU Test";
var name = Path.Combine(path, "assoc.pdu");
if (Directory.Exists(path)) Directory.Delete(path, true);
var pdu = new RawPDU(0x01);
pdu.Save(name);
Assert.True(File.Exists(name));
}
开发者ID:GMZ,项目名称:fo-dicom,代码行数:11,代码来源:PDUTest.cs
示例3: Write
/// <summary>
/// Write PDU.
/// </summary>
/// <param name="pdu">PDU to write.</param>
public void Write(RawPDU pdu)
{
if (null != SubItem)
{
pdu.Write("Item-Type", (byte)0x56);
pdu.Write("Reserved", (byte)0x00);
pdu.MarkLength16("Item-Length");
pdu.Write("SOP Class UID Length", (ushort)(SopClassUid.UID.Length));
pdu.Write("SOP Class UID", SopClassUid.UID);
SubItem.Write(pdu);
pdu.WriteLength16();
}
}
private void EndReadPDU(IAsyncResult result)
{
try {
byte[] buffer = (byte[])result.AsyncState;
int count = _network.EndRead(result);
if (count == 0) {
// disconnected
CloseConnection(0);
return;
}
_readLength -= count;
if (_readLength > 0) {
_network.BeginRead(buffer, buffer.Length - _readLength, _readLength, EndReadPDU, buffer);
return;
}
var raw = new RawPDU(buffer);
switch (raw.Type) {
case 0x01: {
Association = new DicomAssociation();
var pdu = new AAssociateRQ(Association);
pdu.Read(raw);
LogID = Association.CallingAE;
Logger.Log(LogLevel.Info, "{0} <- Association request:\n{1}", LogID, Association.ToString());
if (this is IDicomServiceProvider)
(this as IDicomServiceProvider).OnReceiveAssociationRequest(Association);
break;
}
case 0x02: {
var pdu = new AAssociateAC(Association);
pdu.Read(raw);
LogID = Association.CalledAE;
Logger.Log(LogLevel.Info, "{0} <- Association accept:\n{1}", LogID, Association.ToString());
if (this is IDicomServiceUser)
(this as IDicomServiceUser).OnReceiveAssociationAccept(Association);
break;
}
case 0x03: {
var pdu = new AAssociateRJ();
pdu.Read(raw);
Logger.Log(LogLevel.Info, "{0} <- Association reject [result: {1}; source: {2}; reason: {3}]", LogID, pdu.Result, pdu.Source, pdu.Reason);
if (this is IDicomServiceUser)
(this as IDicomServiceUser).OnReceiveAssociationReject(pdu.Result, pdu.Source, pdu.Reason);
break;
}
case 0x04: {
var pdu = new PDataTF();
pdu.Read(raw);
if (Options.LogDataPDUs)
Logger.Info("{0} <- {1}", LogID, pdu);
_processQueue.Queue(ProcessPDataTF, pdu);
break;
}
case 0x05: {
var pdu = new AReleaseRQ();
pdu.Read(raw);
Logger.Log(LogLevel.Info, "{0} <- Association release request", LogID);
if (this is IDicomServiceProvider)
(this as IDicomServiceProvider).OnReceiveAssociationReleaseRequest();
break;
}
case 0x06: {
var pdu = new AReleaseRP();
pdu.Read(raw);
Logger.Log(LogLevel.Info, "{0} <- Association release response", LogID);
if (this is IDicomServiceUser)
(this as IDicomServiceUser).OnReceiveAssociationReleaseResponse();
CloseConnection(0);
break;
}
case 0x07: {
var pdu = new AAbort();
pdu.Read(raw);
Logger.Log(LogLevel.Info, "{0} <- Abort: {1} - {2}", LogID, pdu.Source, pdu.Reason);
if (this is IDicomServiceProvider)
(this as IDicomServiceProvider).OnReceiveAbort(pdu.Source, pdu.Reason);
else if (this is IDicomServiceUser)
(this as IDicomServiceUser).OnReceiveAbort(pdu.Source, pdu.Reason);
CloseConnection(0);
break;
}
case 0xFF: {
break;
}
default:
throw new DicomNetworkException("Unknown PDU type");
}
BeginReadPDUHeader();
} catch (IOException e) {
int error = 0;
if (e.InnerException is SocketException) {
error = (e.InnerException as SocketException).ErrorCode;
Logger.Error("Socket error while reading PDU: {0} [{1}]", (e.InnerException as SocketException).SocketErrorCode, (e.InnerException as SocketException).ErrorCode);
} else if (!(e.InnerException is ObjectDisposedException))
Logger.Error("IO exception while reading PDU: {0}", e.ToString());
//.........这里部分代码省略.........
/// <summary>
/// Reads A-ASSOCIATE-AC from PDU buffer
/// </summary>
/// <param name="raw">PDU buffer</param>
public void Read(RawPDU raw)
{
uint l = raw.Length;
ushort c = 0;
raw.ReadUInt16("Version");
raw.SkipBytes("Reserved", 2);
raw.SkipBytes("Reserved", 16);
raw.SkipBytes("Reserved", 16);
raw.SkipBytes("Reserved", 32);
l -= 68;
while (l > 0) {
byte type = raw.ReadByte("Item-Type");
l -= 1;
if (type == 0x10) {
// Application Context
raw.SkipBytes("Reserved", 1);
c = raw.ReadUInt16("Item-Length");
raw.SkipBytes("Value", (int)c);
l -= 3 + (uint)c;
} else
if (type == 0x21) {
// Presentation Context
raw.ReadByte("Reserved");
ushort pl = raw.ReadUInt16("Presentation Context Item-Length");
byte id = raw.ReadByte("Presentation Context ID");
raw.ReadByte("Reserved");
byte res = raw.ReadByte("Presentation Context Result/Reason");
raw.ReadByte("Reserved");
l -= (uint)pl + 3;
pl -= 4;
// Presentation Context Transfer Syntax
raw.ReadByte("Presentation Context Item-Type (0x40)");
raw.ReadByte("Reserved");
ushort tl = raw.ReadUInt16("Presentation Context Item-Length");
string tx = raw.ReadString("Presentation Context Syntax UID", tl);
pl -= (ushort)(tl + 4);
_assoc.SetPresentationContextResult(id, (DcmPresContextResult)res);
_assoc.SetAcceptedTransferSyntax(id, DicomTransferSyntax.Lookup(tx));
} else
if (type == 0x50) {
// User Information
raw.ReadByte("Reserved");
ushort il = raw.ReadUInt16("User Information Item-Length");
l -= (uint)(il + 3);
while (il > 0) {
byte ut = raw.ReadByte("User Item-Type");
raw.ReadByte("Reserved");
ushort ul = raw.ReadUInt16("User Item-Length");
il -= (ushort)(ul + 4);
if (ut == 0x51) {
_assoc.MaximumPduLength = raw.ReadUInt32("Max PDU Length");
} else if (ut == 0x52) {
_assoc.ImplementationClass = DicomUID.Lookup(raw.ReadString("Implementation Class UID", ul));
} else if (ut == 0x53) {
_assoc.AsyncOpsInvoked = raw.ReadUInt16("Asynchronous Operations Invoked");
_assoc.AsyncOpsPerformed = raw.ReadUInt16("Asynchronous Operations Performed");
} else if (ut == 0x55) {
_assoc.ImplementationVersion = raw.ReadString("Implementation Version", ul);
} else {
raw.SkipBytes("User Item Value", (int)ul);
}
}
}
else {
raw.SkipBytes("Reserved", 1);
ushort il = raw.ReadUInt16("User Item-Length");
raw.SkipBytes("Unknown User Item", il);
l -= (uint)(il + 3);
}
}
}
开发者ID:hide1980,项目名称:mdcm,代码行数:83,代码来源:PDU.cs
示例10: EndReadPDU
private void EndReadPDU(IAsyncResult result) {
try {
byte[] buffer = (byte[])result.AsyncState;
int count = _network.EndRead(result);
if (count == 0) {
// disconnected
CloseConnection(null);
return;
}
_readLength -= count;
if (_readLength > 0) {
_network.BeginRead(buffer, buffer.Length - _readLength, _readLength, EndReadPDU, buffer);
return;
}
var raw = new RawPDU(buffer);
switch (raw.Type) {
case 0x01: {
Association = new DicomAssociation();
var pdu = new AAssociateRQ(Association);
pdu.Read(raw);
LogID = Association.CallingAE;
if (Options.UseRemoteAEForLogName)
Logger = LogManager.Default.GetLogger(LogID);
Logger.Info("{callingAE} <- Association request:\n{association}", LogID, Association.ToString());
if (this is IDicomServiceProvider)
(this as IDicomServiceProvider).OnReceiveAssociationRequest(Association);
break;
}
case 0x02: {
var pdu = new AAssociateAC(Association);
pdu.Read(raw);
LogID = Association.CalledAE;
Logger.Info("{calledAE} <- Association accept:\n{assocation}", LogID, Association.ToString());
if (this is IDicomServiceUser)
(this as IDicomServiceUser).OnReceiveAssociationAccept(Association);
break;
}
case 0x03: {
var pdu = new AAssociateRJ();
pdu.Read(raw);
Logger.Info("{logId} <- Association reject [result: {pduResult}; source: {pduSource}; reason: {pduReason}]", LogID, pdu.Result, pdu.Source, pdu.Reason);
if (this is IDicomServiceUser)
(this as IDicomServiceUser).OnReceiveAssociationReject(pdu.Result, pdu.Source, pdu.Reason);
break;
}
case 0x04: {
var pdu = new PDataTF();
pdu.Read(raw);
if (Options.LogDataPDUs)
Logger.Info("{logId} <- {@pdu}", LogID, pdu);
_processQueue.Queue(ProcessPDataTF, pdu);
break;
}
case 0x05: {
var pdu = new AReleaseRQ();
pdu.Read(raw);
Logger.Info("{logId} <- Association release request", LogID);
if (this is IDicomServiceProvider)
(this as IDicomServiceProvider).OnReceiveAssociationReleaseRequest();
break;
}
case 0x06: {
var pdu = new AReleaseRP();
pdu.Read(raw);
Logger.Info("{logId} <- Association release response", LogID);
if (this is IDicomServiceUser)
(this as IDicomServiceUser).OnReceiveAssociationReleaseResponse();
CloseConnection(null);
break;
}
case 0x07: {
var pdu = new AAbort();
pdu.Read(raw);
Logger.Info("{logId} <- Abort: {pduSource} - {pduReason}", LogID, pdu.Source, pdu.Reason);
if (this is IDicomServiceProvider)
(this as IDicomServiceProvider).OnReceiveAbort(pdu.Source, pdu.Reason);
else if (this is IDicomServiceUser)
(this as IDicomServiceUser).OnReceiveAbort(pdu.Source, pdu.Reason);
CloseConnection(null);
break;
}
case 0xFF: {
break;
}
default:
throw new DicomNetworkException("Unknown PDU type");
}
BeginReadPDUHeader();
} catch (IOException e) {
if (e.InnerException is SocketException) {
Logger.Error("Socket error while reading PDU: {socketErrorCode} [{errorCode}]", (e.InnerException as SocketException).SocketErrorCode, (e.InnerException as SocketException).ErrorCode);
} else if (!(e.InnerException is ObjectDisposedException))
Logger.Error("IO exception while reading PDU: {@error}", e);
//.........这里部分代码省略.........
请发表评论