Пример #1
0
        public T invoke <T> (int timeoutInMs, string mappingUrl, params object[] paramList)
        {
            makeConnectionInCallerThread(15000);
            if (ioSession == null || !ioSession.Connected)
            {
                return(default(T));
            }
            CallCommand callCmd = new CallCommand();

            callCmd.procedureUri = mappingUrl;
            callCmd.args         = paramList;
            callCmd.options.Add("Cookie", cookieManager.getCookieForSendToServer());
            CallResultFuture asyncResult = new CallResultFuture();

            metaHolder.requestPool.Add(callCmd.requestId, asyncResult);
            asyncResult.resultType = typeof(T);
            ioSession.Write(callCmd);
            lock (asyncResult.monitorLock) {
                //Monitor.Wait (asyncResult.monitorLock, timeoutInMs); commented for test
                Monitor.Wait(asyncResult.monitorLock);
            }
            if (!asyncResult.done)
            {
                throw new TimeoutException("{timeoutInMs:" + timeoutInMs + '}');
            }
            if (!asyncResult.isExceptionResult)
            {
                return((T)asyncResult.result);
            }
            if (asyncResult.isExceptionResult)
            {
                throw new Exception((string)asyncResult.result);
            }
            return(default(T));
        }
Пример #2
0
        public void onMessageReceived(object sender, IoSessionMessageEventArgs eventArgs)
        {
            String json = (String)eventArgs.Message;
            //System.Console.WriteLine (json);
            JArray jsonArray = JArray.Parse(json);
            //System.Console.WriteLine ("jsonArray:" + jsonArray);
            int commandType = jsonArray [0].Value <int> ();

            if (commandType == MsgTypeConstant.result)
            {
                long             requestId = jsonArray [1].ToObject <int> ();
                CallResultFuture future    = (CallResultFuture)metaHolder.requestPool [requestId];
                if (future == null)
                {
                    return;
                }
                Dictionary <string, object> details = jsonArray [2].ToObject <Dictionary <string, object> > ();
                object mapValue;
                if (details.TryGetValue("Set-Cookie", out mapValue))
                {
                    JArray cookieArray = (JArray)mapValue;
                    processSetCookie(cookieArray);
                }
                // JArray resultArray = JArray.Parse (jsonArray [3].ToString ());
                JArray resultArray = (JArray)jsonArray [3];
                future.result            = resultArray.First.ToObject(future.resultType);
                future.done              = true;
                future.isExceptionResult = false;
                lock (future.monitorLock) {
                    Monitor.PulseAll(future.monitorLock);
                }
                return;
            }
            if (commandType == MsgTypeConstant.error)
            {
                long             requestId = jsonArray [2].ToObject <int> ();
                CallResultFuture future    = (CallResultFuture)metaHolder.requestPool [requestId];
                metaHolder.requestPool.Remove(requestId);
                future.done = true;
                future.isExceptionResult = true;
                lock (future.monitorLock) {
                    Monitor.PulseAll(future.monitorLock);
                }
                return;
            }
            // TODO 需要改为线程池处理
        }