private void btnCreateVC_Click(object sender, RoutedEventArgs e) { // Check to see that the launcher is running on the VDA if not dont try to connect to // a virtual channel as one will not be available if launcher running on endpoint // Which will be the case we we're using the launcher to resolve paths in the AppV VFS for utility sake. var sessionName = Environment.GetEnvironmentVariable("SESSIONNAME"); if (sessionName.StartsWith("ICA")) { try { var request = new Request() { MessageBody = @"462dfa90-9e09-429e-a6f3-00cb3e9bf90b;\\londondc.london.local\\appvshare\\EPM 3.appv;4148dc52-d7d5-47c3-a570-396aa63fa9fe;d2022911-4251-4c86-b8cd-e2bb092443fd;EPM Opsætningsmodul", RequestTask = RequestTask.AddClientPackage }; var payload = ServiceBrokerProtocolHelper.Serialize(request); var sb = new StringBuilder(payload.Length + 1); // Call the C++ DLL to do the VirtualChannel Send() SendAndRecieve(payload, payload.Length + 1, sb, sb.Capacity); var response = ServiceBrokerProtocolHelper.Deserialize <Response>(sb.ToString()); var result = string.Format("{0}:{1}", response.ResponseCode, String.IsNullOrEmpty(response.MessageBody) ? "empty message string" : response.MessageBody); Status.Content = result; MessageBox.Show("Result was " + result); } catch (Exception ex) { MessageBox.Show("Error sendreceive: " + ex.Message); } } }
private void ShutdownPipeServer() { // Ask the named pipe server to stop itself gracefully. This is required so that a listening thread isnt waiting for connections which will prevent shutdown of this service // Basically this will be the last connection it will accept. Start the service to being again or call StartNamePipeServer() var pipeClient = new NamedPipeClientStream(".", StringConstants.TechServicePipeName, PipeDirection.InOut, PipeOptions.WriteThrough, TokenImpersonationLevel.Impersonation); pipeClient.Connect(); ServiceBrokerProtocolHelper.SendRequest(new Request { RequestTask = RequestTask.StopNamedPipeServer }, pipeClient, (function, message) => LogMessage(message, EventLogEntryType.Information)); }
/// <summary> /// Sends a synchonous/blocking message to the Tech service. /// </summary> /// <param name="request">The request.</param> /// <remarks>This function blocks until it can connect to the Tech service, ie it waits until the server can make the connection.</remarks> /// <returns>A response from the service</returns> public Response SendMessage(Request request) { // This lock ensures that no other client thread can connect until we're done this function(which includes this function getting a valid response first) lock (ThreadLock) { LogMessage(MethodBase.GetCurrentMethod().Name, "Sending message to service..."); var pipeClient = new NamedPipeClientStream(".", StringConstants.TechServicePipeName, PipeDirection.InOut, PipeOptions.WriteThrough, TokenImpersonationLevel.Impersonation); LogMessage(MethodBase.GetCurrentMethod().Name, "Waiting to connect to service..."); pipeClient.Connect(); LogMessage(MethodBase.GetCurrentMethod().Name, "Connected to service."); return(ServiceBrokerProtocolHelper.SendRequest(request, pipeClient, LogMessage)); } }
private void StartNamedPipeServer() { new Thread(() => { var done = false; do { NamedPipeServerStream pipeServer = null; try { CreatePowerShellRunspace(); _runspace.Open(); var commonAdminFunctions = new CommonAdminFunctions(ref _runspace, (method, message) => { LogMessage(string.Format(CultureInfo.CurrentCulture, "{0}:{1}", method, message), EventLogEntryType.Information); }); var security = new PipeSecurity(); security.AddAccessRule(new PipeAccessRule(@"NT Service\AccountName", PipeAccessRights.FullControl, AccessControlType.Allow)); security.AddAccessRule(new PipeAccessRule(WindowsIdentity.GetCurrent().User, PipeAccessRights.FullControl, AccessControlType.Allow)); security.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow)); pipeServer = new NamedPipeServerStream(StringConstants.TechServicePipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.WriteThrough, 1024, 1024, security); var threadId = Thread.CurrentThread.ManagedThreadId; LogMessage("Waiting for connection on thread..." + threadId, EventLogEntryType.Information); pipeServer.WaitForConnection(); var ss = new StreamString(pipeServer); var rawProtocolRequest = ss.ReadString(); var request = ServiceBrokerProtocolHelper.Deserialize <Request>(rawProtocolRequest); var noResultResponse = new Response { MessageBody = String.Empty, ResponseCode = ResponseCode.None }; var response = noResultResponse; LogMessage(string.Format(CultureInfo.CurrentCulture, "Received new message from plugin. Type={0} MessageBody={1}", request.RequestTask, request.MessageBody), EventLogEntryType.Information); try { switch (request.RequestTask) { case RequestTask.Task1: Task1(request.MessageBody); response = noResultResponse; break; case RequestTask.Task2: Task2(request.MessageBody); response = noResultResponse; break; case RequestTask.StopNamedPipeServer: done = true; LogMessage("Shutting down named pipe server thread on request.", EventLogEntryType.Information); response = noResultResponse; break; default: LogMessage("Unknown message received from broker plugin:" + rawProtocolRequest, EventLogEntryType.Error); response = noResultResponse; break; } } catch (Exception e) { // Problem while running any of the admin functions? Send back an error response var logMessage = "Error occured while running protocol request:" + e.Message; LogMessage(logMessage, EventLogEntryType.Error); response = new Response { ResponseCode = ResponseCode.Error, MessageBody = logMessage }; } finally { // We will *always* send the response of some sort. // This serves in the very least as an acknowledgement of pipe function being finished either successfully or unsuccessfully depending on the response code LogMessage(string.Format(CultureInfo.CurrentCulture, "Sending response of '{0}' type='{1}'", response.MessageBody, response.ResponseCode), EventLogEntryType.Information); ss.WriteString(ServiceBrokerProtocolHelper.Serialize(response)); } } catch (Exception e) { // Problem with the named pipe functionality? Send back an error response var message = string.Format(CultureInfo.CurrentCulture, "Unexpected exception occured setting up named pipe. Message = '{0}', StackTrace = {1}", e.Message, e.StackTrace); LogMessage(message, EventLogEntryType.Error); } finally { LogMessage("Finished with connection...", EventLogEntryType.Information); // Note if this thread is aborted such as when the service restarts, this is guarenteed still to run, freeing up resources in this thread... if (pipeServer != null) { LogMessage("Closing server pipe.", EventLogEntryType.Information); pipeServer.Close(); } ClosePowerShellRunspace(); } } while (!done); }).Start(); }