using MemoryPools; using System; using System.Threading; using ZeroLevel.Services.Pools; namespace ZeroLevel.Network { public static class ExchangeExtension { private static DefaultObjectPool _mrePool; static ExchangeExtension() { _mrePool = new DefaultObjectPool(new DefaultPooledObjectPolicy()); } public static Tresponse Request(this IClientSet exchange, string alias, TimeSpan timeout) { Tresponse response = default; var ev = _mrePool.Get(); ev.Reset(); try { if (exchange.Request(alias, _response => { response = _response; ev.Set(); })) { ev.Wait(timeout); } } finally { _mrePool.Return(ev); } return response; } public static Tresponse Request(this IClientSet exchange, string alias, string inbox, TimeSpan timeout) { Tresponse response = default; var ev = _mrePool.Get(); ev.Reset(); try { if (exchange.Request(alias, inbox, _response => { response = _response; ev.Set(); })) { ev.Wait(timeout); } } finally { _mrePool.Return(ev); } return response; } public static Tresponse Request(this IClientSet exchange, string alias, Trequest request, TimeSpan timeout) { Tresponse response = default; var ev = _mrePool.Get(); ev.Reset(); try { if (exchange.Request(alias, request, _response => { response = _response; ev.Set(); })) { ev.Wait(timeout); } } finally { _mrePool.Return(ev); } return response; } public static Tresponse Request(this IClientSet exchange, string alias, string inbox , Trequest request, TimeSpan timeout) { Tresponse response = default; var ev = _mrePool.Get(); ev.Reset(); try { if (exchange.Request(alias, inbox, request, _response => { response = _response; ev.Set(); })) { ev.Wait(timeout); } } finally { _mrePool.Return(ev); } return response; } public static Tresponse Request(this IClientSet exchange, string alias) { Tresponse response = default; var ev = _mrePool.Get(); ev.Reset(); try { if (exchange.Request(alias, _response => { response = _response; ev.Set(); })) { ev.Wait(Network.BaseSocket.MAX_REQUEST_TIME_MS); } } finally { _mrePool.Return(ev); } return response; } public static Tresponse Request(this IClientSet exchange, string alias, string inbox) { Tresponse response = default; var ev = _mrePool.Get(); ev.Reset(); try { if (exchange.Request(alias, inbox, _response => { response = _response; ev.Set(); })) { ev.Wait(Network.BaseSocket.MAX_REQUEST_TIME_MS); } } finally { _mrePool.Return(ev); } return response; } public static Tresponse Request(this IClientSet exchange, string alias, Trequest request) { Tresponse response = default; var ev = _mrePool.Get(); ev.Reset(); try { if (exchange.Request(alias, request, _response => { response = _response; ev.Set(); })) { ev.Wait(Network.BaseSocket.MAX_REQUEST_TIME_MS); } } finally { _mrePool.Return(ev); } return response; } public static Tresponse Request(this IClientSet exchange, string alias, string inbox , Trequest request) { Tresponse response = default; var ev = _mrePool.Get(); ev.Reset(); try { if (exchange.Request(alias, inbox, request, _response => { response = _response; ev.Set(); })) { ev.Wait(Network.BaseSocket.MAX_REQUEST_TIME_MS); } } finally { _mrePool.Return(ev); } return response; } } }