From e33b3ff92d601af9c4c79266ae30dafddd211dc1 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 09:58:09 +0000 Subject: Utilisateur : Fcolin Date : 1/12/05 Heure : 17:47 Archivé dans $/CSharp/Ivy Commentaire: (vss 24) --- CSharp/Ivy/IvyPPC/IvyClient.cs | 84 ++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 57 deletions(-) diff --git a/CSharp/Ivy/IvyPPC/IvyClient.cs b/CSharp/Ivy/IvyPPC/IvyClient.cs index b236c7e..9e72e92 100644 --- a/CSharp/Ivy/IvyPPC/IvyClient.cs +++ b/CSharp/Ivy/IvyPPC/IvyClient.cs @@ -86,7 +86,6 @@ namespace IvyBus private Ivy bus; private Socket socket; - private IvyTCPStream stream; private Hashtable bindings; private int appPort; private string clientId; /* an unique ID for each IvyClient */ @@ -101,6 +100,7 @@ namespace IvyBus // protected variables internal String appName; + internal IvyTCPStreamV4 stream; internal IvyClient(Ivy bus, Socket socket, string appname) { @@ -111,26 +111,26 @@ namespace IvyBus this.socket = socket; socket.SetSocketOption( SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, 1 ); - stream = new IvyTCPStream( socket ); + stream = new IvyTCPStreamV4( socket ); clientPriority = Ivy.DEFAULT_PRIORITY; - sendApplicationId(); + stream.sendApplicationId(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.sendMsg(IvyTCPStream.MessageType.StartRegexp, bus.applicationPort, Encoding.ASCII.GetBytes( bus.appName )); + stream.sendStartRegexp( bus.applicationPort, bus.appName ); // sends our regexps to the peer lock( bus.bindings.SyncRoot ) { foreach (Ivy.ApplicationBinding bind in bus.bindings.Values ) { - sendBinding(bind); + stream.sendBinding(bind); } } - stream.sendMsg(IvyTCPStream.MessageType.EndRegexp, 0, Encoding.ASCII.GetBytes("")); + stream.sendEndRegexp(); // 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)); @@ -150,20 +150,7 @@ namespace IvyBus /// perhaps we should implement a new IvyApplicationListener method to /// allow the notification of regexp addition and deletion /// - internal void sendApplicationId() - { - stream.sendMsg( IvyTCPStream.MessageType.ApplicationId, bus.applicationPriority, Encoding.ASCII.GetBytes( bus.AppId ) ); - } - - internal void sendBinding(Ivy.ApplicationBinding bind) - { - stream.sendMsg(bind.type == Ivy.BindingType.BindRegexp ? IvyTCPStream.MessageType.AddRegexp: IvyTCPStream.MessageType.AddBinding, bind.key, Encoding.ASCII.GetBytes( bind.regexp )); /* perhaps we should perform some checking here */ - } - internal void delBinding(Ivy.ApplicationBinding bind) - { - stream.sendMsg(bind.type == Ivy.BindingType.BindRegexp ? IvyTCPStream.MessageType.DelRegexp: IvyTCPStream.MessageType.DelBinding, bind.key, Encoding.ASCII.GetBytes( "" )); - } /// sends a direct message to the peer /// /// the numeric value provided to the remote client @@ -175,7 +162,7 @@ namespace IvyBus { try { - stream.sendMsg(IvyTCPStream.MessageType.DirectMsg, id, Encoding.ASCII.GetBytes( message ) ); + stream.sendDirectMsg( id, message); } catch (IOException e) { @@ -209,7 +196,7 @@ namespace IvyBus stopPinging(); } if (notify) - sendBye("hasta la vista"); + stream.sendBye("hasta la vista"); stopListening(); // bus.clientDisconnect(this); // in_stream close in the thread @@ -241,7 +228,7 @@ namespace IvyBus IvyArgument args = bind.Match(message); if (args!=null) { - stream.sendMsg(IvyTCPStream.MessageType.Msg, bind.key, args.Serialize()); + stream.sendMsg(bind.key, args); count++; } } @@ -283,7 +270,7 @@ namespace IvyBus /// private void Run() { - IvyTCPStream.MessageType type; + IvyTCPStreamV4.MessageType type; int id; byte[] data; int length; @@ -518,68 +505,68 @@ namespace IvyBus { // I receive a ping. I can answer a pong. traceDebug("Ping msg from " + appName + " : " + Encoding.ASCII.GetString( arg) ); - sendPong(arg); + stream.sendPong(arg); } private void recvPong( int id, byte[] arg ) { traceDebug("Ping msg from " + appName + " : " + arg); } - private void DispatchMsg(IvyTCPStream.MessageType msgType, int msgId, byte[] msgData) + private void DispatchMsg(IvyTCPStreamV4.MessageType msgType, int msgId, byte[] msgData) { string strarg = Encoding.ASCII.GetString( msgData ); switch (msgType) { - case IvyTCPStream.MessageType.Die: + case IvyTCPStreamV4.MessageType.Die: recvDie( msgId, strarg ); break; - case IvyTCPStream.MessageType.Bye: + case IvyTCPStreamV4.MessageType.Bye: recvBye( msgId, strarg ); break; - case IvyTCPStream.MessageType.AddRegexp: + case IvyTCPStreamV4.MessageType.AddRegexp: recvAddRegexp( msgId, strarg ); break; - case IvyTCPStream.MessageType.DelRegexp: + case IvyTCPStreamV4.MessageType.DelRegexp: recvDelRegexp( msgId, strarg ); break; - case IvyTCPStream.MessageType.AddBinding: + case IvyTCPStreamV4.MessageType.AddBinding: recvAddBinding( msgId, strarg ); break; - case IvyTCPStream.MessageType.DelBinding: + case IvyTCPStreamV4.MessageType.DelBinding: recvDelBinding( msgId, strarg ); break; - case IvyTCPStream.MessageType.EndRegexp: + case IvyTCPStreamV4.MessageType.EndRegexp: recvEndRegexp( msgId, strarg ); break; - case IvyTCPStream.MessageType.Msg: + case IvyTCPStreamV4.MessageType.Msg: recvMsg( msgId, msgData ); break; - case IvyTCPStream.MessageType.Pong: + case IvyTCPStreamV4.MessageType.Pong: recvPong( msgId, msgData ); break; - case IvyTCPStream.MessageType.Ping: + case IvyTCPStreamV4.MessageType.Ping: recvPing( msgId, msgData ); break; - case IvyTCPStream.MessageType.Error: + case IvyTCPStreamV4.MessageType.Error: recvError( msgId, strarg ); break; - case IvyTCPStream.MessageType.StartRegexp: + case IvyTCPStreamV4.MessageType.StartRegexp: recvStartRegexp( msgId, strarg ); break; - case IvyTCPStream.MessageType.DirectMsg: + case IvyTCPStreamV4.MessageType.DirectMsg: recvDirectMsg( msgId, msgData ); break; - case IvyTCPStream.MessageType.ApplicationId: + case IvyTCPStreamV4.MessageType.ApplicationId: recvApplicationId( msgId, strarg ); break; default: @@ -588,24 +575,7 @@ namespace IvyBus } } - private void sendPong(byte[] s) - { - stream.sendMsg(IvyTCPStream.MessageType.Pong, 0, s); - } - public void sendPing(string s) - { - stream.sendMsg(IvyTCPStream.MessageType.Ping, 0, Encoding.ASCII.GetBytes(s)); - } - - private void sendBye(string message) - { - stream.sendMsg(IvyTCPStream.MessageType.Bye, 0, Encoding.ASCII.GetBytes( message )); - } - public void sendDie(string message) - { - stream.sendMsg(IvyTCPStream.MessageType.Die, 0, Encoding.ASCII.GetBytes( message ) ); - } public override String ToString() @@ -629,7 +599,7 @@ namespace IvyBus try { Thread.Sleep(new TimeSpan(10000 * PINGTIMEOUT)); - sendPing("are you here ?"); + stream.sendPing("are you here ?"); } catch (ThreadInterruptedException ie) { -- cgit v1.1