Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14263 was 14263, checked in by thasling, 8 years ago

#2615:
changed behaviour when determening a new free tcp port

File size: 3.4 KB
Line 
1using System;
2using System.Net;
3using System.Net.NetworkInformation;
4using System.Net.Sockets;
5using System.ServiceModel;
6using System.Threading;
7using DistributedGA.Core.Domain;
8using DistributedGA.Core.Interface;
9
10namespace DistributedGA.Core.Implementation {
11  public class WcfMessageService : IMessageService, IDisposable {
12    public event EventHandler<MessageRecieveEventArgs> OnDataRecieved;
13
14    private ManualResetEvent _ResetEvent = new ManualResetEvent(false);
15
16    private IMessageContract messageContract = null;
17
18    public int Init(string ip) {
19      int port = 0;
20      port = FreeTcpPort();
21
22      messageContract = new MessageContractImpl();
23      messageContract.MessageRecieved += new EventHandler<MessageRecieveEventArgs>(OnMessageRecieved);
24
25      var serviceUrl = "DistributedGA.svc";
26      new Thread(() => {
27        try {
28          var baseUri = new Uri(string.Concat("net.tcp://", ip, ":", port, "/DistributedGA"));
29          var serviceUri = new Uri(baseUri, serviceUrl);
30          NetTcpBinding binding = new NetTcpBinding();
31          binding.MaxReceivedMessageSize = 20000000;
32          binding.MaxBufferSize = 20000000;
33          binding.MaxBufferPoolSize = 20000000;
34          binding.ReaderQuotas.MaxArrayLength = 20000000;
35          binding.ReaderQuotas.MaxDepth = 32;
36          binding.ReaderQuotas.MaxStringContentLength = 20000000;
37
38          using (var host = new ServiceHost(messageContract, serviceUri)) {
39            host.AddServiceEndpoint(typeof(IMessageContract), binding, serviceUri);
40
41            host.Open();
42
43            _ResetEvent.WaitOne();
44          }
45        }
46        catch (Exception ex) {
47          int k = 0;
48          int j = 0;
49        }
50      }).Start();
51      return port;
52
53    }
54
55    public void Dispose() {
56      _ResetEvent.Set();
57      _ResetEvent.Dispose();
58    }
59
60    private bool IsPortAvaiable(int port) {
61      //int port = 456; //<--- This is your value
62      bool isAvailable = true;
63
64      // Evaluate current system tcp connections. This is the same information provided
65      // by the netstat command line application, just in .Net strongly-typed object
66      // form.  We will look through the list, and if our port we would like to use
67      // in our TcpClient is occupied, we will set isAvailable to false.
68      IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
69      TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
70      foreach (TcpConnectionInformation tcpi in tcpConnInfoArray) {
71        if (tcpi.LocalEndPoint.Port == port) {
72          isAvailable = false;
73          break;
74        }
75      }
76      foreach (var item in ipGlobalProperties.GetActiveTcpListeners()) {
77        if (item.Port == port) {
78          isAvailable = false;
79          break;
80        }
81      }
82      return isAvailable;
83    }
84
85    private int FreeTcpPort() {
86      for (int i = 11000; i <= 11100; i++) {
87        if (IsPortAvaiable(i))
88          return i;
89      }
90      return 0;
91    }
92
93    //private int FreeTcpPort() {
94    //  TcpListener l = new TcpListener(IPAddress.Loopback, 0);
95    //  l.Start();
96    //  int port = ((IPEndPoint)l.LocalEndpoint).Port;
97    //  l.Stop();
98    //  return port;
99    //}
100
101    private void OnMessageRecieved(object sender, MessageRecieveEventArgs e) {
102      if (OnDataRecieved != null) {
103        OnDataRecieved(this, e);
104      }
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.