[13538] | 1 | using System;
|
---|
[13524] | 2 | using System.Net;
|
---|
[14263] | 3 | using System.Net.NetworkInformation;
|
---|
[13524] | 4 | using System.Net.Sockets;
|
---|
| 5 | using System.ServiceModel;
|
---|
| 6 | using System.Threading;
|
---|
[13538] | 7 | using DistributedGA.Core.Domain;
|
---|
| 8 | using DistributedGA.Core.Interface;
|
---|
[13524] | 9 |
|
---|
[13924] | 10 | namespace DistributedGA.Core.Implementation {
|
---|
[14252] | 11 | public class WcfMessageService : IMessageService, IDisposable {
|
---|
[13924] | 12 | public event EventHandler<MessageRecieveEventArgs> OnDataRecieved;
|
---|
[13524] | 13 |
|
---|
[13956] | 14 | private ManualResetEvent _ResetEvent = new ManualResetEvent(false);
|
---|
[13524] | 15 |
|
---|
[13924] | 16 | private IMessageContract messageContract = null;
|
---|
[13524] | 17 |
|
---|
[13924] | 18 | public int Init(string ip) {
|
---|
| 19 | int port = 0;
|
---|
| 20 | port = FreeTcpPort();
|
---|
[13524] | 21 |
|
---|
[13924] | 22 | messageContract = new MessageContractImpl();
|
---|
| 23 | messageContract.MessageRecieved += new EventHandler<MessageRecieveEventArgs>(OnMessageRecieved);
|
---|
[13524] | 24 |
|
---|
[13924] | 25 | var serviceUrl = "DistributedGA.svc";
|
---|
| 26 | new Thread(() => {
|
---|
[14263] | 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;
|
---|
[13524] | 37 |
|
---|
[14263] | 38 | using (var host = new ServiceHost(messageContract, serviceUri)) {
|
---|
| 39 | host.AddServiceEndpoint(typeof(IMessageContract), binding, serviceUri);
|
---|
[13524] | 40 |
|
---|
[14263] | 41 | host.Open();
|
---|
[13524] | 42 |
|
---|
[14263] | 43 | _ResetEvent.WaitOne();
|
---|
| 44 | }
|
---|
[13524] | 45 | }
|
---|
[14264] | 46 | catch (Exception) { }
|
---|
[13924] | 47 | }).Start();
|
---|
| 48 | return port;
|
---|
[14263] | 49 |
|
---|
[13924] | 50 | }
|
---|
[13524] | 51 |
|
---|
[13924] | 52 | public void Dispose() {
|
---|
| 53 | _ResetEvent.Set();
|
---|
[14252] | 54 | _ResetEvent.Dispose();
|
---|
[13924] | 55 | }
|
---|
[13547] | 56 |
|
---|
[14263] | 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 |
|
---|
[13924] | 82 | private int FreeTcpPort() {
|
---|
[14263] | 83 | for (int i = 11000; i <= 11100; i++) {
|
---|
| 84 | if (IsPortAvaiable(i))
|
---|
| 85 | return i;
|
---|
| 86 | }
|
---|
| 87 | return 0;
|
---|
[13924] | 88 | }
|
---|
[13524] | 89 |
|
---|
[14263] | 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 |
|
---|
[13924] | 98 | private void OnMessageRecieved(object sender, MessageRecieveEventArgs e) {
|
---|
| 99 | if (OnDataRecieved != null) {
|
---|
| 100 | OnDataRecieved(this, e);
|
---|
| 101 | }
|
---|
[13538] | 102 | }
|
---|
[13924] | 103 | }
|
---|
[13524] | 104 | }
|
---|