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

150 lines
4.2 KiB

6 years ago
using System;
using System.Collections.Generic;
5 years ago
using System.Linq;
5 years ago
using System.Threading;
6 years ago
using ZeroLevel.Services.Pools;
namespace ZeroLevel.Network
{
internal sealed class RequestBuffer
{
5 years ago
private SpinLock _reqeust_lock = new SpinLock();
6 years ago
private Dictionary<long, RequestInfo> _requests = new Dictionary<long, RequestInfo>();
private static ObjectPool<RequestInfo> _ri_pool = new ObjectPool<RequestInfo>(() => new RequestInfo());
public void RegisterForFrame(int identity, Action<byte[]> callback, Action<string> fail = null)
6 years ago
{
var ri = _ri_pool.Allocate();
5 years ago
bool take = false;
try
6 years ago
{
5 years ago
_reqeust_lock.Enter(ref take);
6 years ago
ri.Reset(callback, fail);
6 years ago
_requests.Add(identity, ri);
6 years ago
}
5 years ago
finally
{
if (take) _reqeust_lock.Exit(false);
}
6 years ago
}
public void Fail(long frameId, string message)
{
RequestInfo ri = null;
5 years ago
bool take = false;
try
6 years ago
{
5 years ago
_reqeust_lock.Enter(ref take);
6 years ago
if (_requests.ContainsKey(frameId))
{
ri = _requests[frameId];
_requests.Remove(frameId);
}
}
5 years ago
finally
{
if (take) _reqeust_lock.Exit(false);
}
6 years ago
if (ri != null)
{
ri.Fail(message);
_ri_pool.Free(ri);
}
}
public void Success(long frameId, byte[] data)
6 years ago
{
RequestInfo ri = null;
5 years ago
bool take = false;
try
6 years ago
{
5 years ago
_reqeust_lock.Enter(ref take);
6 years ago
if (_requests.ContainsKey(frameId))
{
ri = _requests[frameId];
_requests.Remove(frameId);
}
}
5 years ago
finally
{
if (take) _reqeust_lock.Exit(false);
}
6 years ago
if (ri != null)
{
ri.Success(data);
6 years ago
_ri_pool.Free(ri);
}
}
public void StartSend(long frameId)
{
RequestInfo ri = null;
5 years ago
bool take = false;
try
6 years ago
{
5 years ago
_reqeust_lock.Enter(ref take);
6 years ago
if (_requests.ContainsKey(frameId))
{
ri = _requests[frameId];
}
}
5 years ago
finally
{
if (take) _reqeust_lock.Exit(false);
}
6 years ago
if (ri != null)
{
ri.StartSend();
}
}
5 years ago
public void Timeout(List<long> frameIds)
6 years ago
{
5 years ago
bool take = false;
try
6 years ago
{
5 years ago
_reqeust_lock.Enter(ref take);
5 years ago
for (int i = 0; i < frameIds.Count; i++)
6 years ago
{
5 years ago
if (_requests.ContainsKey(frameIds[i]))
6 years ago
{
5 years ago
_ri_pool.Free(_requests[frameIds[i]]);
_requests.Remove(frameIds[i]);
6 years ago
}
}
}
5 years ago
finally
{
if (take) _reqeust_lock.Exit(false);
}
5 years ago
}
public void TestForTimeouts()
{
var now_ticks = DateTime.UtcNow.Ticks;
var to_remove = new List<long>();
KeyValuePair<long, RequestInfo>[] to_proceed;
bool take = false;
try
{
_reqeust_lock.Enter(ref take);
to_proceed = _requests.Select(x => x).ToArray();
}
finally
{
if (take) _reqeust_lock.Exit(false);
}
for (int i = 0; i < to_proceed.Length; i++)
6 years ago
{
5 years ago
if (to_proceed[i].Value.Sended == false) continue;
var diff = now_ticks - to_proceed[i].Value.Timestamp;
if (diff > BaseSocket.MAX_REQUEST_TIME_TICKS)
{
to_remove.Add(to_proceed[i].Key);
}
6 years ago
}
5 years ago
Timeout(to_remove);
6 years ago
}
}
}

Powered by TurnKey Linux.