From 2e818a691ebdc6a4c41463b56f522d49f673e789 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 10:02:16 +0000 Subject: Utilisateur : Fcolin Date : 19/09/05 Heure : 14:14 Archivé dans $/CSharp/Ivy Commentaire: Passage Nouvel API IVY ( pas teste ) (vss 2) --- CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs | 110 +++++++++--------------------------- 1 file changed, 28 insertions(+), 82 deletions(-) (limited to 'CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs') diff --git a/CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs b/CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs index 780b3d6..e765ab5 100644 --- a/CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs +++ b/CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs @@ -12,12 +12,15 @@ namespace IvyBus /// public class IvyStream : NetworkStream { - StringBuilder msg_buffer; /* the message buffer */ + BinaryReader input; + BinaryWriter output; public IvyStream(Socket socket) : base( socket, true ) { + input = new BinaryReader( new BufferedStream(this),Encoding.ASCII); + output = new BinaryWriter( new BufferedStream(this), Encoding.ASCII); } /* the protocol magic numbers */ - internal enum MessageType : int + internal enum MessageType : ushort { Bye = 0, /* end of the peer */ AddRegexp = 1, /* the peer adds a regexp */ @@ -35,29 +38,21 @@ namespace IvyBus 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 Syntax: + * this is a binary formated message use of network representation + * * message Format: - MessageType + MESSAGE_SEPARATOR + id + MESSAGE_SEPARATOR + string + MESSAGE_SEPARATOR + string .... - MessageType + MESSAGE_SEPARATOR + id + MESSAGE_SEPARATOR + string + MessageType, id , length, string */ - internal void sendMsg(MessageType type, int id, string arg) + internal void sendMsg(MessageType type, int id, byte[] 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(); + output.Write( (ushort)IPAddress.HostToNetworkOrder( (short)type ) ); + output.Write( (ushort)IPAddress.HostToNetworkOrder( (short)id ) ); + output.Write( (ushort)IPAddress.HostToNetworkOrder( (short)arg.Length ) ); + output.Write( arg ); + output.Flush(); } catch (IOException ie) { @@ -65,81 +60,32 @@ namespace IvyBus Console.Error.WriteLine(ie.StackTrace); } } - internal void sendMsg(MessageType type, Int32 id, string[] args) + internal bool receiveMsg(out MessageType type, out int id, out byte[] data) { - 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; + int length = 0; + data = null; 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 ); - + type = MessageType.Die; + try { - msgType = (IvyStream.MessageType)Int32.Parse(msgFields[0]); + type = (MessageType)(ushort) IPAddress.NetworkToHostOrder( (short) input.ReadUInt16() ); + id = (ushort) IPAddress.NetworkToHostOrder( (short) input.ReadUInt16()); + length = (ushort) IPAddress.NetworkToHostOrder( (short) input.ReadUInt16()); + data = input.ReadBytes( length ); } - catch (FormatException nfe) + catch (EndOfStreamException nfe) { - throw new IvyException("protocol error on msgType"); - } - - try - { - msgId = Int32.Parse(msgFields[1]); + return false; } catch (FormatException nfe) { - throw new IvyException("protocol error on identifier"); + throw new IvyException("protocol error on msgType"); } - 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()); - } + return true; } - } } -- cgit v1.1