Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, I think the responsibilty of the server should end with raising the <code>TcpServerMessageReceived</code> event. It's task is to listen for incoming connections and receive data and then notify anyone who is interested.</p> <p>Then you can attach your custom activator to that event maybe like this:</p> <pre><code>class CustomActivator&lt;T&gt; where T : Serialization.ITcpSerializable&lt;T&gt; { public void ListenTo(AndroidTcpServer server) { server.TcpServerMessageReceived += DataReceived; } private void DataReceived(object sender, TcpServerMessageReceivedEventArgs e) { T res = Activator.CreateInstance&lt;T&gt;(); using (MemoryStream stream = new MemoryStream(e.Data)) { OnObjectReceived(res.Deserialize(XElement.Load(stream)); } } protected void OnObjectReceived(T obj) { var handler = ObjectReceived; if (handler != null) { OnObjectReceived(this, new ObjectReceivedEventArgs(obj)); } } } </code></pre> <p>I'm not sure about the types of objects you send across the wire. If you can determine the type which is contained in the serialized xml before you actually need to deserialize the whole object then you can modify your <code>CustomActivator</code> so that it checks the type and ignores it if it is not its own <code>T</code> (hope that makes sense) and you attach activators for each type you can receive (maybe you can use reflection to attach all types implementing the <code>ISerializable</code> interface automatically).</p> <p><strong>Update</strong>: If you can guess the type from the incoming data you could do something like this:</p> <pre><code>class GenericActivator { public void ListenTo(AndroidTcpServer server) { server.TcpServerMessageReceived += DataReceived; } private void DataReceived(object sender, TcpServerMessageReceivedEventArgs e) { var t = Type.GetType(GuessTypeName(e.Data)); var res = Activator.CreateInstance(t) as typeof(ITcpSerializable&lt;&gt;).MakeGenericType(t); using (MemoryStream stream = new MemoryStream(e.Data)) { OnObjectReceived(res.Deserialize(XElement.Load(stream)); } } protected void OnObjectReceived(object obj) { var handler = ObjectReceived; if (handler != null) { OnObjectReceived(this, new ObjectReceivedEventArgs(obj)); } } } </code></pre> <p>You could also add a type registry to <code>GenericActivator</code> where you can register listeners for each type like this:</p> <pre><code>class GenericActivator { private Dictionary&lt;Type, List&lt;Action&lt;object&gt;&gt; _TypeListeners; public void Register&lt;T&gt;(Action&lt;object&gt; objectReceived) { List&lt;Action&lt;object&gt;&gt; listeners; if (!_TypeListeners.TryGet(typeof(T), out listeners) { listeners = new List&lt;Action&lt;object&gt;&gt;(); } listeners.Add(objectReceived); } } </code></pre> <p>And then only call the listeners for the type received. Makes the assumption that <code>GuessTypeName</code> works reliably.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload