Hi, today I would like to share with you idea of creating common communication components. I did it many times and I have even prepared my own presentation with many examples of such kind of problem solution. After some time of experiments I created few design diagrams that implement communication in minimal and advanced way the same time. So, let me show you what is the design on common communicator that you can create for your own solution.
As you can may see, there is only two responsibilities for common communicator. There are creating clients and creating servers. And there need to be able to that from provided TDataContract generic type. Now I want to show you design of typical TDataContract.
As you can see there are two main kind of responsibilities for TDataContract. First is begin and end session, so the session management in two methods, BeginSession and EndSession. The second kind of responsibilities is dealing with messages with two methods, Consume and Publish. We can stop for a moment here, because I want to share with you extremely simple and powerful idea in the same time. Do you see what is the type of messages? It is not a T it is an array of T. And whenever you are designing common communication components please keep in mind that using T is much better then using T in your contract. First of all because you will be able to operate with 3 different type of messages, no message at all is by using null, when everything is fine but you have no data you can send empty array as a T, and of course you may decide on server side that you send T, or T or even T in message. The secret I have to share with you here is that using 100 times T and using one time T have completely different performance in communication. You can guess, which one is faster? :). So that was server site.
Now we can design client site as a consumer of the communication. The design itself it is also very clearly and it looks like it is shown below.
The client/consumer start working with StartWorking method and stop working with StopWorking method. And between those two stages Notified action is invoked. For the dynamic of the client/consumer you can use following design.
As you can see start and stop of working can be also exposed as begin and end session with particular key. And between those two stages consumer just consume messages with Notify events for client code.
As you can see the way of design is very easy. It uses Long Pool pattern. And I want to share with you competition picture of classic implementation with callbacks and this implementation with long pool on server site.
The most important different and benefit of using long pool is the way of re-establish of the connection. As you can see in Long Pool patter it is a lot easier.
At the end of this entry as usually I have a present for you. There is whole implementation of presented patterns and solutions with more than just library. There are also examples of use and performance tests of channels in WCF, Apache Thrift and with Protobuff serialization library as well. So, here is the content you can download.
On the left panel you can see implementation of common communicator component with different channels and on the right panel you can see bunch of examples for different channels. I decided to give you all my examples that I created for one conference, unfortunately I could not came, so it is all yours you need only build examples by yourself. If you want to change something inside let me know, because I am wondering what do you think about this solution. And here it is CommonCommunicatorWithExamples (2117 downloads). Enjoy!