using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;

using Cosmos.BaseFrame;
using Cosmos.UserFrame;
using Cosmos.ServiceProvider;
using Cosmos.Common;

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : SC / HQ IRT Request (SC / 본부 IRT 요청)
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.BackgroundDown
{
    public class NetworkIRT 
    {
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private PosStatus m_cPosStatus;                                             // 기본정보 참조
        private DeviceStatus m_cDevStatus;                                          // 장비정보 참조 
        
        /// <summary>
        /// Sending Socket (전송 소켓)
        /// </summary>
        private static TcpSocket m_IrtSocket = null;

        /// <summary>
        /// IP
        /// </summary>
        private string m_ServerIp = "";

        /// <summary>
        /// Port
        /// </summary>
        private int m_ServerPort = 0;

        /// <summary>
        /// TimeOut
        /// </summary>
        private int m_ServerTimeout = 10000;

        /// ---------------------------------------------------------------------------------------------
        /// <summary>
        /// Constructor (생성자)
        /// </summary>
        /// ---------------------------------------------------------------------------------------------
        public NetworkIRT()
        {
            m_cPosStatus = (PosStatus)StateObject.POS;                              // POS 기본정보
            m_cDevStatus = (DeviceStatus)StateObject.DEVICE;                        // POS 장치정보 (PDA)
        }

        #region OnLine 상태체크
        /// <summary>
        /// Get network online status with Ping check. (Ping체크로 network Online상태를 취득한다,)
        /// </summary>
        /// <param name="pHostAddress">Ip address</param>
        /// <returns>True : online  false : Offline</returns>
        private int CheckNetworkOnline(string pHostAddress)
        {
            //Ping Check (Ping 체크)
            Ping pingSender = new Ping();
            PingOptions optins = new PingOptions();
            optins.DontFragment = true;
            string data = "aaa";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 120;
            PingReply reply = pingSender.Send(pHostAddress, timeout, buffer, optins);

            if (reply.Status == IPStatus.Success)
                return UserCom.OK;
            else
                return UserCom.NG;
        }
        #endregion

        #region 암호화 편집
        /// <summary>
        /// 암호화 위해 전문 편집
        /// </summary>
        /// <param name="plainData">평문</param>
        /// <returns>암호문, 에러시 평문 리턴</returns>
        private byte[] EncryptData(byte[] plainData)
        {
            int nNowPos = 0;

            //통신헤더전문편집
            string[] aCommHead = new string[Column.COMM_HEADER.LEN.Length];
            ItemColumn.ParseMessage(Column.COMM_HEADER.LEN, Column.COMM_HEADER.TYPE, plainData, ref nNowPos, ref aCommHead);

            string sPosNo = aCommHead[Column.COMM_HEADER.SEQ.PosNo].ToString();
            //string sRegNo = aCommHead[Column.COMM_HEADER.SEQ.RegNo].ToString();
            string sRegNo = "";
            string sMsgLen = aCommHead[Column.COMM_HEADER.SEQ.MsgLen].ToString();
            string sEncType = aCommHead[Column.COMM_HEADER.SEQ.EncType].ToString();
            byte[] byteEncryptData = null;
            byte[] editedAllData = null;

            if (sEncType == "0") // "0" 평문
            {
                //Tran헤더 포함 전문 데이터
                byte[] tranBytes = new byte[plainData.Length - nNowPos];

                Array.Copy(plainData, nNowPos, tranBytes, 0, plainData.Length - nNowPos);

                //암호화
                byteEncryptData = AES.DynamicKeyEncrypt(sPosNo, sRegNo, tranBytes);

                int iMsgLen = nNowPos + byteEncryptData.Length;

                aCommHead[Column.COMM_HEADER.SEQ.MsgLen] = iMsgLen.ToString();  //공통헤더와 암호문 더해진 길이
                aCommHead[Column.COMM_HEADER.SEQ.EncType] = "1";                //0:평문 1:암호화

                string sChangedCommHeader = ItemColumn.MakeMessage(aCommHead, Column.COMM_HEADER.LEN, Column.COMM_HEADER.TYPE);
                byte[] byteCommHeader = Encoding.UTF8.GetBytes(sChangedCommHeader);

                editedAllData = new byte[iMsgLen];
                Array.Copy(byteCommHeader, editedAllData, byteCommHeader.Length);
                Array.Copy(byteEncryptData, 0, editedAllData, byteCommHeader.Length, byteEncryptData.Length);

                return editedAllData;
            }
            else // "1" 암호문
            {
                return plainData;
            }

        }

        #endregion

        #region 복호화 편집
        /// <summary>
        /// 복호화 위해 전문 편집
        /// </summary>
        /// <param name="EncryptData"></param>
        /// <returns></returns>
        private byte[] DecryptData(byte[] EncryptData)
        {
            int nNowPos = 0;

            //통신헤더전문편집
            string[] aCommHead = new string[Column.COMM_HEADER.LEN.Length];
            ItemColumn.ParseMessage(Column.COMM_HEADER.LEN, Column.COMM_HEADER.TYPE, EncryptData, ref nNowPos, ref aCommHead);

            string sPosNo = aCommHead[Column.COMM_HEADER.SEQ.PosNo].ToString();
            //string sRegNo = aCommHead[Column.COMM_HEADER.SEQ.RegNo].ToString();
            string sRegNo = "";
            string sMsgLen = aCommHead[Column.COMM_HEADER.SEQ.MsgLen].ToString();
            string sEncType = aCommHead[Column.COMM_HEADER.SEQ.EncType].ToString();
            byte[] bytePlainData = null;
            byte[] editedAllData = null;

            if (sEncType == "1")    //복호화
            {
                //Tran헤더 포함 전문 데이터
                byte[] tranBytes = new byte[EncryptData.Length - nNowPos];

                Array.Copy(EncryptData, nNowPos, tranBytes, 0, tranBytes.Length);

                //복호화
                bytePlainData = AES.DynamicKeyDecrypt(sPosNo, sRegNo, tranBytes);

                int iMsgLen = nNowPos + bytePlainData.Length;

                aCommHead[Column.COMM_HEADER.SEQ.MsgLen] = iMsgLen.ToString();  //공통헤더와 암호문 더해진 길이
                aCommHead[Column.COMM_HEADER.SEQ.EncType] = "0";                //0:평문 1:암호화

                string sChangedCommHeader = ItemColumn.MakeMessage(aCommHead, Column.COMM_HEADER.LEN, Column.COMM_HEADER.TYPE);

                byte[] byteCommHeader = Encoding.Default.GetBytes(sChangedCommHeader);
                editedAllData = new byte[byteCommHeader.Length + bytePlainData.Length];

                Array.Copy(byteCommHeader, editedAllData, byteCommHeader.Length);
                Array.Copy(bytePlainData, 0, editedAllData, byteCommHeader.Length, bytePlainData.Length);

                return editedAllData;

            }
            else
            {
                return EncryptData;
            }

        }

        #endregion

        #region IRT Send/Receive
        /// <summary>
        /// IRT전문의 Send/Receive
        /// </summary>
        /// <param name="pSendData"></param>
        /// <param name="pRecvData"></param>
        /// <returns></returns>
        private int SendReceiveData(byte[] pSendData, ref byte[] pRecvData)
        {
            int nRecvLen = 0;
            int nStat = BaseCom.NG;
            byte[] rRecvData = null;

            try
            {
                m_IrtSocket = new TcpSocket(m_ServerIp, m_ServerPort, m_ServerTimeout);

                // Data Send & Receive
                nRecvLen = m_IrtSocket.SendReceiveData(pSendData, ref rRecvData, m_ServerTimeout);

                if (nRecvLen > 0)
                {
                    pRecvData = rRecvData;
                    nStat = BaseCom.OK;
                }
                else
                {
                    pRecvData = null;
                }
                m_IrtSocket.Close();
            }
            catch (Exception e)
            {
                if (m_IrtSocket != null) m_IrtSocket.Close();
                UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                                    "NetworkIRT.SendReceiveData()", "NetworkIrt Error : ThreadNetworkIrt Exception !!! " + e.ToString());
            }
            finally
            {
            }
            System.Threading.Thread.Sleep(100);
            return nStat;
        }

        /// <summary>
        /// IRT전문의 Send/Receive
        /// </summary>
        /// <param name="pSendData"></param>
        /// <param name="pRecvData"></param>
        /// <returns></returns>
        private int SendReceiveData(string pSendData, ref string pRecvData)
        {
            int nRecvLen = 0;
            int nStat = BaseCom.NG;
            string rRecvData = "";

            try
            {
                m_IrtSocket = new TcpSocket(m_ServerIp, m_ServerPort, m_ServerTimeout);

                // Data Send & Receive
                nRecvLen = m_IrtSocket.SendReceiveData(pSendData, ref rRecvData, m_ServerTimeout);

                if (nRecvLen > 0)
                {
                    pRecvData = rRecvData;
                    nStat = BaseCom.OK;
                }
                else
                {
                    pRecvData = "";
                    nStat = nRecvLen;
                }
                m_IrtSocket.Close();
            }
            catch (Exception e)
            {
                if (m_IrtSocket != null) m_IrtSocket.Close();
                UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                                    "NetworkIRT.SendReceiveData()", "NetworkIrt Error : ThreadNetworkIrt Exception !!! " + e.ToString());
            }
            finally
            {
            }

            System.Threading.Thread.Sleep(100);
            return nStat;
        }
        #endregion

        #region IRT 암호화/복화화 전송
        /// <summary>
        /// IRT String로의 Send/Recv 
        /// </summary>
        /// <param name="sIP"></param>
        /// <param name="iPort"></param>
        /// <param name="iTimeOut"></param>
        /// <param name="sComm"></param>
        /// <param name="sRecvData"></param>
        /// <returns></returns>
        public int IRTSendReceive(string sIP, int iPort, int iTimeOut, string sComm, ref string sRecvData)
        {
            int nStat = 9;
            string sReturn = string.Empty;

            m_ServerIp = sIP;
            m_ServerPort = iPort;
            m_ServerTimeout = iTimeOut;

            byte[] bEditedSendData = null;

            if (m_ServerIp != "" && m_ServerPort != 0)
            {
                //암호화
                if (sComm.Length != 0)
                {
                    //bEditedSendData = EncryptData(Encoding.Default.GetBytes(sComm));
                    bEditedSendData = Encoding.Default.GetBytes(sComm);

                    UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                        "NetworkIRT.IRTSendReceive()", CmUtil.RPadH("[SEND" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + sComm);
                }
                else
                {
                    UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                        "NetworkIRT.IRTSendReceive()", CmUtil.RPadH("[SEND" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] SendData is Null");
                }

                //SendReceive
                nStat = SendReceiveData(Encoding.Default.GetString(bEditedSendData, 0, bEditedSendData.Length), ref sReturn);

                //복호화
                if (sReturn.Length != 0)
                {
                    byte[] byteRecv = DecryptData(Encoding.Default.GetBytes(sReturn));
                    sRecvData = Encoding.Default.GetString(byteRecv, 0, byteRecv.Length);

                    UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                        "NetworkIRT.IRTSendReceive()", CmUtil.RPadH("[RECV" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + sRecvData);
                }
                else
                {
                    UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                        "NetworkIRT.IRTSendReceive()", CmUtil.RPadH("[RECV" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] RecvData Is Null");
                }
            }

            return nStat;
        }

        /// <summary>
        /// IRT Byte로의 Send/Recv 
        /// </summary>
        /// <param name="sIP"></param>
        /// <param name="iPort"></param>
        /// <param name="iTimeOut"></param>
        /// <param name="comm"></param>
        /// <param name="sRecvData"></param>
        /// <returns></returns>
        public int IRTSendReceive(string sIP, int iPort, int iTimeOut, byte[] comm, ref byte[] sRecvData)
        {
            int nStat = 9;
            string sReturn = string.Empty;

            m_ServerIp = sIP;
            m_ServerPort = iPort;
            m_ServerTimeout = iTimeOut;

            byte[] bEditedSendData = null;

            if (m_ServerIp != "" && m_ServerPort != 0)
            {
                //암호화
                if (comm != null)
                {
                    bEditedSendData = EncryptData(comm);

                    string sTmpLog = Encoding.Default.GetString(comm, 0, comm.Length);

                    UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                        "NetworkIRT.IRTSendReceive()", CmUtil.RPadH("[SEND" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + sTmpLog);
                }
                else
                {
                    UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                        "NetworkIRT.IRTSendReceive()", CmUtil.RPadH("[SEND" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] SendData Is Null");
                }

                //SendReceive
                nStat = SendReceiveData(bEditedSendData, ref sRecvData);

                //복호화
                if (sRecvData != null)
                {
                    byte[] sDecryptRecvData = DecryptData(sRecvData);

                    sRecvData = sDecryptRecvData;

                    string sTmpLog = Encoding.Default.GetString(sDecryptRecvData, 0, sDecryptRecvData.Length);

                    UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                        "NetworkIRT.IRTSendReceive()", CmUtil.RPadH("[RECV" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + sTmpLog);
                }
                else
                {
                    UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                        "NetworkIRT.IRTSendReceive()", CmUtil.RPadH("[RECV" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] RecvData Is Null");
                }
            }

            return nStat;
        }
        #endregion 

        #region IRT 전송 - 외부 VAN
        /// <summary>
        /// IRT String로의 Send/Recv  - 외부 Van
        /// </summary>
        /// <param name="sIP"></param>
        /// <param name="iPort"></param>
        /// <param name="iTimeOut"></param>
        /// <param name="sSendData"></param>
        /// <param name="sRecvData"></param>
        /// <param name="nRecvDataLen"></param>
        /// <param name="sAckData"></param>
        /// <returns></returns>
        public int IRTSendReceiveEx(string sIP, int iPort, int iTimeOut, string sSendData, ref string sRecvData, int nRecvDataLen, string sAckData)
        {         
            return IRTSendReceiveEx(sIP, iPort, iTimeOut, sSendData, ref sRecvData, nRecvDataLen, sAckData, false);
        }

        public int IRTSendReceiveEx(string sIP, int iPort, int iTimeOut, string sSendData, ref string sRecvData, int nRecvDataLen, string sAckData, bool bErrorSkip)
        {
            int nStat = BaseCom.NG;
            try
            {
                sRecvData = "";
                if (sIP == "" || iPort == 0) return nStat;

                // Connect 설정
                m_ServerIp = sIP;
                m_ServerPort = iPort;
                m_ServerTimeout = iTimeOut; 

                m_IrtSocket = new TcpSocket(m_ServerIp, m_ServerPort, m_ServerTimeout);

                UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                    "NetworkIRT.IRTSendReceiveEx()", CmUtil.RPadH("[SEND" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + sSendData);

                // Data Send & Receive
                System.Diagnostics.Debug.Print("요청=" + sSendData);
                nStat = m_IrtSocket.SendReceiveData(sSendData, ref sRecvData, m_ServerTimeout, nRecvDataLen, sAckData, bErrorSkip);
                System.Diagnostics.Debug.Print("응답=" + sRecvData);

                if (nStat > 0) nStat = BaseCom.OK;
        
                m_IrtSocket.Close();

                UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                    "NetworkIRT.IRTSendReceiveSEx()", CmUtil.RPadH("[RECV" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + sRecvData);
            }
            catch (Exception e)
            {
                if (m_IrtSocket != null) m_IrtSocket.Close();
                UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                                    "NetworkIRT.SendReceiveDataEx()", "NetworkIrt Error : ThreadNetworkIrt Exception !!! " + e.ToString());
            }

            System.Threading.Thread.Sleep(100);
            return nStat;
        }

        /// <summary>
        /// IRT Byte로의 Send/Recv  - 외부 Van
        /// </summary>
        /// <param name="sIP"></param>
        /// <param name="iPort"></param>
        /// <param name="iTimeOut"></param>
        /// <param name="bSendData"></param>
        /// <param name="bRecvData"></param>
        /// <param name="nRecvDataLen"></param>
        /// <param name="sAckData"></param>
        /// <returns></returns>
        public int IRTSendReceiveEx(string sIP, int iPort, int iTimeOut, byte[] bSendData, ref byte[] bRecvData, int nRecvDataLen, byte[] sAckData)
        {
            int nStat = BaseCom.NG;
            try
            {
                bRecvData[0] = 0;
                if (sIP == "" || iPort == 0) return nStat;

                // Connect 설정
                m_ServerIp = sIP;
                m_ServerPort = iPort;
                m_ServerTimeout = iTimeOut;
                m_IrtSocket = new TcpSocket(m_ServerIp, m_ServerPort, m_ServerTimeout);

                UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                    "NetworkIRT.IRTSendReceiveEx()", CmUtil.RPadH("[SEND" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + Encoding.Default.GetString(bSendData, 0, bSendData.Length));

                // Data Send & Receive
                System.Diagnostics.Debug.Print("요청=" + Encoding.Default.GetString(bSendData, 0, bSendData.Length));
                nStat = m_IrtSocket.SendReceiveData(bSendData, ref bRecvData, m_ServerTimeout, nRecvDataLen, sAckData);
                System.Diagnostics.Debug.Print("응답=" + Encoding.Default.GetString(bRecvData, 0, bRecvData.Length));
                if (nStat > 0) nStat = BaseCom.OK;

                m_IrtSocket.Close();

                UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                    "NetworkIRT.IRTSendReceiveEx()", CmUtil.RPadH("[RECV" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + Encoding.Default.GetString(bRecvData, 0, bRecvData.Length));
            }
            catch (Exception e)
            {
                if (m_IrtSocket != null) m_IrtSocket.Close();
                UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                                    "NetworkIRT.SendReceiveDataEx()", "NetworkIrt Error : ThreadNetworkIrt Exception !!! " + e.ToString());
            }
            return nStat;
        } 
        #endregion 

        #region IRT 전송 - 외부 VAN (전문길이 위치지정)

        /// <summary>
        /// IRT String로의 Send/Recv  - 외부 Van (길이 위치지정)
        /// </summary>
        /// <param name="sIP"></param>
        /// <param name="iPort"></param>
        /// <param name="iTimeOut"></param>
        /// <param name="sSendData"></param>
        /// <param name="sRecvData"></param>
        /// <param name="bFullSize"></param>
        /// <param name="nRecvDataLen"></param>
        /// <param name="nRecvDataStartPosion"></param>
        /// <returns></returns>
        public int IRTSendReceiveLen(string sIP, int iPort, int iTimeOut, string sSendData, ref string sRecvData, bool bFullSize, int nRecvDataLen, int nRecvDataStartPosion)
        {
            int nStat = BaseCom.NG;
            try
            {
                sRecvData = "";
                if (sIP == "" || iPort == 0) return nStat;

                // Connect 설정
                m_ServerIp = sIP;
                m_ServerPort = iPort;
                m_ServerTimeout = iTimeOut;

                m_IrtSocket = new TcpSocket(m_ServerIp, m_ServerPort, m_ServerTimeout);

                UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                    "NetworkIRT.IRTSendReceiveLen()", CmUtil.RPadH("[SEND" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + sSendData);

                // Data Send & Receive
                System.Diagnostics.Debug.Print("요청=" + sSendData);
                nStat = m_IrtSocket.SendReceiveData(sSendData, ref sRecvData, m_ServerTimeout, bFullSize, nRecvDataLen, "", nRecvDataStartPosion, false );
                System.Diagnostics.Debug.Print("응답=" + sRecvData);

                if (nStat > 0) nStat = BaseCom.OK;

                m_IrtSocket.Close();

                UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                    "NetworkIRT.IRTSendReceiveLen()", CmUtil.RPadH("[RECV" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + sRecvData);
            }
            catch (Exception e)
            {
                if (m_IrtSocket != null) m_IrtSocket.Close();
                UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                                    "NetworkIRT.SendReceiveDataEx()", "NetworkIrt Error : ThreadNetworkIrt Exception !!! " + e.ToString());
            }

            System.Threading.Thread.Sleep(100);
            return nStat;
        }

        /// <summary>
        /// IRT Byte로의 Send/Recv  - 외부 Van (전문길이 위치지정)
        /// </summary>
        /// <param name="sIP"></param>
        /// <param name="iPort"></param>
        /// <param name="iTimeOut"></param>
        /// <param name="bSendData"></param>
        /// <param name="bRecvData"></param>
        /// <param name="nRecvDataLen"></param>
        /// <param name="sAckData"></param>
        /// <returns></returns>
        public int IRTSendReceiveLen(string sIP, int iPort, int iTimeOut, byte[] bSendData, ref byte[] bRecvData, int nRecvDataLen, int nRecvDataStartPosion)
        {
            int nStat = BaseCom.NG;
            try
            {
                bRecvData[0] = 0;
                if (sIP == "" || iPort == 0) return nStat;

                // Connect 설정
                m_ServerIp = sIP;
                m_ServerPort = iPort;
                m_ServerTimeout = iTimeOut;
                m_IrtSocket = new TcpSocket(m_ServerIp, m_ServerPort, m_ServerTimeout);

                UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                    "NetworkIRT.IRTSendReceiveLen()", CmUtil.RPadH("[SEND" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + Encoding.Default.GetString(bSendData, 0, bSendData.Length));

                // Data Send & Receive
                System.Diagnostics.Debug.Print("요청=" + Encoding.Default.GetString(bSendData, 0, bSendData.Length));
                nStat = m_IrtSocket.SendReceiveData(bSendData, ref bRecvData, m_ServerTimeout, nRecvDataLen, null, nRecvDataStartPosion);
                System.Diagnostics.Debug.Print("응답=" + Encoding.Default.GetString(bRecvData, 0, bRecvData.Length));
                if (nStat > 0) nStat = BaseCom.OK;

                m_IrtSocket.Close();

                UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                    "NetworkIRT.IRTSendReceiveLen()", CmUtil.RPadH("[RECV" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + Encoding.Default.GetString(bRecvData, 0, bRecvData.Length));
            }
            catch (Exception e)
            {
                if (m_IrtSocket != null) m_IrtSocket.Close();
                UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                                    "NetworkIRT.SendReceiveDataLen()", "NetworkIrt Error : ThreadNetworkIrt Exception !!! " + e.ToString());
            }
            return nStat;
        }
        #endregion

        #region IRT Byte로의 Send/Recv  - 외부 Van (Bypass Byte)
        /// <summary>
        /// IRT Byte로의 Send/Recv  - 외부 Van (Bypass Byte)
        /// </summary>
        /// <param name="sIP"></param>
        /// <param name="iPort"></param>
        /// <param name="iTimeOut"></param>
        /// <param name="bSendData"></param>
        /// <param name="bRecvData"></param>
        /// <param name="nRecvDataLen"></param>
        /// <returns></returns>
        public int ByPassSendReceiveData(string sIP, int iPort, int iTimeOut, byte[] bSendData, ref byte[] bRecvData, int nRecvDataLen) 
        {
            int nStat = BaseCom.NG;
            try
            {
                //bRecvData[0] = 0;
                if (sIP == "" || iPort == 0) return nStat;

                // Connect 설정
                m_ServerIp = sIP;
                m_ServerPort = iPort;
                m_ServerTimeout = iTimeOut;
                m_IrtSocket = new TcpSocket(m_ServerIp, m_ServerPort, m_ServerTimeout);

                UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                    "NetworkIRT.IRTSendReceiveLen()", CmUtil.RPadH("[SEND" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + Encoding.Default.GetString(bSendData, 0, bSendData.Length));

                // Data Send & Receive
                System.Diagnostics.Debug.Print("요청=" + Encoding.Default.GetString(bSendData, 0, bSendData.Length));
                nStat = m_IrtSocket.ByPassSendReceiveData(bSendData, nRecvDataLen, ref bRecvData, m_ServerTimeout);
                System.Diagnostics.Debug.Print("응답=" + Encoding.Default.GetString(bRecvData, 0, bRecvData.Length));
                if (nStat > 0) nStat = BaseCom.OK;

                m_IrtSocket.Close();

                UserLog.WriteLogFile(UserCom.LOG_SOCK, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (Project Name (프로젝트명))
                                    "NetworkIRT.IRTSendReceiveLen()", CmUtil.RPadH("[RECV" + ":" + m_ServerIp + ":" + m_ServerPort, 27) + "] " + Encoding.Default.GetString(bRecvData, 0, bRecvData.Length));
            }
            catch (Exception e)
            {
                if (m_IrtSocket != null) m_IrtSocket.Close();
                UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                                    "NetworkIRT.SendReceiveDataLen()", "NetworkIrt Error : ThreadNetworkIrt Exception !!! " + e.ToString());
            }
            return nStat;
        }
        #endregion 
    }
}
