summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfcolin2007-02-01 10:03:22 +0000
committerfcolin2007-02-01 10:03:22 +0000
commitd5c2c6b9e10e8c86a07b1a6d73dc7d087a174ef5 (patch)
treed4515f5507f579635add69097dd24d28067460ef
parent58a4790b0859a23990bc747432365ba797c9a522 (diff)
downloadivy-csharp-d5c2c6b9e10e8c86a07b1a6d73dc7d087a174ef5.zip
ivy-csharp-d5c2c6b9e10e8c86a07b1a6d73dc7d087a174ef5.tar.gz
ivy-csharp-d5c2c6b9e10e8c86a07b1a6d73dc7d087a174ef5.tar.bz2
ivy-csharp-d5c2c6b9e10e8c86a07b1a6d73dc7d087a174ef5.tar.xz
Utilisateur : Fcolin Date : 19/09/05 Heure : 14:14 Archivé dans $/CSharp/Ivy Commentaire: Passage Nouvel API IVY ( pas teste ) (vss 9)
-rw-r--r--CSharp/Ivy/IvyPPC/IvyWatcher.cs46
1 files changed, 27 insertions, 19 deletions
diff --git a/CSharp/Ivy/IvyPPC/IvyWatcher.cs b/CSharp/Ivy/IvyPPC/IvyWatcher.cs
index caac8b7..cf84233 100644
--- a/CSharp/Ivy/IvyPPC/IvyWatcher.cs
+++ b/CSharp/Ivy/IvyPPC/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
}
}