Пример #1
0
            /// <summary>
            /// Starts operation processing.
            /// </summary>
            /// <param name="dnsClient">DNS client.</param>
            /// <returns>Returns true if asynchronous operation in progress or false if operation completed synchronously.</returns>
            /// <exception cref="ArgumentNullException">Is raised when <b>dnsClient</b> is null reference.</exception>
            internal bool Start(Dns_Client dnsClient)
            {   
                if(dnsClient == null){
                    throw new ArgumentNullException("dnsClient");
                }

                SetState(AsyncOP_State.Active);

                m_pHostEntries = new HostEntry[m_pHostNames.Length];

                // Create look up operations for hosts. The "opList" copy array is needed because
                // when we start asyn OP, m_pIpLookupQueue may be altered when OP completes.
                Dictionary<int,GetHostAddressesAsyncOP> opList = new Dictionary<int,GetHostAddressesAsyncOP>();
                for(int i=0;i<m_pHostNames.Length;i++){
                    GetHostAddressesAsyncOP op = new GetHostAddressesAsyncOP(m_pHostNames[i]);
                    m_pIpLookupQueue.Add(i,op);
                    opList.Add(i,op);
                }

                // Start operations.
                foreach(KeyValuePair<int,GetHostAddressesAsyncOP> entry in opList){
                    // NOTE: We may not access "entry" in CompletedAsync, because next for loop reassigns this value.
                    int index = entry.Key;

                    // This event is raised when GetHostAddressesAsync completes asynchronously.
                    entry.Value.CompletedAsync += delegate(object s1,EventArgs<GetHostAddressesAsyncOP> e1){                        
                        GetHostAddressesCompleted(e1.Value,index);
                    };                    
                    // GetHostAddressesAsync completes synchronously.
                    if(!dnsClient.GetHostAddressesAsync(entry.Value)){
                        GetHostAddressesCompleted(entry.Value,index);
                    }
                }

                // Set flag rise CompletedAsync event flag. The event is raised when async op completes.
                // If already completed sync, that flag has no effect.
                lock(m_pLock){
                    m_RiseCompleted = true;

                    return m_State == AsyncOP_State.Active;
                }
            }
Пример #2
0
            /// <summary>
            /// This method is called when GetHostAddresses operation has completed.
            /// </summary>
            /// <param name="op">Asynchronous operation.</param>
            /// <param name="index">Index in 'm_pHostEntries' where to store lookup result.</param>
            private void GetHostAddressesCompleted(GetHostAddressesAsyncOP op,int index)
            {
                lock(m_pLock){
                    try{
                        if(op.Error != null){
                            // We wanted any of the host names to resolve:
                            //  *) We have already one resolved host name.
                            //  *) We have more names to resolve, so next may succeed.
                            if(m_ResolveAny && (m_ResolvedCount > 0 || m_pIpLookupQueue.Count > 1)){
                            }
                            else{
                                m_pException = op.Error;
                            }
                        }
                        else{
                            m_pHostEntries[index] = new HostEntry(op.HostNameOrIP,op.Addresses,null);
                            m_ResolvedCount++;
                        }

                        m_pIpLookupQueue.Remove(index);
                        if(m_pIpLookupQueue.Count == 0){
                            // We wanted resolve any, so some host names may not be resolved and are null, remove them from response.
                            if(m_ResolveAny){
                                List<HostEntry> retVal = new List<HostEntry>();
                                foreach(HostEntry host in m_pHostEntries){
                                    if(host != null){
                                        retVal.Add(host);
                                    }
                                }

                                m_pHostEntries = retVal.ToArray();
                            }

                            SetState(AsyncOP_State.Completed);
                        }
                    }
                    catch(Exception x){
                        m_pException = x;

                        SetState(AsyncOP_State.Completed);
                    }
                }

                op.Dispose();
            }
Пример #3
0
        /// <summary>
        /// Starts resolving host IPv4 and IPv6 addresses.
        /// </summary>
        /// <param name="op">Asynchronous operation.</param>
        /// <returns>Returns true if aynchronous operation is pending (The <see cref="GetHostAddressesAsyncOP.CompletedAsync"/> event is raised upon completion of the operation).
        /// Returns false if operation completed synchronously.</returns>
        /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and and this method is accessed.</exception>
        /// <exception cref="ArgumentNullException">Is raised when <b>op</b> is null reference.</exception>
        public bool GetHostAddressesAsync(GetHostAddressesAsyncOP op)
        {
            if(m_IsDisposed){
                throw new ObjectDisposedException(this.GetType().Name);
            }
            if(op == null){
                throw new ArgumentNullException("op");
            }
            if(op.State != AsyncOP_State.WaitingForStart){
                throw new ArgumentException("Invalid argument 'op' state, 'op' must be in 'AsyncOP_State.WaitingForStart' state.","op");
            }

            return op.Start(this);
        }