接下来可以进行消息传递了 ,也就是dimse ,再来复习下 什么是dimse 。n-set  n-create c-echo 这些都是dimse  他们都是属于一种结构的pdu 那就是tf-pdu(传输数据和命令的都称之为tf-pdu 或者transfer pdu ,协商连接的都称之为associcate pdu) 。dimse 由 许多tag组成,就像文件解析那篇博文一样。
tf-pdu数据结构分析如下:

dicom网络通讯入门(3)-LMLPHP

如果你又要问此图是怎么来的 ,dicom标准第八章 33页。你可能又要问 dimse又是什么 ,dimse全称 dicom 消息服务元素DIMSE(DICOM Message Service Element)
为什么你就说echo属于dimse 。请看dicom标准第七章 的目录: 9     DIMSE-C        9.1.5      C-ECHO SERVICE 。不用我多说了噻。
在这之前还是像以前一样先把tf-pdu的数据结构跟序列化搞了吧:

     struct PDVset
{
//0x04
public byte pduType;
//pdu长度
public uint pduLen;
//pdv长度从作用来看他跟上面有所重复 pdv, presentation data value
public uint itemLen;
//这个contextID其实是指协商连接时的presentation context id
//最好判断下是否跟协商时的一致
public byte contextID;
//消息控制头 确定pdv类型是command 还是data 发送完成与否
public byte msgControlHeader; public SortedDictionary<uint, DataElement> elements; public byte[] serial()
{
if ((pduLen != && itemLen != ) == false)
return null;
//header
MemoryStream _stream = new MemoryStream((int)pduLen + );
WarpedStream stream = new WarpedStream(_stream); stream.writeByte(0x04);
stream.skip_write();
stream.writeUint(pduLen);
stream.writeUint(itemLen);
stream.writeByte(contextID);
stream.writeByte(msgControlHeader);
//items
foreach (DataElement item in elements.Values)
{
stream.writeBytes(item.serial());
} _stream.Flush(); byte[] data = new byte[_stream.Length];
Array.Copy(_stream.GetBuffer(), data, _stream.Length);
stream.close();
_stream.Close();
return data;
}
}
enum pdvType
{
command, data, commandAndData
} struct DataElement
{
public uint _tag;
public WarpedStream.byteOrder bytOrder;
public bool explicitVR;//是显式VR的 否则隐式VR
public uint tag
{
get { return _tag; }
set
{
_tag = value;
VR = VRs.GetVR(value);
uint _len = VRs.getLen(VR);
if (_len != )
len = _len;
}
}
public ushort VR;
//虽然长度为uint 但要看情况隐式时都是4字节 显式时除ow那几个外都是2字节
//如果为ow 显示不但长度为4 在之前还要跳过2字节,除ow那几个之外不用跳过
public uint len;
public byte[] value;
public IList<DataElement> items ;//子项
public bool haveItems;//是否包含子项 //值的显示
public string showValue()
{
if (haveItems )
return null; if (value != null)
return Tags.VFdecoding(VR, value, bytOrder);
else
return null;
}
//赋值
public void setValue(string valStr)
{
if (haveItems )
return; if (VRs.IsStringValue(VR)) {
len = (uint)valStr.Length;
value = Tags.VFencoding(VR, valStr, bytOrder, len);
}
else if (len != )//就是这个破地方 因为element的连续使用 导致会截断字符串
value = Tags.VFencoding(VR, valStr, bytOrder, len);
else
{
value = Tags.VFencoding(VR, valStr, bytOrder);
if (VRs.IsStringValue(VR))
len = (uint)value.Length;
}
}
//序列化
public byte[] serial()
{
MemoryStream data_serial = new MemoryStream();
serial(this, data_serial);
byte[] data = new byte[data_serial.Length];
Array.Copy(data_serial.GetBuffer(), data, data.Length);
data_serial.Close();
return data;
}
//序列化的递归调用
public void serial(DataElement element, MemoryStream data_serial)
{
//int len_serial = element.getSerialLen();
//if ((VR == VRs.SQ && len_serial == UInt32.MaxValue) || (tag == 0xfffee000 && len == UInt32.MaxValue))//靠 遇到文件夹开始标签了
if (element.haveItems )
{
//开始标记
data_serial.WriteByte((byte)((element._tag & 0x00ff0000) >> ));
data_serial.WriteByte((byte)((element._tag & 0xff000000) >> ));
data_serial.WriteByte((byte)(element._tag & 0x000000ff));
data_serial.WriteByte((byte)((element._tag & 0x0000ff00) >> ));
data_serial.WriteByte(0xff); data_serial.WriteByte(0xff); data_serial.WriteByte(0xff); data_serial.WriteByte(0xff); foreach (DataElement item in element.items)
{
item.serial(item, data_serial);
} //结束标记
if (element.VR == VRs.SQ)
{
data_serial.WriteByte((byte)((0xfffee0dd & 0x00ff0000) >> ));
data_serial.WriteByte((byte)((0xfffee0dd & 0xff000000) >> ));
data_serial.WriteByte((byte)(0xfffee0dd & 0x000000ff));
data_serial.WriteByte((byte)((0xfffee0dd & 0x0000ff00) >> ));
}
else
{
data_serial.WriteByte((byte)((0xfffee00d & 0x00ff0000) >> ));
data_serial.WriteByte((byte)((0xfffee00d & 0xff000000) >> ));
data_serial.WriteByte((byte)(0xfffee00d & 0x000000ff));
data_serial.WriteByte((byte)((0xfffee00d & 0x0000ff00) >> ));
}
data_serial.WriteByte(0x00); data_serial.WriteByte(0x00); data_serial.WriteByte(0x00); data_serial.WriteByte(0x00);
}
else
{
byte[] data = new byte[element.getSerialLen()];
uint _len = element.len;
if (_len % != )
_len++;
if (element.VR == VRs.SQ)
_len = 0xffffffff; data[] = (byte)((element._tag & 0x00ff0000) >> );
data[] = (byte)((element._tag & 0xff000000) >> );
data[] = (byte)(element._tag & 0x000000ff);
data[] = (byte)((element._tag & 0x0000ff00) >> ); if (element.explicitVR)//显示VR
{
data[] = 0x00;
data[] = 0x00;
if (VRs.IsLengthField16Bit(VR))
{
if (element.bytOrder == WarpedStream.byteOrder.bigEdition)
{
data[] = (byte)(_len & 0x000000ff);
data[] = (byte)((_len & 0x0000ff00) >> );
}
else
{
data[] = (byte)((_len & 0x0000ff00) >> );
data[] = (byte)(_len & 0x000000ff);
} for (int i = ; i < element.value.Length; i++)
data[ + i] = element.value[i];
}
else
{
if (element.bytOrder == WarpedStream.byteOrder.bigEdition)
{
data[] = (byte)((_len & 0xff000000) >> );
data[] = (byte)((_len & 0x00ff0000) >> );
data[] = (byte)((_len & 0x0000ff00) >> );
data[] = (byte)(_len & 0x000000ff); }
else
{
data[] = (byte)(_len & 0x000000ff);
data[] = (byte)((_len & 0x0000ff00) >> );
data[] = (byte)((_len & 0x00ff0000) >> );
data[] = (byte)((_len & 0xff000000) >> );
}
if (element.value == null)
throw new Exception(string.Format("异常:tag{0} value未赋值 ", Tags.ToHexString(element.tag))); for (int i = ; i < element.value.Length; i++)
data[ + i] = element.value[i];
}
//len_ser = (int)(4 + 2 + 4 + len);
//len_ser = (int)(4 + 2 + len);
}
else //隐式Vr
{
if (element.bytOrder == WarpedStream.byteOrder.bigEdition)
{
data[] = (byte)((_len & 0xff000000) >> );
data[] = (byte)((_len & 0x00ff0000) >> );
data[] = (byte)((_len & 0x0000ff00) >> );
data[] = (byte)(_len & 0x000000ff);
}
else
{
data[] = (byte)(_len & 0x000000ff);
data[] = (byte)((_len & 0x0000ff00) >> );
data[] = (byte)((_len & 0x00ff0000) >> );
data[] = (byte)((_len & 0xff000000) >> ); }
if (element.value == null)
throw new Exception(string.Format("异常:tag{0} value未赋值 ", Tags.ToHexString(element.tag))); for (int i = ; i < element.value.Length; i++)
data[ + i] = element.value[i];
}
data_serial.Write(data, , data.Length);
}
} //获取单个元素序列化长度的递归调用
public void getSerialLen_item(DataElement element, ref int len)
{
if (element.haveItems)
{
len += element.getHeaderLen();
foreach (DataElement item in element.items)
getSerialLen_item(item, ref len);
len += ;
}
else
{
if (element.value != null)
len += element.getHeaderLen() + element.value.Length;
else
len += element.getHeaderLen();
}
if (len % != )//文件元信息元素整体字节数一定是偶数(包括tag VR 数据长度 数据 这些一起)
len++;
} //获取序列化后整个元素的长度
public int getSerialLen()
{
int serial_len=;
getSerialLen_item(this, ref serial_len);
return serial_len;
} //获取item的header长度
public int getHeaderLen()
{
int len_ser = ;
int len_tmp = ;
if (explicitVR)//显示VR
{
if (tag == 0xfffee000 || tag == 0xfffee00d || tag == 0xfffee0dd)
len_ser = + ;
else if (VR == VRs.OB || VR == VRs.OW || VR == VRs.OF ||
VR == VRs.UT || VR == VRs.SQ || VR == VRs.UN)
len_ser = (int)( + + + len_tmp);
else
len_ser = (int)( + + len_tmp);
}
else //隐式Vr
{
len_ser = (int)( + + len_tmp);
} return len_ser;
}
}

