diff options
-rw-r--r-- | CSharp/Ivy/IvyPPC/IvyClient.cs | 121 |
1 files changed, 77 insertions, 44 deletions
diff --git a/CSharp/Ivy/IvyPPC/IvyClient.cs b/CSharp/Ivy/IvyPPC/IvyClient.cs index 0e46765..5b46c6b 100644 --- a/CSharp/Ivy/IvyPPC/IvyClient.cs +++ b/CSharp/Ivy/IvyPPC/IvyClient.cs @@ -81,7 +81,7 @@ namespace IvyBus }
private Ivy bus;
- private Dictionary<ushort,IvyBindingBase> bindings;
+ private Dictionary<ushort,IvyBindingBase> bindings;
private int appPort;
private string clientId; /* an unique ID for each IvyClient */
private int clientPriority; /* client priority */
@@ -103,16 +103,13 @@ namespace IvyBus bindings = new Dictionary<ushort,IvyBindingBase>();
appName = appname;
this.bus = bus;
-
+
IPEndPoint endpoint = (IPEndPoint)socket.RemoteEndPoint;
remoteHost = endpoint.Address;
remotePort = endpoint.Port;
#if (!PocketPC )
- doping = Properties.Settings.Default.IvyPing;
-#endif
-#if (!PocketPC )
socket.SetSocketOption( SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1 );
#endif
@@ -122,35 +119,56 @@ namespace IvyBus stream = new IvyTCPStreamV3(socket, this);
clientPriority = Ivy.DEFAULT_PRIORITY;
+ // spawns a thread to manage the incoming traffic on this
+ // socket. We should be ready to receive messages now.
+ clientThread = new Thread(new ThreadStart(this.Run));
+ clientThread.Name = "Ivy Tcp Client Reader Thread ("+appname+")";
+ clientThread.Start();
- stream.TokenApplicationId(bus.applicationPriority, bus.AppId);
-
- // sends our ID, whether we initiated the connexion or not
- // the ID is the couple "host name,application Port", the host name
- // information is in the socket itself, the port is not known if we
- // initiate the connexion
- stream.TokenStartRegexp( bus.applicationPort, bus.appName );
- // sends our regexps to the peer
- lock( bus.bindings )
- {
- foreach (IvyApplicationBinding bind in bus.bindings.Values)
- {
- stream.TokenAddBinding(bind.Binding, bind.Key, bind.Expression);
- }
- }
- stream.TokenEndRegexp();
- // spawns a thread to manage the incoming traffic on this
- // socket. We should be ready to receive messages now.
- clientThread = new Thread(new ThreadStart(this.Run));
- clientThread.Name = "Ivy Tcp Client Reader Thread";
- clientThread.Start();
- if (doping)
- {
- pingerThread = new Thread(new ThreadStart(PingerRun));
- pingerThread.Name = "Ivy Pinger Thread";
- pingerThread.Start();
- }
}
+
+ internal void SendBindings()
+ {
+ try
+ {
+ stream.TokenApplicationId(bus.applicationPriority, bus.AppId);
+
+ // sends our ID, whether we initiated the connexion or not
+ // the ID is the couple "host name,application Port", the host name
+ // information is in the socket itself, the port is not known if we
+ // initiate the connexion
+ stream.TokenStartRegexp(bus.applicationPort, bus.appName);
+ // sends our regexps to the peer
+ lock (bus.bindings)
+ {
+ foreach (IvyApplicationBinding bind in bus.bindings.Values)
+ {
+ stream.TokenAddBinding(bind.Binding, bind.Key, bind.Expression);
+ }
+ }
+ stream.TokenEndRegexp();
+
+#if (!PocketPC )
+ doping = Properties.Settings.Default.IvyPing;
+#endif
+ if (doping)
+ {
+ pingerThread = new Thread(new ThreadStart(PingerRun));
+ pingerThread.Name = "Ivy Pinger Thread";
+ pingerThread.Start();
+ }
+
+
+ }
+ catch (IOException ex)
+ { // the client nous a coupé l'herbe sous le pied
+ traceDebug("I can't send my message to this client. He probably left " + ex.Message);
+ // invokes the Disconnected applicationListeners
+ //bus.OnClientDisconnected(new IvyEventArgs(this,id, message ));
+ // called by the receiver Thread
+ close(false);
+ }
+ }
/// <summary> returns the name of the remote agent.
/// allow an Ivy package class to access the list of regexps at a
@@ -178,7 +196,8 @@ namespace IvyBus // first, I'm not a first class IvyClient any more
bus.removeClient(this);
// invokes the Disconnected applicationListeners
- bus.OnClientDisconnected(new IvyEventArgs(this,id, message ));
+ //bus.OnClientDisconnected(new IvyEventArgs(this,id, message ));
+ // should be called by receiver thread
close(false);
}
}
@@ -247,16 +266,30 @@ namespace IvyBus int count = 0;
lock( bindings )
- {
- foreach (IvyBindingBase bind in bindings.Values )
- {
- string[] args = bind.Match(message);
- if ( stream != null && args!=null )
- {
- stream.TokenMsg(bind.Key, args);
- count++;
- }
- }
+ {
+ try
+ {
+ foreach (IvyBindingBase bind in bindings.Values)
+ {
+ string[] args = bind.Match(message);
+ if (stream != null && args != null)
+ {
+ stream.TokenMsg(bind.Key, args);
+ count++;
+ }
+ }
+ }
+ catch (IOException ex)
+ {
+ 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
+ // in the receiver thread
+ close(false);
+ }
+
+
}
return count;
}
@@ -377,7 +410,7 @@ namespace IvyBus bus.removeClient(this);
// invokes the disconnect applicationListeners
//bus.FireClientDisconnected(this); done in Running Thread
- close(false); // will fire diconnected
+ close(false); // will fire disconnected
}
void IvyProtocol.TokenAddBinding(BindingType type, ushort id, string expression)
|