0

I am designing a webservice interface for use between a Windows CE device and a PC. The Windows CE device is server and the PC is client.

I have decided to use the gSOAP library to implement the server and I am using .NET/C# for the client. I have followed the approach described here and everything is working well.

My question is about how to best implement an asynchronous callback/event from the server to the client. I can think of two methods:

  1. Continuously polling the server for active events
  2. A blocking method that keeps the connection open until an event occurs

I have currently chosen option 2 and it seems to be working well. I use an asynchronous method in the client and therefore get a callback when the method completes, i.e. when an event occurs on the Windows CE device. I then immediately call the same method again so it is ready for the next event.

Example server method (no error handling):

int ns__WaitForEvent(struct soap* soap, int *eventId)
{
    WaitForSingleObject(hMyServerEvent, INFINITE);
    *eventId = GetCurrentEventId();
    return SOAP_OK;
}

Example client (no error handling):

private void SubscribeToServerEvents()
{
    var server = new MyMethods.ServicePortTypeClient(
                        new BasicHttpBinding(), 
                        new EndpointAddress(myIpAddress));
    AsyncCallback cb = this.Callback;
    server.BeginWaitForEvent(cb, server);
}

private void Callback(IAsyncResult ar)
{
    var server = (MyMethods.ServicePortType)ar.AsyncState;
    var result = server.EndWaitForEvent(ar);
    // Do stuff with result
}

The server must be multi-threaded for this approach to work, and the number of clients should be limited so the server does not have a large number of threads hanging with blocking methods. In my case none of these issues are a problem - it is simple to setup a multi-threaded server using gSOAP and there will only ever be one client (which I control) attached to each server.

Are there any significant disadvantages to this approach? Can you suggest a better solution?

Allan Larsen
  • 476
  • 4
  • 10
  • The only disadvantage I can think of is that you may lose some events if they follow directly one after another. If you don't care about this then it should be fine. – Sergey Kudriavtsev Oct 05 '11 at 09:14
  • I used an event in the example for simplicity, but in my implementation I will use WaitForSingleObject with a message queue handle instead, and then return the event at the front of the queue. This should prevent lost events. – Allan Larsen Oct 05 '11 at 13:42
  • 1
    Then I think you shouldn't have any serious problems. – Sergey Kudriavtsev Oct 05 '11 at 14:07

1 Answers1

0

I suggest to turn the WinCE device into a webclient instead of a webserver and the PC into a server, that will be notified on something happens on the client. It is more natural this approach, you can still use gSoap for a soap client. On the PC you should have a web-server like Apache or IIS installed, or you could make a Windows server that will host an embedded light webserver.

garzanti
  • 2,162
  • 1
  • 22
  • 30
  • I don't think that is not going to work for my setup. The device is a data acquisition unit which is controlled by an application on the PC, and most of the communication will be initiated by the PC application. – Allan Larsen Oct 07 '11 at 15:07
  • ok, but I still believe that is not right to have a blocking method to keep only a connection open. How Gmail/Yahoo webmail client works to keep you informed that a new email has arrived? Some kind of polling through AJAX. – garzanti Oct 07 '11 at 18:09
  • That is also what is bothering me, which is why I asked the question. :-) I think the mobile push notifications (Android, iOS, etc.) are implemented by constantly keeping an socket open to the server, but they probably use a smarter approach than a blocking method... – Allan Larsen Oct 07 '11 at 20:28
  • The connection is not closed immediately it has a time to live which can configured (of course it can be closed immediately after each call but is not productive), so you don't have to worried so much about this. The mail webclients have an adaptive polling, they check every 15 seconds let's say for a new email, if the connection is broken has no sense to consume CPU, it will reask in 20 seconds and so on. The PUSH mail notifications it's something else related also with the phone operator I guess. – garzanti Oct 08 '11 at 05:35