summaryrefslogtreecommitdiff
path: root/Gestor.Application/Helpers/PipeServer.cs
blob: fa42e038c418db0de408b88d393fe53f88618f39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using Assinador.Model.Common;
using Gestor.Application.Actions;
using Newtonsoft.Json;
using System;
using System.IO;
using System.IO.Pipes;
using System.Runtime.CompilerServices;
using System.Security.AccessControl;
using System.Security.Principal;

namespace Gestor.Application.Helpers
{
	public class PipeServer : IDisposable
	{
		private string _pipeName;

		private NamedPipeServerStream Pipe
		{
			get;
			set;
		}

		public PipeServer()
		{
		}

		private bool Create()
		{
			bool flag;
			bool flag1 = true;
			try
			{
				(new NamedPipeClientStream(".", this._pipeName, PipeDirection.Out, PipeOptions.Asynchronous)).Connect(1000);
			}
			catch (TimeoutException timeoutException)
			{
				flag1 = false;
			}
			catch (Exception exception)
			{
				flag = false;
				return flag;
			}
			if (flag1)
			{
				return true;
			}
			try
			{
				PipeSecurity pipeSecurity = new PipeSecurity();
				SecurityIdentifier securityIdentifier = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
				securityIdentifier.Translate(typeof(NTAccount));
				pipeSecurity.SetAccessRule(new PipeAccessRule(securityIdentifier, PipeAccessRights.ReadWrite, AccessControlType.Allow));
				this.Pipe = new NamedPipeServerStream(this._pipeName, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 1, 1, pipeSecurity);
				this.Pipe.BeginWaitForConnection(new AsyncCallback(this.WaitForConnectionCallBack), this.Pipe);
				return true;
			}
			catch (Exception exception1)
			{
				flag = false;
			}
			return flag;
		}

		public bool CreateServer(string name)
		{
			this._pipeName = name;
			return this.Create();
		}

		public void Dispose()
		{
			this.Pipe.Dispose();
		}

		private void Handle(string message)
		{
			if (message == null)
			{
				return;
			}
			PipeMessageResult pipeMessageResult = JsonConvert.DeserializeObject<PipeMessageResult>(message);
			Action<PipeMessageResult> acessarHoster = Gestor.Application.Actions.Actions.AcessarHoster;
			if (acessarHoster == null)
			{
				return;
			}
			acessarHoster(pipeMessageResult);
		}

		private void WaitForConnectionCallBack(IAsyncResult iar)
		{
			try
			{
				NamedPipeServerStream asyncState = (NamedPipeServerStream)iar.AsyncState;
				asyncState.EndWaitForConnection(iar);
				using (StreamReader streamReader = new StreamReader(this.Pipe))
				{
					string str = streamReader.ReadLine();
					if ((str == null ? false : str.IndexOf("exit", StringComparison.InvariantCultureIgnoreCase) > -1))
					{
						this.Dispose();
					}
					this.Handle(str);
				}
				asyncState.Close();
				asyncState = null;
				this.Create();
			}
			catch
			{
			}
		}
	}
}