summaryrefslogtreecommitdiff
path: root/CSharp/Ivy
diff options
context:
space:
mode:
authorfcolin2007-02-01 09:50:27 +0000
committerfcolin2007-02-01 09:50:27 +0000
commitf06cd1d53e05df333c5f8a34faa2805cfca8c11b (patch)
tree15194538cb8315feed89c71433776de815e89e94 /CSharp/Ivy
parent3e73595d0971595c3c68353fc5290c39a1e1ce20 (diff)
downloadivy-csharp-f06cd1d53e05df333c5f8a34faa2805cfca8c11b.zip
ivy-csharp-f06cd1d53e05df333c5f8a34faa2805cfca8c11b.tar.gz
ivy-csharp-f06cd1d53e05df333c5f8a34faa2805cfca8c11b.tar.bz2
ivy-csharp-f06cd1d53e05df333c5f8a34faa2805cfca8c11b.tar.xz
Utilisateur : Fcolin Date : 19/09/05 Heure : 14:14 Archivé dans $/CSharp/Ivy Commentaire: Passage Nouvel API IVY ( pas teste ) (vss 2)
Diffstat (limited to 'CSharp/Ivy')
-rw-r--r--CSharp/Ivy/Ivy/IvyTCPStreamV4.cs110
1 files changed, 28 insertions, 82 deletions
diff --git a/CSharp/Ivy/Ivy/IvyTCPStreamV4.cs b/CSharp/Ivy/Ivy/IvyTCPStreamV4.cs
index 780b3d6..e765ab5 100644
--- a/CSharp/Ivy/Ivy/IvyTCPStreamV4.cs
+++ b/CSharp/Ivy/Ivy/IvyTCPStreamV4.cs
@@ -12,12 +12,15 @@ namespace IvyBus
/// </summary>
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;
}
-
}
}