1 | using System;
|
---|
2 | using System.Net;
|
---|
3 | using System.Net.NetworkInformation;
|
---|
4 | using System.Net.Sockets;
|
---|
5 | using System.ServiceModel;
|
---|
6 | using System.Threading;
|
---|
7 | using DistributedGA.Core.Domain;
|
---|
8 | using DistributedGA.Core.Interface;
|
---|
9 |
|
---|
10 | namespace 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) { }
|
---|
47 | }).Start();
|
---|
48 | return port;
|
---|
49 |
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void Dispose() {
|
---|
53 | _ResetEvent.Set();
|
---|
54 | _ResetEvent.Dispose();
|
---|
55 | }
|
---|
56 |
|
---|
57 | private bool IsPortAvaiable(int port) {
|
---|
58 | //int port = 456; //<--- This is your value
|
---|
59 | bool isAvailable = true;
|
---|
60 |
|
---|
61 | // Evaluate current system tcp connections. This is the same information provided
|
---|
62 | // by the netstat command line application, just in .Net strongly-typed object
|
---|
63 | // form. We will look through the list, and if our port we would like to use
|
---|
64 | // in our TcpClient is occupied, we will set isAvailable to false.
|
---|
65 | IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
|
---|
66 | TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
|
---|
67 | foreach (TcpConnectionInformation tcpi in tcpConnInfoArray) {
|
---|
68 | if (tcpi.LocalEndPoint.Port == port) {
|
---|
69 | isAvailable = false;
|
---|
70 | break;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | foreach (var item in ipGlobalProperties.GetActiveTcpListeners()) {
|
---|
74 | if (item.Port == port) {
|
---|
75 | isAvailable = false;
|
---|
76 | break;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | return isAvailable;
|
---|
80 | }
|
---|
81 |
|
---|
82 | private int FreeTcpPort() {
|
---|
83 | for (int i = 11000; i <= 11100; i++) {
|
---|
84 | if (IsPortAvaiable(i))
|
---|
85 | return i;
|
---|
86 | }
|
---|
87 | return 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 | //private int FreeTcpPort() {
|
---|
91 | // TcpListener l = new TcpListener(IPAddress.Loopback, 0);
|
---|
92 | // l.Start();
|
---|
93 | // int port = ((IPEndPoint)l.LocalEndpoint).Port;
|
---|
94 | // l.Stop();
|
---|
95 | // return port;
|
---|
96 | //}
|
---|
97 |
|
---|
98 | private void OnMessageRecieved(object sender, MessageRecieveEventArgs e) {
|
---|
99 | if (OnDataRecieved != null) {
|
---|
100 | OnDataRecieved(this, e);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|