0

I am stuck with the following problem: I am communicating with another application using TcpClients/Streams.

This application needs input parameters passed to it, encoded in a special way (something along first byte: parameter type identifier, next 4 bytes: content length; next length bytes: content).

The application processes the input and gives me the output back in the same format. My problem is that I usually don't know the exact order of different return values. The application could return nothing, one int, 5 ints, 2ints and an IntArray...

EDIT: To clarify this, I can not change the underlying protocol or the source code of the application I am communicating with!

The problem is that I want to be able to use the params as if they were "native" C# objects. What I tried achieving is the following approach:

https://dotnetfiddle.net/ZjbUix

I am not really sure whether this is a good idea. Maybe it would be a better alternative to have a EncodingStream(NetworkStream) with Write() methods for all possible parameter types, and a DecodingStream(NetworkStream) with GetInt(), GetIntArray() etc. methods. This would, however, more or less disregard the type identifier and could only be prevented with throwing an Exception along "Attempted to read an Int value but found Int Array parameter type".

EDIT: I implemented this version here: https://dotnetfiddle.net/8WVXPz

Another requirement is that it should be possible to easily add new parameter types while at best detecting errors at compile-time. Additionally, I'd prefer a non-reflection solution.

Maybe someone can make up an even better architecture to support this?

7
  • This answer seems like what you are after Commented Aug 11, 2015 at 11:17
  • This would still leave me with the problem that I don't know the result parameter's order, thus it might happen that ReadObject<int> attempts to read an int[] array instead. Commented Aug 11, 2015 at 11:21
  • Zahlii, Don't send parameters one by one. Create a , for example, Command class, put your methodname and all parameters in it and send as one object. public class Command { public string Cmd { set; get; } public List<object> parameters { set; get; } } or maybe paramters as key-value pairs public class Command { public string Cmd { set; get; } public Dictionary<string, object> parameters { set; get; } } Commented Aug 11, 2015 at 11:23
  • The simplest method is to send a CSV line where parameters are separated by commas. Use string.Join(",", string[]) Commented Aug 11, 2015 at 12:24
  • @jdweng If one of the parameter is a complex type (like Employee) than csv can be a problem. Commented Aug 11, 2015 at 12:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.