summaryrefslogtreecommitdiff
path: root/CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs
diff options
context:
space:
mode:
Diffstat (limited to 'CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs')
-rw-r--r--CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs145
1 files changed, 145 insertions, 0 deletions
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
+{
+ /// <summary>
+ /// Description résumée de IvyStream.
+ /// </summary>
+ 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());
+ }
+
+ }
+
+
+ }
+}