Пример #1
0
 /// <summary>
 /// Exit State handler(執行上一層物件的方法)
 /// </summary>
 /// <param name="absClientRequestHandler">client object</param>
 public void Handle(AbsClientRequestHandler absClientRequestHandler)
 {
     log.Debug("State : Exit => ClientNo:" + absClientRequestHandler.ClientNo);
     absClientRequestHandler.KeepService = false;
     absClientRequestHandler.SocketServer.RemoveClient(absClientRequestHandler.ClientNo);
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="absClientRequestHandler"></param>
        public void Handle(AbsClientRequestHandler absClientRequestHandler)
        {
            #region variable
            byte[]               receiveBuffer  = null;
            int                  readCount      = 0;
            string               requestJsonStr = null;
            string               outputCmd      = null;
            IKMSGetter           kmsGetter      = null;
            IList <EskmsKeyPOCO> request_list   = null;
            IList <EskmsKeyPOCO> response_list  = null;
            EskmsKeyPOCO         response       = null;
            IList <string>       UID_Failed_List;
            Stopwatch            timer;
            //string requestCheckErrMsg = null;
            string      responseJsonStr = null;
            string      failed_list     = null;
            byte[]      responseBytes   = null;
            byte[]      diversKey       = null;
            int         sendCount       = -1;
            AL_DBModule obDB            = null;
            Shipment    shipment        = null;
            bool        hasReaderId     = false;
            #endregion

            try
            {
                receiveBuffer = new byte[0x4000];//4k
                readCount     = absClientRequestHandler.ClientSocket.Receive(receiveBuffer, SocketFlags.None);
                if (readCount == 0)
                {
                    return;
                }
                //command 輸出狀態 TODO...
                else if (readCount == 6 && Encoding.UTF8.GetString(receiveBuffer, 0, readCount).ToLower().Contains("status"))
                {
                    outputCmd     = "Hello";
                    receiveBuffer = Encoding.UTF8.GetBytes(outputCmd);
                    absClientRequestHandler.ClientSocket.Send(receiveBuffer);
                    return;
                }
                else
                {
                    log.Debug(m => m(">> {0}: {1}", this.GetType().Name, absClientRequestHandler.ClientNo));
                    //resize buffer
                    Array.Resize(ref receiveBuffer, readCount);
                    //init
                    obDB            = new AL_DBModule();
                    shipment        = new Shipment();
                    UID_Failed_List = new List <string>();
                    response_list   = new List <EskmsKeyPOCO>();
                    timer           = new Stopwatch();
                    timer.Start();
                    //casting jsonstring from buffer array
                    requestJsonStr = Encoding.UTF8.GetString(receiveBuffer);
                    log.Debug(m => m("[{0}]Request: {1}", this.GetType().Name, requestJsonStr));
                    request_list = JsonConvert.DeserializeObject <IList <EskmsKeyPOCO> >(requestJsonStr);

                    //檢查Request資料長度(Attribute) //不能檢查  因為沒有DeviceID
                    //poco.CheckLength(true, out requestCheckErrMsg);
                    log.Debug(m => m("1.Open DB Connection"));
                    obDB.OpenConnection();
                    log.Debug(m => m("2.Run DB Command"));
                    foreach (EskmsKeyPOCO request in request_list)
                    {
                        hasReaderId = shipment.Check_ReaderId_FromSAM_D(obDB, request.Input_UID, "21");
                        if (hasReaderId)
                        {
                            //設定Authenticate參數
                            kmsGetter = new KMSGetter()
                            {
                                Input_KeyLabel   = request.Input_KeyLabel,
                                Input_KeyVersion = request.Input_KeyVersion,
                                Input_UID        = request.Input_UID,
                                Input_DeviceID   = request.Input_DeviceID
                            };

                            log.Debug(m => m("3.開始取Divers Key"));
                            diversKey = kmsGetter.GetDiversKey();//會傳送數據到KMS並取回DiverseKey後做運算並將結果寫入Output屬性中
                            if (diversKey == null)
                            {
                                UID_Failed_List.Add(request.Input_UID);
                            }
                            else
                            {
                                //回應資料設定
                                response = new EskmsKeyPOCO()
                                {
                                    Input_KeyLabel   = request.Input_KeyLabel,
                                    Input_KeyVersion = request.Input_KeyVersion,
                                    Input_UID        = request.Input_UID,
                                    Output_DiversKey = diversKey
                                };
                                response_list.Add(response);
                            }
                        }
                        else
                        {
                            UID_Failed_List.Add(request.Input_UID);
                        }
                    }
                    if (UID_Failed_List.Count > 0)
                    {
                        failed_list = JsonConvert.SerializeObject(UID_Failed_List);
                        log.Debug(m => m("[{0}] 取Key失敗的UID列表:{1}", this.GetType().Name, failed_list));
                    }
                    responseJsonStr = JsonConvert.SerializeObject(response_list);
                    responseBytes   = Encoding.UTF8.GetBytes(responseJsonStr);
                    log.Debug(m => m("[{0}] Response:{1}", this.GetType().Name, responseJsonStr));
                    sendCount = absClientRequestHandler.ClientSocket.Send(responseBytes);
                    if (sendCount != responseBytes.Length)
                    {
                        log.Error(m => m("異常:送出資料(length:{0}不等於原始資料(length:{1}))", sendCount, responseBytes.Length));
                    }
                    timer.Stop();
                    log.Debug(m => m("[{0}] Response End TimeSpend:{1}ms", this.GetType().Name, timer.ElapsedMilliseconds));
                }
            }
            catch (ArgumentOutOfRangeException ex)
            {
                log.Error(m => m("資料檢核失敗:{0}", ex.ToString()));
            }
            catch (System.Data.SqlClient.SqlException sqlEx)
            {
                log.Error(m => m("DB Operate Failed:{0}", sqlEx.ToString()));
            }
            catch (JsonException ex)
            {
                log.Error(m => m("Request(JsonString) Parse Request(Object) Failed:{0}", ex.ToString()));
            }
            catch (Exception ex)
            {
                log.Error(m => m("[{0}] Error:{1} {2}", this.GetType().Name, ex.Message, ex.StackTrace));
            }
            finally
            {
                if (obDB != null)
                {
                    obDB.CloseConnection();
                    shipment = null;
                }
                absClientRequestHandler.ServiceState = new State_Exit();
            }
        }
        public void Handle(AbsClientRequestHandler absClientRequestHandler)
        {
            #region variable
            byte[] receiveBuffer  = null;
            int    readCount      = 0;
            string requestJsonStr = null;
            string outputCmd      = null;

            EskmsKeyTxLogPOCO_v2 request   = null;
            EskmsKeyTxLogPOCO_v2 response  = null;
            string      requestCheckErrMsg = null;
            string      responseJsonStr    = null;
            byte[]      responseBytes      = null;
            int         sendCount          = -1;
            AL_DBModule obDB            = null;
            Shipment    shipment        = null;
            string      txLogReturnCode = String.Empty;
            #endregion

            try
            {
                receiveBuffer = new byte[0x1000];//4k
                readCount     = absClientRequestHandler.ClientSocket.Receive(receiveBuffer, SocketFlags.None);
                if (readCount == 0)
                {
                    return;
                }
                //command 輸出狀態 TODO...
                else if (readCount == 6 && Encoding.UTF8.GetString(receiveBuffer, 0, readCount).ToLower().Contains("status"))
                {
                    outputCmd     = "Hello";
                    receiveBuffer = Encoding.UTF8.GetBytes(outputCmd);
                    absClientRequestHandler.ClientSocket.Send(receiveBuffer);
                    return;
                }
                else
                {
                    log.Debug(m => m(">> {0}: {1}", this.GetType().Name, absClientRequestHandler.ClientNo));
                    //resize buffer
                    Array.Resize(ref receiveBuffer, readCount);
                    //init
                    obDB     = new AL_DBModule();
                    shipment = new Shipment();

                    //casting jsonstring from buffer array
                    requestJsonStr = Encoding.UTF8.GetString(receiveBuffer);
                    log.Debug(m => m("[{0}]Request: {1}", this.GetType().Name, requestJsonStr));
                    request = JsonConvert.DeserializeObject <EskmsKeyTxLogPOCO_v2>(requestJsonStr);
                    //檢查Request資料長度(Attribute)
                    //request.CheckLength(true, out requestCheckErrMsg);

                    //回應資料設定
                    response = new EskmsKeyTxLogPOCO_v2()
                    {
                        SAM_UID    = request.SAM_UID,
                        DeviceId   = request.DeviceId,
                        ReturnCode = "000001",//預設為fail
                    };

                    txLogReturnCode = request.TestTxLog.Substring(16, 8);//取第16~23個字串

                    //簡易判斷:TxLog ReturnCode
                    if (txLogReturnCode.Equals("00000000"))
                    {
                        log.Debug(m => m("1.Open DB Connection"));
                        obDB.OpenConnection();

                        log.Debug(m => m("2.Run Shipment DB Operate SAM_UID:{0}, DeviceId:{1}", request.SAM_UID, request.DeviceId));
                        //若有輸入顧客識別符號和卡機種別則寫入時就依據此資料寫入,若無則使用預設値寫入(在DB_Operate.SQL.Sql_Getter_Reader_D裡)
                        if (!String.IsNullOrEmpty(request.Merc_Flg) && !String.IsNullOrEmpty(request.Reader_Type))
                        {
                            log.Debug(m => m("2-1. 自訂卡機格式與顧客識別[Reader_Type:{0}, Merc_Flg:{1}]", request.Reader_Type, request.Merc_Flg));
                            //執行Reader出貨的DB操作流程
                            shipment.Shipment_Reader(obDB, request.SAM_UID, request.DeviceId, request.Reader_Type, request.Merc_Flg);//
                        }
                        else
                        {
                            log.Debug(m => m("2-2.預設的卡機格式與顧客識別[Reader_Type:{0}, Merc_Flg:{1}]", "03", "ICA"));
                            //執行Reader出貨的DB操作流程
                            shipment.Shipment_Reader(obDB, request.SAM_UID, request.DeviceId);//
                        }
                        response.ReturnCode = "000000";
                        log.Debug(m => m("3.Shipment DB Operate End ..."));
                    }
                    else
                    {
                        log.Error(m => m("UID:{0}  DeviceId:{1}  TxLog ReturnCode異常:{2}", request.SAM_UID, request.DeviceId, txLogReturnCode));
                    }
                    responseJsonStr = JsonConvert.SerializeObject(response);
                    responseBytes   = Encoding.UTF8.GetBytes(responseJsonStr);
                    log.Debug(m => m("[{0}] Response:{1}", this.GetType().Name, responseJsonStr));
                    sendCount = absClientRequestHandler.ClientSocket.Send(responseBytes);
                    if (sendCount != responseBytes.Length)
                    {
                        log.Error(m => m("異常:送出資料(length:{0}不等於原始資料(length:{1}))", sendCount, responseBytes.Length));
                    }
                    log.Debug(m => m("[{0}] Response End", this.GetType().Name));
                }
            }
            catch (ArgumentOutOfRangeException ex)
            {
                log.Error(m => m("資料檢核失敗:{0}", ex.ToString()));
            }
            catch (JsonException ex)
            {
                log.Error(m => m("Request(JsonString) Parse Request(Object) Failed:{0}", ex.ToString()));
            }
            catch (Exception ex)
            {
                log.Error(m => m("[{0}] Error:{1} {2}", this.GetType().Name, ex.Message, ex.StackTrace));
            }
            finally
            {
                if (obDB != null && obDB.ALCon != null)
                {
                    obDB.CloseConnection();
                    shipment = null;
                }
                absClientRequestHandler.ServiceState = new State_Exit();
            }
        }
Пример #4
0
 /// <summary>
 /// Exit State handler(執行上一層物件的方法)
 /// </summary>
 /// <param name="absClientRequestHandler">client object</param>
 public void Handle(AbsClientRequestHandler absClientRequestHandler)
 {
     log.Debug("State : Exit => ClientNo:" + absClientRequestHandler.ClientNo);
     absClientRequestHandler.KeepService = false;
     absClientRequestHandler.SocketServer.RemoveClient(absClientRequestHandler.ClientNo);
 }
Пример #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="absClientRequestHandler"></param>
        public void Handle(AbsClientRequestHandler absClientRequestHandler)
        {
            #region variable
            byte[]           receiveBuffer      = null;
            int              readCount          = 0;
            string           requestJsonStr     = null;
            string           outputCmd          = null;
            iBonAuthenticate iBonAuthObj        = null;
            EskmsPOCO        request            = null;
            EskmsPOCO        response           = null;
            string           requestCheckErrMsg = null;
            string           responseJsonStr    = null;
            byte[]           responseBytes      = null;
            int              sendCount          = -1;
            #endregion

            try
            {
                receiveBuffer = new byte[0x1000];//4k
                readCount     = absClientRequestHandler.ClientSocket.Receive(receiveBuffer, SocketFlags.None);
                if (readCount == 0)
                {
                    return;
                }
                //command 輸出狀態 TODO...
                else if (readCount == 6 && Encoding.UTF8.GetString(receiveBuffer, 0, readCount).ToLower().Contains("status"))
                {
                    outputCmd     = "Hello";
                    receiveBuffer = Encoding.UTF8.GetBytes(outputCmd);
                    absClientRequestHandler.ClientSocket.Send(receiveBuffer);
                    return;
                }
                else
                {
                    log.Debug(m => m(">> {0}: {1}", this.GetType().Name, absClientRequestHandler.ClientNo));
                    //resize buffer
                    Array.Resize(ref receiveBuffer, readCount);
                    //casting jsonstring from buffer array
                    requestJsonStr = Encoding.UTF8.GetString(receiveBuffer);
                    log.Debug(m => m("[{0}]Request: {1}", this.GetType().Name, requestJsonStr));
                    request = JsonConvert.DeserializeObject <EskmsPOCO>(requestJsonStr);
                    //檢查Request資料長度(Attribute)
                    request.CheckLength(true, out requestCheckErrMsg);
                    //設定Authenticate參數
                    iBonAuthObj = new iBonAuthenticate()
                    {
                        Input_KeyLabel   = request.Input_KeyLabel,
                        Input_KeyVersion = request.Input_KeyVersion,
                        Input_UID        = request.Input_UID,
                        Input_Enc_RanB   = request.Input_Enc_RanB
                    };
                    log.Debug(m => m("開始執行Authenticate"));
                    iBonAuthObj.StartAuthenticate(true);//會傳送數據到KMS並取回DiverseKey後做運算並將結果寫入Output屬性中

                    //回應資料設定
                    response = new EskmsPOCO()
                    {
                        Input_KeyLabel             = request.Input_KeyLabel,
                        Input_KeyVersion           = request.Input_KeyVersion,
                        Input_UID                  = request.Input_UID,
                        Input_Enc_RanB             = request.Input_Enc_RanB,
                        Output_RanB                = iBonAuthObj.Output_RanB,
                        Output_Enc_RanAandRanBRol8 = iBonAuthObj.Output_Enc_RanAandRanBRol8,
                        Output_Enc_IVandRanARol8   = iBonAuthObj.Output_Enc_IVandRanARol8,
                        Output_RandAStartIndex     = iBonAuthObj.Output_RandAStartIndex,
                        Output_SessionKey          = iBonAuthObj.Output_SessionKey
                    };
                    responseJsonStr = JsonConvert.SerializeObject(response);
                    responseBytes   = Encoding.UTF8.GetBytes(responseJsonStr);
                    log.Debug(m => m("[{0}] Response:{1}", this.GetType().Name, responseJsonStr));
                    sendCount = absClientRequestHandler.ClientSocket.Send(responseBytes);
                    if (sendCount != responseBytes.Length)
                    {
                        log.Error(m => m("異常:送出資料(length:{0}不等於原始資料(length:{1}))", sendCount, responseBytes.Length));
                    }
                    log.Debug(m => m("[{0}] Response End", this.GetType().Name));
                }
            }
            catch (ArgumentOutOfRangeException ex)
            {
                log.Error(m => m("資料檢核失敗:{0}", ex.ToString()));
            }
            catch (JsonException ex)
            {
                log.Error(m => m("Request(JsonString) Parse Request(Object) Failed:{0}", ex.ToString()));
            }
            catch (Exception ex)
            {
                log.Error(m => m("[{0}] Error:{1} {2}", this.GetType().Name, ex.Message, ex.StackTrace));
            }
            finally
            {
                absClientRequestHandler.ServiceState = new State_Exit();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="absClientRequestHandler"></param>
        public void Handle(AbsClientRequestHandler absClientRequestHandler)
        {
            #region variable
            byte[] receiveBuffer = null;
            int readCount = 0;
            string requestJsonStr = null;
            string outputCmd = null;
            IKMSGetter kmsGetter = null;
            IList<EskmsKeyPOCO> request_list = null;
            IList<EskmsKeyPOCO> response_list = null;
            EskmsKeyPOCO response = null;
            IList<string> UID_Failed_List;
            Stopwatch timer;
            //string requestCheckErrMsg = null;
            string responseJsonStr = null;
            string failed_list = null;
            byte[] responseBytes = null;
            byte[] diversKey = null;
            int sendCount = -1;
            AL_DBModule obDB = null;
            Shipment shipment = null;
            bool hasReaderId = false;
            #endregion

            try
            {
                receiveBuffer = new byte[0x4000];//4k
                readCount = absClientRequestHandler.ClientSocket.Receive(receiveBuffer, SocketFlags.None);
                if (readCount == 0) { return; }
                //command 輸出狀態 TODO...
                else if (readCount == 6 && Encoding.UTF8.GetString(receiveBuffer, 0, readCount).ToLower().Contains("status"))
                {
                    outputCmd = "Hello";
                    receiveBuffer = Encoding.UTF8.GetBytes(outputCmd);
                    absClientRequestHandler.ClientSocket.Send(receiveBuffer);
                    return;
                }
                else
                {
                    log.Debug(m => m(">> {0}: {1}", this.GetType().Name, absClientRequestHandler.ClientNo));
                    //resize buffer
                    Array.Resize(ref receiveBuffer, readCount);
                    //init
                    obDB = new AL_DBModule();
                    shipment = new Shipment();
                    UID_Failed_List = new List<string>();
                    response_list = new List<EskmsKeyPOCO>();
                    timer = new Stopwatch();
                    timer.Start();
                    //casting jsonstring from buffer array
                    requestJsonStr = Encoding.UTF8.GetString(receiveBuffer);
                    log.Debug(m => m("[{0}]Request: {1}", this.GetType().Name, requestJsonStr));
                    request_list = JsonConvert.DeserializeObject<IList<EskmsKeyPOCO>>(requestJsonStr);

                    //檢查Request資料長度(Attribute) //不能檢查  因為沒有DeviceID
                    //poco.CheckLength(true, out requestCheckErrMsg);
                    log.Debug(m => m("1.Open DB Connection"));
                    obDB.OpenConnection();
                    log.Debug(m => m("2.Run DB Command"));
                    foreach (EskmsKeyPOCO request in request_list)
                    {
                        hasReaderId = shipment.Check_ReaderId_FromSAM_D(obDB, request.Input_UID, "21");
                        if (hasReaderId)
                        {
                            //設定Authenticate參數
                            kmsGetter = new KMSGetter()
                            {
                                Input_KeyLabel = request.Input_KeyLabel,
                                Input_KeyVersion = request.Input_KeyVersion,
                                Input_UID = request.Input_UID,
                                Input_DeviceID = request.Input_DeviceID
                            };

                            log.Debug(m => m("3.開始取Divers Key"));
                            diversKey = kmsGetter.GetDiversKey();//會傳送數據到KMS並取回DiverseKey後做運算並將結果寫入Output屬性中
                            if (diversKey == null)
                            {
                                UID_Failed_List.Add(request.Input_UID);
                            }
                            else
                            {
                                //回應資料設定
                                response = new EskmsKeyPOCO()
                                {
                                    Input_KeyLabel = request.Input_KeyLabel,
                                    Input_KeyVersion = request.Input_KeyVersion,
                                    Input_UID = request.Input_UID,
                                    Output_DiversKey = diversKey
                                };
                                response_list.Add(response);
                            }
                        }
                        else
                        {
                            UID_Failed_List.Add(request.Input_UID);
                        }
                    }
                    if (UID_Failed_List.Count > 0)
                    {
                        failed_list = JsonConvert.SerializeObject(UID_Failed_List);
                        log.Debug(m => m("[{0}] 取Key失敗的UID列表:{1}", this.GetType().Name, failed_list));
                    }
                    responseJsonStr = JsonConvert.SerializeObject(response_list);
                    responseBytes = Encoding.UTF8.GetBytes(responseJsonStr);
                    log.Debug(m => m("[{0}] Response:{1}", this.GetType().Name, responseJsonStr));
                    sendCount = absClientRequestHandler.ClientSocket.Send(responseBytes);
                    if (sendCount != responseBytes.Length)
                    {
                        log.Error(m => m("異常:送出資料(length:{0}不等於原始資料(length:{1}))", sendCount, responseBytes.Length));
                    }
                    timer.Stop();
                    log.Debug(m => m("[{0}] Response End TimeSpend:{1}ms", this.GetType().Name, timer.ElapsedMilliseconds));
                }

            }
            catch (ArgumentOutOfRangeException ex)
            {
                log.Error(m => m("資料檢核失敗:{0}", ex.ToString()));
            }
            catch (System.Data.SqlClient.SqlException sqlEx)
            {
                log.Error(m => m("DB Operate Failed:{0}", sqlEx.ToString()));
            }
            catch (JsonException ex)
            {
                log.Error(m => m("Request(JsonString) Parse Request(Object) Failed:{0}", ex.ToString()));
            }
            catch (Exception ex)
            {
                log.Error(m => m("[{0}] Error:{1} {2}", this.GetType().Name, ex.Message, ex.StackTrace));
            }
            finally
            {
                if (obDB != null)
                {
                    obDB.CloseConnection();
                    shipment = null;
                }
                absClientRequestHandler.ServiceState = new State_Exit();
            }
        }
Пример #7
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="absClientRequestHandler"></param>
        public void Handle(AbsClientRequestHandler absClientRequestHandler)
        {
            #region variable
            byte[] receiveBuffer = null;
            int readCount = 0;
            string requestJsonStr = null;
            string outputCmd = null;
            iBonAuthenticate iBonAuthObj = null;
            EskmsPOCO request = null;
            EskmsPOCO response = null;
            string requestCheckErrMsg = null;
            string responseJsonStr = null;
            byte[] responseBytes = null;
            int sendCount = -1;
            #endregion

            try
            {
                receiveBuffer = new byte[0x1000];//4k
                readCount = absClientRequestHandler.ClientSocket.Receive(receiveBuffer, SocketFlags.None);
                if (readCount == 0) { return; }
                    //command 輸出狀態 TODO...
                else if (readCount == 6 && Encoding.UTF8.GetString(receiveBuffer, 0, readCount).ToLower().Contains("status"))
                {
                    outputCmd = "Hello";
                    receiveBuffer = Encoding.UTF8.GetBytes(outputCmd);
                    absClientRequestHandler.ClientSocket.Send(receiveBuffer);
                    return;
                }
                else
                {
                    log.Debug(m => m(">> {0}: {1}", this.GetType().Name, absClientRequestHandler.ClientNo));
                    //resize buffer
                    Array.Resize(ref receiveBuffer, readCount);
                    //casting jsonstring from buffer array
                    requestJsonStr = Encoding.UTF8.GetString(receiveBuffer);
                    log.Debug(m => m("[{0}]Request: {1}", this.GetType().Name, requestJsonStr));
                    request = JsonConvert.DeserializeObject<EskmsPOCO>(requestJsonStr);
                    //檢查Request資料長度(Attribute)
                    request.CheckLength(true, out requestCheckErrMsg);
                    //設定Authenticate參數
                    iBonAuthObj = new iBonAuthenticate()
                    {
                        Input_KeyLabel = request.Input_KeyLabel,
                        Input_KeyVersion = request.Input_KeyVersion,
                        Input_UID = request.Input_UID,
                        Input_Enc_RanB = request.Input_Enc_RanB
                    };
                    log.Debug(m => m("開始執行Authenticate"));
                    iBonAuthObj.StartAuthenticate(true);//會傳送數據到KMS並取回DiverseKey後做運算並將結果寫入Output屬性中

                    //回應資料設定
                    response = new EskmsPOCO()
                    {
                        Input_KeyLabel = request.Input_KeyLabel,
                        Input_KeyVersion = request.Input_KeyVersion,
                        Input_UID = request.Input_UID,
                        Input_Enc_RanB = request.Input_Enc_RanB,
                        Output_RanB = iBonAuthObj.Output_RanB,
                        Output_Enc_RanAandRanBRol8 = iBonAuthObj.Output_Enc_RanAandRanBRol8,
                        Output_Enc_IVandRanARol8 = iBonAuthObj.Output_Enc_IVandRanARol8,
                        Output_RandAStartIndex = iBonAuthObj.Output_RandAStartIndex,
                        Output_SessionKey= iBonAuthObj.Output_SessionKey
                    };
                    responseJsonStr = JsonConvert.SerializeObject(response);
                    responseBytes = Encoding.UTF8.GetBytes(responseJsonStr);
                    log.Debug(m => m("[{0}] Response:{1}", this.GetType().Name, responseJsonStr));
                    sendCount = absClientRequestHandler.ClientSocket.Send(responseBytes);
                    if (sendCount != responseBytes.Length)
                    {
                        log.Error(m => m("異常:送出資料(length:{0}不等於原始資料(length:{1}))", sendCount, responseBytes.Length));
                    }
                    log.Debug(m => m("[{0}] Response End", this.GetType().Name));
                }
            }
            catch (ArgumentOutOfRangeException ex)
            {
                log.Error(m => m("資料檢核失敗:{0}",ex.ToString()));
            }
            catch(JsonException ex)
            {
                log.Error(m => m("Request(JsonString) Parse Request(Object) Failed:{0}", ex.ToString()));
            }
            catch (Exception ex)
            {
                log.Error(m => m("[{0}] Error:{1} {2}", this.GetType().Name, ex.Message, ex.StackTrace));
            }
            finally
            {

                absClientRequestHandler.ServiceState = new State_Exit();
            }
        }
        public void Handle(AbsClientRequestHandler absClientRequestHandler)
        {
            #region variable
            byte[] receiveBuffer = null;
            int readCount = 0;
            string requestJsonStr = null;
            string outputCmd = null;

            EskmsKeyTxLogPOCO_v2 request = null;
            EskmsKeyTxLogPOCO_v2 response = null;
            string requestCheckErrMsg = null;
            string responseJsonStr = null;
            byte[] responseBytes = null;
            int sendCount = -1;
            AL_DBModule obDB = null;
            Shipment shipment = null;
            string txLogReturnCode = String.Empty;
            #endregion

            try
            {
                receiveBuffer = new byte[0x1000];//4k
                readCount = absClientRequestHandler.ClientSocket.Receive(receiveBuffer, SocketFlags.None);
                if (readCount == 0) { return; }
                //command 輸出狀態 TODO...
                else if (readCount == 6 && Encoding.UTF8.GetString(receiveBuffer, 0, readCount).ToLower().Contains("status"))
                {
                    outputCmd = "Hello";
                    receiveBuffer = Encoding.UTF8.GetBytes(outputCmd);
                    absClientRequestHandler.ClientSocket.Send(receiveBuffer);
                    return;
                }
                else
                {
                    log.Debug(m => m(">> {0}: {1}", this.GetType().Name, absClientRequestHandler.ClientNo));
                    //resize buffer
                    Array.Resize(ref receiveBuffer, readCount);
                    //init
                    obDB = new AL_DBModule();
                    shipment = new Shipment();

                    //casting jsonstring from buffer array
                    requestJsonStr = Encoding.UTF8.GetString(receiveBuffer);
                    log.Debug(m => m("[{0}]Request: {1}", this.GetType().Name, requestJsonStr));
                    request = JsonConvert.DeserializeObject<EskmsKeyTxLogPOCO_v2>(requestJsonStr);
                    //檢查Request資料長度(Attribute)
                    //request.CheckLength(true, out requestCheckErrMsg);

                    //回應資料設定
                    response = new EskmsKeyTxLogPOCO_v2()
                    {
                        SAM_UID = request.SAM_UID,
                        DeviceId = request.DeviceId,
                        ReturnCode = "000001",//預設為fail
                    };

                    txLogReturnCode = request.TestTxLog.Substring(16, 8);//取第16~23個字串

                    //簡易判斷:TxLog ReturnCode
                    if (txLogReturnCode.Equals("00000000"))
                    {
                        log.Debug(m => m("1.Open DB Connection"));
                        obDB.OpenConnection();

                        log.Debug(m => m("2.Run Shipment DB Operate SAM_UID:{0}, DeviceId:{1}", request.SAM_UID, request.DeviceId));
                        //若有輸入顧客識別符號和卡機種別則寫入時就依據此資料寫入,若無則使用預設値寫入(在DB_Operate.SQL.Sql_Getter_Reader_D裡)
                        if (!String.IsNullOrEmpty(request.Merc_Flg) && !String.IsNullOrEmpty(request.Reader_Type))
                        {
                            log.Debug(m => m("2-1. 自訂卡機格式與顧客識別[Reader_Type:{0}, Merc_Flg:{1}]", request.Reader_Type, request.Merc_Flg));
                            //執行Reader出貨的DB操作流程
                            shipment.Shipment_Reader(obDB, request.SAM_UID, request.DeviceId, request.Reader_Type, request.Merc_Flg);//
                        }
                        else
                        {
                            log.Debug(m => m("2-2.預設的卡機格式與顧客識別[Reader_Type:{0}, Merc_Flg:{1}]", "03", "ICA"));
                            //執行Reader出貨的DB操作流程
                            shipment.Shipment_Reader(obDB, request.SAM_UID, request.DeviceId);//
                        }
                        response.ReturnCode = "000000";
                        log.Debug(m => m("3.Shipment DB Operate End ..."));
                    }
                    else
                    {
                        log.Error(m => m("UID:{0}  DeviceId:{1}  TxLog ReturnCode異常:{2}",request.SAM_UID, request.DeviceId, txLogReturnCode));
                    }
                    responseJsonStr = JsonConvert.SerializeObject(response);
                    responseBytes = Encoding.UTF8.GetBytes(responseJsonStr);
                    log.Debug(m => m("[{0}] Response:{1}", this.GetType().Name, responseJsonStr));
                    sendCount = absClientRequestHandler.ClientSocket.Send(responseBytes);
                    if (sendCount != responseBytes.Length)
                    {
                        log.Error(m => m("異常:送出資料(length:{0}不等於原始資料(length:{1}))", sendCount, responseBytes.Length));
                    }
                    log.Debug(m => m("[{0}] Response End", this.GetType().Name));
                }

            }
            catch (ArgumentOutOfRangeException ex)
            {
                log.Error(m => m("資料檢核失敗:{0}", ex.ToString()));
            }
            catch (JsonException ex)
            {
                log.Error(m => m("Request(JsonString) Parse Request(Object) Failed:{0}", ex.ToString()));
            }
            catch (Exception ex)
            {
                log.Error(m => m("[{0}] Error:{1} {2}", this.GetType().Name, ex.Message, ex.StackTrace));
            }
            finally
            {
                if (obDB != null && obDB.ALCon != null)
                {
                    obDB.CloseConnection();
                    shipment = null;
                }
                absClientRequestHandler.ServiceState = new State_Exit();
            }
        }