/// <summary> /// This method retrieves a PDU from a byte stream. /// </summary> /// <param name="stm">Byte stream</param> public void Deserialize(SmppReader stm) { state_ = PduStatus.invalid; try { // Get the header from the buffer length_ = stm.ReadInt32(); int id = stm.ReadInt32(); status_ = stm.ReadInt32(); sequenceNumber_ = stm.ReadInt32(); if (id != this.CommandId) { throw new UnexpectedPduException(id); } state_ = PduStatus.validHeader; // Now read the body of the PDU if (length_ > REQUIRED_SIZE) { this.GetFromStream(stm); state_ |= PduStatus.validBody; // Read any optional parameters that are present. while (!((SmppByteStream)stm.Stream).EOS) { TlvParameter tlv = new TlvParameter(); try { tlv.GetFromStream(stm); TlvParameter tlvExisting = GetOptionalElement(tlv.Tag); if (tlvExisting == null) { AddOptionalElements(tlv); } else { if (tlvExisting.Tag == tlv.Tag) { tlvExisting.Data = tlv.Data; } else { throw new TlvException("Read bad Tlv stream!"); } } } catch (ArgumentOutOfRangeException) { } } } } catch (InvalidOperationException e) { throw new IncompletePduException(this, e); } catch (PduException e) { e.PDU = this; throw e; } catch (System.Exception e) { throw new PduException(this, e); } }
/// <summary> /// This method adds the PDU to the given byte stream. /// </summary> /// <param name="stm"></param> public void Serialize(SmppWriter stm) { bool includeBody = (this.Status == StatusCodes.ESME_ROK); int length = REQUIRED_SIZE; byte version = stm.Version; SmppByteStream body = null, optBody = null; // Get the optional (Tlv) parameters if (includeBody == true) { // Serialize the body into a stream. body = new SmppByteStream(); new SmppWriter(body, version).Add(this); length += (int)body.Length; // If we are using at least V3.4 of the SMPP spec // then include Tlvs, otherwise we do not. if (version >= SmppVersion.SMPP_V34) { int size = optionalParameters_.Count; if (size > 0) { optBody = new SmppByteStream(); SmppWriter writer = new SmppWriter(optBody, version); for (int i = 0; i < size; i++) { TlvParameter tlv = (TlvParameter)optionalParameters_[i]; if (tlv != null && tlv.HasValue) { writer.Add(tlv); } } // Add the length of all optional parameters length += (int)optBody.Length; } } } // Now serialize the whole thing together. stm.Add(length); stm.Add(CommandId); stm.Add(status_); stm.Add(sequenceNumber_); if (includeBody == true) { if (body != null && body.Length > 0) { stm.Add(body); } if (optBody != null && optBody.Length > 0) { stm.Add(optBody); } } }
/// <summary> /// This method returns the count of elements matching the given tag. /// </summary> /// <param name="tag">Element Tag of the Tlv</param> /// <returns>Count of found elements</returns> public int GetOptionalElementCount(short tag) { int size = optionalParameters_.Count, count = 0; for (int i = 0; i < size; i++) { TlvParameter tlv = (TlvParameter)optionalParameters_[i]; if (tlv != null && tlv.Tag == tag) { count++; } } return(count); }
/// <summary> /// Locates and returns Tlvs which have the same tag (i.e. repeating Tlvs) /// </summary> /// <param name="tag">Tag of the Tlv to find</param> /// <returns>Array of TlvParameter objects</returns> public TlvParameter[] GetRepeatedOptionalElements(short tag) { ArrayList al = new ArrayList(); int size = optionalParameters_.Count; for (int i = 0; i < size; i++) { TlvParameter tlv = (TlvParameter)optionalParameters_[i]; if (tlv != null && tlv.Tag == tag) { al.Add(tlv); } } return((TlvParameter[])al.ToArray(typeof(TlvParameter))); }
/// <summary> /// Locates any registered Tlv with the given Tlv tag. /// </summary> /// <param name="tag">the tag of the Tlv required</param> /// <returns> the Tlv</returns> public TlvParameter GetOptionalElement(short tag) { int size = optionalParameters_.Count; TlvParameter tlv = null; for (int i = 0; i < size; i++) { tlv = (TlvParameter)optionalParameters_[i]; if (tlv != null) { if (tlv.Tag == tag) { return(tlv); } } } return(null); }