http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx.net asincrona presa timeout di controllo thread-sicurezza
A partire dal sopra MSDN esempio sto cercando di scrivere un assegno timeout che chiuderà i socket client inattivi e liberare risorse.
Questo è quello che ho trovato. Ma non sono sicuro che sia completamente sicuro per i thread e se esiste un modo migliore per farlo. Spero che qualcuno possa dare qualche consiglio.
void IO_Completed(object sender, SocketAsyncEventArgs e)
{
if (e.SocketError != SocketError.Success)
{
CloseClientSocket(e);
return;
}
if (1 < Interlocked.CompareExchange(ref token.Status, 1, 0))
return;
switch (e.LastOperation)
{
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
...
}
token.LastActive = Environment.TickCount;
Interlocked.CompareExchange(ref token.Status, 0, 1);
}
void ProcessReceive(SocketAsyncEventArgs e)
{
AsyncUserToken token = (AsyncUserToken)e.UserToken;
if (e.BytesTransferred > 0)
if (!token.Socket.SendAsync(e))
ProcessSend(e);
else
CloseClientSocket(e);
}
void ProcessSend(SocketAsyncEventArgs e)
{
AsyncUserToken token = (AsyncUserToken)e.UserToken;
if (!token.Socket.ReceiveAsync(e))
ProcessReceive(e);
}
TimeoutCheck verrà eseguito una volta ogni 20 secondi. allReadWriteArgs è un array con tutto SocketAsyncEventArgs. Dopo la chiusura, il socket IO_Completed verrà richiamato con SocketError.OperationAborted.
void TimeoutCheck(object state)
{
AsyncUserToken token;
int timeout = Environment.TickCount - 20000;
for (int i = 0; i < allReadWriteArgs.Length; i++)
{
token = (AsyncUserToken)allReadWriteArgs[i].UserToken;
if (token.LastActive < timeout)
if (0 == Interlocked.CompareExchange(ref token.Status, 2, 0))
Interlocked.Exchange(ref token.Socket, null).Close();
}
}
void CloseClientSocket(SocketAsyncEventArgs e)
{
AsyncUserToken token = e.UserToken as AsyncUserToken;
if (token.Socket != null)
{
try
{
token.Socket.Shutdown(SocketShutdown.Both);
}
catch (SocketException) { }
token.Socket.Close();
}
token.Status = 2;
bufferManager.FreeBuffer(e);
readWritePool.Push(e);
...
}
Questo è un pò vecchio, ma per chi legge che in futuro: non utilizzare 'Environment.TickCount' per qualsiasi calcolo nell'applicazione. È rappresentato come 'Int32' che si esaurirà in 24,9 giorni e passerà ad un numero intero negativo e alla fine restituirà 0. Utilizzare invece' DateTime.UtcNow.Ticks'. –