From ad64569520526bef65e3bf6de7cdea94499c8b19 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 09:51:33 +0000 Subject: Utilisateur : Fcolin Date : 19/09/05 Heure : 14:14 Archivé dans $/CSharp/Ivy Commentaire: Passage Nouvel API IVY ( pas teste ) (vss 9) --- CSharp/Ivy/Ivy/IvyWatcher.cs | 46 ++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'CSharp/Ivy') diff --git a/CSharp/Ivy/Ivy/IvyWatcher.cs b/CSharp/Ivy/Ivy/IvyWatcher.cs index caac8b7..cf84233 100644 --- a/CSharp/Ivy/Ivy/IvyWatcher.cs +++ b/CSharp/Ivy/Ivy/IvyWatcher.cs @@ -52,7 +52,7 @@ namespace IvyBus // create the MulticastSocket try { - group = Dns.GetHostByName(domainaddr).AddressList[0]; + group = IPAddress.Parse(domainaddr); broadcast = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp); IPEndPoint EPhost = new IPEndPoint(IPAddress.Any, port); broadcast.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast,1); @@ -92,31 +92,30 @@ namespace IvyBus { len = broadcast.ReceiveFrom(buf, ref tempRemoteEP); // I was summoned to leave during the receive - String msg = Encoding.ASCII.GetString(buf,0,len); remoteEP = (IPEndPoint)tempRemoteEP; IPAddress remotehost = remoteEP.Address; - traceDebug("BUSWATCHER Receive Broadcast from " + Dns.GetHostName() + ":" + remoteEP.Port); + 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)); + //TODO if ( !isInDomain( remotehost ) ) continue; - String[] st = msg.Split(new char[]{' ','\n'},4); - if (st.Length != 4) - { - Console.Error.WriteLine("Bad format " + msg); - continue; - } - int version = Int32.Parse(st[0]); - int port = Int32.Parse(st[1]); - string appId = st[2]; - string appName = st[3]; if (version != Ivy.PROCOCOLVERSION) { Console.Error.WriteLine("Ignoring bad protocol version broadcast"); continue; } - if ((bus.applicationPort == port)) // self Broadcast + if ( appId == bus.AppId ) // self Broadcast continue; - traceDebug("BUSWATCHER Broadcast de " + Dns.GetHostName() + ":" + remoteEP.Port + " port " + port + + traceDebug("BUSWATCHER Broadcast de " + Dns.GetHostByAddress(remotehost).HostName + ":" + remoteEP.Port + " port " + port + " version " + version + " id " + appId + " name " + appName); @@ -125,7 +124,7 @@ namespace IvyBus Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint hostEndPoint = new IPEndPoint(remoteEP.Address, port); socket.Connect(hostEndPoint); - bus.addClient(socket); + bus.addClient(socket,appName); } catch (Exception e) { @@ -176,11 +175,20 @@ namespace IvyBus { lock(this) { - String hello = Ivy.PROCOCOLVERSION + " " + bus.applicationPort + " " + bus.AppId + " " + bus.AppName + "\n"; + 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 = Encoding.ASCII.GetBytes( hello ); + byte[] hellob = stream.GetBuffer(); IPEndPoint EPhost = new IPEndPoint(group, port); - broadcast.SendTo(hellob,0,EPhost); // notifies our arrival on each domain: protocol version + port + broadcast.SendTo(hellob,(int) stream.Length,0,EPhost); // notifies our arrival on each domain: protocol version + port } } -- cgit v1.1