You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Network/Utils/RequestBuffer.cs

78 lines
2.2 KiB

using MemoryPools;
using System;
5 years ago
using System.Collections.Concurrent;
6 years ago
using System.Collections.Generic;
namespace ZeroLevel.Network
{
internal sealed class RequestBuffer
{
private ConcurrentDictionary<long, RequestInfo> _requests = new ConcurrentDictionary<long, RequestInfo>();
private static DefaultObjectPool<RequestInfo> _ri_pool = new DefaultObjectPool<RequestInfo>(new DefaultPooledObjectPolicy<RequestInfo>());
6 years ago
7 months ago
public void RegisterForFrame(int identity, Action<byte[]> callback, Action<string> fail = null!)
6 years ago
{
var ri = _ri_pool.Get();
ri.Reset(callback, fail);
_requests[identity] = ri;
6 years ago
}
public void Fail(long frameId, string message)
6 years ago
{
RequestInfo ri;
if (_requests.TryRemove(frameId, out ri))
6 years ago
{
ri.Fail(message);
_ri_pool.Return(ri);
6 years ago
}
}
public void Success(long frameId, byte[] data)
6 years ago
{
RequestInfo ri;
if (_requests.TryRemove(frameId, out ri))
6 years ago
{
ri.Success(data);
_ri_pool.Return(ri);
6 years ago
}
}
public void StartSend(long frameId)
6 years ago
{
RequestInfo ri;
if (_requests.TryGetValue(frameId, out ri))
6 years ago
{
ri.StartSend();
6 years ago
}
}
public void Timeout(List<long> frameIds)
6 years ago
{
RequestInfo ri;
for (int i = 0; i < frameIds.Count; i++)
6 years ago
{
if (_requests.TryRemove(frameIds[i], out ri))
6 years ago
{
_ri_pool.Return(ri);
6 years ago
}
}
5 years ago
}
public void TestForTimeouts()
{
var now_ticks = DateTime.UtcNow.Ticks;
var to_remove = new List<long>();
foreach (var pair in _requests)
6 years ago
{
if (pair.Value.Sended == false) continue;
var diff = now_ticks - pair.Value.Timestamp;
5 years ago
if (diff > BaseSocket.MAX_REQUEST_TIME_TICKS)
{
5 years ago
to_remove.Add(pair.Key);
5 years ago
}
6 years ago
}
5 years ago
Timeout(to_remove);
6 years ago
}
}
}

Powered by TurnKey Linux.