markus / IIpc / WcfServer.cs @ 26ec6226
이력 | 보기 | 이력해설 | 다운로드 (5.46 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.ServiceModel; |
5 |
using System.ServiceModel.Channels; |
6 |
using System.ServiceModel.Description; |
7 |
using System.Text; |
8 |
using System.Threading.Tasks; |
9 |
using System.Xml; |
10 |
|
11 |
namespace IIpc |
12 |
{ |
13 |
public sealed class WcfServer : IIpcServer |
14 |
{ |
15 |
[ServiceBehavior(IncludeExceptionDetailInFaults = false,InstanceContextMode = InstanceContextMode.Single)] |
16 |
private class _Server : IIpcClient |
17 |
{ |
18 |
private readonly WcfServer server; |
19 |
|
20 |
public _Server(WcfServer server) |
21 |
{ |
22 |
this.server = server; |
23 |
} |
24 |
|
25 |
public void SendFileDownloadReceived(double progress, bool isFinish) |
26 |
{ |
27 |
this.server.OnFileDownloadReceived(new IpcDownloadStatusArgs(progress, isFinish)); |
28 |
} |
29 |
|
30 |
public void SendThumbnailReceived(int pageno, string path,bool isLast) |
31 |
{ |
32 |
this.server.OnThumbnailReceived(new IpcThumbnailEventArgs(pageno,path, isLast)); |
33 |
} |
34 |
} |
35 |
|
36 |
private readonly ServiceHost host; |
37 |
|
38 |
private void OnThumbnailReceived(IpcThumbnailEventArgs e) |
39 |
{ |
40 |
var handler = this.IpcThumbnailReceived; |
41 |
|
42 |
if (handler != null) |
43 |
{ |
44 |
handler(this, e); |
45 |
} |
46 |
} |
47 |
|
48 |
private void OnFileDownloadReceived(IpcDownloadStatusArgs e) |
49 |
{ |
50 |
var handler = this.IpcFileDownloadReceived; |
51 |
|
52 |
if (handler != null) |
53 |
{ |
54 |
handler(this, e); |
55 |
} |
56 |
} |
57 |
|
58 |
|
59 |
/// <summary> |
60 |
/// |
61 |
/// </summary> |
62 |
/// <param name="endpoint">지정된 끝점으로 유일하게 통신한다.</param> |
63 |
public WcfServer(string endpoint) |
64 |
{ |
65 |
try |
66 |
{ |
67 |
this.host = new ServiceHost(new _Server(this), new Uri(string.Format("net.pipe://localhost/{0}", endpoint))); |
68 |
this.host.Closing += Host_Closing; |
69 |
//ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); |
70 |
//smb.HttpGetEnabled = false; |
71 |
//host.Description.Behaviors.Add(smb); |
72 |
|
73 |
//var endpoints = this.host.AddDefaultEndpoints(); |
74 |
//System.Diagnostics.Debug.WriteLine(endpoints); |
75 |
} |
76 |
catch (Exception ex) |
77 |
{ |
78 |
System.Diagnostics.Debug.WriteLine(ex); |
79 |
} |
80 |
//this.host = new ServiceHost(this, new Uri(string.Format("net.pipe://localhost"))); |
81 |
|
82 |
//ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior(); |
83 |
//mBehave.HttpGetEnabled = false; |
84 |
//mBehave.HttpsGetEnabled = false; |
85 |
//this.host.Description.Behaviors.Add(mBehave); |
86 |
|
87 |
//if (endpoint != null) |
88 |
//{ |
89 |
// NetNamedPipeBinding binding = new NetNamedPipeBinding { TransferMode = TransferMode.Buffered }; |
90 |
|
91 |
// binding.CloseTimeout = new TimeSpan(0, 1, 0); |
92 |
// binding.ReceiveTimeout = new TimeSpan(0, 1, 0); |
93 |
// binding.SendTimeout = new TimeSpan(0, 1, 0); |
94 |
// binding.OpenTimeout = new TimeSpan(0, 1, 0); |
95 |
|
96 |
// binding.CreateBindingElements().Add(gBindingElement); |
97 |
|
98 |
// var httpEndpoint = this.host.AddServiceEndpoint(typeof(IIpcServer), binding, endpoint); |
99 |
//} |
100 |
} |
101 |
|
102 |
private void Host_Closing(object sender, EventArgs e) |
103 |
{ |
104 |
System.Diagnostics.Debug.WriteLine($"IIpc.WcfServer {this.host.BaseAddresses?.First()} closing"); |
105 |
} |
106 |
|
107 |
public static BinaryMessageEncodingBindingElement gBindingElement = new BinaryMessageEncodingBindingElement |
108 |
{ |
109 |
MaxReadPoolSize = Int16.MaxValue, |
110 |
MaxWritePoolSize = Int16.MaxValue, |
111 |
MaxSessionSize = Int16.MaxValue, |
112 |
ReaderQuotas = GetReaderQuotas() |
113 |
}; |
114 |
|
115 |
public static XmlDictionaryReaderQuotas GetReaderQuotas() |
116 |
{ |
117 |
return new XmlDictionaryReaderQuotas |
118 |
{ |
119 |
MaxDepth = Int16.MaxValue, |
120 |
MaxStringContentLength = Int16.MaxValue, |
121 |
MaxArrayLength = Int16.MaxValue, |
122 |
MaxBytesPerRead = Int16.MaxValue, |
123 |
MaxNameTableCharCount = Int16.MaxValue |
124 |
}; |
125 |
} |
126 |
|
127 |
|
128 |
public event EventHandler<IpcThumbnailEventArgs> IpcThumbnailReceived; |
129 |
public event EventHandler<IpcDownloadStatusArgs> IpcFileDownloadReceived; |
130 |
|
131 |
public void Start() |
132 |
|
133 |
{ |
134 |
this.host.Open(); |
135 |
} |
136 |
|
137 |
public bool IsDispose |
138 |
{ |
139 |
get { return (this.host == null ? true : false); } |
140 |
} |
141 |
|
142 |
public bool IsOpen |
143 |
{ |
144 |
get { return (this.host?.State == CommunicationState.Opened ? true : false); } |
145 |
} |
146 |
|
147 |
public void Stop() |
148 |
{ |
149 |
try |
150 |
{ |
151 |
System.Diagnostics.Debug.WriteLine($"IIpc.WcfServer {this.host.BaseAddresses?.First()} Stop"); |
152 |
this.host.Close(new TimeSpan(1)); |
153 |
|
154 |
System.Diagnostics.Debug.WriteLine($"IIpc.WcfServer {this.host.BaseAddresses?.First()} Stoped"); |
155 |
|
156 |
} |
157 |
catch (Exception ex) |
158 |
{ |
159 |
System.Diagnostics.Debug.WriteLine(ex.ToString()); |
160 |
} |
161 |
} |
162 |
|
163 |
void IDisposable.Dispose() |
164 |
{ |
165 |
this.Stop(); |
166 |
|
167 |
(this.host as IDisposable).Dispose(); |
168 |
|
169 |
System.Diagnostics.Debug.WriteLine($"IIpc.WcfServer {this.host.BaseAddresses?.First()} Dispose"); |
170 |
} |
171 |
} |
172 |
} |