Silverlight 套接字中的超时

发布于 2024-11-04 18:06:00 字数 640 浏览 4 评论 0 原文

我正在使用 <我的 Silverlight 应用程序中的 code>Socket 用于将数据从服务器流式传输到客户端。

但是,我不太确定 Silverlight 中如何处理超时 套接字
在文档中,我看不到类似 Silverlight 的接收超时

  • 用户可以定义超时吗?我该如何设置它们?当发送/接收操作超时时如何收到通知?
  • 有默认超时吗?它们有多大?
  • 如果没有超时:手动实现这些超时的最简单方法是什么?

I'm using Sockets in my Silverlight application to stream data from a server to a client.

However, I'm not quite sure how timeouts are handled in a Silverlight Socket.
In the documentation, I cannot see anything like ReceiveTimeout for Silverlight.

  • Are user-defined timeouts possible? How can I set them? How can I get notifications when a send / receive operation times out?
  • Are there default timeouts? How big are they?
  • If there are no timeouts: what's the easiest method to implement these timeouts manually?

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

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

发布评论

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

评论(3

海之角 2024-11-11 18:06:00

我检查了 Reflector 中的 Socket 类,除了 Dispose 方法之外,没有一个相关的 setsockopt 调用来处理超时。看起来 Silverlight 只是依赖于 WinSock API 的默认超时。

Socket 类还包含一个“SetSocketOption”方法,该方法是私有的,您可以通过反射调用该方法 - 尽管您很可能会遇到安全异常。

I've checked the Socket class in Reflector and there's not a single relevant setsockopt call that deals with timeouts - except in the Dispose method. Looks like Silverlight simply relies on the default timeout of the WinSock API.

The Socket class also contains a "SetSocketOption" method which is private that you might be able to call via reflection - though it is very likely that you will run into a security exception.

单身狗的梦 2024-11-11 18:06:00

由于我找不到任何好的解决方案,因此我通过创建一个 System.Threading.Timer 手动解决了该问题,其代码类似于以下内容:

System.Threading.Timer t;
bool timeout;

[...]

// Initialization
t = new Timer((s) => {
    lock (this) {
        timeout = true;
        Disconnected();
    }
});

[...]

// Before each asynchronous socket operation
t.Change(10000, System.Threading.Timeout.Infinite);

[...]

// In the callback of the asynchronous socket operations
lock (this) {
    t.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
    if (!timeout) {
        // Perform work
    }
}

这还可以处理由简单的操作产生的超时发生的情况滞后,如果操作花费太多时间,则让回调立即返回。

Since I couldn't find any nice solution, I solved the problem manually by creating a System.Threading.Timer with code similar to the following:

System.Threading.Timer t;
bool timeout;

[...]

// Initialization
t = new Timer((s) => {
    lock (this) {
        timeout = true;
        Disconnected();
    }
});

[...]

// Before each asynchronous socket operation
t.Change(10000, System.Threading.Timeout.Infinite);

[...]

// In the callback of the asynchronous socket operations
lock (this) {
    t.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
    if (!timeout) {
        // Perform work
    }
}

This handles also cases where a timeout occurs which is produced by simple lag, and lets the callback return immediately if the operation took too much time.

月朦胧 2024-11-11 18:06:00

我为我的项目 sharpLightFtp 解决了这个问题,例如:

创建了一个 注入 UserToken - System.Net.Sockets.SocketAsyncEventArgs< /a> 并具有 系统.Threading.AutoResetEvent,用于在ConnectAsyncReceiveAsyncSendAsync 超时 (就像这里:第22行用于获取自定义增强< code>SocketAsyncEventArgs-实例,第 270 行用于创建和增强 SocketEventArgs-实例,第 286 行用于发送信号,第 30 行用于等待)

I solved this issue for my project sharpLightFtp like:

Created a class which is injected in the UserToken-property of an instance of System.Net.Sockets.SocketAsyncEventArgs and has an System.Threading.AutoResetEvent, which is used to receive a signal after ConnectAsync, ReceiveAsync and SendAsync with a timeout (like here: line 22 for getting a custom enhanced SocketAsyncEventArgs-instance, line 270 for creating and enhancing the SocketEventArgs-instance, line 286 for sending the signal and line 30 for waiting)

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