Example #1
0
        /// <summary>
        /// The NspiBind method initiates a session between a client and the server.
        /// </summary>
        /// <param name="flags">A DWORD value that contains a set of bit flags.</param>
        /// <param name="stat">A pointer to a STAT block that describes a logical position in a specific address book container.</param>
        /// <param name="serverGuid">The value NULL or a pointer to a GUID value that is associated with the specific server.</param>
        /// <param name="needRetry">A Boolean value indicates if need to retry to get an expected result. This parameter is designed to avoid meaningless retry when an error response is expected.</param>
        /// <returns>Status of NSPI method.</returns>
        public ErrorCodeValue NspiBind(uint flags, STAT stat, ref FlatUID_r?serverGuid, bool needRetry = true)
        {
            int result;

            IntPtr ptrServerGuid = IntPtr.Zero;
            IntPtr ptrStat       = AdapterHelper.AllocStat(stat);

            if (serverGuid.HasValue && serverGuid.Value.Ab != null)
            {
                ptrServerGuid = AdapterHelper.AllocFlatUID_r(serverGuid.Value);
            }

            int retryCount = 0;

            do
            {
                try
                {
                    result = OxnspiInterop.NspiBind(this.rpcBinding, flags, ref stat, ptrServerGuid, ref this.contextHandle);
                }
                catch (SEHException e)
                {
                    result = (int)NativeMethods.RpcExceptionCode(e);
                    this.site.Log.Add(LogEntryKind.Comment, "RPC component throws exception, the error code is {0}, the error message is: {1}", result, new Win32Exception(result).ToString());
                }

                if ((ErrorCodeValue)result == ErrorCodeValue.GeneralFailure && needRetry)
                {
                    Thread.Sleep(this.waitTime);
                }
                else
                {
                    break;
                }

                retryCount++;
            }while ((ErrorCodeValue)result == ErrorCodeValue.GeneralFailure && retryCount < this.maxRetryCount);

            if (!Enum.IsDefined(typeof(ErrorCodeValue), (uint)result))
            {
                throw new ArgumentException(string.Format("An unknown error is returned, the error code is: {0} and the error message is: {1}", result, new Win32Exception(result).ToString()));
            }

            // Parse ServerGuid from ptrServerGuid.
            if (ptrServerGuid == IntPtr.Zero)
            {
                serverGuid = null;
            }
            else
            {
                serverGuid = AdapterHelper.ParseFlatUID_r(ptrServerGuid);
            }

            // Free allocated memory for serverGuid.
            if (serverGuid.HasValue)
            {
                if (serverGuid.Value.Ab != null)
                {
                    Marshal.FreeHGlobal(ptrServerGuid);
                }
            }

            // Free allocated memory for stat.
            Marshal.FreeHGlobal(ptrStat);
            return((ErrorCodeValue)result);
        }