不要问我pdv是啥 第一篇就出现过,pdv 即p data value ,它包括许多的data element 也就是俗称tag。一个元素接一个元素 直到结束 跟文件解析的时候一样 ,他的vr方式 以及字节序 在协商连接的时候就已确定 你只管读就是了。那么新的问题又来了 echo这个dimse到底包含哪些tag 他们的值又应该各是多少?为了解决你这个疑问我又要翻一个表出来:
dicom网络通讯入门(3)-LMLPHP
你又要问这个表是怎么来的 ,dicom第七章 53页。具体每个tag的作用各是什么 请参照右边的说明,有三个地方我要提一下:

affected sop class uid
受影响的sop uid ,看过第一篇里图的筒子都知道 打印有打印sop uid ,filmbox 有filmboxuid,那么这里echo也有 对应的 他就是 :

 //SOPClass: Verification SOP Class
public const String Verification = "1.2.840.10008.1.1";

为啥是这个 我不会再说了 你懂的。
command field
这个是作甚的,他的作用是用来区分不同的dimse 比如 c-create  c-find ,不用我多讲了 旁边的说明已经很明显了 echo时他的值应设置成0x0030  。
command data set type
数据集选项 ,说白了就是给个标识 后面有无数据集,我们这里自然是没有 那么应设置成0x0101 。

