From 79a6e113a0556839cb9bb37927b5b9a11ee74ca1 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 09:58:41 +0000 Subject: Utilisateur : Fcolin Date : 10/02/06 Heure : 15:32 Archivé dans $/CSharp/Ivy/Ivy Commentaire: simplification traitement exception (vss 40) --- CSharp/Ivy/IvyPPC/IvyClient.cs | 123 ++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 75 deletions(-) (limited to 'CSharp/Ivy/IvyPPC/IvyClient.cs') diff --git a/CSharp/Ivy/IvyPPC/IvyClient.cs b/CSharp/Ivy/IvyPPC/IvyClient.cs index b71a28c..547875c 100644 --- a/CSharp/Ivy/IvyPPC/IvyClient.cs +++ b/CSharp/Ivy/IvyPPC/IvyClient.cs @@ -79,8 +79,7 @@ namespace IvyBus } private Ivy bus; - private Socket socket; - private Dictionary bindings; + private Dictionary bindings; private int appPort; private string clientId; /* an unique ID for each IvyClient */ private int clientPriority; /* client priority */ @@ -103,8 +102,7 @@ namespace IvyBus appName = appname; appPort = 0; this.bus = bus; - this.socket = socket; - + IPEndPoint endpoint = (IPEndPoint)socket.RemoteEndPoint; remoteHost = endpoint.Address; @@ -180,14 +178,7 @@ namespace IvyBus bus.removeClient(this); // invokes the Disconnected applicationListeners bus.FireClientDisconnected(this); - try - { - close(false); - } - catch (IOException ioe) - { - throw new IvyException("close failed" + ioe.Message); - } + close(false); } } @@ -205,17 +196,42 @@ namespace IvyBus stopPinging(); } if (notify) - stream.TokenBye(0,"hasta la vista"); - stopListening(); - // bus.clientDisconnect(this); - // in_stream close in the thread - if ( stream != null ) - { - stream.Close(); - stream = null; - //socket.Close(); // pris en charge par stream ( NetWorkStream ) - socket = null; - } + try + { + stream.TokenBye(0, "hasta la vista"); + } + catch (IOException ioe) + { + throw new IvyException(ioe.Message); + } + // stop the thread and close the stream + if (clientThread == null) + return; + // Tell Thread to stop. + if (stream != null) + { + try + { + stream.Close(); // should stop the Reading Client Thread + } + catch (IOException ioe) + { + throw new IvyException(ioe.Message); + } + //socket.Close(); // pris en charge par stream ( NetWorkStream ) + stream = null; + } + // Potential dead lok when thread issue ClientDisconnected event + //if (Thread.CurrentThread != clientThread && (clientThread != null)) + //{ + // // Wait for Thread to end. + // bool term = clientThread.Join(10000); + // if (!term && (clientThread != null)) clientThread.Abort(); + //} + + clientThread = null; + + } /// sends the substrings of a message to the peer for each matching regexp. @@ -247,23 +263,6 @@ namespace IvyBus return count; } - internal void stopListening() - { - if (clientThread == null) - return ; - // Tell Thread to stop. - if ( stream != null ) - stream.Close(); - if (Thread.CurrentThread != clientThread && (clientThread != null) ) - { - // Wait for Thread to end. - bool term = clientThread.Join(10000); - if (!term && (clientThread != null) ) clientThread.Abort(); - } - - clientThread = null; - } - /// compares two peers the id is the couple (host,service port). /// /// the other peer @@ -281,15 +280,8 @@ namespace IvyBus /// private void Run() { + traceDebug("Connected from " + RemoteAddress + ":" + RemotePort); - try - { - traceDebug("Connected from " + RemoteAddress + ":" + RemotePort); - } - catch (Exception ie) - { - traceDebug("Interrupted while resolving remote hostname "+ie.Message); - } traceDebug("Thread started"); bool running = true; @@ -306,6 +298,7 @@ namespace IvyBus else { traceDebug("receiveMsg false ! leaving the thread"); + running = false; break; } } @@ -360,20 +353,13 @@ namespace IvyBus override internal void TokenDie(ushort id, string arg) { traceDebug("received die Message from " + appName + "Raison: "+ arg); - // first, I'm not a first class IvyClient any more - bus.removeClient(this); // invokes the die applicationListeners Ivy.ApplicationExit killapp = bus.FireDie(this, id, arg); - // makes the bus die + // first, I'm not a first class IvyClient any more + bus.removeClient(this); + // makes the bus die bus.stop(); - try - { - close(false); - } - catch (IOException ioe) - { - throw new IvyException(ioe.Message); - } + close(false); if (killapp == Ivy.ApplicationExit.FORCE_EXIT) System.Windows.Forms.Application.Exit(); } @@ -385,14 +371,7 @@ namespace IvyBus bus.removeClient(this); // invokes the disconnect applicationListeners //bus.FireClientDisconnected(this); done in Running Thread - try - { - close(false); // will fire diconnected - } - catch (IOException ioe) - { - throw new IvyException(ioe.Message); - } + close(false); // will fire diconnected } override internal void TokenAddBinding(Ivy.ApplicationBinding bind) @@ -438,6 +417,7 @@ namespace IvyBus } override internal void TokenError(ushort id, string arg) { + bus.FireError(this, id, arg); traceDebug("Error msg " + id + " " + arg); } override internal void TokenApplicationId(ushort id, string arg) @@ -465,14 +445,7 @@ namespace IvyBus appPort = id; if (bus.checkConnected(this)) { - try - { - close(false); - } - catch (IOException ioe) - { - throw new IvyException("io " + ioe.Message); - } + close(false); throw new IvyException("Rare ! A concurrent connect occured"); } -- cgit v1.1