From 288cdc32626d778a1c2ce353a9813eb49e10492a Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 10:02:14 +0000 Subject: Utilisateur : Fcolin Date : 30/06/05 Heure : 14:21 Créé Commentaire: Bug start time < 0 en l'absence de rejeu a voir ajouter 1s de blank au debut (vss 1) --- CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs | 145 ++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs diff --git a/CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs b/CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs new file mode 100644 index 0000000..780b3d6 --- /dev/null +++ b/CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs @@ -0,0 +1,145 @@ +using System; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Collections; +using System.IO; + +namespace IvyBus +{ + /// + /// Description résumée de IvyStream. + /// + public class IvyStream : NetworkStream + { + StringBuilder msg_buffer; /* the message buffer */ + public IvyStream(Socket socket) : base( socket, true ) + { + } + /* the protocol magic numbers */ + internal enum MessageType : int + { + Bye = 0, /* end of the peer */ + AddRegexp = 1, /* the peer adds a regexp */ + Msg = 2, /* the peer sends a message */ + Error = 3, /* error message */ + DelRegexp = 4, /* the peer removes one of his regex */ + EndRegexp = 5, /* no more regexp in the handshake */ + StartRegexp = 6, /* avoid race condition in concurrent connexions */ + DirectMsg = 7, /* the peer sends a direct message */ + Die = 8, /* the peer wants us to quit */ + Ping = 9, /* checks the presence of the other */ + Pong = 10, /* checks the presence of the other */ + AddBinding = 11, /* other methods for binding message based on hash table */ + DelBinding = 12, /* other methods for binding message based on hash table */ + ApplicationId = 13, /* on start send my ID and priority */ + }; + /* + * message Separator: + */ + private const char MESSAGE_TERMINATOR = '\0'; /* the previous protocol used \n */ + private const char MESSAGE_SEPARATOR = '\x01'; /* end of arguments */ + /* + * message Format: + MessageType + MESSAGE_SEPARATOR + id + MESSAGE_SEPARATOR + string + MESSAGE_SEPARATOR + string .... + MessageType + MESSAGE_SEPARATOR + id + MESSAGE_SEPARATOR + string + */ + internal void sendMsg(MessageType type, int id, string arg) + { + try + { + StringBuilder builder = new StringBuilder(1024); + builder.Append( (int)type ); + builder.Append( MESSAGE_SEPARATOR ); + builder.Append( id ); + builder.Append( MESSAGE_SEPARATOR ); + builder.Append( arg ); + builder.Append( MESSAGE_TERMINATOR ); + byte[] buffer = Encoding.ASCII.GetBytes( builder.ToString() ); + Write(buffer,0,buffer.Length); + Flush(); + } + catch (IOException ie) + { + Console.Error.WriteLine("received an exception: " + ie.Message); + Console.Error.WriteLine(ie.StackTrace); + } + } + internal void sendMsg(MessageType type, Int32 id, string[] args) + { + StringBuilder builder = new StringBuilder(1024); + builder.Append( (int)type ); + builder.Append( MESSAGE_SEPARATOR ); + builder.Append( id ); + for (int sub = 0; sub < args.Length; sub++) + { + builder.Append( MESSAGE_SEPARATOR ); + builder.Append( args[sub] ); + } + builder.Append( MESSAGE_TERMINATOR ); + try + { + byte[] buffer = Encoding.ASCII.GetBytes( builder.ToString() ); + Write(buffer,0,buffer.Length); + Flush(); + } + catch (IOException ie) + { + Console.Error.WriteLine("received an exception: " + ie.Message); + Console.Error.WriteLine(ie.StackTrace); + } + } + internal bool receiveMsg(out MessageType type, out int id, out string[] args) + { + int data; + + type = MessageType.Die; + id = 0; + args = null; + msg_buffer = new StringBuilder(1024); + while ( true ) + { + data = ReadByte(); + if ( data == -1 ) return false; + if ( data == MESSAGE_TERMINATOR ) break; + msg_buffer.Append( (char)data ); + } + ParseMsg( out type, out id , out args ); + return true; + } + private void ParseMsg( out MessageType msgType, out int msgId, out string[] args) + { + string appID; + string[] msgFields = msg_buffer.ToString().Split( MESSAGE_SEPARATOR ); + + try + { + msgType = (IvyStream.MessageType)Int32.Parse(msgFields[0]); + } + catch (FormatException nfe) + { + throw new IvyException("protocol error on msgType"); + } + + try + { + msgId = Int32.Parse(msgFields[1]); + } + catch (FormatException nfe) + { + throw new IvyException("protocol error on identifier"); + } + + args = new String[msgFields.Length-2]; + for ( int i = 0; i < args.Length; i++) + { + args[i] = msgFields[2+i]; + // for developpemnt purposes + // out.println(" *"+tab[i]+"* "+(tab[i]).length()); + } + + } + + + } +} -- cgit v1.1