From 71b778ec26e8d0a7b627019312430b15ffd687ca Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 10:03:24 +0000 Subject: Utilisateur : Fcolin Date : 30/11/05 Heure : 10:27 Archivé dans $/CSharp/Ivy Commentaire: (vss 10) --- CSharp/Ivy/IvyPPC/IvyWatcher.cs | 65 +++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 42 deletions(-) (limited to 'CSharp/Ivy/IvyPPC/IvyWatcher.cs') diff --git a/CSharp/Ivy/IvyPPC/IvyWatcher.cs b/CSharp/Ivy/IvyPPC/IvyWatcher.cs index cf84233..d9be71e 100644 --- a/CSharp/Ivy/IvyPPC/IvyWatcher.cs +++ b/CSharp/Ivy/IvyPPC/IvyWatcher.cs @@ -26,13 +26,12 @@ namespace IvyBus /// internal class IvyWatcher { - private bool isMulticastAddress = false; private Ivy bus; /* master bus controler */ - private Socket broadcast; /* supervision socket */// To do reuseaddr we must use a Socket not a udp client private String domainaddr; private int port; private volatile Thread listenThread; private IPAddress group; + private IvyUDPStream stream; /// creates an Ivy watcher /// @@ -49,22 +48,24 @@ namespace IvyBus this.port = port; listenThread = new Thread(new ThreadStart(this.Run)); listenThread.Name = "Ivy UDP Listener Thread"; - // create the MulticastSocket try { group = IPAddress.Parse(domainaddr); - broadcast = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp); + /* supervision socket */ + // To do reuseaddr we must use a Socket not a udp client + Socket broadcast = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint EPhost = new IPEndPoint(IPAddress.Any, port); broadcast.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast,1); broadcast.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress,1); broadcast.Bind(EPhost); - //test isMulticastAddress // TODO better check + //test isMulticastAddress // TODO better check + //if ( group.IsIPv6Multicast ) yes but in IPV4 how to do if ((group.Address & 0xf0000000) == 0xe0000000) { - isMulticastAddress = true; broadcast.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.AddMembership, new MulticastOption( group )); } + stream = new IvyUDPStream(broadcast); } catch (IOException e) { @@ -77,12 +78,7 @@ namespace IvyBus public void Run() { traceDebug("beginning of a watcher Thread"); - byte[] buf = new byte[4096]; - int len; - IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); - EndPoint tempRemoteEP = (EndPoint)remoteEP; - - remoteEP = null; + try { bool running = true; @@ -90,26 +86,22 @@ namespace IvyBus { try { - len = broadcast.ReceiveFrom(buf, ref tempRemoteEP); - // I was summoned to leave during the receive - remoteEP = (IPEndPoint)tempRemoteEP; + + int version; + int port; + string appId; + string appName; + IPEndPoint remoteEP; + + stream.receiveMsg(out remoteEP, out version,out port,out appId,out appName); IPAddress remotehost = remoteEP.Address; - traceDebug("BUSWATCHER Receive Broadcast from " + Dns.GetHostByAddress(remotehost).HostName+ ":" + remoteEP.Port); - MemoryStream stream = new MemoryStream( buf,0, len ); - BinaryReader input = new BinaryReader(stream, Encoding.ASCII); - - int version = (ushort)IPAddress.NetworkToHostOrder( (short) input.ReadUInt16()); - int port = (ushort)IPAddress.NetworkToHostOrder( (short)input.ReadUInt16() ); - int lenAppId = (ushort)IPAddress.NetworkToHostOrder( (short)input.ReadUInt16() ) ; - string appId = new String( input.ReadChars(lenAppId)); - int lenAppNameId = (ushort)IPAddress.NetworkToHostOrder( (short)input.ReadUInt16() ); - string appName = new String(input.ReadChars(lenAppNameId)); + traceDebug("BUSWATCHER Receive Broadcast from " + Dns.GetHostByAddress(remotehost).HostName+ ":" + remoteEP.Port); //TODO if ( !isInDomain( remotehost ) ) continue; - - if (version != Ivy.PROCOCOLVERSION) + + if (version != IvyUDPStream.PROCOCOLVERSION) { - Console.Error.WriteLine("Ignoring bad protocol version broadcast"); + Console.Error.WriteLine("Ignoring bad protocol version {0} expected {1}", version, IvyUDPStream.PROCOCOLVERSION); continue; } @@ -160,7 +152,7 @@ namespace IvyBus lock(this) { traceDebug("begining stopping an IvyWatcher"); - broadcast.Close(); + stream.Close(); if (listenThread != null) { listenThread.Join(); @@ -175,20 +167,9 @@ namespace IvyBus { lock(this) { - MemoryStream stream = new MemoryStream( 3*2 + bus.AppId.Length + bus.AppName.Length); - BinaryWriter output = new BinaryWriter(stream,Encoding.ASCII); - output.Write( (ushort) IPAddress.HostToNetworkOrder( (short)(Ivy.PROCOCOLVERSION )) ); - output.Write( (ushort) IPAddress.HostToNetworkOrder( (short)bus.applicationPort ) ); - output.Write( (ushort) IPAddress.HostToNetworkOrder( (short)bus.AppId.Length ) ); - output.Write( bus.AppId.ToCharArray()); - output.Write( (ushort) IPAddress.HostToNetworkOrder( (short)bus.AppName.Length ) ); - output.Write( bus.AppName.ToCharArray() ); - output.Flush(); - listenThread.Start(); - byte[] hellob = stream.GetBuffer(); - IPEndPoint EPhost = new IPEndPoint(group, port); - broadcast.SendTo(hellob,(int) stream.Length,0,EPhost); // notifies our arrival on each domain: protocol version + port + IPEndPoint EPhost = new IPEndPoint(group, port); + stream.sendMsg(EPhost, bus.applicationPort, bus.AppId, bus.AppName);// notifies our arrival on each domain: protocol version + port } } -- cgit v1.1