来自 gSOAP 服务器的异步回调/事件的最佳方法是什么?

发布于 2024-12-08 09:54:56 字数 1436 浏览 1 评论 0原文

我正在设计一个用于 Windows CE 设备和 PC 之间的 Web 服务接口。 Windows CE 设备是服务器,PC 是客户端。

我决定使用 gSOAP 库来实现服务器,并使用 .NET/C# 作为客户端。我遵循了此处 一切正常。

我的问题是关于如何最好地实现从服务器到客户端的异步回调/事件。我可以想到两种方法:

  1. 连续轮询服务器以获取活动事件
  2. 一种阻塞方法,使连接保持打开状态,直到事件发生

我当前选择了选项 2,它似乎运行良好。我在客户端中使用异步方法,因此当该方法完成时(即 Windows CE 设备上发生事件时)会收到回调。然后,我立即再次调用相同的方法,以便为下一个事件做好准备。

服务器方法示例(无错误处理):

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

客户端示例(无错误处理):

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
}

服务器必须是多线程才能使此方法工作,并且应限制客户端的数量,以便服务器不会有大量线程挂起与阻塞方法。就我而言,这些问题都不是问题 - 使用 gSOAP 设置多线程服务器很简单,并且每台服务器只会连接一个客户端(我控制)。

这种方法有什么明显的缺点吗?您能提出更好的解决方案吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

時窥 2024-12-15 09:54:56

我建议将 WinCE 设备变成 Web 客户端而不是 Web 服务器,将 PC 变成服务器,客户端发生任何情况都会收到通知。这种方法更自然,您仍然可以使用 gSoap 作为肥皂客户端。在 PC 上,您应该安装一个 Web 服务器,例如 Apache 或 IIS,或者您可以创建一个 Windows 服务器来托管嵌入式轻型 Web 服务器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文