From 4df0612f64a60e00215046e3c8138e81f952d577 Mon Sep 17 00:00:00 2001 From: Jim Norman Date: Wed, 5 Dec 2007 20:38:20 +0000 Subject: [PATCH] Bug 339461. Fixed memory leak that caused random errors or hangs. Also increased NamedPipe Channels to 1024. --- .../ImpersonateWrapper.cs | 425 +++++++++--------- CASA/micasad/cache/SecretStore.cs | 4 +- CASA/micasad/communication/win/PipeManager.cs | 290 ++++++------ CASA/test/c_sample/cSample.c | 12 +- 4 files changed, 371 insertions(+), 360 deletions(-) diff --git a/CASA/extern/w32/namedpipes/AppModule.NamedPipes/ImpersonateWrapper.cs b/CASA/extern/w32/namedpipes/AppModule.NamedPipes/ImpersonateWrapper.cs index e6ea2319..293827e7 100644 --- a/CASA/extern/w32/namedpipes/AppModule.NamedPipes/ImpersonateWrapper.cs +++ b/CASA/extern/w32/namedpipes/AppModule.NamedPipes/ImpersonateWrapper.cs @@ -18,8 +18,8 @@ * To contact Novell about this file by physical or electronic mail, * you may find current contact information at www.novell.com. * - ***********************************************************************/ - + ***********************************************************************/ + using System; using AppModule.NamedPipes; @@ -28,133 +28,131 @@ using System.Text; using HANDLE = System.IntPtr; -namespace AppModule.NamedPipes +namespace AppModule.NamedPipes { - #region Comments - /// - /// A utility class that exposes named pipes operations. - /// - /// - /// This class uses the exposed exposed kernel32.dll methods by the - /// NamedPipeNative class - /// to provided controlled named pipe functionality. - /// - #endregion - public sealed class ImpersonateWrapper - { + #region Comments + /// + /// A utility class that exposes named pipes operations. + /// + /// + /// This class uses the exposed exposed kernel32.dll methods by the + /// NamedPipeNative class + /// to provided controlled named pipe functionality. + /// + #endregion + public sealed class ImpersonateWrapper + { - public const int TOKEN_QUERY = 0X00000008; + public const int TOKEN_QUERY = 0X00000008; - const int ERROR_NO_MORE_ITEMS = 259; + const int ERROR_NO_MORE_ITEMS = 259; - // Client USERID stuff - // 1. call ImpersonateNamedPipeClient(hPipe) - // 2. call OpenThreadToken(GetCurrentThread(), - // TOKEN_QUERY | TOKEN_QUERY_SOURCE, - // FALSE, - // phUserToken); + // Client USERID stuff + // 1. call ImpersonateNamedPipeClient(hPipe) + // 2. call OpenThreadToken(GetCurrentThread(), + // TOKEN_QUERY | TOKEN_QUERY_SOURCE, + // FALSE, + // phUserToken); - public static int ImpersonateNamedPipeClient(IntPtr hPipeHandle) - { - int rcode = ImpersonateNative.ImpersonateNamedPipeClient(hPipeHandle); - return rcode; - } + public static int ImpersonateNamedPipeClient(IntPtr hPipeHandle) + { + int rcode = ImpersonateNative.ImpersonateNamedPipeClient(hPipeHandle); + return rcode; + } - - static int PerformDump(HANDLE token) - { - StringBuilder sb = new StringBuilder(); - ImpersonateNative.TOKEN_USER tokUser; - const int bufLength = 256; - IntPtr tu = Marshal.AllocHGlobal( bufLength ); - int cb = bufLength; - if (ImpersonateNative.GetTokenInformation( token, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenUser, tu, cb, ref cb )) - Console.WriteLine("GetTokenInformation successful"); - else - { - Console.WriteLine("GetTokenInformation NOT successful"); - uint error = NamedPipeNative.GetLastError(); - Console.WriteLine("error" + error.ToString()); - } - - tokUser = (ImpersonateNative.TOKEN_USER) Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_USER) ); - //sb.Append(DumpAccountSid(tokUser.User.Sid)); - IntPtr pUserID = tokUser.User.Sid; - //Console.WriteLine("UserID: " + pUserID); - - DumpAccountSid(pUserID); - Marshal.FreeHGlobal( tu ); - - - tu = Marshal.AllocHGlobal(bufLength); - cb = bufLength; - - // get token states - ImpersonateNative.TOKEN_STATISTICS stats; - - if (ImpersonateNative.GetTokenInformation(token, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenStatistics, tu, cb, ref cb)) - { - stats = (ImpersonateNative.TOKEN_STATISTICS) Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_STATISTICS)); - Console.WriteLine("UserLow: "+stats.AuthenticationId.LowPart.ToString()); - Console.WriteLine("UserHigh: "+stats.AuthenticationId.HighPart.ToString()); - } - else - { - Console.WriteLine("failed"); - } - - Marshal.FreeHGlobal(tu); - - return (int)pUserID; - } - - - static string DumpAccountSid(IntPtr SID) - { - int cchAccount = 0; - int cchDomain = 0; - int snu = 0 ; - StringBuilder sb = new StringBuilder(); - - // Caller allocated buffer - StringBuilder Account= null; - StringBuilder Domain = null; - bool ret = ImpersonateNative.LookupAccountSid(null, SID, Account, ref cchAccount, Domain, ref cchDomain, ref snu); - if ( ret == true ) - if ( Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS ) - return "Error"; - try - { - Account = new StringBuilder( cchAccount ); - Domain = new StringBuilder( cchDomain ); - ret = ImpersonateNative.LookupAccountSid(null, SID, Account, ref cchAccount, Domain, ref cchDomain, ref snu); - if (ret) - { - sb.Append(Domain); - sb.Append(@"\\"); - sb.Append(Account); - } - else - Console.WriteLine("logon account (no name) "); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - finally - { - } - string SidString = null; - ImpersonateNative.ConvertSidToStringSid(SID, ref SidString); - sb.Append("\nSID: "); - sb.Append(SidString); - - - Console.WriteLine("Acct info: "+ sb.ToString()); - return sb.ToString(); - } +#if DEBUG + static int PerformDump(HANDLE token) + { + StringBuilder sb = new StringBuilder(); + ImpersonateNative.TOKEN_USER tokUser; + const int bufLength = 256; + IntPtr tu = Marshal.AllocHGlobal( bufLength ); + int cb = bufLength; + if (ImpersonateNative.GetTokenInformation( token, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenUser, tu, cb, ref cb )) + Console.WriteLine("GetTokenInformation successful"); + else + { + Console.WriteLine("GetTokenInformation NOT successful"); + uint error = NamedPipeNative.GetLastError(); + Console.WriteLine("error" + error.ToString()); + } + + tokUser = (ImpersonateNative.TOKEN_USER) Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_USER) ); + IntPtr pUserID = tokUser.User.Sid; + + DumpAccountSid(pUserID); + Marshal.FreeHGlobal( tu ); + + tu = Marshal.AllocHGlobal(bufLength); + cb = bufLength; + + // get token states + ImpersonateNative.TOKEN_STATISTICS stats; + + if (ImpersonateNative.GetTokenInformation(token, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenStatistics, tu, cb, ref cb)) + { + stats = (ImpersonateNative.TOKEN_STATISTICS) Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_STATISTICS)); + Console.WriteLine("UserLow: "+stats.AuthenticationId.LowPart.ToString()); + Console.WriteLine("UserHigh: "+stats.AuthenticationId.HighPart.ToString()); + } + else + { + Console.WriteLine("failed"); + } + + Marshal.FreeHGlobal(tu); + + return (int)pUserID; + } + + + static string DumpAccountSid(IntPtr SID) + { + int cchAccount = 0; + int cchDomain = 0; + int snu = 0 ; + StringBuilder sb = new StringBuilder(); + + // Caller allocated buffer + StringBuilder Account= null; + StringBuilder Domain = null; + bool ret = ImpersonateNative.LookupAccountSid(null, SID, Account, ref cchAccount, Domain, ref cchDomain, ref snu); + if ( ret == true ) + if ( Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS ) + return "Error"; + try + { + Account = new StringBuilder( cchAccount ); + Domain = new StringBuilder( cchDomain ); + ret = ImpersonateNative.LookupAccountSid(null, SID, Account, ref cchAccount, Domain, ref cchDomain, ref snu); + if (ret) + { + sb.Append(Domain); + sb.Append(@"\\"); + sb.Append(Account); + } + else + Console.WriteLine("logon account (no name) "); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + finally + { + } + string SidString = null; + ImpersonateNative.ConvertSidToStringSid(SID, ref SidString); + sb.Append("\nSID: "); + sb.Append(SidString); + + + Console.WriteLine("Acct info: "+ sb.ToString()); + return sb.ToString(); + } +#endif public static bool GetLinkedToken(IntPtr token, out ImpersonateNative.TOKEN_LINKED_TOKEN linkedToken) { @@ -169,7 +167,6 @@ namespace AppModule.NamedPipes ImpersonateNative.GetTokenInformation(token, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenLinkedToken, IntPtr.Zero, TokenInfLength, ref TokenInfLength); - try { ptrLinkedToken = Marshal.AllocHGlobal(TokenInfLength); @@ -198,53 +195,48 @@ namespace AppModule.NamedPipes return TokenInfoSuccess; } - public static int GetLocalUserID(PipeHandle handle, ref int lowPart, ref int highPart, ref string SidString, ref int lowPartElevated, ref int highPartElevated) - { - int rcode = -1; - // get client userID - int code = ImpersonateNative.ImpersonateNamedPipeClient(handle.Handle); - if (code == 0) - { - uint lastError = NamedPipeNative.GetLastError(); - Console.WriteLine("ImpersonateNamedPipeClient Error: "+rcode.ToString()); - return -1; - } + public static int GetLocalUserID(PipeHandle handle, ref int lowPart, ref int highPart, ref string SidString, ref int lowPartElevated, ref int highPartElevated) + { + int rcode = -1; + // get client userID + int code = ImpersonateNative.ImpersonateNamedPipeClient(handle.Handle); + if (code == 0) + { + uint lastError = NamedPipeNative.GetLastError(); + System.Diagnostics.Debug.WriteLine("CASA: ImpersonateNamedPipeClient Error: " + rcode.ToString()); + return -1; + } - try - { - IntPtr hThread = ImpersonateNative.GetCurrentThread(); - uint iDesiredInfo = 24; //TOKEN_QUERY | TOKEN_QUERY_SOURCE; - IntPtr userToken = Marshal.AllocHGlobal(4); + try + { + IntPtr hThread = ImpersonateNative.GetCurrentThread(); + uint iDesiredInfo = 24; //TOKEN_QUERY | TOKEN_QUERY_SOURCE; + IntPtr userToken = IntPtr.Zero; IntPtr userTokenElevated = IntPtr.Zero; - if (ImpersonateNative.OpenThreadToken(hThread, iDesiredInfo, true, out userToken)) - { - StringBuilder sb = new StringBuilder(); - ImpersonateNative.TOKEN_USER tokUser; - const int bufLength = 256; - IntPtr tu = Marshal.AllocHGlobal( bufLength ); - int cb = bufLength; + if (ImpersonateNative.OpenThreadToken(hThread, iDesiredInfo, true, out userToken)) + { + StringBuilder sb = new StringBuilder(); + ImpersonateNative.TOKEN_USER tokUser; + const int bufLength = 256; + IntPtr tu = Marshal.AllocHGlobal(bufLength); + int cb = bufLength; - // on Vista use the elevated token if there is one. + // on Vista use the elevated token if there is one. System.OperatingSystem os = System.Environment.OSVersion; - System.Diagnostics.Trace.WriteLine("OS Version: " + os.Version.ToString()); + if (os.Version.Major > 5) - { + { if (ImpersonateNative.GetTokenInformation(userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenElevationType, tu, cb, ref cb)) { - int iTokenType; + int iTokenType; iTokenType = (int)Marshal.PtrToStructure(tu, typeof(int)); - System.Diagnostics.Trace.WriteLine("Token Type: " + iTokenType.ToString()); if (iTokenType == (int)ImpersonateNative.TOKEN_ELEVATION_TYPE.TokenElevationTypeLimited) { - System.Diagnostics.Trace.WriteLine("Getting linked token"); - ImpersonateNative.TOKEN_LINKED_TOKEN newLinkedToken; if (GetLinkedToken(userToken, out newLinkedToken)) { - //userToken = newLinkedToken.LinkedToken; - userTokenElevated = Marshal.AllocHGlobal(4); userTokenElevated = newLinkedToken.LinkedToken; } } @@ -252,38 +244,38 @@ namespace AppModule.NamedPipes else { uint error = ImpersonateNative.GetLastError(); - System.Diagnostics.Trace.WriteLine("linked token error: " + error.ToString()); + System.Diagnostics.Debug.WriteLine("CASA: linked token error: " + error.ToString()); } } - cb = bufLength; - if (ImpersonateNative.GetTokenInformation( userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenUser, tu, cb, ref cb )) - { - tokUser = (ImpersonateNative.TOKEN_USER) Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_USER) ); - IntPtr pUserID = tokUser.User.Sid; - Marshal.FreeHGlobal( tu ); + cb = bufLength; + if (ImpersonateNative.GetTokenInformation(userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenUser, tu, cb, ref cb)) + { + tokUser = (ImpersonateNative.TOKEN_USER)Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_USER)); + IntPtr pUserID = tokUser.User.Sid; + Marshal.FreeHGlobal(tu); - // get SID - //string SidString = null; - ImpersonateNative.ConvertSidToStringSid(pUserID, ref SidString); + // get SID + ImpersonateNative.ConvertSidToStringSid(pUserID, ref SidString); - // get token states - tu = Marshal.AllocHGlobal(bufLength); - cb = bufLength; - ImpersonateNative.TOKEN_STATISTICS stats; - if (ImpersonateNative.GetTokenInformation(userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenStatistics, tu, cb, ref cb)) - { - stats = (ImpersonateNative.TOKEN_STATISTICS) Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_STATISTICS)); - // copy low and high part - lowPart = stats.AuthenticationId.LowPart; - highPart = stats.AuthenticationId.HighPart; - rcode = -1; - } + // get token states + tu = Marshal.AllocHGlobal(bufLength); + cb = bufLength; + ImpersonateNative.TOKEN_STATISTICS stats; + if (ImpersonateNative.GetTokenInformation(userToken, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenStatistics, tu, cb, ref cb)) + { + stats = (ImpersonateNative.TOKEN_STATISTICS)Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_STATISTICS)); + + // copy low and high part + lowPart = stats.AuthenticationId.LowPart; + highPart = stats.AuthenticationId.HighPart; + rcode = -1; + } // get elevated token stats if (userTokenElevated != IntPtr.Zero) { - cb = bufLength; + cb = bufLength; if (ImpersonateNative.GetTokenInformation(userTokenElevated, ImpersonateNative.TOKEN_INFORMATION_CLASS.TokenStatistics, tu, cb, ref cb)) { stats = (ImpersonateNative.TOKEN_STATISTICS)Marshal.PtrToStructure(tu, typeof(ImpersonateNative.TOKEN_STATISTICS)); @@ -292,44 +284,57 @@ namespace AppModule.NamedPipes highPartElevated = stats.AuthenticationId.HighPart; rcode = -1; } + + if (!ImpersonateNative.CloseHandle(userTokenElevated)) + { + uint errCode = ImpersonateNative.GetLastError(); + System.Diagnostics.Debug.WriteLine("CASA: Closing elevated handle failed: {0}", errCode.ToString()); + } + + userTokenElevated = IntPtr.Zero; } - } - else - { - Console.WriteLine("GetTokenInformation NOT successful"); - uint error = NamedPipeNative.GetLastError(); - Console.WriteLine("error" + error.ToString()); - } + } + else + { + uint error = NamedPipeNative.GetLastError(); + System.Diagnostics.Debug.WriteLine("CASA: GetTokenInformation NOT successful " + error.ToString()); + } Marshal.FreeHGlobal(tu); - // close handle - ImpersonateNative.CloseHandle(hThread); - ImpersonateNative.RevertToSelf(); - } - else - { - int lastError = Marshal.GetLastWin32Error(); - uint errorcode = NamedPipeNative.GetLastError(); - Console.WriteLine("OpenThreadToken Error: "+ errorcode.ToString() + " code2: "+rcode.ToString()); - } + // close handles + if (!ImpersonateNative.CloseHandle(userToken)) + { + uint errCode = ImpersonateNative.GetLastError(); + System.Diagnostics.Debug.WriteLine("CASA: Close userToken failed: {0}", errCode.ToString()); + } + else + { + userToken = IntPtr.Zero; + } + + if (!ImpersonateNative.RevertToSelf()) + { + System.Diagnostics.Debug.WriteLine("CASA: RevertToSelf failed"); + } - Marshal.FreeHGlobal(userToken); - if (userTokenElevated != IntPtr.Zero) - { - Marshal.FreeHGlobal(userTokenElevated); } - } - catch (Exception ex) - { - int error = Marshal.GetLastWin32Error(); - Console.WriteLine(ex.ToString()); - return rcode; - } - // end + else + { + uint errorcode = NamedPipeNative.GetLastError(); + System.Diagnostics.Debug.WriteLine("CASA: OpenThreadToken Error: " + errorcode.ToString()); + } + } + catch (Exception ex) + { + int error = Marshal.GetLastWin32Error(); + System.Diagnostics.Debug.WriteLine(ex.ToString()); + return rcode; + } + // end - return rcode; + return rcode; - } - } -} \ No newline at end of file + } + } +} diff --git a/CASA/micasad/cache/SecretStore.cs b/CASA/micasad/cache/SecretStore.cs index b0b26057..f9c1cb9b 100644 --- a/CASA/micasad/cache/SecretStore.cs +++ b/CASA/micasad/cache/SecretStore.cs @@ -895,8 +895,8 @@ namespace sscs.cache return passwd; } catch (Exception e) - { - CSSSLogger.ExpLog(e.ToString()); + { + CSSSLogger.DbgLog("Desktop password not set"); } return null; } diff --git a/CASA/micasad/communication/win/PipeManager.cs b/CASA/micasad/communication/win/PipeManager.cs index 90708d61..9d233f9c 100644 --- a/CASA/micasad/communication/win/PipeManager.cs +++ b/CASA/micasad/communication/win/PipeManager.cs @@ -20,148 +20,148 @@ * ***********************************************************************/ -using System; -using System.Collections; -using System.Threading; -using System.Web; -using System.IO; -using System.Configuration; -using System.Diagnostics; - -using AppModule.InterProcessComm; -using AppModule.NamedPipes; - -namespace sscs.communication.win { - - public class PipeManager : IChannelManager { - - public Hashtable Pipes; - - private uint NumberPipes = 16; - private uint OutBuffer = 65536; //512; - private uint InBuffer = 65536; //512; - private const int MAX_READ_BYTES = 15000; - private bool _listen = true; - public bool Listen { - get { - return _listen; - } - set { - _listen=value; - } - } - private int numChannels = 0; - private Hashtable _pipes = new Hashtable(); - - //private Thread MainThread; - private string CASA_RPC_PIPE = "\\\\.\\PIPE\\SS_RPC_PIPE"; - private ManualResetEvent Mre; - private const int PIPE_MAX_STUFFED_TIME = 5000; - - public object SyncRoot = new object(); - - public void Initialize() { - Pipes = Hashtable.Synchronized(_pipes); - Mre = new ManualResetEvent(false); - /* - MainThread = new Thread(new ThreadStart(Start)); - MainThread.IsBackground = true; - MainThread.Name = "Main Pipe Thread"; - MainThread.Start(); - */ - Thread.Sleep(1000); - } - public string HandleRequest(string request) { - string returnVal; - - //Form1.ActivityRef.AppendText(request + Environment.NewLine); - returnVal = "Response to: " + request; - - return returnVal; - } - - public void Start() { - try { - while (_listen) { - int[] keys = new int[Pipes.Keys.Count]; - Pipes.Keys.CopyTo(keys,0); - foreach (int key in keys) { - ServerNamedPipe serverPipe = (ServerNamedPipe)Pipes[key]; - if (serverPipe != null && DateTime.Now.Subtract(serverPipe.LastAction).Milliseconds > PIPE_MAX_STUFFED_TIME && serverPipe.PipeConnection.GetState() != InterProcessConnectionState.WaitingForClient) { - serverPipe.Listen = false; - serverPipe.PipeThread.Abort(); - RemoveServerChannel(serverPipe.PipeConnection.NativeHandle); - } - } - if (numChannels <= NumberPipes) { - ServerNamedPipe pipe = new ServerNamedPipe(CASA_RPC_PIPE, OutBuffer, InBuffer, MAX_READ_BYTES); - try { - pipe.Connect(); - pipe.LastAction = DateTime.Now; - System.Threading.Interlocked.Increment(ref numChannels); - pipe.Start(); - Pipes.Add(pipe.PipeConnection.NativeHandle, pipe); - } - catch (InterProcessIOException) { - RemoveServerChannel(pipe.PipeConnection.NativeHandle); - pipe.Dispose(); - } - } - else { - Mre.Reset(); - Mre.WaitOne(1000, false); - } - } - } - catch (Exception e) - { - Console.WriteLine("Exception starting server: "+e.ToString()); - // Log exception - } - } - public void Stop() { - _listen = false; - Mre.Set(); - try { - int[] keys = new int[Pipes.Keys.Count]; - Pipes.Keys.CopyTo(keys,0); - foreach (int key in keys) { - ((ServerNamedPipe)Pipes[key]).Listen = false; - } - int i = numChannels * 3; - for (int j = 0; j < i; j++) { - StopServerPipe(); - } - Pipes.Clear(); - Mre.Close(); - Mre = null; - } - catch { - // Log exception - } - } - - public void WakeUp() { - if (Mre != null) { - Mre.Set(); - } - } - private void StopServerPipe() { - try { - ClientPipeConnection pipe = new ClientPipeConnection(CASA_RPC_PIPE); - if (pipe.TryConnect()) { - pipe.Close(); - } - } catch { - // Log exception - } - } - - public void RemoveServerChannel(object param) { - int handle = (int)param; - System.Threading.Interlocked.Decrement(ref numChannels); - Pipes.Remove(handle); - this.WakeUp(); - } - } -} \ No newline at end of file +using System; +using System.Collections; +using System.Threading; +using System.Web; +using System.IO; +using System.Configuration; +using System.Diagnostics; + +using AppModule.InterProcessComm; +using AppModule.NamedPipes; + +namespace sscs.communication.win { + + public class PipeManager : IChannelManager { + + public Hashtable Pipes; + + private uint NumberPipes = 1024; + private uint OutBuffer = 65536; //512; + private uint InBuffer = 65536; //512; + private const int MAX_READ_BYTES = 15000; + private bool _listen = true; + public bool Listen { + get { + return _listen; + } + set { + _listen=value; + } + } + private int numChannels = 0; + private Hashtable _pipes = new Hashtable(); + + //private Thread MainThread; + private string CASA_RPC_PIPE = "\\\\.\\PIPE\\SS_RPC_PIPE"; + private ManualResetEvent Mre; + private const int PIPE_MAX_STUFFED_TIME = 5000; + + public object SyncRoot = new object(); + + public void Initialize() { + Pipes = Hashtable.Synchronized(_pipes); + Mre = new ManualResetEvent(false); + /* + MainThread = new Thread(new ThreadStart(Start)); + MainThread.IsBackground = true; + MainThread.Name = "Main Pipe Thread"; + MainThread.Start(); + */ + Thread.Sleep(1000); + } + public string HandleRequest(string request) { + string returnVal; + + //Form1.ActivityRef.AppendText(request + Environment.NewLine); + returnVal = "Response to: " + request; + + return returnVal; + } + + public void Start() { + try { + while (_listen) { + int[] keys = new int[Pipes.Keys.Count]; + Pipes.Keys.CopyTo(keys,0); + foreach (int key in keys) { + ServerNamedPipe serverPipe = (ServerNamedPipe)Pipes[key]; + if (serverPipe != null && DateTime.Now.Subtract(serverPipe.LastAction).Milliseconds > PIPE_MAX_STUFFED_TIME && serverPipe.PipeConnection.GetState() != InterProcessConnectionState.WaitingForClient) { + serverPipe.Listen = false; + serverPipe.PipeThread.Abort(); + RemoveServerChannel(serverPipe.PipeConnection.NativeHandle); + } + } + if (numChannels <= NumberPipes) { + ServerNamedPipe pipe = new ServerNamedPipe(CASA_RPC_PIPE, OutBuffer, InBuffer, MAX_READ_BYTES); + try { + pipe.Connect(); + pipe.LastAction = DateTime.Now; + System.Threading.Interlocked.Increment(ref numChannels); + pipe.Start(); + Pipes.Add(pipe.PipeConnection.NativeHandle, pipe); + } + catch (InterProcessIOException) { + RemoveServerChannel(pipe.PipeConnection.NativeHandle); + pipe.Dispose(); + } + } + else { + Mre.Reset(); + Mre.WaitOne(1000, false); + } + } + } + catch (Exception e) + { + Console.WriteLine("Exception starting server: "+e.ToString()); + // Log exception + } + } + public void Stop() { + _listen = false; + Mre.Set(); + try { + int[] keys = new int[Pipes.Keys.Count]; + Pipes.Keys.CopyTo(keys,0); + foreach (int key in keys) { + ((ServerNamedPipe)Pipes[key]).Listen = false; + } + int i = numChannels * 3; + for (int j = 0; j < i; j++) { + StopServerPipe(); + } + Pipes.Clear(); + Mre.Close(); + Mre = null; + } + catch { + // Log exception + } + } + + public void WakeUp() { + if (Mre != null) { + Mre.Set(); + } + } + private void StopServerPipe() { + try { + ClientPipeConnection pipe = new ClientPipeConnection(CASA_RPC_PIPE); + if (pipe.TryConnect()) { + pipe.Close(); + } + } catch { + // Log exception + } + } + + public void RemoveServerChannel(object param) { + int handle = (int)param; + System.Threading.Interlocked.Decrement(ref numChannels); + Pipes.Remove(handle); + this.WakeUp(); + } + } +} diff --git a/CASA/test/c_sample/cSample.c b/CASA/test/c_sample/cSample.c index ab6c08c5..a83cb234 100644 --- a/CASA/test/c_sample/cSample.c +++ b/CASA/test/c_sample/cSample.c @@ -594,7 +594,7 @@ void RemoveServerSecret() RemoveSecret(SSCS_CRED_TYPE_SERVER_F); } -void RunTest() +void RunTest(char *SecretID) { { SSCS_BASIC_CREDENTIAL credential = {0}; @@ -604,7 +604,8 @@ void RunTest() int rcode = 0; int iFlags = 0; - sscs_Utf8Strcpy(appSecretId.id, "NativeC.AppSecretID"); + //sscs_Utf8Strcpy(appSecretId.id, "NativeC.AppSecretID"); + sscs_Utf8Strcpy(appSecretId.id, SecretID); appSecretId.len = sscs_Utf8Strlen(appSecretId.id) + 1; credential.unFlags = USERNAME_TYPE_CN_F; @@ -765,6 +766,7 @@ void RunTests() int iCount = 1; int iTemp = 0; int i = 0; + char inputID[20]; printf("Enter number interations to run (default 1): "); gets(runtimes); @@ -773,9 +775,13 @@ void RunTests() if (iTemp > 1) iCount=iTemp; + printf("Enter secretID: "); + gets(inputID); + for (i=0; i