Exemple #1
0
        //----------------[ Operations ]-----------------------//
        public bool SendQuest(Quest quest, IAnswerCallback callback, int timeout = 0)
        {
            if (AutoConnect)
            {
                AsyncConnect();     //-- Auto check and reconnect if necessary.
            }
            TCPConnection conn = null;

            lock (interLocker)
            {
                conn = connection;
            }

            if (conn != null)
            {
                if (timeout == 0)
                {
                    timeout = QuestTimeout;
                }

                if (timeout == 0)
                {
                    timeout = ClientEngine.globalQuestTimeoutSeconds;
                }

                conn.SendQuest(quest, callback, timeout);
                return(true);
            }
            else
            {
                return(false);
            }
        }
        //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.");
                }
            }
        }
 static void RunCallback(IAnswerCallback callback, int errorCode)
 {
     ClientEngine.RunTask(() => {
         callback.OnException(null, errorCode);
     });
 }