public T Message(T message, bool throwError, TimeSpan timeout) { T result = default(T); if (pipe == null || !pipe.isConnected) { return(result); } var queue = new queuemsg <T>(message); replyqueue.Add(queue); Log.Debug("Send and queue message " + message.messageid); using (queue.autoReset = new AutoResetEvent(false)) { pipe.PushMessage(message); queue.autoReset.WaitOne(timeout); queue.sw.Stop(); } Log.Debug("received reply for " + message.messageid + " " + string.Format("Time elapsed: {0:mm\\:ss\\.fff}", queue.sw.Elapsed)); replyqueue.Remove(queue); result = queue.result; if (result != null && result.error != null) { string s = result.error.ToString().Trim(); if (!string.IsNullOrEmpty(s) && s != "{}") { throw new NamedPipeException(result.error.ToString()); } } return(result); }
async public Task <T> MessageAsync(T message, bool throwError) { T result = default(T); if (pipe == null || !pipe.isConnected) { return(result); } var queue = new queuemsg <T>(message); replyqueue.Add(queue); Log.Debug("ASYNC Send and queue message " + message.messageid); using (queue.autoReset = new AutoResetEvent(false)) { pipe.PushMessage(message); await queue.autoReset.WaitOneAsync(); queue.sw.Stop(); } Log.Debug("ASYNC received reply for " + message.messageid + " " + string.Format("Time elapsed: {0:mm\\:ss\\.fff}", queue.sw.Elapsed)); replyqueue.Remove(queue); result = queue.result; if (!string.IsNullOrEmpty(result.error) && throwError) { throw new NamedPipeException(result.error); } return(result); }
public NamedPipeClient(string pipeName) { if (System.Threading.Monitor.TryEnter(lockobj, Config.local.thread_lock_timeout_seconds * 1000)) { try { replyqueue = new List <queuemsg <T> >(); } finally { System.Threading.Monitor.Exit(lockobj); } } pipe = new NamedPipeWrapper.NamedPipeClient <T>(pipeName); pipe.AutoReconnect = true; pipe.Disconnected += (sender) => { Disconnected?.Invoke(); }; pipe.Connected += (sender) => { Connected?.Invoke(); }; pipe.Error += (e) => { Error?.Invoke(e); }; pipe.ServerMessage += (sender, message) => { queuemsg <T> queue = null; if (System.Threading.Monitor.TryEnter(lockobj, Config.local.thread_lock_timeout_seconds * 1000)) { try { queue = replyqueue.Where(x => x != null && x.messageid == message.messageid).FirstOrDefault(); } finally { System.Threading.Monitor.Exit(lockobj); } } if (queue != null) { // Log.Information("received reply for " + message.messageid + " " + string.Format("Time elapsed: {0:mm\\:ss\\.fff}", queue.sw.Elapsed)); if (queue.Received) { return; } queue.result = message; queue.Received = true; if (queue.autoReset != null) { queue.autoReset.Set(); } return; } else { // Log.Information("received reply for unknown message id: " + message.messageid); } ServerMessage?.Invoke(message); }; }
async public Task <T> MessageAsync(T message, TimeSpan timeout) { T result = default(T); if (pipe == null || !pipe.isConnected) { return(result); } var queue = new queuemsg <T>(message); if (System.Threading.Monitor.TryEnter(lockobj, Config.local.thread_lock_timeout_seconds * 1000)) { try { replyqueue.Add(queue); } finally { System.Threading.Monitor.Exit(lockobj); } } // Log.Debug("ASYNC Send and queue message " + message.messageid); using (queue.autoReset = new AutoResetEvent(false)) { pipe.PushMessage(message); await queue.autoReset.WaitOneAsync(timeout, CancellationToken.None); queue.sw.Stop(); } // Log.Debug("ASYNC received reply for " + message.messageid + " " + string.Format("Time elapsed: {0:mm\\:ss\\.fff}", queue.sw.Elapsed)); if (System.Threading.Monitor.TryEnter(lockobj, Config.local.thread_lock_timeout_seconds * 1000)) { try { replyqueue.Remove(queue); } finally { System.Threading.Monitor.Exit(lockobj); } } result = queue.result; if (result != null && result.error != null) { string s = result.error.ToString().Trim(); if (!string.IsNullOrEmpty(s) && s != "{}") { throw new NamedPipeException(result.error.ToString()); } } return(result); }
public T Message(T message, TimeSpan timeout) { T result = default(T); if (pipe == null || !pipe.isConnected) { return(result); } var queue = new queuemsg <T>(message); if (System.Threading.Monitor.TryEnter(lockobj, Config.local.thread_lock_timeout_seconds * 1000)) { try { replyqueue.Add(queue); } finally { System.Threading.Monitor.Exit(lockobj); } } // Log.Debug("Send and queue message " + message.messageid); using (queue.autoReset = new AutoResetEvent(false)) { pipe.PushMessage(message); queue.autoReset.WaitOne(timeout); queue.sw.Stop(); } // Log.Debug("received reply for " + message.messageid); if (System.Threading.Monitor.TryEnter(lockobj, Config.local.thread_lock_timeout_seconds * 1000)) { try { replyqueue.Remove(queue); } finally { System.Threading.Monitor.Exit(lockobj); } } result = queue.result; if (result != null && result.error != null) { string s = result.error.ToString().Trim(); if (!string.IsNullOrEmpty(s) && s != "{}") { Log.Error(result.error.ToString()); throw new NamedPipeException(result.error.ToString()); } } return(result); }