summaryrefslogtreecommitdiff
path: root/CSharp
diff options
context:
space:
mode:
authorfcolin2007-02-01 09:51:33 +0000
committerfcolin2007-02-01 09:51:33 +0000
commitad64569520526bef65e3bf6de7cdea94499c8b19 (patch)
treea5a5dbb78e8a0b0b5e92c255fd49f82658cc0e3d /CSharp
parent5b07cdad68c7d1fb9da8a3b963dd749cc833b59a (diff)
downloadivy-csharp-ad64569520526bef65e3bf6de7cdea94499c8b19.zip
ivy-csharp-ad64569520526bef65e3bf6de7cdea94499c8b19.tar.gz
ivy-csharp-ad64569520526bef65e3bf6de7cdea94499c8b19.tar.bz2
ivy-csharp-ad64569520526bef65e3bf6de7cdea94499c8b19.tar.xz
Utilisateur : Fcolin Date : 19/09/05 Heure : 14:14 Archivé dans $/CSharp/Ivy Commentaire: Passage Nouvel API IVY ( pas teste ) (vss 9)
Diffstat (limited to 'CSharp')
-rw-r--r--CSharp/Ivy/Ivy/IvyWatcher.cs46
1 files changed, 27 insertions, 19 deletions
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
}
}