Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeouts in Silverlight Sockets

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?
like image 990
Etan Avatar asked Apr 29 '11 10:04

Etan


2 Answers

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.

like image 59
Oliver Weichhold Avatar answered Nov 17 '22 07:11

Oliver Weichhold


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.

like image 4
Etan Avatar answered Nov 17 '22 08:11

Etan