private void GetResultsHelper(LdapPartialAsyncResult asyncResult)
        {
            LdapConnection ldapConnection = asyncResult.con;
            ResultAll      resultAll      = ResultAll.LDAP_MSG_RECEIVED;

            if (asyncResult.resultStatus == ResultsStatus.CompleteResult)
            {
                resultAll = ResultAll.LDAP_MSG_POLLINGALL;
            }
            try
            {
                SearchResponse searchResponse = (SearchResponse)ldapConnection.ConstructResponse(asyncResult.messageID, LdapOperation.LdapSearch, resultAll, asyncResult.requestTimeout, false);
                if (searchResponse != null)
                {
                    if (asyncResult.response == null)
                    {
                        asyncResult.response = searchResponse;
                    }
                    else
                    {
                        this.AddResult(asyncResult.response, searchResponse);
                    }
                    if (searchResponse.searchDone)
                    {
                        asyncResult.resultStatus = ResultsStatus.Done;
                    }
                }
                else
                {
                    DateTime now = DateTime.Now;
                    if (asyncResult.startTime.Ticks + asyncResult.requestTimeout.Ticks <= now.Ticks)
                    {
                        throw new LdapException(85, LdapErrorMappings.MapResultCode(85));
                    }
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                if (exception as DirectoryOperationException == null)
                {
                    if (exception as LdapException != null)
                    {
                        LdapException ldapException = (LdapException)exception;
                        //TODO: Review: ldapException.ErrorCode;
                        if (asyncResult.response != null)
                        {
                            if (asyncResult.response.Entries != null)
                            {
                                for (int i = 0; i < asyncResult.response.Entries.Count; i++)
                                {
                                    ldapException.results.Add(asyncResult.response.Entries[i]);
                                }
                            }
                            if (asyncResult.response.References != null)
                            {
                                for (int j = 0; j < asyncResult.response.References.Count; j++)
                                {
                                    ldapException.results.Add(asyncResult.response.References[j]);
                                }
                            }
                        }
                    }
                }
                else
                {
                    SearchResponse response = (SearchResponse)((DirectoryOperationException)exception).Response;
                    if (asyncResult.response == null)
                    {
                        asyncResult.response = response;
                    }
                    else
                    {
                        this.AddResult(asyncResult.response, response);
                    }
                    ((DirectoryOperationException)exception).response = asyncResult.response;
                }
                asyncResult.exception    = exception;
                asyncResult.resultStatus = ResultsStatus.Done;
                Wldap32.ldap_abandon(ldapConnection.ldapHandle, asyncResult.messageID);
            }
        }
Ejemplo n.º 2
0
        private void GetResultsHelper(LdapPartialAsyncResult asyncResult)
        {
            LdapConnection con          = asyncResult.con;
            IntPtr         ldapResult   = (IntPtr)0;
            IntPtr         entryMessage = (IntPtr)0;
            ResultAll      resultType   = ResultAll.LDAP_MSG_RECEIVED;

            if (asyncResult.resultStatus == ResultsStatus.CompleteResult)
            {
                resultType = ResultAll.LDAP_MSG_POLLINGALL;
            }

            try
            {
                SearchResponse response = (SearchResponse)con.ConstructResponse(asyncResult.messageID, LdapOperation.LdapSearch, resultType, asyncResult.requestTimeout, false);
                // this should only happen in the polling thread case
                if (response == null)
                {
                    // only when request time out has not yet expiered
                    if ((asyncResult.startTime.Ticks + asyncResult.requestTimeout.Ticks) > DateTime.Now.Ticks)
                    {
                        // this is expected, just the client does not have the result yet
                        return;
                    }
                    else
                    {
                        // time out, now we need to throw proper exception
                        throw new LdapException((int)LdapError.TimeOut, LdapErrorMappings.MapResultCode((int)LdapError.TimeOut));
                    }
                }

                if (asyncResult.response != null)
                {
                    AddResult(asyncResult.response, response);
                }
                else
                {
                    asyncResult.response = response;
                }

                // if search is done, set the flag
                if (response.searchDone)
                {
                    asyncResult.resultStatus = ResultsStatus.Done;
                }
            }
            catch (Exception e)
            {
                if (e is DirectoryOperationException)
                {
                    SearchResponse response = (SearchResponse)(((DirectoryOperationException)e).Response);

                    if (asyncResult.response != null)
                    {
                        AddResult(asyncResult.response, response);
                    }
                    else
                    {
                        asyncResult.response = response;
                    }

                    // set the response back to the exception so it holds all the results up to now
                    ((DirectoryOperationException)e).Response = asyncResult.response;
                }
                else if (e is LdapException)
                {
                    LdapException ldapE     = (LdapException)e;
                    LdapError     errorCode = (LdapError)ldapE.ErrorCode;

                    if (asyncResult.response != null)
                    {
                        // add previous retrieved entries if available
                        if (asyncResult.response.Entries != null)
                        {
                            for (int i = 0; i < asyncResult.response.Entries.Count; i++)
                            {
                                ldapE.results.Add(asyncResult.response.Entries[i]);
                            }
                        }

                        // add previous retrieved references if available
                        if (asyncResult.response.References != null)
                        {
                            for (int i = 0; i < asyncResult.response.References.Count; i++)
                            {
                                ldapE.results.Add(asyncResult.response.References[i]);
                            }
                        }
                    }
                }

                // exception occurs, this operation is done.
                asyncResult.exception    = e;
                asyncResult.resultStatus = ResultsStatus.Done;

                // need to abandon this request
                Wldap32.ldap_abandon(con.ldapHandle, asyncResult.messageID);
            }
        }
        private void GetResultsHelper(LdapPartialAsyncResult asyncResult)
        {
            LdapConnection con        = asyncResult.con;
            IntPtr         ldapHandle = con.ldapHandle;
            ResultAll      resultType = ResultAll.LDAP_MSG_RECEIVED;

            if (asyncResult.resultStatus == ResultsStatus.CompleteResult)
            {
                resultType = ResultAll.LDAP_MSG_POLLINGALL;
            }
            try
            {
                SearchResponse newResult = (SearchResponse)con.ConstructResponse(asyncResult.messageID, LdapOperation.LdapSearch, resultType, asyncResult.requestTimeout, false);
                if (newResult == null)
                {
                    if ((asyncResult.startTime.Ticks + asyncResult.requestTimeout.Ticks) <= DateTime.Now.Ticks)
                    {
                        throw new LdapException(0x55, LdapErrorMappings.MapResultCode(0x55));
                    }
                }
                else
                {
                    if (asyncResult.response != null)
                    {
                        this.AddResult(asyncResult.response, newResult);
                    }
                    else
                    {
                        asyncResult.response = newResult;
                    }
                    if (newResult.searchDone)
                    {
                        asyncResult.resultStatus = ResultsStatus.Done;
                    }
                }
            }
            catch (Exception exception)
            {
                if (exception is DirectoryOperationException)
                {
                    SearchResponse response = (SearchResponse)((DirectoryOperationException)exception).Response;
                    if (asyncResult.response != null)
                    {
                        this.AddResult(asyncResult.response, response);
                    }
                    else
                    {
                        asyncResult.response = response;
                    }
                    ((DirectoryOperationException)exception).response = asyncResult.response;
                }
                else if (exception is LdapException)
                {
                    LdapException exception2 = (LdapException)exception;
                    int           errorCode  = exception2.ErrorCode;
                    if (asyncResult.response != null)
                    {
                        if (asyncResult.response.Entries != null)
                        {
                            for (int i = 0; i < asyncResult.response.Entries.Count; i++)
                            {
                                exception2.results.Add(asyncResult.response.Entries[i]);
                            }
                        }
                        if (asyncResult.response.References != null)
                        {
                            for (int j = 0; j < asyncResult.response.References.Count; j++)
                            {
                                exception2.results.Add(asyncResult.response.References[j]);
                            }
                        }
                    }
                }
                asyncResult.exception    = exception;
                asyncResult.resultStatus = ResultsStatus.Done;
                Wldap32.ldap_abandon(con.ldapHandle, asyncResult.messageID);
            }
        }