Microsoft.Protocols.TestSuites.MS_OXORULE.NspiRpcAdapter.NspiBind C# (CSharp) Method

NspiBind() public method

The NspiBind method initiates a session between a client and the server.
public NspiBind ( uint flags, STAT stat, FlatUID_r &serverGuid, bool needRetry = true ) : ErrorCodeValue
flags uint A DWORD value that contains a set of bit flags.
stat STAT A pointer to a STAT block that describes a logical position in a specific address book container.
serverGuid FlatUID_r The value NULL or a pointer to a GUID value that is associated with the specific server.
needRetry bool 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.
return ErrorCodeValue
        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;
        }