Exemplo n.º 1
0
        private void DealAnswer(Answer answer)
        {
            AnswerCallbackUnit unit = null;
            UInt32             seq  = answer.SeqNum();

            lock (interLocker)
            {
                if (callbackSeqNumMap.TryGetValue(seq, out unit))
                {
                    callbackSeqNumMap.Remove(seq);

                    if (callbackTimeoutMap.TryGetValue(unit.timeoutTime, out HashSet <AnswerCallbackUnit> cbSet))
                    {
                        cbSet.Remove(unit);
                        if (cbSet.Count == 0)
                        {
                            callbackTimeoutMap.Remove(unit.timeoutTime);
                        }
                    }
                }
            }

            if (unit != null)
            {
                ClientEngine.RunTask(() => {
                    unit.callback.OnAnswer(answer);
                });
            }
        }
Exemplo n.º 2
0
        //private void sendQuest(Quest quest, AnswerCallback callback, int timeoutInSeconds, boolean keyExchangedQuest)

        public void SendQuest(Quest quest, IAnswerCallback callback, int timeoutInSeconds)
        {
            if (quest == null)
            {
                if (callback != null)
                {
                    RunCallback(callback, ErrorCode.FPNN_EC_CORE_INVALID_PACKAGE);
                }

                return;
            }

            bool isClosed;

            byte[] raw;
            try
            {
                raw = quest.Raw();
            }
            catch (Exception ex)
            {
                if (callback != null)
                {
                    RunCallback(callback, ErrorCode.FPNN_EC_PROTO_UNKNOWN_ERROR);
                }

                if (errorRecorder != null)
                {
                    errorRecorder.RecordError("Send quest cannelled. Quest.Raw() exception.", ex);
                }

                return;
            }

            lock (interLocker)
            {
                isClosed = (status == TCPClient.ClientStatus.Closed);
                if (!isClosed)
                {
                    sendQueue.Enqueue(raw);

                    if (callback != null)
                    {
                        if (timeoutInSeconds == 0)
                        {
                            timeoutInSeconds = ClientEngine.globalQuestTimeoutSeconds;
                        }

                        TimeSpan span    = DateTime.Now - ClientEngine.originDateTime;
                        Int64    seconds = (Int64)Math.Floor(span.TotalSeconds) + timeoutInSeconds;

                        AnswerCallbackUnit unit = new AnswerCallbackUnit();
                        unit.callback    = callback;
                        unit.seqNum      = quest.SeqNum();
                        unit.timeoutTime = seconds;

                        callbackSeqNumMap.Add(quest.SeqNum(), unit);

                        if (callbackTimeoutMap.TryGetValue(seconds, out HashSet <AnswerCallbackUnit> cbSet))
                        {
                            cbSet.Add(unit);
                        }
                        else
                        {
                            cbSet = new HashSet <AnswerCallbackUnit>();
                            cbSet.Add(unit);
                            callbackTimeoutMap.Add(seconds, cbSet);
                        }
                    }

                    if (status == TCPClient.ClientStatus.Connecting)
                    {
                        return;
                    }
                }
            }

            if (!isClosed)
            {
                CheckSending();
            }
            else
            {
                if (callback != null)
                {
                    RunCallback(callback, ErrorCode.FPNN_EC_CORE_INVALID_CONNECTION);
                }

                if (errorRecorder != null)
                {
                    errorRecorder.RecordError("Send Quest " + quest.Method() + " on closed connection.");
                }
            }
        }