/// <summary> /// タイムアウト付でリクエストを出します。 /// </summary> public void SendRequest <TReq, TRes>(TReq request, TimeSpan timeout, PbResponseHandler <TRes> handler, bool isOutLog = true) where TReq : class { if (request == null) { return; } var sendData = new PbSendData(request); var id = GetNextSendId(); var reqData = new PbRequestData <TReq, TRes>() { Id = id, Connection = this, ResponseReceived = handler, }; reqData.SetTimeout(timeout); // 未処理のリクエストとして、リストに追加します。 lock (this.requestDataDic) { this.requestDataDic.Add(id, reqData); } // データを送信します。 SendDataInternal(id, false, sendData, isOutLog); }
/// <summary> /// 受信したレスポンスを処理します。 /// </summary> private void HandleResponse(int id, object message) { var response = message as IPbResponse; PbRequestData reqData = null; if (response == null) { throw new InvalidOperationException( "レスポンスの型が正しくありません。"); } // リクエストリストの中から、レスポンスと同じIdを持つ // リクエストを探します。 lock (this.requestDataDic) { if (!this.requestDataDic.TryGetValue(id, out reqData)) { Log.Error(this, "サーバーから不正なレスポンスが返されました。" + "(id={0})", id); return; } this.requestDataDic.Remove(id); } Log.Debug(this, "{0}を受信しました。", message.GetType()); // レスポンス処理用のハンドラを呼びます。 if (reqData != null) { reqData.OnResponseReceived(response); // タイムアウト検出用タイマを殺すために必要です。 reqData.Dispose(); } }