Пример #1
0
        /// <summary>
        /// リクエストかコマンドを処理します。
        /// </summary>
        private void HandleRequestOrCommand(int id, object message)
        {
            HandlerInfo handlerInfo = null;
            IPbResponse response    = null;

            // コマンドを処理するハンドラオブジェクトを取得します。
            lock (this.handlerDic)
            {
                if (!this.handlerDic.TryGetValue(message.GetType(), out handlerInfo))
                {
                    Log.Error(this,
                              "{0}: 適切なハンドラが見つかりませんでした。",
                              message.GetType());
                    return;
                }
            }

            // ログを出力したくない場合もあります。
            if (handlerInfo.IsOutLog)
            {
                Log.Debug(this,
                          "{0}を受信しました。", message.GetType());
            }

            if (handlerInfo.Handler != null)
            {
                try
                {
                    // レスポンスはnullのことがありますが、
                    // それは合法です。
                    response = handlerInfo.Handler(message);
                }
                catch (Exception ex)
                {
                    Log.ErrorException(this, ex,
                                       "受信データの処理ハンドラでエラーが発生しました。");

                    response = new PbResponse <PbDummy>()
                    {
                        ErrorCode = PbErrorCode.HandlerException,
                    };
                }
            }

            // もしリクエストなら、レスポンスを返します。
            if (handlerInfo.IsRequestHandler)
            {
                // responseはnullのことがあります。
                SendResponse(id, response);
            }
        }