Note that there are some explanatory texts on larger screens.

plurals
  1. POPattern for generic communication framework (handling and exposing received data)
    primarykey
    data
    text
    <p>I am currently a good portion of the way through developing a socket based implementation of a generic communication "framework" that works between C# and Android.</p> <p>Building the server and client has been easy enough. Nothing too technically challenging about that.</p> <p>At this point the implementation contains 2 major components.</p> <blockquote> <p>AndroidTcpListener (tcp socket class that handles communication) <Br> AndroidTcpServer (class that creates an instance of the TcpListener and handles the DataReceived (from the TcpListener) when data is received)</p> <p>Data is sent between the client and the server using the standard XML serializer (I may move to JSON at some point but for the time being I have settled on XML)</p> </blockquote> <p>Where I seem to be completely stuck is how to handle and expose the data received.</p> <p>I have tried using generics but no matter how I structure it, it seems that in the end I end up having to create an <code>AndroidTcpServer&lt;T&gt;</code> which then restricts me to a single datatype.</p> <p>I have tried creating a CustomActivator class as such</p> <pre><code>class CustomActivator&lt;T&gt; where T : Serialization.ITcpSerializable&lt;T&gt; { public CustomActivator(String xmlTypeName, Byte[] data) { this.XmlTypeName = xmlTypeName; this.Data = data; } public String XmlTypeName { get; set; } public Byte[] Data { get; set; } public T Activate() { T res = Activator.CreateInstance&lt;T&gt;(); using (MemoryStream stream = new MemoryStream(this.Data)) { return res.Deserialize(XElement.Load(stream)); } } } </code></pre> <p>But again this seems to restrict me to implementing <code>AndroidTcpServer&lt;T&gt;</code></p> <p>I have tried creating an AndroidTcpEvent class which is structured as such</p> <pre><code>public class AndroidTcpEvent&lt;T&gt; { public AndroidTcpEvent() { } public AndroidTcpEvent(String eventName, T data) { this.EventName = eventName; this.Data = data; } public String EventName { get; set; } public T Data { get; set; } } </code></pre> <p>But the XMLSerializer seems to refuse to want to serialize it (I guess I could implement my own serializer but this is far from trivial and doesnt really solve how I will expose the data)</p> <p>I have tried countless other approaches which all seem to hit a dead end somewhere.</p> <p>So basically at this point I have (Inside of the AndroidTcpServer class a method as such</p> <pre><code>private void DataReceived(Object sender, TcpServerMessageReceivedEventArgs e) { //e.Data = Byte[] of XML serialized data of the message sent (Semantics are unimportant) } </code></pre> <p>In conclusion I am hoping for some guidance on how one would typically handle exposing data received and actually creating instances of the data in such a generic framework and if there are any known patterns that can be used for this problem?</p> <p>Obviously I could scrape all this generic implementation stuff and just create a custom implementation for whatever app I end up trying to build but I am doing this as both a learning exercise and something that I would like to release into the public for others to use.</p> <p>Hopefully I have been clear but if anyone requires clarification on any aspect of this please let me know. Thanks in advance for everyones input.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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