Ejemplo n.º 1
0
        /// <summary>
        /// Create a new com service that can connect or listen at the comServiceAddress
        /// </summary>
        /// <param name="comServiceAddress">The addres of the service, the schema must be ws://. i.e. "ws://localhost:50000/CScom"</param>
        public CSCom(string comServiceAddress = "ws://localhost:50000/CSCom")
        {
            Pipe         = new WebsocketPipe <NPMessage>(new Uri(comServiceAddress));
            Pipe.Timeout = 30000;

            BindMessageHandling();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Called to process a message
        /// </summary>
        /// <param name="e"></param>
        /// <param name="waitingForAsyncEvents"></param>
        private void ProcessMessage(WebsocketPipe <NPMessage> .MessageEventArgs e)
        {
            if (this.MessageRecived == null)
            {
                return;
            }

            if (!e.RequiresResponse)
            {
                this.MessageRecived(this, e);
                return;
            }

            string wsid = e.WebsocketID;

            if (!SingleThreadedResponseExecution)
            {
                try
                {
                    this.MessageRecived(this, e);
                }
                catch (Exception ex)
                {
                    SendErrorMessage(wsid, ex);
                }
                finally
                {
                    if (RequiresAsyncEventLock)
                    {
                        e.WaitForAsynchroniusEvent(true, ASynchroniusEventExecutionTimeout);
                    }
                }
                return;
            }

            if (m_pendingResponseMessagesByID.ContainsKey(wsid))
            {
                // push to end and return.
                if (m_pendingResponseMessagesByID[wsid].Count > SingleThreadedResponseExecutionMaxStackSize)
                {
                    throw new StackOverflowException("Reached the maximal number of pending requests that require a response.");
                }

                m_pendingResponseMessagesByID[wsid].Enqueue(e);
                return;
            }

            m_pendingResponseMessagesByID[wsid] = new Queue <WebsocketPipe <NPMessage> .MessageEventArgs>();
            m_pendingResponseMessagesByID[wsid].Enqueue(e);

            while (m_pendingResponseMessagesByID[wsid].Count > 0)
            {
                e = m_pendingResponseMessagesByID[wsid].Dequeue();
                try
                {
                    this.MessageRecived(this, e);
                }
                catch (Exception ex)
                {
                    SendErrorMessage(wsid, ex);
                }
                finally
                {
                    if (RequiresAsyncEventLock)
                    {
                        e.WaitForAsynchroniusEvent(true, ASynchroniusEventExecutionTimeout);
                    }
                }
            }

            lock (m_pendingResponseMessagesByID)
            {
                m_pendingResponseMessagesByID.Remove(wsid);
            }

            //if (this.RequiresAsyncEventLock)
            //{
            //    if (waitingForAsyncEvents.ContainsKey(e.WebsocketID))
            //    {
            //        var lastEv = waitingForAsyncEvents[e.WebsocketID];
            //        throw new Exception("Called handler " + e.WebsocketID + " to wait for async events though already waiting for async event." +
            //            " \nPending message: " + (lastEv.Message != null ? lastEv.Message.ToString() : "[none]") +
            //            " \nCurrent message: " + (e.Message != null ? e.Message.ToString() : "[none]"));
            //    }
            //    waitingForAsyncEvents[e.WebsocketID] = e;
            //}
            //if (this.MessageRecived != null)
            //    this.MessageRecived(this, e);

            //if (this.RequiresAsyncEventLock)
            //{
            //    e.WaitForAsynchroniusEvent(true, ASynchroniusEventExecutionTimeout);
            //    waitingForAsyncEvents.Remove(e.WebsocketID);
            //}
        }