I am developing a client library for a network application protocol.
Client code calls the library to initialize it and connect to the server. The client can, of course, send requests to the server, but the server can also send requests to the client (commands called Cmd below).
The transport protocol is TCP / IP, so basically the client library connects to the server and makes an asynchronization method call to receive the next request or response from the server to avoid blocking I / O while waiting for a response / requests from the server.
At the same time, I see in the library two possible solutions (only using C # constructors and no special third-party structure) so that the client can receive requests from the server:
Or suggest an event in the library, for example
public EventHandler<ReceivedCmdEventArgs> event ReceivedCmd;
which the client would subscribe to receive notification of requests coming from the server. Of course, for this mechanism I will have to make an asynchronous loop in the client library to receive requests from the server and raise the event to Cmd reception.
Or another solution would be to make such a method in the client library
public async Task<Cmd> GetNextCmdAsync()
so that the client code calls in an async loop to get cmds.
Are these solutions the same? Is it better to make full use of async / wait constrcuts C # 5 and no longer rely on events? What are the differences? Any recommendation, note?
Thank!
source
share