using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Collections; using System.Collections.Specialized; using System.IO; namespace IvyBus { /// /// Description résumée de IvyStream. /// internal class IvyTCPStreamV4 : IvyTCPStream { BinaryReader input; BinaryWriter output; IvyProtocol 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, IvyProtocol _receiver) : base( socket ) { input = new BinaryReader(stream, Encoding.ASCII); output = new BinaryWriter(stream, Encoding.ASCII); receiver = _receiver; } /* * message Syntax: * this is a binary formated message use of network representation * * message Format: MessageType, id , length, string */ 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) { Serialize( (short)type ); Serialize( (short)id ); Serialize(arg); output.Flush(); } override internal void TokenStartRegexp(ushort port, string appName) { sendMsg(MessageType.StartRegexp, port, appName); } override internal void TokenEndRegexp() { sendMsg(MessageType.EndRegexp, 0, ""); } override internal void TokenApplicationId(ushort priority, string appId) { sendMsg(MessageType.ApplicationId, priority, appId); } override internal void TokenAddBinding(Ivy.ApplicationBinding bind) { if (bind.type == Ivy.BindingType.BindRegexp) { sendMsg(MessageType.AddRegexp, bind.key, bind.expression); /* perhaps we should perform some checking here */ } else { sendMsg(MessageType.AddBinding, bind.key, bind.expression); /* perhaps we should perform some checking here */ } } override internal void TokenDelBinding(ushort id) { sendMsg(MessageType.DelBinding, id, null ); } override internal void TokenDirectMsg(ushort id, string message) { sendMsg(MessageType.DirectMsg, id, message); } override internal void TokenPong(string s) { sendMsg(MessageType.Pong, 0, s); } override internal void TokenPing(string s) { sendMsg(MessageType.Ping, 0, s); } override internal void TokenBye(ushort id, string message) { sendMsg(MessageType.Bye, id, message); } override internal void TokenDie(ushort id, string message) { sendMsg(MessageType.Die, id, message); } override internal void TokenMsg(ushort key, string[] args) { sendMsg(MessageType.Msg, key, args ); } override internal void TokenError(ushort key, string arg) { sendMsg(MessageType.Msg, key, arg); } private short DeserializeShort() { return IPAddress.NetworkToHostOrder((short)input.ReadUInt16()); } 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; } override internal bool receiveMsg() { Ivy.ApplicationBinding bind = new Ivy.ApplicationBinding(); MessageType msgType = MessageType.Die; ushort msgId = 0; string[] msgData = null; try { 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: bind.type = Ivy.BindingType.BindRegexp; bind.key = msgId; bind.expression = msgData[0]; receiver.TokenAddBinding(bind); break; case MessageType.AddBinding: bind.type = Ivy.BindingType.BindSimple; bind.key = msgId; bind.expression = msgData[0]; receiver.TokenAddBinding(bind); 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) { return false; } catch (FormatException) { throw new IvyException("protocol error on msgType"); } return true; } } }