diff options
author | fcolin | 2007-02-01 10:02:22 +0000 |
---|---|---|
committer | fcolin | 2007-02-01 10:02:22 +0000 |
commit | 66a6d038d1a141695ada1a463178fe997724304f (patch) | |
tree | d843a0c68d2e755a9c06a47a7fad9faa60e4a3a4 /CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs | |
parent | 433b00c556ddff4b86e4c97dc4e63675f9aaec78 (diff) | |
download | ivy-csharp-66a6d038d1a141695ada1a463178fe997724304f.zip ivy-csharp-66a6d038d1a141695ada1a463178fe997724304f.tar.gz ivy-csharp-66a6d038d1a141695ada1a463178fe997724304f.tar.bz2 ivy-csharp-66a6d038d1a141695ada1a463178fe997724304f.tar.xz |
Utilisateur : Fcolin Date : 19/12/05 Heure : 16:54 Archivé dans $/CSharp/Ivy Commentaire: (vss 5)
Diffstat (limited to 'CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs')
-rw-r--r-- | CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs | 260 |
1 files changed, 194 insertions, 66 deletions
diff --git a/CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs b/CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs index 1fead5a..0be051e 100644 --- a/CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs +++ b/CSharp/Ivy/IvyPPC/IvyTCPStreamV4.cs @@ -3,6 +3,7 @@ using System.Net; using System.Net.Sockets;
using System.Text;
using System.Collections;
+using System.Collections.Specialized;
using System.IO;
namespace IvyBus
@@ -10,33 +11,40 @@ namespace IvyBus /// <summary>
/// Description résumée de IvyStream.
/// </summary>
- public class IvyTCPStreamV4 : NetworkStream
+ internal class IvyTCPStreamV4 : IvyTCPStream
{
BinaryReader input;
BinaryWriter output;
- public IvyTCPStreamV4(Socket socket) : base( socket, true )
+ IvyProtocolInterface receiver;
+
+ /* the protocol magic numbers */
+ internal enum MessageType : ushort
+ {
+ Bye = 0, /* end of the peer */
+ AddRegexp = 1, /* the peer adds a regexp */
+ Msg = 2, /* the peer sends a message */
+ Error = 3, /* error message */
+ DelBinding = 4, /* the peer removes one of his regex */ // OLD DelRegexp rename to DelBinding
+ 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 */
+ ApplicationId = 11, /* on start send my ID and priority */
+ AddBinding = 12, /* other methods for binding message based on hash table */
+
+ };
+
+ internal IvyTCPStreamV4(Socket socket, IvyProtocolInterface _receiver) : base( socket )
{
- input = new BinaryReader( new BufferedStream(this),Encoding.ASCII);
- output = new BinaryWriter( new BufferedStream(this), Encoding.ASCII);
+
+ input = new BinaryReader(stream, Encoding.ASCII);
+ output = new BinaryWriter(stream, Encoding.ASCII);
+ receiver = _receiver;
}
- /* the protocol magic numbers */
- internal enum MessageType : ushort
- {
- 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 Syntax:
* this is a binary formated message use of network representation
@@ -44,85 +52,205 @@ namespace IvyBus * message Format:
MessageType, id , length, string
*/
- private void sendMsg(MessageType type, int id, byte[] arg)
+ private void Serialize(short arg)
+ {
+ output.Write((ushort)IPAddress.HostToNetworkOrder(arg));
+ }
+ private void Serialize(string arg)
+ {
+ short length = arg != null ? (short)arg.Length : (short)0;
+ Serialize(length);
+ if (length != 0)
+ output.Write(arg.ToCharArray());
+ }
+ private void Serialize(string[] arg)
+ {
+
+ /* serialize count */
+ Serialize((short)arg.Length);
+
+ for (int i = 0; i < arg.Length; i++)
+ {
+ Serialize(arg[i]);
+ }
+ }
+ private void sendMsg(MessageType type, int id, params string[] arg)
{
- try
- {
- 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)
- {
- Console.Error.WriteLine("received an exception: " + ie.Message);
- Console.Error.WriteLine(ie.StackTrace);
- }
+
+ Serialize( (short)type );
+ Serialize( (short)id );
+ Serialize(arg);
+ output.Flush();
+
}
- internal void sendStartRegexp(int port, string appName)
+ public override void TokenStartRegexp(ushort port, string appName)
{
- sendMsg(MessageType.StartRegexp, port, Encoding.ASCII.GetBytes(appName));
+ sendMsg(MessageType.StartRegexp, port, appName);
}
- internal void sendEndRegexp()
+ public override void TokenEndRegexp()
{
- sendMsg(MessageType.EndRegexp, 0, Encoding.ASCII.GetBytes(""));
+ sendMsg(MessageType.EndRegexp, 0, "");
}
- internal void sendApplicationId(int priority, string appId)
+ public override void TokenApplicationId(ushort priority, string appId)
{
- sendMsg(MessageType.ApplicationId, priority, Encoding.ASCII.GetBytes(appId));
+ sendMsg(MessageType.ApplicationId, priority, appId);
}
- internal void sendBinding(Ivy.ApplicationBinding bind)
+ public override void TokenAddRegexp(ushort id, string bind)
{
- sendMsg(bind.type == Ivy.BindingType.BindRegexp ? IvyTCPStreamV4.MessageType.AddRegexp : IvyTCPStreamV4.MessageType.AddBinding, bind.key, Encoding.ASCII.GetBytes(bind.regexp)); /* perhaps we should perform some checking here */
+ sendMsg(MessageType.AddRegexp , id, bind); /* perhaps we should perform some checking here */
+ }
+ public override void TokenAddBinding(ushort id, string bind)
+ {
+ sendMsg(MessageType.AddBinding, id, bind); /* perhaps we should perform some checking here */
}
- internal void delBinding(Ivy.ApplicationBinding bind)
+ public override void TokenDelBinding(ushort id)
{
- sendMsg(bind.type == Ivy.BindingType.BindRegexp ? IvyTCPStreamV4.MessageType.DelRegexp : IvyTCPStreamV4.MessageType.DelBinding, bind.key, Encoding.ASCII.GetBytes(""));
+ sendMsg(MessageType.DelBinding, id, null );
}
- internal void sendDirectMsg(int id, string message)
+ public override void TokenDirectMsg(ushort id, string message)
{
- sendMsg(MessageType.DirectMsg, id, Encoding.ASCII.GetBytes(message));
+ sendMsg(MessageType.DirectMsg, id, message);
}
- internal void sendPong(byte[] s)
+ public override void TokenPong(string s)
{
sendMsg(MessageType.Pong, 0, s);
}
- internal void sendPing(string s)
+ public override void TokenPing(string s)
{
- sendMsg(MessageType.Ping, 0, Encoding.ASCII.GetBytes(s));
+ sendMsg(MessageType.Ping, 0, s);
}
- internal void sendBye(string message)
+ public override void TokenBye(ushort id, string message)
{
- sendMsg(MessageType.Bye, 0, Encoding.ASCII.GetBytes(message));
+ sendMsg(MessageType.Bye, id, message);
}
- internal void sendDie(string message)
+ public override void TokenDie(ushort id, string message)
{
- sendMsg(MessageType.Die, 0, Encoding.ASCII.GetBytes(message));
+ sendMsg(MessageType.Die, id, message);
}
- internal void sendMsg(int key, IvyArgument args)
+ public override void TokenMsg(ushort key, string[] args)
{
- sendMsg(MessageType.Msg, key, args.Serialize() );
+ sendMsg(MessageType.Msg, key, args );
+ }
+
+ public override void TokenError(ushort key, string arg)
+ {
+ sendMsg(MessageType.Msg, key, arg);
+ }
+
+ private short DeserializeShort()
+ {
+ return IPAddress.NetworkToHostOrder((short)input.ReadUInt16());
}
- internal bool receiveMsg(out MessageType type, out int id, out byte[] data)
+ private string DeserializeString()
+ {
+ string arg;
+ int val_len;
+ char[] data;
+ val_len = (ushort)DeserializeShort();
+ if (val_len != 0)
+ {
+ data = input.ReadChars(val_len);
+ arg = new String(data);
+ }
+ else
+ arg = "";
+ return arg;
+ }
+
+
+ private string[] DeserializeArgument()
+ {
+ int nb_children;
+ string[] arg;
+
+ /* Deserialize childrens */
+ nb_children = (ushort)DeserializeShort();
+ /* deserialize Value */
+ arg = new string[nb_children];
+
+ for (int i = 0; i < nb_children; i++)
+ {
+ arg[i] = DeserializeString();
+ }
+ return arg;
+ }
+ internal override bool receiveMsg()
{
- int length = 0;
- data = null;
- id = 0;
- type = MessageType.Die;
+ MessageType msgType = MessageType.Die;
+ ushort msgId = 0;
+ string[] msgData = null;
try
{
- 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 );
+ msgType = (MessageType)(ushort)DeserializeShort();
+ msgId = (ushort)DeserializeShort();
+ msgData = DeserializeArgument();
+
+ switch (msgType)
+ {
+ case MessageType.Die:
+ receiver.TokenDie(msgId, msgData[0]);
+ break;
+
+ case MessageType.Bye:
+ receiver.TokenBye(msgId, msgData[0]);
+ break;
+
+ case MessageType.AddRegexp:
+ receiver.TokenAddRegexp(msgId, msgData[0]);
+ break;
+
+ case MessageType.AddBinding:
+ receiver.TokenAddBinding(msgId, msgData[0]);
+ break;
+
+ case MessageType.DelBinding:
+ receiver.TokenDelBinding(msgId);
+ break;
+
+ case MessageType.EndRegexp:
+ receiver.TokenEndRegexp();
+ break;
+
+ case MessageType.Msg:
+ receiver.TokenMsg( msgId, msgData );
+ break;
+
+ case MessageType.Pong:
+ receiver.TokenPong(msgData[0]);
+ break;
+
+ case MessageType.Ping:
+ receiver.TokenPing(msgData[0]);
+ break;
+
+ case MessageType.Error:
+ receiver.TokenError(msgId, msgData[0]);
+ break;
+
+ case MessageType.StartRegexp:
+ receiver.TokenStartRegexp(msgId, msgData[0]);
+ break;
+
+ case MessageType.DirectMsg:
+ receiver.TokenDirectMsg(msgId, msgData[0]);
+ break;
+ case MessageType.ApplicationId:
+ receiver.TokenApplicationId(msgId, msgData[0]);
+ break;
+ default:
+ throw new IvyException("protocol error, unknown message type " + msgType);
+
+ }
+
+
+
}
catch (EndOfStreamException nfe)
{
|