From 8d10e8bbd1e19adc7c70e1101dbb69c213c910dd Mon Sep 17 00:00:00 2001 From: fcolin Date: Fri, 22 Aug 2008 16:44:01 +0000 Subject: optimisation for parsing same regular expression from multiple client using fxCop for code beauty fix bug on concurrent connect --- Ivy/IvyTCPStreamV4.cs | 107 ++++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 52 deletions(-) (limited to 'Ivy/IvyTCPStreamV4.cs') diff --git a/Ivy/IvyTCPStreamV4.cs b/Ivy/IvyTCPStreamV4.cs index d477908..2b1640a 100644 --- a/Ivy/IvyTCPStreamV4.cs +++ b/Ivy/IvyTCPStreamV4.cs @@ -1,17 +1,20 @@ -using System; -using System.Net; -using System.Net.Sockets; -using System.Text; -using System.Collections; -using System.Collections.Specialized; -using System.IO; + namespace IvyBus { - /// + using System; + using System.Net; + using System.Net.Sockets; + using System.Text; + using System.Collections; + using System.Collections.Specialized; + using System.IO; + using IvyBus.Properties; + + /// /// Description résumée de IvyStream. /// - internal class IvyTCPStreamV4 : NetworkStream , IvyProtocol + internal class IvyTCPStreamV4 : NetworkStream, IvyProtocol { BinaryReader input; BinaryWriter output; @@ -31,19 +34,24 @@ namespace IvyBus 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 */ + AddBinding = 11, /* other methods for binding message based on hash table */ }; internal IvyTCPStreamV4(Socket socket, IvyProtocol _receiver) : base( socket ) { - input = new BinaryReader(this, Ivy.ivyEncoding ); - output = new BinaryWriter(this, Ivy.ivyEncoding ); - receiver = _receiver; + this.input = new BinaryReader(this, Ivy.ivyEncoding); + this.output = new BinaryWriter(this, Ivy.ivyEncoding); + this.receiver = _receiver; } + internal void Close() + { + this.input.Close(); + this.output.Close(); + base.Close(); + } /* * message Syntax: @@ -52,9 +60,9 @@ namespace IvyBus * message Format: MessageType, id , length, string */ - private void Serialize(short arg) + private void Serialize(int arg) { - output.Write((ushort)IPAddress.HostToNetworkOrder(arg)); + output.Write((short)IPAddress.HostToNetworkOrder(arg)); } private void Serialize(string arg) { @@ -67,7 +75,7 @@ namespace IvyBus { /* serialize count */ - Serialize((short)arg.Length); + Serialize(arg.Length); for (int i = 0; i < arg.Length; i++) { @@ -76,31 +84,29 @@ namespace IvyBus } private void sendMsg(MessageType type, int id, params string[] arg) { - - Serialize( (short)type ); - Serialize( (short)id ); - Serialize(arg); - output.Flush(); - + lock (this.output) + { + Serialize((int)type); + Serialize(id); + Serialize(arg); + output.Flush(); + } } - void IvyProtocol.TokenStartRegexp(ushort port, string appName) + void IvyProtocol.TokenStartRegexp(int port, string appName) { sendMsg(MessageType.StartRegexp, port, appName); } void IvyProtocol.TokenEndRegexp() { - sendMsg(MessageType.EndRegexp, 0, ""); - } - void IvyProtocol.TokenApplicationId(ushort priority, string appId) - { - sendMsg(MessageType.ApplicationId, priority, appId); + sendMsg(MessageType.EndRegexp, 0, string.Empty); } + - void IvyProtocol.TokenAddBinding(BindingType type, ushort id, string expression) + void IvyProtocol.TokenAddBinding(BindingType type, int id, string expression) { switch (type) { - case BindingType.Regexp: + case BindingType.RegularExpression: sendMsg(MessageType.AddRegexp, id, expression); /* perhaps we should perform some checking here */ break; case BindingType.Simple: @@ -109,12 +115,12 @@ namespace IvyBus } } - void IvyProtocol.TokenDelBinding(ushort id) + void IvyProtocol.TokenDelBinding(int id) { sendMsg(MessageType.DelBinding, id, null ); } - void IvyProtocol.TokenDirectMsg(ushort id, string message) + void IvyProtocol.TokenDirectMsg(int id, string message) { sendMsg(MessageType.DirectMsg, id, message); } @@ -127,43 +133,43 @@ namespace IvyBus sendMsg(MessageType.Ping, 0, s); } - void IvyProtocol.TokenBye(ushort id, string message) + void IvyProtocol.TokenBye(int id, string message) { sendMsg(MessageType.Bye, id, message); } - void IvyProtocol.TokenDie(ushort id, string message) + void IvyProtocol.TokenDie(int id, string message) { sendMsg(MessageType.Die, id, message); } - void IvyProtocol.TokenMsg(ushort key, string[] args) + void IvyProtocol.TokenMsg(int key, string[] args) { sendMsg(MessageType.Msg, key, args ); } - void IvyProtocol.TokenError(ushort key, string arg) + void IvyProtocol.TokenError(int key, string arg) { sendMsg(MessageType.Msg, key, arg); } - private short DeserializeShort() + private int DeserializeInt() { - return IPAddress.NetworkToHostOrder((short)input.ReadUInt16()); + return IPAddress.NetworkToHostOrder((short)input.ReadInt16()); } private string DeserializeString() { string arg; int val_len; char[] data; - val_len = (ushort)DeserializeShort(); + val_len = DeserializeInt(); if (val_len != 0) { data = input.ReadChars(val_len); arg = new String(data); } else - arg = ""; + arg = string.Empty; return arg; } @@ -174,7 +180,7 @@ namespace IvyBus string[] arg; /* Deserialize childrens */ - nb_children = (ushort)DeserializeShort(); + nb_children = DeserializeInt(); /* deserialize Value */ arg = new string[nb_children]; @@ -184,16 +190,16 @@ namespace IvyBus } return arg; } - bool IvyProtocol.receiveMsg() + bool IvyProtocol.ReceiveMsg() { MessageType msgType = MessageType.Die; - ushort msgId = 0; + int msgId = 0; string[] msgData = null; try { - msgType = (MessageType)(ushort)DeserializeShort(); - msgId = (ushort)DeserializeShort(); + msgType = (MessageType)DeserializeInt(); + msgId = DeserializeInt(); msgData = DeserializeArgument(); switch (msgType) @@ -207,7 +213,7 @@ namespace IvyBus break; case MessageType.AddRegexp: - receiver.TokenAddBinding(BindingType.Regexp, msgId, msgData[0]); + receiver.TokenAddBinding(BindingType.RegularExpression, msgId, msgData[0]); break; case MessageType.AddBinding: @@ -245,11 +251,8 @@ namespace IvyBus 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); + throw new IvyException(Resources.UnknownMessage + msgType); } @@ -262,7 +265,7 @@ namespace IvyBus } catch (FormatException) { - throw new IvyException("protocol error on msgType"); + throw new IvyException(Resources.ProtocolError); } -- cgit v1.1