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/IvyUDPStream.cs | 72 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 21 deletions(-) (limited to 'Ivy/IvyUDPStream.cs') diff --git a/Ivy/IvyUDPStream.cs b/Ivy/IvyUDPStream.cs index 0e3f517..986a550 100644 --- a/Ivy/IvyUDPStream.cs +++ b/Ivy/IvyUDPStream.cs @@ -1,34 +1,34 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Net; -using System.Net.Sockets; -using System.IO; namespace IvyBus { - abstract class IvyUDPStream + using System; + using System.Collections.Generic; + using System.Text; + using System.Net; + using System.Net.Sockets; + using System.IO; + + abstract class IvyUDPStream: IDisposable { - Socket socket; - byte[] buffer; - protected MemoryStream out_stream; protected MemoryStream in_stream; + private Socket socket; + private byte[] buffer; + + private int protocolVersion; - ushort protocol_version; - - public ushort ProtocolVersion + public int ProtocolVersion { - get { return protocol_version; } + get { return protocolVersion; } } - public IvyUDPStream(Socket _socket, ushort protocol) + public IvyUDPStream(Socket _socket, int protocol) { socket = _socket; buffer = new byte[4096]; in_stream = new MemoryStream(buffer); out_stream = new MemoryStream(); - protocol_version = protocol; + protocolVersion = protocol; } internal void Close() { @@ -37,7 +37,7 @@ namespace IvyBus socket.Shutdown(SocketShutdown.Both); socket.Close(); } - internal void receiveMsg(out IPEndPoint remote, out ushort version, out ushort port, out string appId, out string appName) + internal void receiveMsg(out IPEndPoint remote, out int version, out int port, out string appId, out string appName) { int len; IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); @@ -51,7 +51,7 @@ namespace IvyBus //Call Deserialization Deserialize( out version, out port, out appId, out appName ); } - internal void sendMsg(IPEndPoint EPhost, ushort port, string appId, string appName) + internal void sendMsg(IPEndPoint EPhost, int port, string appId, string appName) { // Call Serialisation Serialize(port, appId, appName); @@ -59,8 +59,38 @@ namespace IvyBus byte[] hellob = out_stream.GetBuffer(); socket.SendTo(hellob, (int)out_stream.Length, 0, EPhost); } - abstract internal void Serialize(ushort port, string appId, string appName); - abstract internal void Deserialize(out ushort version, out ushort port, out string appId, out string appName); - + abstract internal void Serialize(int port, string appId, string appName); + abstract internal void Deserialize(out int version, out int port, out string appId, out string appName); + + + #region IDisposable Membres + + //Implement IDisposable. + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + // Free other state (managed objects). + } + // Free your own state (unmanaged objects). + // Set large fields to null. + if (out_stream != null) + { + out_stream.Close(); + out_stream = null; + } + if (in_stream != null) + { + in_stream.Close(); + in_stream = null; + } + } + #endregion } } -- cgit v1.1