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;
}