From d5a7d0b42afff3f083d7d6cdcd21671aeb9c9449 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 09:58:51 +0000 Subject: Utilisateur : Fcolin Date : 2/06/06 Heure : 16:47 Archivé dans $/CSharp/Ivy/Ivy Commentaire: Mise en conformite du code avec les guidelines , et changement prototype evenements (vss 45) --- CSharp/Ivy/IvyPPC/IvyClient.cs | 134 +++++++++++++++++++++++++---------------- 1 file changed, 83 insertions(+), 51 deletions(-) (limited to 'CSharp/Ivy/IvyPPC/IvyClient.cs') diff --git a/CSharp/Ivy/IvyPPC/IvyClient.cs b/CSharp/Ivy/IvyPPC/IvyClient.cs index d3dbca2..7f2c838 100644 --- a/CSharp/Ivy/IvyPPC/IvyClient.cs +++ b/CSharp/Ivy/IvyPPC/IvyClient.cs @@ -25,11 +25,11 @@ namespace IvyBus /// die messages, direct messages, add or remove regexps, or quit. A thread is /// created for each remote client. /// - public class IvyClient : IvyProtocol, IComparable + public class IvyClient : IvyProtocol, IComparable, IDisposable { - public int CompareTo(IvyClient y) + public int CompareTo(IvyClient other) { - return (y.clientPriority - clientPriority); + return (other.clientPriority - clientPriority); } public String ApplicationName @@ -99,7 +99,6 @@ namespace IvyBus { bindings = new Dictionary(); appName = appname; - appPort = 0; this.bus = bus; IPEndPoint endpoint = (IPEndPoint)socket.RemoteEndPoint; @@ -164,19 +163,19 @@ namespace IvyBus /// the string that will be match-tested /// /// - public void sendDirectMsg(ushort id, string message) + public void SendDirectMsg(ushort id, string message) { try { stream.TokenDirectMsg( id, message); } - catch (IOException e) + catch (IOException ex) { - traceDebug("I can't send my message to this client. He probably left "+e.Message); + traceDebug("I can't send my message to this client. He probably left "+ex.Message); // first, I'm not a first class IvyClient any more bus.removeClient(this); // invokes the Disconnected applicationListeners - bus.FireClientDisconnected(this); + bus.OnClientDisconnected(new IvyEventArgs(this,id, message )); close(false); } } @@ -192,7 +191,7 @@ namespace IvyBus traceDebug("closing connexion to " + appName); if (doping ) { - stopPinging(); + StopPinging(); } if (notify) try @@ -301,9 +300,9 @@ namespace IvyBus break; } } - catch ( ObjectDisposedException e ) + catch ( ObjectDisposedException ex ) { - traceDebug( "socket closed "+e.Message ); + traceDebug( "socket closed "+ex.Message ); running = false; break; } @@ -319,9 +318,9 @@ namespace IvyBus running = false; break; } - catch (IOException e) + catch (IOException ex) { - if ( e.InnerException is SocketException ) + if ( ex.InnerException is SocketException ) { traceDebug( "socket closed" ); @@ -337,7 +336,7 @@ namespace IvyBus traceDebug("normally Disconnected from " + appName); traceDebug("Thread stopped"); // invokes the Disconnected applicationListeners - bus.FireClientDisconnected(this); + bus.OnClientDisconnected(new IvyEventArgs(this,0, "" )); // first, I'm not a first class IvyClient any more if (stream != null) { @@ -360,13 +359,14 @@ namespace IvyBus { traceDebug("received die Message from " + appName + "Raison: "+ arg); // invokes the die applicationListeners - Ivy.ApplicationExit killapp = bus.FireDie(this, id, arg); + IvyDieEventArgs ev = new IvyDieEventArgs(this, id, arg); + bus.OnDie(ev); // first, I'm not a first class IvyClient any more bus.removeClient(this); // makes the bus die - bus.stop(); + bus.Stop(); close(false); - if (killapp == Ivy.ApplicationExit.FORCE_EXIT) + if (ev.ForceExit) System.Windows.Forms.Application.Exit(); } void IvyProtocol.TokenBye(ushort err, string arg) @@ -384,28 +384,27 @@ namespace IvyBus { try { - if (bind.type == Ivy.BindingType.BindRegexp && !bus.CheckRegexp(bind.expression) ) + if (bind.type == Ivy.BindingType.BindRegexp && !bus.CheckRegexp(bind.expression)) { - bus.FireClientFilterBinding(this, bind.expression); + bus.OnClientFilterBinding(new IvyEventArgs(this, bind.key, bind.expression )); return; } - lock (bindings) - { - IvyBindingBase binding; - if (bind.type == Ivy.BindingType.BindRegexp) - { - binding = new IvyBindingRegexp(bind.key, bind.expression); - } - else - binding = new IvyBindingSimple(bind.key, bind.expression); - bindings.Add(bind.key, binding); - } - bus.FireClientAddBinding(this, bind.expression); + lock (bindings) + { + IvyBindingBase binding; + if (bind.type == Ivy.BindingType.BindRegexp) + binding = new IvyBindingRegexp(bind.key, bind.expression); + else + binding = new IvyBindingSimple(bind.key, bind.expression); + bindings.Add(bind.key, binding); + } + + bus.OnClientAddBinding(new IvyEventArgs(this, bind.key, bind.expression)); } - catch (ArgumentException e) + catch (ArgumentException ex) { - throw new IvyException("binding expression error " + e.Message); + throw new IvyException("binding expression error " + ex.Message); } } @@ -416,7 +415,7 @@ namespace IvyBus try { IvyBindingBase bind = (IvyBindingBase)bindings[id]; - bus.FireClientRemoveBinding(this, bind.expression); + bus.OnClientRemoveBinding(new IvyEventArgs(this, bind.key, bind.expression)); bindings.Remove(id); } catch (KeyNotFoundException ex) @@ -425,13 +424,13 @@ namespace IvyBus } } } - void IvyProtocol.TokenMsg(ushort id, string[] arg) + void IvyProtocol.TokenMsg(ushort id, string[] args) { - bus.FireCallback(this, id, arg); + bus.FireCallback(new IvyMessageEventArgs(this, id, args)); } void IvyProtocol.TokenError(ushort id, string arg) { - bus.FireError(this, id, arg); + bus.OnError(new IvyEventArgs(this, id, arg)); traceDebug("Error msg " + id + " " + arg); } void IvyProtocol.TokenApplicationId(ushort id, string arg) @@ -445,13 +444,13 @@ namespace IvyBus } void IvyProtocol.TokenEndRegexp() { - bus.FireClientConnected(this); - /* - * the peer is perhaps not ready to handle this message - * an assymetric processing should be written - */ - if (bus.ReadyMessage != null) - sendMsg(bus.ReadyMessage); + bus.OnClientConnected(new IvyEventArgs(this, 0, "")); + /* + * the peer is perhaps not ready to handle this message + * an assymetric processing should be written + */ + if (bus.ReadyMessage != null) + sendMsg(bus.ReadyMessage); } void IvyProtocol.TokenStartRegexp(ushort id, string arg) { @@ -466,8 +465,7 @@ namespace IvyBus } void IvyProtocol.TokenDirectMsg(ushort id, string arg) { - - bus.FireDirectMessage(this, id, arg ); + bus.OnDirectMessage(new IvyEventArgs(this,id,arg)); } void IvyProtocol.TokenPing(string arg) { @@ -490,10 +488,11 @@ namespace IvyBus [Conditional("DEBUG")] private void traceDebug(String s) { - Trace.Assert(!Ivy.VerboseDebug, "-->IvyClient " + this.bus.appName + ":" + appName + "<-- " + s); + Trace.Assert(!Ivy.VerboseDebug, "-->IvyClient " + bus.appName + ":" + appName + "<-- " + s); } - - internal bool isPinging = false; + /* is the Pinging Thread Runninng */ + internal bool isPinging; + private void PingerRun() { isPinging = true; @@ -512,11 +511,44 @@ namespace IvyBus } traceDebug("Pinger Thread stopped"); } - public virtual void stopPinging() + public virtual void StopPinging() { isPinging = false; //pingerThread.Interrupt(); pingerThread.Abort(); } - } + + #region IDisposable Members + + //Implement IDisposable. + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + // Free other state (managed objects). + } + // Free your own state (unmanaged objects). + // Set large fields to null. + if (stream != null) + { + stream.Close(); + stream = null; + } + } + + // Use C# destructor syntax for finalization code. + ~IvyClient() + { + // Simply call Dispose(false). + Dispose(false); + } + + #endregion + } } \ No newline at end of file -- cgit v1.1