我正在使用 <我的 Silverlight 应用程序中的 code>Socket 用于将数据从服务器流式传输到客户端。
但是,我不太确定 Silverlight 中如何处理超时 套接字
。
在文档中,我看不到类似 Silverlight 的接收超时
。
- 用户可以定义超时吗?我该如何设置它们?当发送/接收操作超时时如何收到通知?
- 有默认超时吗?它们有多大?
- 如果没有超时:手动实现这些超时的最简单方法是什么?
I'm using Socket
s 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?
发布评论
评论(3)
我检查了 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.
由于我找不到任何好的解决方案,因此我通过创建一个 System.Threading.Timer 手动解决了该问题,其代码类似于以下内容:
这还可以处理由简单的操作产生的超时发生的情况滞后,如果操作花费太多时间,则让回调立即返回。
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: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.
我为我的项目 sharpLightFtp 解决了这个问题,例如:
创建了一个 类 注入
UserToken
- System.Net.Sockets.SocketAsyncEventArgs< /a> 并具有 系统.Threading.AutoResetEvent,用于在ConnectAsync
,ReceiveAsync
和SendAsync
超时 (就像这里:第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 afterConnectAsync
,ReceiveAsync
andSendAsync
with a timeout (like here: line 22 for getting a custom enhancedSocketAsyncEventArgs
-instance, line 270 for creating and enhancing theSocketEventArgs
-instance, line 286 for sending the signal and line 30 for waiting)