using System; namespace Unosquare.Swan.Networking.Ldap { /// /// The base class for Ldap request and response messages. /// Subclassed by response messages used in asynchronous operations. /// public class LdapMessage { internal RfcLdapMessage Message; private Int32 _imsgNum = -1; // This instance LdapMessage number private LdapOperation _messageType = LdapOperation.Unknown; private String _stringTag; internal LdapMessage() { } /// /// Initializes a new instance of the class. /// Creates an LdapMessage when sending a protocol operation and sends /// some optional controls with the message. /// /// The type. /// The operation type of message. /// The controls to use with the operation. /// internal LdapMessage(LdapOperation type, IRfcRequest op, LdapControl[] controls = null) { // Get a unique number for this request message this._messageType = type; RfcControls asn1Ctrls = null; if(controls != null) { // Move LdapControls into an RFC 2251 Controls object. asn1Ctrls = new RfcControls(); foreach(LdapControl t in controls) { asn1Ctrls.Add(t.Asn1Object); } } // create RFC 2251 LdapMessage this.Message = new RfcLdapMessage(op, asn1Ctrls); } /// /// Initializes a new instance of the class. /// Creates an Rfc 2251 LdapMessage when the libraries receive a response /// from a command. /// /// A response message. internal LdapMessage(RfcLdapMessage message) => this.Message = message; /// /// Returns the message ID. The message ID is an integer value /// identifying the Ldap request and its response. /// /// /// The message identifier. /// public virtual Int32 MessageId { get { if(this._imsgNum == -1) { this._imsgNum = this.Message.MessageId; } return this._imsgNum; } } /// /// Indicates whether the message is a request or a response. /// /// /// true if request; otherwise, false. /// public virtual Boolean Request => this.Message.IsRequest(); internal LdapOperation Type { get { if(this._messageType == LdapOperation.Unknown) { this._messageType = this.Message.Type; } return this._messageType; } } internal virtual RfcLdapMessage Asn1Object => this.Message; internal virtual LdapMessage RequestingMessage => this.Message.RequestingMessage; /// /// Retrieves the identifier tag for this message. /// An identifier can be associated with a message with the /// setTag method. /// Tags are set by the application and not by the API or the server. /// If a server response isRequest() == false has no tag, /// the tag associated with the corresponding server request is used. /// /// /// The tag. /// public virtual String Tag { get => this._stringTag ?? (this.Request ? null : this.RequestingMessage?._stringTag); set => this._stringTag = value; } private String Name => this.Type.ToString(); /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override String ToString() => $"{this.Name}({this.MessageId}): {this.Message}"; } }