source: branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfMessageService.cs @ 13971

Last change on this file since 13971 was 13971, checked in by thasling, 6 years ago

#2615:
increased message size to 20MB
made changes in some interfaces

File size: 2.0 KB
Line 
1using System;
2using System.Net;
3using System.Net.Sockets;
4using System.ServiceModel;
5using System.Threading;
6using DistributedGA.Core.Domain;
7using DistributedGA.Core.Interface;
8
9namespace DistributedGA.Core.Implementation {
10  public class WcfMessageService : IMessageService {
11    public event EventHandler<MessageRecieveEventArgs> OnDataRecieved;
12
13    private ManualResetEvent _ResetEvent = new ManualResetEvent(false);
14
15    private IMessageContract messageContract = null;
16
17    public int Init(string ip) {
18      int port = 0;
19      port = FreeTcpPort();
20
21      messageContract = new MessageContractImpl();
22      messageContract.MessageRecieved += new EventHandler<MessageRecieveEventArgs>(OnMessageRecieved);
23
24      var serviceUrl = "DistributedGA.svc";
25      new Thread(() => {
26        var baseUri = new Uri(string.Concat("net.tcp://", ip, ":", port, "/DistributedGA"));
27        var serviceUri = new Uri(baseUri, serviceUrl);
28        NetTcpBinding binding = new NetTcpBinding();
29        binding.MaxReceivedMessageSize = 20000000;
30        binding.MaxBufferSize = 20000000;
31        binding.MaxBufferPoolSize = 20000000;
32        binding.ReaderQuotas.MaxArrayLength = 20000000;
33        binding.ReaderQuotas.MaxDepth = 32;
34        binding.ReaderQuotas.MaxStringContentLength = 20000000;
35
36        using (var host = new ServiceHost(messageContract, serviceUri)) {
37          host.AddServiceEndpoint(typeof(IMessageContract), binding, serviceUri);
38
39          host.Open();
40
41          _ResetEvent.WaitOne();
42        }
43      }).Start();
44      return port;
45    }
46
47    public void Dispose() {
48      _ResetEvent.Set();
49    }
50
51    private int FreeTcpPort() {
52      TcpListener l = new TcpListener(IPAddress.Loopback, 0);
53      l.Start();
54      int port = ((IPEndPoint)l.LocalEndpoint).Port;
55      l.Stop();
56      return port;
57    }
58
59    private void OnMessageRecieved(object sender, MessageRecieveEventArgs e) {
60      if (OnDataRecieved != null) {
61        OnDataRecieved(this, e);
62      }
63    }
64  }
65}
Note: See TracBrowser for help on using the repository browser.