protected void Init() { On <Data.ResponseAdd>(Data.Cmd.TcsCommand.ResponseAdd.ToString(), (data) => { // when received data ResponseAdd from server //log4j.Info("OnResponseAdd: " + data.Result); //OnResponseAdd?.Invoke(data); // test BeginInvoke if (OnResponseAdd != null) { Delegate[] eventListeners = OnResponseAdd.GetInvocationList(); for (int i = 0; i < eventListeners.Length; i++) { ResponseAddHandler methodToInvoke = (ResponseAddHandler)eventListeners[i]; methodToInvoke.BeginInvoke(data, (iar) => { log4j.Info("endInvoke"); var ar = (System.Runtime.Remoting.Messaging.AsyncResult)iar; var invokeMethod = (ResponseAddHandler)ar.AsyncDelegate; try { invokeMethod.EndInvoke(iar); } catch (Exception ex) { log4j.Error(invokeMethod.ToString(), ex); } }, null); } } /// cannot use this (must use GetInvocationList()) //OnResponseAdd?.BeginInvoke(data, (iar) => //{ // log4j.Info("endInvoke"); // var ar = (System.Runtime.Remoting.Messaging.AsyncResult)iar; // var invokeMethod = (ResponseAddHandler)ar.AsyncDelegate; // try // { // invokeMethod.EndInvoke(iar); // } // catch (Exception ex) // { // log4j.Error(invokeMethod.ToString(), ex); // } //}, null); }); On <Data.ResponseEcho>(Data.Cmd.TcsCommand.ResponseEcho.ToString(), (data) => { OnResponseEcho?.Invoke(data); }); }
public ResponseAdd RequestAdd(int timeOutMilliSec, params int[] param) { RequestAdd requestAdd = new RequestAdd { UUID = Guid.NewGuid().ToString() }; foreach (int p in param) { requestAdd.Param.Add(p); } // set a timeout on this call to server! // if we do not hv this timeout, calling this RequestAdd method will forever waiting if server never response! TaskCompletionSource <ResponseAdd> tcs = new TaskCompletionSource <ResponseAdd>(); //CancellationTokenSource ct = new CancellationTokenSource(timeOutMilliSec); // package TaskParallelLibrary CancellationTokenSource only hv empty constructor!! CancellationTokenSource ct = new CancellationTokenSource(); ResponseAddHandler rah = ((rpAdd) => { // only want the response UUID which is same as what we send if (rpAdd.UUID == requestAdd.UUID) { tcs.TrySetResult(rpAdd); } }); // when timeout occur, set Exception to TaskCompletionSource // also remove the callback from eventhandler ct.Token.Register(() => { OnResponseAdd -= rah; tcs.TrySetException(new TimeoutException("TimeOut " + timeOutMilliSec)); }, useSynchronizationContext: false); // use Rackspace.Threading.CancellationTokenSourceExtensions CancelAfter ct.CancelAfter(TimeSpan.FromMilliseconds(timeOutMilliSec)); OnResponseAdd += rah; //hook to the eventHandler //string sendCmd = "RequestAdd " + Newtonsoft.Json.JsonConvert.SerializeObject(requestAdd) + "\r\n"; string sendCmd = Cmd.TcsCommand.RequestAdd.ToString() + " " + Newtonsoft.Json.JsonConvert.SerializeObject(requestAdd) + "\r\n"; base.Send(Encoding.UTF8.GetBytes(sendCmd)); tcs.Task.Wait(); ResponseAdd responseAdd = tcs.Task.Result; OnResponseAdd -= rah; //after received our response, unhook it. we only expecting 1 response. return(responseAdd); }
/// <summary> /// Send command RequestAdd to server /// Receive a Data.ResponseAdd object /// - async method /// - will throw Exception "TimeOut" /// </summary> /// <param name="param"></param> /// <returns></returns> public async Task <ResponseAdd> RequestAddAsync(params int[] param) { RequestAdd requestAdd = new RequestAdd { UUID = Guid.NewGuid().ToString() }; foreach (int p in param) { requestAdd.Param.Add(p); } // set a timeout on this call to server! TaskCompletionSource <ResponseAdd> tcs = new TaskCompletionSource <ResponseAdd>(); const int timeOuts = 10000; //miliseconds CancellationTokenSource ct = new CancellationTokenSource(timeOuts); ResponseAddHandler rah = ((rpAdd) => { // only want the response UUID which is same as what we send if (rpAdd.UUID == requestAdd.UUID) { tcs.TrySetResult(rpAdd); } }); // when timeout occur, set Exception to TaskCompletionSource // also remove the callback from eventhandler ct.Token.Register(() => { OnResponseAdd -= rah; tcs.TrySetException(new TimeoutException("TimeOut " + timeOuts)); }, useSynchronizationContext: false); OnResponseAdd += rah; //hook to the eventHandler //string sendCmd = "RequestAdd " + Newtonsoft.Json.JsonConvert.SerializeObject(requestAdd) + "\r\n"; string sendCmd = Cmd.TcsCommand.RequestAdd.ToString() + " " + Newtonsoft.Json.JsonConvert.SerializeObject(requestAdd) + "\r\n"; base.Send(Encoding.UTF8.GetBytes(sendCmd)); ResponseAdd responseAdd = await tcs.Task; OnResponseAdd -= rah; //after received our response, unhook it. we only expecting 1 response. return(responseAdd); }
public WebSocket.Data.ResponseAdd RequestAdd(int timeOutMilliSec, params int[] param) { Data.RequestAdd requestAdd = new Data.RequestAdd { UUID = Guid.NewGuid().ToString() }; foreach (int p in param) { requestAdd.Param.Add(p); } // set a timeout on this call to server! // if we do not hv this timeout, calling this RequestAdd method will forever waiting if server never response! TaskCompletionSource <Data.ResponseAdd> tcs = new TaskCompletionSource <Data.ResponseAdd>(); CancellationTokenSource ct = new CancellationTokenSource(timeOutMilliSec); ResponseAddHandler rah = ((rpAdd) => { // only want the response UUID which is same as what we send if (rpAdd.UUID == requestAdd.UUID) { tcs.TrySetResult(rpAdd); } }); // when timeout occur, set Exception to TaskCompletionSource // also remove the callback from eventhandler ct.Token.Register(() => { OnResponseAdd -= rah; tcs.TrySetException(new TimeoutException("TimeOut " + timeOutMilliSec)); }, useSynchronizationContext: false); OnResponseAdd += rah; //hook to the eventHandler Send(WebSocket.Data.Cmd.TcsCommand.RequestAdd.ToString(), requestAdd); tcs.Task.Wait(); WebSocket.Data.ResponseAdd responseAdd = tcs.Task.Result; OnResponseAdd -= rah; //after received our response, unhook it. we only expecting 1 response. return(responseAdd); }