Last change
on this file since 13923 was
13923,
checked in by thasling, 8 years ago
|
#2615:
re-implemented WcfPeerListManager
also sending messages is now done in the background async
|
File size:
2.0 KB
|
Rev | Line | |
---|
[13538] | 1 | using System;
|
---|
[13524] | 2 | using System.Net;
|
---|
| 3 | using System.Net.Sockets;
|
---|
| 4 | using System.ServiceModel;
|
---|
| 5 | using System.Threading;
|
---|
[13538] | 6 | using DistributedGA.Core.Domain;
|
---|
| 7 | using DistributedGA.Core.Interface;
|
---|
[13524] | 8 |
|
---|
[13887] | 9 | namespace DistributedGA.Core.Implementation
|
---|
| 10 | {
|
---|
| 11 | public class WcfMessageService : IMessageService
|
---|
| 12 | {
|
---|
| 13 | public event EventHandler<MessageRecieveEventArgs> OnDataRecieved;
|
---|
[13524] | 14 |
|
---|
[13887] | 15 | private static ManualResetEvent _ResetEvent = new ManualResetEvent(false);
|
---|
[13524] | 16 |
|
---|
[13887] | 17 | private IMessageContract messageContract = null;
|
---|
[13524] | 18 |
|
---|
[13887] | 19 | public int Init(string ip)
|
---|
| 20 | {
|
---|
| 21 | int port = 0;
|
---|
| 22 | port = FreeTcpPort();
|
---|
[13524] | 23 |
|
---|
[13887] | 24 | messageContract = new MessageContractImpl();
|
---|
| 25 | messageContract.MessageRecieved += new EventHandler<MessageRecieveEventArgs>(OnMessageRecieved);
|
---|
[13524] | 26 |
|
---|
[13887] | 27 | var serviceUrl = "DistributedGA.svc";
|
---|
| 28 | new Thread(() =>
|
---|
| 29 | {
|
---|
| 30 | var baseUri = new Uri(string.Concat("net.tcp://", ip, ":", port, "/DistributedGA"));
|
---|
| 31 | var serviceUri = new Uri(baseUri, serviceUrl);
|
---|
| 32 | NetTcpBinding binding = new NetTcpBinding();
|
---|
[13524] | 33 |
|
---|
[13887] | 34 | using (var host = new ServiceHost(messageContract, serviceUri))
|
---|
| 35 | {
|
---|
| 36 | host.AddServiceEndpoint(typeof(IMessageContract), binding, serviceUri);
|
---|
[13524] | 37 |
|
---|
[13887] | 38 | host.Open();
|
---|
[13524] | 39 |
|
---|
[13887] | 40 | _ResetEvent.WaitOne();
|
---|
| 41 | }
|
---|
| 42 | }).Start();
|
---|
| 43 | return port;
|
---|
[13524] | 44 | }
|
---|
| 45 |
|
---|
[13887] | 46 | public void Dispose()
|
---|
| 47 | {
|
---|
| 48 | _ResetEvent.Set();
|
---|
| 49 | }
|
---|
[13547] | 50 |
|
---|
[13887] | 51 | private int FreeTcpPort()
|
---|
| 52 | {
|
---|
| 53 | TcpListener l = new TcpListener(IPAddress.Loopback, 0);
|
---|
| 54 | l.Start();
|
---|
| 55 | int port = ((IPEndPoint)l.LocalEndpoint).Port;
|
---|
| 56 | l.Stop();
|
---|
| 57 | return port;
|
---|
| 58 | }
|
---|
[13524] | 59 |
|
---|
[13887] | 60 | private void OnMessageRecieved(object sender, MessageRecieveEventArgs e)
|
---|
| 61 | {
|
---|
| 62 | if (OnDataRecieved != null)
|
---|
| 63 | {
|
---|
| 64 | OnDataRecieved(this, e);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
[13538] | 67 | }
|
---|
[13524] | 68 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.