summaryrefslogtreecommitdiff
path: root/CSharp
diff options
context:
space:
mode:
authorfcolin2007-02-01 09:50:26 +0000
committerfcolin2007-02-01 09:50:26 +0000
commit3e73595d0971595c3c68353fc5290c39a1e1ce20 (patch)
treec5990ee1bb25d8aa09ad293d0a2d45054341a793 /CSharp
parent6a181e965c242866e159bd55dd4d434f7a6cb8bb (diff)
downloadivy-csharp-3e73595d0971595c3c68353fc5290c39a1e1ce20.zip
ivy-csharp-3e73595d0971595c3c68353fc5290c39a1e1ce20.tar.gz
ivy-csharp-3e73595d0971595c3c68353fc5290c39a1e1ce20.tar.bz2
ivy-csharp-3e73595d0971595c3c68353fc5290c39a1e1ce20.tar.xz
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)
Diffstat (limited to 'CSharp')
-rw-r--r--CSharp/Ivy/Ivy/IvyTCPStreamV4.cs145
1 files changed, 145 insertions, 0 deletions
diff --git a/CSharp/Ivy/Ivy/IvyTCPStreamV4.cs b/CSharp/Ivy/Ivy/IvyTCPStreamV4.cs
new file mode 100644
index 0000000..780b3d6
--- /dev/null
+++ b/CSharp/Ivy/Ivy/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());
+ }
+
+ }
+
+
+ }
+}