using System; using System.Collections.Generic; using System.Net; using System.Threading; using System.Threading.Tasks; using ZeroLevel.Models; using ZeroLevel.Services.Serialization; namespace ZeroLevel.Network { public sealed class ExClientSet : IClientSet, IDisposable { public void Dispose() { throw new NotImplementedException(); } #region IMultiClient public InvokeResult Request(string alias, Action callback) { throw new NotImplementedException(); } public InvokeResult Request(string alias, string inbox, Action callback) { throw new NotImplementedException(); } public InvokeResult Request(string alias, Trequest request, Action callback) { throw new NotImplementedException(); } public InvokeResult Request(string alias, string inbox, Trequest request, Action callback) { throw new NotImplementedException(); } public InvokeResult RequestBroadcast(string alias, Action> callback) { throw new NotImplementedException(); } public InvokeResult RequestBroadcast(string alias, string inbox, Action> callback) { throw new NotImplementedException(); } public InvokeResult RequestBroadcast(string alias, Trequest data, Action> callback) { throw new NotImplementedException(); } public InvokeResult RequestBroadcast(string alias, string inbox, Trequest data, Action> callback) { throw new NotImplementedException(); } public InvokeResult RequestBroadcastByGroup(string serviceGroup, Action> callback) { throw new NotImplementedException(); } public InvokeResult RequestBroadcastByGroup(string serviceGroup, string inbox, Action> callback) { throw new NotImplementedException(); } public InvokeResult RequestBroadcastByGroup(string serviceGroup, Trequest data, Action> callback) { throw new NotImplementedException(); } public InvokeResult RequestBroadcastByGroup(string serviceGroup, string inbox, Trequest data, Action> callback) { throw new NotImplementedException(); } public InvokeResult RequestBroadcastByType(string serviceType, Action> callback) { throw new NotImplementedException(); } public InvokeResult RequestBroadcastByType(string serviceType, string inbox, Action> callback) { throw new NotImplementedException(); } public InvokeResult RequestBroadcastByType(string serviceType, Trequest data, Action> callback) { throw new NotImplementedException(); } public InvokeResult RequestBroadcastByType(string serviceType, string inbox, Trequest data, Action> callback) { throw new NotImplementedException(); } public InvokeResult Send(string alias, T data) { throw new NotImplementedException(); } public InvokeResult Send(string alias, string inbox, T data) { throw new NotImplementedException(); } public InvokeResult SendBroadcast(string alias, T data) { throw new NotImplementedException(); } public InvokeResult SendBroadcast(string alias, string inbox, T data) { throw new NotImplementedException(); } public InvokeResult SendBroadcastByGroup(string serviceGroup, T data) { throw new NotImplementedException(); } public InvokeResult SendBroadcastByGroup(string serviceGroup, string inbox, T data) { throw new NotImplementedException(); } public InvokeResult SendBroadcastByType(string serviceType, T data) { throw new NotImplementedException(); } public InvokeResult SendBroadcastByType(string serviceType, string inbox, T data) { throw new NotImplementedException(); } #endregion #region Private private IEnumerable _RequestBroadcast(List clients, string inbox, Treq data) { var response = new List(); using (var waiter = new CountdownEvent(clients.Count)) { foreach (var client in clients) { Task.Run(() => { try { if (false == client.Request(inbox, data, resp => { waiter.Signal(); response.Add(resp); }).Success) { waiter.Signal(); } } catch (Exception ex) { Log.SystemError(ex, $"[ExClientSet._RequestBroadcast] Error direct request to service '{client.EndPoint}' in broadcast request. Inbox '{inbox}'"); waiter.Signal(); } }); } waiter.Wait(BaseSocket.MAX_REQUEST_TIME_MS); } return response; } private IEnumerable _RequestBroadcast(List clients, string inbox) { var response = new List(); using (var waiter = new CountdownEvent(clients.Count)) { foreach (var client in clients) { Task.Run(() => { try { if (false == client.Request(inbox, resp => { waiter.Signal(); response.Add(resp); }).Success) { waiter.Signal(); } } catch (Exception ex) { Log.SystemError(ex, $"[ExClientSet._RequestBroadcast] Error direct request to service '{client.EndPoint}' in broadcast request. Inbox '{inbox}'"); waiter.Signal(); } }); } waiter.Wait(BaseSocket.MAX_REQUEST_TIME_MS); } return response; } #endregion } public sealed class ExClient : IClient, IDisposable { private readonly ISocketClient _client; public IPEndPoint EndPoint => _client?.Endpoint; public SocketClientStatus Status => _client.Status; public IRouter Router => _client.Router; public ExClient(ISocketClient client) { _client = client; } public void ForceConnect() => _client.ForceConnect(); public InvokeResult Send(string inbox) { try { _client.Send(Frame.FromPool(inbox)); } catch (Exception ex) { Log.Error(ex, "[NetworkNode.Send]"); return InvokeResult.Fault(ex.Message); } return InvokeResult.Succeeding(); } public InvokeResult Send(string inbox, byte[] data) { try { _client.Send(Frame.FromPool(inbox, data)); } catch (Exception ex) { Log.Error(ex, "[NetworkNode.Send]"); return InvokeResult.Fault(ex.Message); } return InvokeResult.Succeeding(); } public InvokeResult Send(T message) { try { _client.Send(Frame.FromPool(BaseSocket.DEFAULT_MESSAGE_INBOX, MessageSerializer.SerializeCompatible(message))); } catch (Exception ex) { Log.Error(ex, "[NetworkNode.Send]"); return InvokeResult.Fault(ex.Message); } return InvokeResult.Succeeding(); } public InvokeResult Send(string inbox, T message) { try { _client.Send(Frame.FromPool(inbox, MessageSerializer.SerializeCompatible(message))); } catch (Exception ex) { Log.Error(ex, "[NetworkNode.Send]"); return InvokeResult.Fault(ex.Message); } return InvokeResult.Succeeding(); } public InvokeResult Request(string inbox, Action callback) { try { _client.Request(Frame.FromPool(inbox), f => callback(f)); } catch (Exception ex) { Log.Error(ex, "[NetworkNode.Request]"); return InvokeResult.Fault(ex.Message); } return InvokeResult.Succeeding(); } public InvokeResult Request(string inbox, byte[] data, Action callback) { try { _client.Request(Frame.FromPool(inbox, data), f => callback(f)); } catch (Exception ex) { Log.Error(ex, "[NetworkNode.Request]"); return InvokeResult.Fault(ex.Message); } return InvokeResult.Succeeding(); } public InvokeResult Request(string inbox, Action callback) { try { _client.Request(Frame.FromPool(inbox), f => callback(MessageSerializer.DeserializeCompatible(f))); } catch (Exception ex) { Log.Error(ex, "[NetworkNode.Request]"); return InvokeResult.Fault(ex.Message); } return InvokeResult.Succeeding(); } public InvokeResult Request(Action callback) { try { _client.Request(Frame.FromPool(BaseSocket.DEFAULT_REQUEST_INBOX), f => callback(MessageSerializer.DeserializeCompatible(f))); } catch (Exception ex) { Log.Error(ex, "[NetworkNode.Request]"); return InvokeResult.Fault(ex.Message); } return InvokeResult.Succeeding(); } public InvokeResult Request(string inbox, Trequest request, Action callback) { try { _client.Request(Frame.FromPool(inbox, MessageSerializer.SerializeCompatible(request)), f => callback(MessageSerializer.DeserializeCompatible(f))); } catch (Exception ex) { Log.Error(ex, "[NetworkNode.Request]"); return InvokeResult.Fault(ex.Message); } return InvokeResult.Succeeding(); } public InvokeResult Request(Trequest request, Action callback) { try { _client.Request(Frame.FromPool(BaseSocket.DEFAULT_REQUEST_WITHOUT_ARGS_INBOX, MessageSerializer.SerializeCompatible(request)), f => callback(MessageSerializer.DeserializeCompatible(f))); } catch (Exception ex) { Log.Error(ex, "[NetworkNode.Request]"); return InvokeResult.Fault(ex.Message); } return InvokeResult.Succeeding(); } public void Dispose() { _client.Dispose(); } } }