프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / IIpc / WcfServer.cs @ a4aac5fe

이력 | 보기 | 이력해설 | 다운로드 (4.7 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 = true,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

    
37
        private readonly ServiceHost host;
38

    
39
        private void OnThumbnailReceived(IpcThumbnailEventArgs e)
40
        {
41
            var handler = this.IpcThumbnailReceived;
42

    
43
            if (handler != null)
44
            {
45
                handler(this, e);
46
            }
47
        }
48

    
49
        private void OnFileDownloadReceived(IpcDownloadStatusArgs e)
50
        {
51
            var handler = this.IpcFileDownloadReceived;
52

    
53
            if (handler != null)
54
            {
55
                handler(this, e);
56
            }
57
        }
58

    
59

    
60
        /// <summary>
61
        /// 
62
        /// </summary>
63
        /// <param name="endpoint">지정된 끝점으로 유일하게 통신한다.</param>
64
        public WcfServer(string endpoint)
65
        {
66
            try
67
            {
68
                this.host = new ServiceHost(new _Server(this), new Uri(string.Format("net.pipe://localhost/{0}", endpoint)));
69
                this.host.Closing += Host_Closing;
70
                //ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
71
                //smb.HttpGetEnabled = false;
72
                //host.Description.Behaviors.Add(smb);
73

    
74
                //var endpoints = this.host.AddDefaultEndpoints();
75
                //System.Diagnostics.Debug.WriteLine(endpoints);
76
            }
77
            catch (Exception ex)
78
            {
79
                System.Diagnostics.Debug.WriteLine(ex);
80
            }
81
            //this.host = new ServiceHost(this, new Uri(string.Format("net.pipe://localhost")));
82

    
83
            //ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
84
            //mBehave.HttpGetEnabled = false;
85
            //mBehave.HttpsGetEnabled = false;
86
            //this.host.Description.Behaviors.Add(mBehave);
87

    
88
            //if (endpoint != null)
89
            //{
90
            //    NetNamedPipeBinding binding = new NetNamedPipeBinding { TransferMode = TransferMode.Buffered };
91

    
92
            //    binding.CloseTimeout = new TimeSpan(0, 1, 0);
93
            //    binding.ReceiveTimeout = new TimeSpan(0, 1, 0);
94
            //    binding.SendTimeout = new TimeSpan(0, 1, 0);
95
            //    binding.OpenTimeout = new TimeSpan(0, 1, 0);
96

    
97
            //    binding.CreateBindingElements().Add(gBindingElement);
98

    
99
            //    var httpEndpoint = this.host.AddServiceEndpoint(typeof(IIpcServer), binding, endpoint);
100
            //}
101
        }
102

    
103
        private void Host_Closing(object sender, EventArgs e)
104
        {
105
            System.Diagnostics.Debug.WriteLine($"IIpc.WcfServer {this.host.BaseAddresses?.First()} closing");
106
        }
107

    
108
        public static BinaryMessageEncodingBindingElement gBindingElement = new BinaryMessageEncodingBindingElement
109
        {
110
            MaxReadPoolSize = Int16.MaxValue,
111
            MaxWritePoolSize = Int16.MaxValue,
112
            MaxSessionSize = Int16.MaxValue,
113
            ReaderQuotas = GetReaderQuotas()
114
        };
115

    
116
        public static XmlDictionaryReaderQuotas GetReaderQuotas()
117
        {
118
            return new XmlDictionaryReaderQuotas
119
            {
120
                MaxDepth = Int16.MaxValue,
121
                MaxStringContentLength = Int16.MaxValue,
122
                MaxArrayLength = Int16.MaxValue,
123
                MaxBytesPerRead = Int16.MaxValue,
124
                MaxNameTableCharCount = Int16.MaxValue
125
            };
126
        }
127

    
128

    
129
        public event EventHandler<IpcThumbnailEventArgs> IpcThumbnailReceived;
130
        public event EventHandler<IpcDownloadStatusArgs> IpcFileDownloadReceived;
131

    
132
        public void Start()
133

    
134
        {
135
            this.host.Open();
136
        }
137

    
138
        public void Stop()
139
        {
140
            this.host.Abort();
141
        }
142

    
143
        void IDisposable.Dispose()
144
        {
145
            this.Stop();
146

    
147
            (this.host as IDisposable).Dispose();
148
        }
149
    }
150
}
클립보드 이미지 추가 (최대 크기: 500 MB)