Exemplo n.º 1
0
        // ExecuteDetails can continue in child class

        /// <summary>
        ///
        /// </summary>
        /// <param name="env"></param>
        /// <param name="messageTypes"> the expected type to receive from the request </param>
        /// <returns></returns>
        protected Envelope DoReliableRequestReply(Envelope env, Type[] messageTypes)
        {
            Logger.Debug($"{this.GetType().Name}-{this.ConversationId}, Sending Envelope with message Type {env.Message.GetType()}");
            SubSystem.outQueue.Enqueue(env);

            //Start timeout for message.
            int maxAttemps = 25;
            int attempts   = 1;

            while (!IncomingEvent.WaitOne(1000) && attempts < maxAttemps)
            {
                Logger.Debug($"{this.GetType().Name}-{this.ConversationId}, Sending Envelope with message Type {env.Message.GetType()} - Retry number {attempts}");
                SubSystem.outQueue.Enqueue(env);
                attempts++;
            }

            Envelope receivedEnv = null;

            if (IncomingEnvelopes.TryDequeue(out receivedEnv))
            {
                if (!messageTypes.Contains(receivedEnv.Message.GetType()))
                {
                    //We recieved the wrong message, or message we not in queue.
                    Logger.DebugFormat("{0}-{1}, Recieved wrong message.", this.GetType().Name, this.ConversationId);
                    receivedEnv = null; //Makes sure failure is null.
                    this.State  = PossibleState.Failed;
                }
            }
            else
            {
                Logger.DebugFormat("{0}-{1}, Didn't receive a message in Request Reply.", this.GetType().Name, this.ConversationId);
            }
            return(receivedEnv); //Returns null on a failure.
        }
        protected Message ProcessIncomingEnvelope(Envelope env)
        {
            IncomingEnvelopes.Enqueue(env);
            Identifier messageNumber = new Identifier(SubSystem.ProcessID, SubSystem.GetNextSeqNumber());

            if (env.Message.GetType() == typeof(EndGameResultMessage))
            {
                ((LobbyAppState)SubSystem.appState).removeGame(((EndGameResultMessage)env.Message).getGameId());
                return(new AckMessage(0, 0, messageNumber, ConversationId));
            }
            return(new ErrorMessage(0, messageNumber, ConversationId));
        }