private bool ProcessNextPDU() { var raw = new RawPDU(_network); if (raw.Type == 0x04) { if (_dimse == null) { _dimse = new DcmDimseInfo(); _assoc.TotalDimseReceived++; } } raw.ReadPDU(); if (_multiThreaded) { _processingQueue.Enqueue(delegate { ProcessRawPDU(raw); }); return true; } return ProcessRawPDU(raw); }
public uint Read(RawPDU raw) { uint len = raw.ReadUInt32("PDV-Length"); _pcid = raw.ReadByte("Presentation Context ID"); byte mch = raw.ReadByte("Message Control Header"); _value = raw.ReadBytes("PDV Value", (int)len - 2); _command = (mch & 0x01) != 0; _last = (mch & 0x02) != 0; return len + 4; }
private bool ProcessRawPDU(RawPDU raw) { try { switch (raw.Type) { case 0x01: { _assoc = new ServerAssociationParameters(); var pdu = new AAssociateRQ(_assoc); pdu.Read(raw); State = DicomAssociationState.Sta3_AwaitingLocalAAssociationResponsePrimative; OnReceiveAssociateRequest(_assoc as ServerAssociationParameters); if (State != DicomAssociationState.Sta13_AwaitingTransportConnectionClose && State != DicomAssociationState.Sta6_AssociationEstablished) { Platform.Log(LogLevel.Error, "Association incorrectly not accepted or rejected, aborting."); return false; } //if derived class call SendAssociateAccept, it has fired this event //if (AssociationEstablished != null) // AssociationEstablished(_assoc); return true; } case 0x02: { var pdu = new AAssociateAC(_assoc); pdu.Read(raw); State = DicomAssociationState.Sta6_AssociationEstablished; OnReceiveAssociateAccept(_assoc); if (AssociationEstablished != null) AssociationEstablished(_assoc); return true; } case 0x03: { var pdu = new AAssociateRJ(); pdu.Read(raw); State = DicomAssociationState.Sta13_AwaitingTransportConnectionClose; if (AssociationRejected != null) AssociationRejected(pdu.Source, pdu.Reason); OnReceiveAssociateReject(pdu.Result, pdu.Source, pdu.Reason); return true; } case 0x04: { var pdu = new PDataTF(); pdu.Read(raw); return ProcessPDataTF(pdu); } case 0x05: { var pdu = new AReleaseRQ(); pdu.Read(raw); State = DicomAssociationState.Sta8_AwaitingAReleaseRPLocalUser; OnReceiveReleaseRequest(); return true; } case 0x06: { var pdu = new AReleaseRP(); pdu.Read(raw); State = DicomAssociationState.Sta13_AwaitingTransportConnectionClose; if (AssociationReleased != null) AssociationReleased(_assoc); OnReceiveReleaseResponse(); return true; } case 0x07: { var pdu = new AAbort(); pdu.Read(raw); State = DicomAssociationState.Sta1_Idle; if (AssociationAborted != null) AssociationAborted(_assoc, pdu.Reason); OnReceiveAbort(pdu.Source, pdu.Reason); return true; } case 0xFF: { Platform.Log(LogLevel.Error, "Unexpected PDU type: 0xFF. Potential parsing error."); return false; } default: throw new DicomNetworkException("Unknown PDU type"); } } catch (Exception e) { OnNetworkError(e, true); if (NetworkError != null) NetworkError(e); Platform.Log(LogLevel.Error, e, "Unexpected exception when processing PDU."); return false; } }
public void Read(RawPDU raw) { uint len = raw.Length; uint read = 0; while (read < len) { PDV pdv = new PDV(); read += pdv.Read(raw); _pdvs.Add(pdv); } }
public void Write(RawPDU pdu) { byte mch = (byte)((_last ? 2 : 0) + (_command ? 1 : 0)); pdu.MarkLength32("PDV-Length"); pdu.Write("Presentation Context ID", _pcid); pdu.Write("Message Control Header", mch); pdu.Write("PDV Value", _value); pdu.WriteLength32(); }
public void Read(RawPDU raw) { uint l = raw.Length; raw.ReadUInt16("Version"); raw.SkipBytes("Reserved", 2); _assoc.CalledAE = raw.ReadString("Called AE", 16); _assoc.CallingAE = raw.ReadString("Calling AE", 16); raw.SkipBytes("Reserved", 32); l -= 2 + 2 + 16 + 16 + 32; while (l > 0) { byte type = raw.ReadByte("Item-Type"); raw.SkipBytes("Reserved", 1); ushort il = raw.ReadUInt16("Item-Length"); l -= 4 + (uint)il; if (type == 0x10) { // Application Context raw.SkipBytes("Application Context", il); } else if (type == 0x20) { // Presentation Context byte id = raw.ReadByte("Presentation Context ID"); raw.SkipBytes("Reserved", 3); il -= 4; while (il > 0) { byte pt = raw.ReadByte("Presentation Context Item-Type"); raw.SkipBytes("Reserved", 1); ushort pl = raw.ReadUInt16("Presentation Context Item-Length"); string sx = raw.ReadString("Presentation Context Syntax UID", pl); if (pt == 0x30) { SopClass sopClass = SopClass.GetSopClass(sx); if (sopClass == null) sopClass = new SopClass("Private SOP Class", sx, false); _assoc.AddPresentationContext(id, sopClass); } else if (pt == 0x40) { TransferSyntax transferSyntax = TransferSyntax.GetTransferSyntax(sx); if (transferSyntax == null) transferSyntax = new TransferSyntax("Private Syntax", sx, true, false, true, false, false, false); _assoc.AddTransferSyntax(id, transferSyntax); } il -= (ushort)(4 + pl); } } else if (type == 0x50) { // User Information while (il > 0) { byte ut = raw.ReadByte("User Information Item-Type"); raw.SkipBytes("Reserved", 1); ushort ul = raw.ReadUInt16("User Information Item-Length"); il -= (ushort)(4 + ul); if (ut == 0x51) { _assoc.RemoteMaximumPduLength = raw.ReadUInt32("Max PDU Length"); } else if (ut == 0x52) { _assoc.ImplementationClass = new DicomUid(raw.ReadString("Implementation Class UID", ul), "Implementation Class UID", UidType.Unknown); } else if (ut == 0x53) { _assoc.RemoteMaxOperationsInvoked = raw.ReadUInt16("Max Operations Invoked"); _assoc.RemoteMaxOperationsPerformed = raw.ReadUInt16("Max Operations Performed"); } else if (ut == 0x55) { _assoc.ImplementationVersion = raw.ReadString("Implementation Version", ul); } else if (ut == 0x54) { raw.SkipBytes("SCU/SCP Role Selection", ul); /* ushort rsul = raw.ReadUInt16(); if ((rsul + 4) != ul) { throw new DicomNetworkException("SCU/SCP role selection length (" + ul + " bytes) does not match uid length (" + rsul + " + 4 bytes)"); } raw.ReadChars(rsul); // Abstract Syntax raw.ReadByte(); // SCU role raw.ReadByte(); // SCP role */ } else { Platform.Log(LogLevel.Error, "Unhandled user item: 0x{0:x2} ({1} + 4 bytes)", ut, ul); raw.SkipBytes("Unhandled User Item", ul); } } } } }
public RawPDU Write() { RawPDU pdu = new RawPDU(0x04); foreach (PDV pdv in _pdvs) { pdv.Write(pdu); } return pdu; }
public void Read(RawPDU raw) { raw.ReadUInt32("Reserved"); }
public RawPDU Write() { RawPDU pdu = new RawPDU((byte)0x07); pdu.Write("Reserved", (byte)0x00); pdu.Write("Reserved", (byte)0x00); pdu.Write("Source", (byte)_s); pdu.Write("Reason", (byte)_r); return pdu; }
public void Read(RawPDU raw) { raw.ReadByte("Reserved"); _rt = (DicomRejectResult)raw.ReadByte("Result"); _so = (DicomRejectSource)raw.ReadByte("Source"); _rn = (DicomRejectReason)raw.ReadByte("Reason"); }
public RawPDU Write() { RawPDU pdu = new RawPDU((byte)0x06); pdu.Write("Reserved", (uint)0x00000000); return pdu; }
public RawPDU Write() { RawPDU pdu = new RawPDU((byte)0x03); pdu.Write("Reserved", (byte)0x00); pdu.Write("Result", (byte)_rt); pdu.Write("Source", (byte)_so); pdu.Write("Reason", (byte)_rn); return pdu; }
public void Read(RawPDU raw) { uint l = raw.Length; 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); ushort 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, (DicomPresContextResult)res); TransferSyntax acceptedSyntax = TransferSyntax.GetTransferSyntax(tx); if (acceptedSyntax != null) _assoc.SetAcceptedTransferSyntax(id, acceptedSyntax); else _assoc.GetPresentationContext(id).ClearTransfers(); } 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.RemoteMaximumPduLength = raw.ReadUInt32("Max PDU Length"); } else if (ut == 0x52) { _assoc.ImplementationClass = DicomUids.Lookup(raw.ReadString("Implementation Class UID", ul)); } 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); } } }
public RawPDU Write() { RawPDU pdu = new RawPDU((byte)0x02); pdu.Write("Version", (ushort)0x0001); pdu.Write("Reserved", 0x00, 2); pdu.Write("Called AE", _assoc.CalledAE, 16, ' '); pdu.Write("Calling AE", _assoc.CallingAE, 16, ' '); pdu.Write("Reserved", 0x00, 32); // Application Context pdu.Write("Item-Type", (byte)0x10); pdu.Write("Reserved", (byte)0x00); pdu.MarkLength16("Item-Length"); pdu.Write("Application Context Name", DicomUids.DICOMApplicationContextName.UID); pdu.WriteLength16(); foreach (DicomPresContext pc in _assoc.GetPresentationContexts()) { // Presentation Context pdu.Write("Item-Type", (byte)0x21); pdu.Write("Reserved", (byte)0x00); pdu.MarkLength16("Item-Length"); pdu.Write("Presentation Context ID", (byte)pc.ID); pdu.Write("Reserved", (byte)0x00); pdu.Write("Result", (byte)pc.Result); pdu.Write("Reserved", (byte)0x00); // Transfer Syntax pdu.Write("Item-Type", (byte)0x40); pdu.Write("Reserved", (byte)0x00); pdu.MarkLength16("Item-Length"); if (pc.AcceptedTransferSyntax!=null) // pdu.Write("Transfer Syntax UID", pc.AcceptedTransferSyntax.DicomUid.UID); pdu.WriteLength16(); pdu.WriteLength16(); } // User Data Fields pdu.Write("Item-Type", (byte)0x50); pdu.Write("Reserved", (byte)0x00); pdu.MarkLength16("Item-Length"); // Maximum PDU pdu.Write("Item-Type", (byte)0x51); pdu.Write("Reserved", (byte)0x00); pdu.Write("Item-Length", (ushort)0x0004); pdu.Write("Max PDU Length", (uint)_assoc.LocalMaximumPduLength); // Implementation Class UID pdu.Write("Item-Type", (byte)0x52); pdu.Write("Reserved", (byte)0x00); pdu.MarkLength16("Item-Length"); pdu.Write("Implementation Class UID", DicomImplementation.ClassUID.UID); pdu.WriteLength16(); // Implementation Version pdu.Write("Item-Type", (byte)0x55); pdu.Write("Reserved", (byte)0x00); pdu.MarkLength16("Item-Length"); pdu.Write("Implementation Version", DicomImplementation.Version); pdu.WriteLength16(); pdu.WriteLength16(); return pdu; }
private void SendRawPDU(RawPDU pdu) { ResetDimseTimeout(); // If the try/catch is reintroduced here, it must // throw an exception, if the exception is just eaten, // you can get into a case where there's repetetive errors // trying to send PDUs, until a whole message is sent. //try //{ if (_network != null) pdu.WritePDU(_network); //} //catch (Exception e) //{ // OnNetworkError(e); // throw new DicomException("Unexpected exception when writing PDU",e); //} ResetDimseTimeout(); }
public void Read(RawPDU raw) { raw.ReadByte("Reserved"); raw.ReadByte("Reserved"); _s = (DicomAbortSource)raw.ReadByte("Source"); _r = (DicomAbortReason)raw.ReadByte("Reason"); }
/// <summary> /// Internal routine for enqueueing a PDU for transfer. /// </summary> /// <param name="pdu"></param> internal void EnqueuePdu(RawPDU pdu) { if (_multiThreaded) _pduQueue.Enqueue(pdu); else SendRawPDU(pdu); }
public RawPDU Write() { RawPDU pdu = new RawPDU(0x01); pdu.Write("Version", (ushort)0x0001); pdu.Write("Reserved", 0x00, 2); pdu.Write("Called AE", _assoc.CalledAE, 16, ' '); pdu.Write("Calling AE", _assoc.CallingAE, 16, ' '); pdu.Write("Reserved", 0x00, 32); // Application Context pdu.Write("Item-Type", (byte)0x10); pdu.Write("Reserved", (byte)0x00); pdu.MarkLength16("Item-Length"); pdu.Write("Application Context Name", DicomUids.DICOMApplicationContextName.UID); pdu.WriteLength16(); if (_assoc.GetPresentationContexts().Count == 0) throw new DicomException("No presentation contexts set for association"); foreach (DicomPresContext pc in _assoc.GetPresentationContexts()) { // Presentation Context pdu.Write("Item-Type", (byte)0x20); pdu.Write("Reserved", (byte)0x00); pdu.MarkLength16("Item-Length"); pdu.Write("Presentation Context ID", (byte)pc.ID); pdu.Write("Reserved", (byte)0x00, 3); // Abstract Syntax pdu.Write("Item-Type", (byte)0x30); pdu.Write("Reserved", (byte)0x00); pdu.MarkLength16("Item-Length"); pdu.Write("Abstract Syntax UID", pc.AbstractSyntax.Uid); pdu.WriteLength16(); if (pc.GetTransfers().Count == 0) throw new DicomException("No transfer syntaxes set for presentation context " + pc.AbstractSyntax.Name ); // Transfer Syntax foreach (TransferSyntax ts in pc.GetTransfers()) { pdu.Write("Item-Type", (byte)0x40); pdu.Write("Reserved", (byte)0x00); pdu.MarkLength16("Item-Length"); pdu.Write("Transfer Syntax UID", ts.DicomUid.UID); pdu.WriteLength16(); } pdu.WriteLength16(); } // User Data Fields pdu.Write("Item-Type", 0x50); pdu.Write("Reserved", 0x00); pdu.MarkLength16("Item-Length"); // Maximum PDU pdu.Write("Item-Type", 0x51); pdu.Write("Reserved", 0x00); pdu.Write("Item-Length", (ushort)0x0004); pdu.Write("Max PDU Length", _assoc.LocalMaximumPduLength); // Asychronous Window if (_assoc.LocalMaxOperationsInvoked != 1 || _assoc.LocalMaxOperationsPerformed != 1) { pdu.Write("Item-Type", (byte) 0x53); pdu.Write("Reserved", (byte) 0x00); pdu.Write("Item-Length", (ushort) 0x0004); pdu.Write("Max Operations Invoked", (ushort)_assoc.LocalMaxOperationsInvoked); pdu.Write("Max Operations Invoked", (ushort)_assoc.LocalMaxOperationsPerformed); } // SCU / SCP Role Selection // Implementation Class UID pdu.Write("Item-Type", (byte)0x52); pdu.Write("Reserved", (byte)0x00); pdu.MarkLength16("Item-Length"); pdu.Write("Implementation Class UID", DicomImplementation.ClassUID.UID); pdu.WriteLength16(); // Implementation Version pdu.Write("Item-Type", (byte)0x55); pdu.Write("Reserved", (byte)0x00); pdu.MarkLength16("Item-Length"); pdu.Write("Implementation Version", DicomImplementation.Version); pdu.WriteLength16(); pdu.WriteLength16(); return pdu; }