From 39841e75b7c3c06f7c263cfffbcfd50dbee761e4 Mon Sep 17 00:00:00 2001 From: Andreas Niedermair Date: Wed, 17 Feb 2016 23:22:34 +0100 Subject: [PATCH] more S, more L, more I, more D --- README.md | 14 +- UDPBroadcast.Example/Program.cs | 15 +- UDPBroadcast.Net40/Broker.cs | 258 ++++++++----------- UDPBroadcast.Net40/IMessage.cs | 18 +- UDPBroadcast.Net40/IMessageBodySerializer.cs | 18 ++ UDPBroadcast.Net40/IMessageFactory.cs | 2 +- UDPBroadcast.Net40/IMessageObserver.cs | 1 + UDPBroadcast.Net40/IMessageSerializer.cs | 19 +- UDPBroadcast.Net40/IPathFactory.cs | 10 + UDPBroadcast.Net40/Message.cs | 60 ----- UDPBroadcast.Net40/MessageBodySerializer.cs | 53 ++++ UDPBroadcast.Net40/MessageObserver.cs | 81 ++++-- UDPBroadcast.Net40/MessageSerializer.cs | 89 +++---- UDPBroadcast.Net40/Partial.Broker.cs | 2 - UDPBroadcast.Net40/PathFactory.cs | 18 ++ UDPBroadcast.Net40/UDPBroadcast.Net40.csproj | 4 + UDPBroadcast.Net45/Partial.Broker.cs | 2 - UDPBroadcast.Net45/UDPBroadcast.Net45.csproj | 12 + UDPBroadcast.Net46/Partial.Broker.cs | 2 - UDPBroadcast.Net46/UDPBroadcast.Net46.csproj | 12 + 20 files changed, 381 insertions(+), 309 deletions(-) create mode 100644 UDPBroadcast.Net40/IMessageBodySerializer.cs create mode 100644 UDPBroadcast.Net40/IPathFactory.cs create mode 100644 UDPBroadcast.Net40/MessageBodySerializer.cs create mode 100644 UDPBroadcast.Net40/PathFactory.cs diff --git a/README.md b/README.md index 3e870a5..2364fb6 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,14 @@ https://www.nuget.org/packages/UDPBroadcast/ } var messageSerializer = new MessageSerializer(); + var messageBodySerializer = new MessageBodySerializer(); var messageFactory = new MessageFactory(); + var pathFactory = new PathFactory(); using (var broker = new Broker(1337, messageSerializer, - messageFactory)) + messageBodySerializer, + messageFactory, + pathFactory)) { var messageObserver = new MessageObserver(broker.ID) { @@ -49,11 +53,15 @@ https://www.nuget.org/packages/UDPBroadcast/ **IMessageFactory** You can inject your own `IMessageFactory` implementation, to send eg `CustomMessage` instances instead to default `Message`. -Please be aware that you have to implement your own serialization-mechanics when creating your own `IMessage` implementation in the `SetInstance` and `GetInstance` methods. - **IMessageSerializer** Instead of using the default `BinaryFormatter` as the serializer of `IMessage` instances, you can provide your own serialization mechanism. +**IMessageBodySerializer** +Instead of using the default `BinaryFormatter` as the serializer of `IMessage.Body`, you can provide your own serialization mechanism. + +**IPathFactory** +If you ever come across the need to adapt routing from the default `Type.FullName` of the encapsulated instance, you can provider your own implementation. + ## License csharp-UDPBroadcast is published under [WTFNMFPLv3](http://andreas.niedermair.name/introducing-wtfnmfplv3). diff --git a/UDPBroadcast.Example/Program.cs b/UDPBroadcast.Example/Program.cs index 3f5eb7a..c3a43c6 100644 --- a/UDPBroadcast.Example/Program.cs +++ b/UDPBroadcast.Example/Program.cs @@ -1,5 +1,9 @@ using System; +// ReSharper disable ArrangeTypeModifiers +// ReSharper disable UnusedParameter.Local +// ReSharper disable ClassNeverInstantiated.Global + namespace UDPBroadcast.Example { class Program @@ -7,11 +11,16 @@ class Program static void Main(string[] args) { var messageSerializer = new MessageSerializer(); + var messageBodySerializer = new MessageBodySerializer(); var messageFactory = new MessageFactory(); + var pathFactory = new PathFactory(); var broker = new Broker(1337, messageSerializer, - messageFactory); - var messageObserver = new MessageObserver(broker.ID) + messageBodySerializer, + messageFactory, + pathFactory); + var messageObserver = new MessageObserver(broker.ID, + messageBodySerializer) { InterceptRemoteMessagesOnly = false, InterceptOnNext = foo => @@ -24,7 +33,7 @@ static void Main(string[] args) broker.Publish(new Foo { - Bar = "hello" + Bar = "hello" // Not L10N }); Console.ReadLine(); diff --git a/UDPBroadcast.Net40/Broker.cs b/UDPBroadcast.Net40/Broker.cs index 6199d9b..b4f02d3 100644 --- a/UDPBroadcast.Net40/Broker.cs +++ b/UDPBroadcast.Net40/Broker.cs @@ -1,15 +1,17 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Sockets; +using System.Runtime.Serialization; using System.Threading; using Anotar.CommonLogging; -// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global -// ReSharper disable UnusedAutoPropertyAccessor.Global -// ReSharper disable MemberCanBePrivate.Global +// ReSharper disable UnusedParameter.Local +// ReSharper disable ExceptionNotDocumentedOptional // ReSharper disable CatchAllClause -// ReSharper disable UnusedMember.Global +// ReSharper disable MemberCanBePrivate.Global +// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global namespace UDPBroadcast { @@ -18,99 +20,56 @@ public partial class Broker : IDisposable private CancellationTokenSource _cancellationTokenSource; private bool _isDisposed; - /// is . - /// is . + /// is . + /// is . + /// is . + /// is . public Broker(int port, IMessageSerializer messageSerializer, - IMessageFactory messageFactory) + IMessageBodySerializer messageBodySerializer, + IMessageFactory messageFactory, + IPathFactory pathFactory) { if (messageSerializer == null) { throw new ArgumentNullException(nameof(messageSerializer)); } + if (messageBodySerializer == null) + { + throw new ArgumentNullException(nameof(messageBodySerializer)); + } if (messageFactory == null) { throw new ArgumentNullException(nameof(messageFactory)); } + if (pathFactory == null) + { + throw new ArgumentNullException(nameof(pathFactory)); + } this.Port = port; - this.ID = Guid.NewGuid(); this.MessageSerializer = messageSerializer; + this.MessageBodySerializer = messageBodySerializer; this.MessageFactory = messageFactory; - this.TypeBasedObservers = new Dictionary>(); - this.PathFactory = type => type.FullName; + this.PathFactory = pathFactory; + + this.ID = Guid.NewGuid(); } public Guid ID { get; } - private IMessageSerializer MessageSerializer { get; } + private IMessageBodySerializer MessageBodySerializer { get; } private IMessageFactory MessageFactory { get; } - public Func PathFactory { get; set; } + private IMessageSerializer MessageSerializer { get; } + private IPathFactory PathFactory { get; } private int Port { get; } - private IDictionary> TypeBasedObservers { get; } + private IDictionary> TypeBasedObservers { get; } = new Dictionary>(); public void Dispose() { this.Dispose(true); - // ReSharper disable ExceptionNotDocumentedOptional GC.SuppressFinalize(this); - // ReSharper restore ExceptionNotDocumentedOptional - } - -#if NET40 || NET46 - /// The token source has been disposed. - /// An aggregate exception containing all the exceptions thrown by the registered callbacks on the associated . -#endif - public void Start() - { - this.Stop(); - - var cancellationTokenSource = new CancellationTokenSource(); - var cancellationToken = cancellationTokenSource.Token; - - this._cancellationTokenSource = cancellationTokenSource; - - this.Start(cancellationToken); - } - - partial void Start(CancellationToken cancellationToken); - -#if NET40 || NET46 - /// This has been disposed. - /// An aggregate exception containing all the exceptions thrown by the registered callbacks on the associated . -#endif - public void Stop() - { - this._cancellationTokenSource?.Cancel(); - } - - ~Broker() - { - this.Dispose(false); } - // ReSharper disable UnusedParameter.Local - private void Dispose(bool disposing) - { - if (this._isDisposed) - { - return; - } - this._isDisposed = true; - - try - { - this._cancellationTokenSource?.Dispose(); - this._cancellationTokenSource = null; - } - catch (Exception exception) - { - LogTo.ErrorException("could not dispose CancellationTokenSource", // Not L10N - exception); - } - } - - // ReSharper restore UnusedParameter.Local - private void Receive(CancellationToken cancellationToken) { var ipEndPoint = new IPEndPoint(IPAddress.Any, @@ -126,8 +85,8 @@ private void Receive(CancellationToken cancellationToken) } catch (SocketException ex) { - LogTo.WarnException("some socket excpetion occured while receiving UDP packages", // Not L10N - ex); + LogTo.ErrorException("some socket excpetion occured while receiving UDP packages", // Not L10N + ex); continue; } catch (ObjectDisposedException) @@ -136,8 +95,8 @@ private void Receive(CancellationToken cancellationToken) } catch (Exception ex) { - LogTo.WarnException("some exception occured during receiving UDP packages", // Not L10N - ex); + LogTo.ErrorException("some exception occured during receiving UDP packages", // Not L10N + ex); continue; } @@ -173,34 +132,15 @@ private void Receive(CancellationToken cancellationToken) foreach (var observer in observers) { - try - { - observer.OnNext(message); - } - catch (Exception exNext) - { - try - { - observer.OnError(exNext); - } - catch (Exception exError) - { - LogTo.ErrorException("failed to invoke {0} for message at {1}", // Not L10N - exError, - nameof(IObserver.OnError), - message.Path); - } - - LogTo.ErrorException("could not handle message for {0}", // Not L10N - exNext, - message.Path); - } + observer.OnNext(message); } } } } - /// A delegate callback throws an exception. + /// is . + /// An error has occurred during serialization. + /// A generic exception occured during the creation of an instance. /// /// An error occurred when accessing the socket. See the Remarks section for more /// information. @@ -210,12 +150,6 @@ private void Receive(CancellationToken cancellationToken) /// .-or- is greater than /// . /// - /// If is null. - /// If is null. - /// is . -#if NET45 || NET46 - /// The array is multidimensional and contains more than elements. -#endif public void Publish(object obj) { if (obj == null) @@ -223,30 +157,26 @@ public void Publish(object obj) throw new ArgumentNullException(nameof(obj)); } - var messageFactory = this.MessageFactory; - if (messageFactory == null) - { - throw new InvalidOperationException($"{nameof(this.MessageFactory)} is null"); - } + var body = this.MessageBodySerializer.Serialize(obj); + var bodyType = obj.GetType(); + var path = this.PathFactory.GetPath(bodyType); - var pathFactory = this.PathFactory; - if (pathFactory == null) + var message = this.MessageFactory.Create(); + if (message == null) { - throw new InvalidOperationException($"{nameof(this.PathFactory)} is null"); + return; } - var bodyType = obj.GetType(); - var path = pathFactory.Invoke(bodyType); - - var message = this.MessageFactory.Create(); - message.SetBrokerID(this.ID); - message.SetInstance(obj); - message.SetPath(path); + message.BrokerID = this.ID; + message.Body = body; + message.Path = path; this.Publish(message); } - /// A delegate callback throws an exception. + /// is . + /// An error has occurred during serialization. + /// A generic error has occurred during serialization. /// /// An error occurred when accessing the socket. See the Remarks section for more /// information. @@ -256,10 +186,6 @@ public void Publish(object obj) /// .-or- is greater than /// . /// - /// is . -#if NET45 || NET46 - /// The array is multidimensional and contains more than elements. -#endif public void Publish(IMessage message) { if (message == null) @@ -268,37 +194,30 @@ public void Publish(IMessage message) } var dgram = this.MessageSerializer.Serialize(message); + if (dgram == null) + { + return; + } using (var udpClient = new UdpClient()) { - // ReSharper disable ExceptionNotDocumentedOptional udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); - // ReSharper restore ExceptionNotDocumentedOptional - // ReSharper disable ExceptionNotDocumentedOptional udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, 1); - // ReSharper restore ExceptionNotDocumentedOptional - // ReSharper disable ExceptionNotDocumentedOptional var ipEndPoint = new IPEndPoint(IPAddress.Broadcast, this.Port); - // ReSharper restore ExceptionNotDocumentedOptional - // ReSharper disable ExceptionNotDocumentedOptional udpClient.Send(dgram, - dgram.Length, + dgram.Count(), ipEndPoint); - // ReSharper restore ExceptionNotDocumentedOptional } } - /// A delegate callback throws an exception. - /// If no observer collection could be created. - /// If is null. - /// is . + /// is . public void Subscribe(IMessageObserver messageObserver) { if (messageObserver == null) @@ -306,21 +225,10 @@ public void Subscribe(IMessageObserver messageObserver) throw new ArgumentNullException(nameof(messageObserver)); } - var pathFactory = this.PathFactory; - if (pathFactory == null) - { - throw new InvalidOperationException($"{nameof(this.PathFactory)} is null"); - } - var bodyType = messageObserver.GetBodyType(); - var path = pathFactory.Invoke(bodyType); - if (path == null) - { - throw new InvalidOperationException($"Path for {bodyType} was null"); - } + var path = this.PathFactory.GetPath(bodyType); ICollection observers; - // ReSharper disable ExceptionNotDocumentedOptional if (!this.TypeBasedObservers.TryGetValue(path, out observers)) { @@ -329,11 +237,59 @@ public void Subscribe(IMessageObserver messageObserver) this.TypeBasedObservers.Add(path, observers); } - // ReSharper restore ExceptionNotDocumentedOptional - // ReSharper disable ExceptionNotDocumentedOptional observers.Add(messageObserver); - // ReSharper restore ExceptionNotDocumentedOptional + } + +#if NET40 || NET46 + /// An aggregate exception containing all the exceptions thrown by the registered callbacks on the associated . +#endif + public void Start() + { + this.Stop(); + + var cancellationTokenSource = new CancellationTokenSource(); + var cancellationToken = cancellationTokenSource.Token; + + this._cancellationTokenSource = cancellationTokenSource; + + this.Start(cancellationToken); + } + + partial void Start(CancellationToken cancellationToken); + +#if NET40 || NET46 + /// An aggregate exception containing all the exceptions thrown by the registered callbacks on the associated . +#endif + public void Stop() + { + this._cancellationTokenSource?.Cancel(); + this._cancellationTokenSource = null; + } + + ~Broker() + { + this.Dispose(false); + } + + private void Dispose(bool disposing) + { + if (this._isDisposed) + { + return; + } + this._isDisposed = true; + + try + { + this._cancellationTokenSource?.Dispose(); + this._cancellationTokenSource = null; + } + catch (Exception exception) + { + LogTo.ErrorException("could not dispose CancellationTokenSource", // Not L10N + exception); + } } } } diff --git a/UDPBroadcast.Net40/IMessage.cs b/UDPBroadcast.Net40/IMessage.cs index 0b0e37e..ec7d2bf 100644 --- a/UDPBroadcast.Net40/IMessage.cs +++ b/UDPBroadcast.Net40/IMessage.cs @@ -1,23 +1,11 @@ using System; -// ReSharper disable UnusedMemberInSuper.Global -// ReSharper disable UnusedMember.Global - namespace UDPBroadcast { public interface IMessage { - byte[] Body { get; } - Guid BrokerID { get; } - string Path { get; } - - /// A generic exception may occur during . - void SetInstance(object obj); - - void SetPath(string path); - void SetBrokerID(Guid brokerID); - - /// A generic exception may occur during . - object GetInstance(); + byte[] Body { get; set; } + Guid BrokerID { get; set; } + string Path { get; set; } } } diff --git a/UDPBroadcast.Net40/IMessageBodySerializer.cs b/UDPBroadcast.Net40/IMessageBodySerializer.cs new file mode 100644 index 0000000..4eb1c6c --- /dev/null +++ b/UDPBroadcast.Net40/IMessageBodySerializer.cs @@ -0,0 +1,18 @@ +using System; +using System.Runtime.Serialization; + +namespace UDPBroadcast +{ + public interface IMessageBodySerializer + { + /// A generic error has occurred during deserialization. + /// is . + /// An error has occurred during deserialization. + object Deserialize(byte[] buffer); + + /// A generic error has occurred during serialization. + /// is . + /// An error has occurred during serialization. + byte[] Serialize(object obj); + } +} diff --git a/UDPBroadcast.Net40/IMessageFactory.cs b/UDPBroadcast.Net40/IMessageFactory.cs index 7017ed8..b4e6d99 100644 --- a/UDPBroadcast.Net40/IMessageFactory.cs +++ b/UDPBroadcast.Net40/IMessageFactory.cs @@ -4,7 +4,7 @@ namespace UDPBroadcast { public interface IMessageFactory { - /// A generic exception may occur during . + /// A generic exception occured during . IMessage Create(); } } diff --git a/UDPBroadcast.Net40/IMessageObserver.cs b/UDPBroadcast.Net40/IMessageObserver.cs index 0fdc580..c2cd203 100644 --- a/UDPBroadcast.Net40/IMessageObserver.cs +++ b/UDPBroadcast.Net40/IMessageObserver.cs @@ -8,6 +8,7 @@ public interface IMessageObserver : IObserver } public interface IMessageObserver : IMessageObserver + where T : class { } } diff --git a/UDPBroadcast.Net40/IMessageSerializer.cs b/UDPBroadcast.Net40/IMessageSerializer.cs index 2f060eb..c3a92a5 100644 --- a/UDPBroadcast.Net40/IMessageSerializer.cs +++ b/UDPBroadcast.Net40/IMessageSerializer.cs @@ -1,13 +1,18 @@ using System; +using System.Runtime.Serialization; namespace UDPBroadcast { - public interface IMessageSerializer - { - /// A generic exception may occur during . - IMessage Deserialize(byte[] buffer); + public interface IMessageSerializer + { + /// A generic error has occurred during deserialization. + /// is . + /// An error has occurred during deserialization. + IMessage Deserialize(byte[] buffer); - /// A generic exception may occur during . - byte[] Serialize(IMessage message); - } + /// A generic error has occurred during serialization. + /// is . + /// An error has occurred during serialization. + byte[] Serialize(IMessage message); + } } diff --git a/UDPBroadcast.Net40/IPathFactory.cs b/UDPBroadcast.Net40/IPathFactory.cs new file mode 100644 index 0000000..8f9a3fc --- /dev/null +++ b/UDPBroadcast.Net40/IPathFactory.cs @@ -0,0 +1,10 @@ +using System; + +namespace UDPBroadcast +{ + public interface IPathFactory + { + /// is . + string GetPath(Type type); + } +} diff --git a/UDPBroadcast.Net40/Message.cs b/UDPBroadcast.Net40/Message.cs index 880e99d..ea02e52 100644 --- a/UDPBroadcast.Net40/Message.cs +++ b/UDPBroadcast.Net40/Message.cs @@ -1,10 +1,4 @@ using System; -using System.IO; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Formatters.Binary; -using System.Security; - -// ReSharper disable MemberCanBePrivate.Global namespace UDPBroadcast { @@ -14,59 +8,5 @@ public class Message : IMessage public string Path { get; set; } public byte[] Body { get; set; } public Guid BrokerID { get; set; } - - /// is . - /// An error has occurred during serialization, such as if an object in the parameter is not marked as serializable. - /// The caller does not have the required permission. - public void SetInstance(object obj) - { - if (obj == null) - { - throw new ArgumentNullException(nameof(obj)); - } - - byte[] body; - - var binaryFormatter = new BinaryFormatter(); - using (var memoryStream = new MemoryStream()) - { - binaryFormatter.Serialize(memoryStream, - obj); - body = memoryStream.ToArray(); - } - - this.Body = body; - } - - public void SetPath(string path) - { - this.Path = path; - } - - public void SetBrokerID(Guid brokerID) - { - this.BrokerID = brokerID; - } - - /// If is null. - /// The supports seeking, but its length is 0. -or-The target type is a , but the value is out of range of the type. - /// The caller does not have the required permission. - public object GetInstance() - { - var body = this.Body; - if (body == null) - { - throw new InvalidOperationException($"{nameof(this.Body)} is null"); - } - - object obj; - using (var memoryStream = new MemoryStream(body)) - { - var binaryFormatter = new BinaryFormatter(); - obj = binaryFormatter.Deserialize(memoryStream); - } - - return obj; - } } } diff --git a/UDPBroadcast.Net40/MessageBodySerializer.cs b/UDPBroadcast.Net40/MessageBodySerializer.cs new file mode 100644 index 0000000..f7e0049 --- /dev/null +++ b/UDPBroadcast.Net40/MessageBodySerializer.cs @@ -0,0 +1,53 @@ +using System; +using System.IO; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; + +// ReSharper disable ExceptionNotThrown +// ReSharper disable ExceptionNotDocumented + +namespace UDPBroadcast +{ + public class MessageBodySerializer : IMessageBodySerializer + { + /// A generic error has occurred during deserialization. + /// is . + /// An error has occurred during deserialization. + public object Deserialize(byte[] buffer) + { + if (buffer == null) + { + throw new ArgumentNullException(nameof(buffer)); + } + + using (var memoryStream = new MemoryStream(buffer)) + { + var binaryFormatter = new BinaryFormatter(); + var obj = binaryFormatter.Deserialize(memoryStream); + + return obj; + } + } + + /// A generic error has occurred during serialization. + /// is . + /// An error has occurred during serialization. + public byte[] Serialize(object obj) + { + if (obj == null) + { + throw new ArgumentNullException(nameof(obj)); + } + + var binaryFormatter = new BinaryFormatter(); + using (var memoryStream = new MemoryStream()) + { + binaryFormatter.Serialize(memoryStream, + obj); + var body = memoryStream.ToArray(); + + return body; + } + } + } +} diff --git a/UDPBroadcast.Net40/MessageObserver.cs b/UDPBroadcast.Net40/MessageObserver.cs index a5c255e..f9fac2f 100644 --- a/UDPBroadcast.Net40/MessageObserver.cs +++ b/UDPBroadcast.Net40/MessageObserver.cs @@ -1,32 +1,37 @@ -using System; +// ReSharper disable RedundantUsingDirective +using System; +using System.Runtime.Remoting.Messaging; +using System.Runtime.Serialization; +using Anotar.CommonLogging; -// ReSharper disable UnusedMember.Global +// ReSharper disable CatchAllClause // ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedAutoPropertyAccessor.Global namespace UDPBroadcast { public class MessageObserver : IMessageObserver + where T : class { - public MessageObserver(Guid brokerID) + public MessageObserver(Guid brokerID, + IMessageBodySerializer messageBodySerializer) { this.BrokerID = brokerID; - this.InterceptRemoteMessagesOnly = true; + this.MessageBodySerializer = messageBodySerializer; } private Guid BrokerID { get; } public Action InterceptCompleted { get; set; } public Action InterceptOnError { get; set; } public Action InterceptOnNext { get; set; } - public bool InterceptRemoteMessagesOnly { get; set; } + public bool InterceptRemoteMessagesOnly { get; set; } = true; + private IMessageBodySerializer MessageBodySerializer { get; } - /// A delegate callback throws an exception. - /// is . public void OnNext(IMessage value) { if (value == null) { - throw new ArgumentNullException(nameof(value)); + return; } if (this.InterceptRemoteMessagesOnly) @@ -37,22 +42,68 @@ public void OnNext(IMessage value) } } - var obj = value.GetInstance(); - var instance = (T) obj; + object obj; + try + { + obj = this.MessageBodySerializer.Deserialize(value.Body); + } + catch (SerializationException serializationException) + { + LogTo.ErrorException($"could not deserialize the {nameof(IMessage.Body)} of {nameof(value)}", + serializationException); + return; + } + catch (ArgumentNullException argumentNullException) + { + LogTo.ErrorException($"the {nameof(IMessage.Body)} property of {nameof(value)} was null", + argumentNullException); + return; + } + catch (Exception exception) + { + LogTo.ErrorException($"a generic error occurred during {nameof(this.OnNext)}", + exception); + return; + } + var instance = obj as T; - this.InterceptOnNext?.Invoke(instance); + try + { + this.InterceptOnNext?.Invoke(instance); + } + catch (Exception exception) + { + LogTo.ErrorException($"error during calling {nameof(this.InterceptOnNext)}", + exception); + + this.OnError(exception); + } } - /// A delegate callback throws an exception. public void OnError(Exception error) { - this.InterceptOnError?.Invoke(error); + try + { + this.InterceptOnError?.Invoke(error); + } + catch (Exception exception) + { + LogTo.ErrorException($"exception during calling {nameof(this.InterceptOnError)}", + exception); + } } - /// A delegate callback throws an exception. public void OnCompleted() { - this.InterceptCompleted?.Invoke(); + try + { + this.InterceptCompleted?.Invoke(); + } + catch (Exception exception) + { + LogTo.ErrorException($"exception during calling {nameof(this.InterceptCompleted)}", + exception); + } } public Type GetBodyType() diff --git a/UDPBroadcast.Net40/MessageSerializer.cs b/UDPBroadcast.Net40/MessageSerializer.cs index 632a1e6..0f5754c 100644 --- a/UDPBroadcast.Net40/MessageSerializer.cs +++ b/UDPBroadcast.Net40/MessageSerializer.cs @@ -2,63 +2,56 @@ using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; -using System.Security; -// ReSharper disable UnusedMember.Global +// ReSharper disable ExceptionNotThrown +// ReSharper disable ExceptionNotDocumented namespace UDPBroadcast { - public class MessageSerializer : IMessageSerializer + public class MessageSerializer : IMessageSerializer + { + /// A generic error has occurred during deserialization. + /// is . + /// An error has occurred during deserialization. + public IMessage Deserialize(byte[] buffer) { - /// is . - /// - /// The supports seeking, but its length is - /// 0. -or-The target type is a , but the value is out of range of the - /// type. - /// - /// The caller does not have the required permission. - public IMessage Deserialize(byte[] buffer) - { - if (buffer == null) - { - throw new ArgumentNullException(nameof(buffer)); - } - - using (var memoryStream = new MemoryStream(buffer)) - { - var binaryFormatter = new BinaryFormatter(); - var obj = binaryFormatter.Deserialize(memoryStream); - var message = (IMessage) obj; - - return message; - } - } + if (buffer == null) + { + throw new ArgumentNullException(nameof(buffer)); + } + + using (var memoryStream = new MemoryStream(buffer)) + { + var binaryFormatter = new BinaryFormatter(); + var obj = binaryFormatter.Deserialize(memoryStream); + var message = obj as IMessage; + + return message; + } + } - /// is . - /// - /// An error has occurred during serialization, such as if an object in the - /// parameter is not marked as serializable. - /// - /// The caller does not have the required permission. - public byte[] Serialize(IMessage message) - { - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } + /// A generic error has occurred during serialization. + /// is . + /// An error has occurred during serialization. + public byte[] Serialize(IMessage message) + { + if (message == null) + { + throw new ArgumentNullException(nameof(message)); + } - byte[] buffer; + byte[] buffer; - var binaryFormatter = new BinaryFormatter(); - using (var memoryStream = new MemoryStream()) - { - binaryFormatter.Serialize(memoryStream, - message); + var binaryFormatter = new BinaryFormatter(); + using (var memoryStream = new MemoryStream()) + { + binaryFormatter.Serialize(memoryStream, + message); - buffer = memoryStream.ToArray(); - } + buffer = memoryStream.ToArray(); + } - return buffer; - } + return buffer; } + } } diff --git a/UDPBroadcast.Net40/Partial.Broker.cs b/UDPBroadcast.Net40/Partial.Broker.cs index f574acb..ed2151f 100644 --- a/UDPBroadcast.Net40/Partial.Broker.cs +++ b/UDPBroadcast.Net40/Partial.Broker.cs @@ -1,8 +1,6 @@ using System.Threading; using System.Threading.Tasks; -// ReSharper disable UnusedMember.Global - namespace UDPBroadcast { public partial class Broker diff --git a/UDPBroadcast.Net40/PathFactory.cs b/UDPBroadcast.Net40/PathFactory.cs new file mode 100644 index 0000000..f063ba8 --- /dev/null +++ b/UDPBroadcast.Net40/PathFactory.cs @@ -0,0 +1,18 @@ +using System; + +namespace UDPBroadcast +{ + public class PathFactory : IPathFactory + { + /// is . + public string GetPath(Type type) + { + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + + return type.FullName; + } + } +} diff --git a/UDPBroadcast.Net40/UDPBroadcast.Net40.csproj b/UDPBroadcast.Net40/UDPBroadcast.Net40.csproj index 116c90d..661b352 100644 --- a/UDPBroadcast.Net40/UDPBroadcast.Net40.csproj +++ b/UDPBroadcast.Net40/UDPBroadcast.Net40.csproj @@ -80,13 +80,17 @@ + + + + diff --git a/UDPBroadcast.Net45/Partial.Broker.cs b/UDPBroadcast.Net45/Partial.Broker.cs index 73a0536..99c8a3f 100644 --- a/UDPBroadcast.Net45/Partial.Broker.cs +++ b/UDPBroadcast.Net45/Partial.Broker.cs @@ -1,8 +1,6 @@ using System.Threading; using System.Threading.Tasks; -// ReSharper disable UnusedMember.Global - namespace UDPBroadcast { public partial class Broker diff --git a/UDPBroadcast.Net45/UDPBroadcast.Net45.csproj b/UDPBroadcast.Net45/UDPBroadcast.Net45.csproj index 11b89b6..805da7e 100644 --- a/UDPBroadcast.Net45/UDPBroadcast.Net45.csproj +++ b/UDPBroadcast.Net45/UDPBroadcast.Net45.csproj @@ -60,6 +60,9 @@ IMessage.cs + + IMessageBodySerializer.cs + IMessageFactory.cs @@ -69,9 +72,15 @@ IMessageSerializer.cs + + IPathFactory.cs + Message.cs + + MessageBodySerializer.cs + MessageFactory.cs @@ -81,6 +90,9 @@ MessageSerializer.cs + + PathFactory.cs + diff --git a/UDPBroadcast.Net46/Partial.Broker.cs b/UDPBroadcast.Net46/Partial.Broker.cs index 73a0536..99c8a3f 100644 --- a/UDPBroadcast.Net46/Partial.Broker.cs +++ b/UDPBroadcast.Net46/Partial.Broker.cs @@ -1,8 +1,6 @@ using System.Threading; using System.Threading.Tasks; -// ReSharper disable UnusedMember.Global - namespace UDPBroadcast { public partial class Broker diff --git a/UDPBroadcast.Net46/UDPBroadcast.Net46.csproj b/UDPBroadcast.Net46/UDPBroadcast.Net46.csproj index 2f01188..d7da7a9 100644 --- a/UDPBroadcast.Net46/UDPBroadcast.Net46.csproj +++ b/UDPBroadcast.Net46/UDPBroadcast.Net46.csproj @@ -60,6 +60,9 @@ IMessage.cs + + IMessageBodySerializer.cs + IMessageFactory.cs @@ -69,9 +72,15 @@ IMessageSerializer.cs + + IPathFactory.cs + Message.cs + + MessageBodySerializer.cs + MessageFactory.cs @@ -81,6 +90,9 @@ MessageSerializer.cs + + PathFactory.cs +