好了开工吧,打住 不是说还有服务类规范么 ,只有复杂的才有服务类规范 我们这个echo是非常非常非常之简单的 所以没有服务类规范 直接开动吧:

         //组织Verification_CECHORSP响应原语
//rq端无data ,rsp端无data
public void Verification_CECHORQ()
{
PDVset rq = new PDVset();
rq.pduType = 0x04;
rq.contextID = pstContextId;
rq.msgControlHeader = 0x03;
rq.elements = new SortedDictionary<uint, DataElement>(); int len = ; DataElement element = new DataElement();
element.bytOrder = bytOrder; element.tag = 0x00000002;
element.setValue(UIDs.Verification);
rq.elements.Add(0x00000002, element);
len += (element.getSerialLen()); element.tag = 0x00000100;
element.setValue(0x0030.ToString());
rq.elements.Add(0x00000100, element);
len += (element.getSerialLen()); element.tag = 0x00000110;
element.setValue(0x03.ToString());
rq.elements.Add(0x00000110, element);
len += (element.getSerialLen()); element.tag = 0x00000800;//有无对应的数据段
element.setValue(0x0101.ToString());
rq.elements.Add(0x00000800, element);
len += (element.getSerialLen()); element.tag = 0x00000000;//消息原语数据长度
element.setValue(len.ToString());
rq.elements.Add(0x00000000, element);
//len += (element.getSerialLen()); rq.itemLen = (uint)( + + len); rq.pduLen = rq.itemLen + ; //进行c-echo-rsp响应
stream.writeBytes(rq.serial());
}

看看代码里面特定的地方 是不是跟我上面描述的一样?就这样so easy  。看到没 其实我这些都是在dicom文档里翻的 就这样而已没什么神奇的 相信你也能。再来复习下dicom标准跟网络通讯相关的几个章节 :

DICOM Part 4: Service Class Specifications  ,服务类规范

DICOM Part 7: Message Exchange 消息交换

DICOM Part 8: Network Communication Support for Message Exchange 网络通讯对消息交换的支持

按照他们的套路来 就水到渠成 。

这是我的测试结果 不用怀疑哥的水平 哥是拿到医院去测试过的:

dicom网络通讯入门(3)-LMLPHP

05-08 15:42