public void Write(IProtocolBuffer buffer) { IProtocolBuffer bodybuffer = null; try { if (Packet != null) { Property.IsPacket = true; } if (Body != null) { Serializer serializer = SerializerFactory.Defalut.Get(Body.GetType()); if (serializer == null) { throw new ProtocolProcessError(string.Format("{0} serializer not found!", Body)); } ID = serializer.MessageType.ID; if (!serializer.MessageType.NoBody) { bodybuffer = ProtocolBufferPool.Default.Pop(); serializer.Serialize(Body, bodybuffer); if (bodybuffer.Length > MessageBodyProperty.BODY_LENGTH) { throw new ProtocolProcessError("message body to long!"); } Property.BodyLength = (ushort)bodybuffer.Length; } } buffer.WriteTag(); //write start buffer.Write(ID); //write id Property.Write(buffer); //write body property buffer.WriteBCD(SIM); //write sim buffer.Write(No); //write no if (Packet != null) //write packet { Packet.Save(buffer); } if (bodybuffer != null) //write body { buffer.WriteSubBuffer(bodybuffer); } byte crc = BufferUtils.GetCRC(buffer.Array, 1, buffer.Length - 1); buffer.Write(crc); //write crc buffer.WriteTag(); //write end } finally { if (bodybuffer != null) { ProtocolBufferPool.Default.Push(bodybuffer); } } }