Tinkerforge/generators — pull request #3 by NobodysNightmare (2012-04-30 09:27:34+00:00)
Title: Device-Locking in C# will break on Timeout

What is the problem?
-------------------------------
The current approach to ensure that no message is sent to the device while waiting for an answer involves many classes:
- the IPConnection aquires the device (and releases it in case of no return-value)
- the device-subclass sending the request will release its lock if there was a return-value

This breaks encapsulation between device, its sub-classes and the IPConnection. and most important: the current approach will not release the device if there was an answer expected and the device timed out (even when this was only a temporary outage).

What does this Pull Request contain?
---------------------------------------------------------
- a simplified "dump" version of the IPConnection.write() method that will only do communication
- two methods on Device doing the former job of IPConnection.write():
- sendOneWayMessage will lock the device while sending a message via the IPConnection, not waiting for an answer
- sendReturningMessage will lock the device while sending and waiting for the answer (and will even unlock, if there was a timeout)
- adapted generator-code that uses the two new methods instead of the IPConnection.write()-method

The locking is now realized with the lock-keyword provided by .NET, as the AutoResetEvent was only neccessary due to the spreaded aquire-release-logic.

Caveats?
-------------
While I am still waiting for my own hardware, I could only verify the code by review and compiling. This means I did not test it.

Hope you like it. If there are any further questions regarding this request feel free to ask :)

Best Regards
Jan