using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using SystemX.Common.Archive;
using SystemX.Common.Serialization;
using SystemX.Net.BaseProtocol;
using static SystemX.Net.Platform.Common.Util.LogMessage;

namespace SystemX.Common
{
    public static partial class XCommons
    {
        public static byte[] SpecialObjectToByteStream(BASE_PROTOCOL SET_PROTOCOL, object refObj)
        {
            byte[] ucGetStreamBytes = new byte[1];

            try
            {
                try
                {
                    int iSize = Marshal.SizeOf(refObj);
                    byte[] SendBuffer = SystemXNetSerialization.RawSerialize(refObj, Marshal.SizeOf(refObj));

                    var memoryStream = new MemoryStream(SendBuffer);
                    byte[] getSendCompressFileByte = new byte[1];
                    getSendCompressFileByte = XDataArchive.CompressDeflateByteToByte(SendBuffer);
                    iSize = getSendCompressFileByte.Count();

                    HEADER_PACKET setHeader = new HEADER_PACKET();
                    int iHeaderSize = Marshal.SizeOf(setHeader);
                    byte[] ucHeaderMake = new byte[iHeaderSize];
                    setHeader = (HEADER_PACKET)SystemXNetSerialization.RawDeSerialize(ucHeaderMake, setHeader.GetType());
                    iHeaderSize = Marshal.SizeOf(setHeader);

                    setHeader.usCommand = SET_PROTOCOL.COMMAND;
                    setHeader.usSubCommand = SET_PROTOCOL.SUB_COMMAND;
                    setHeader.uiOptionCommand = SET_PROTOCOL.OPTION_COMMAND;

                    setHeader.uiHeaderLength = (uint)iHeaderSize;
                    setHeader.uiDataLength = (uint)iSize;

                    setHeader.objOptionName[0].Data = "-";
                    setHeader.objOptionExtension[0].Data = "-";

                    setHeader.uiSourDataNum = 0;
                    setHeader.uiDestDataNum = 0;

                    byte[] SendHeadBuffer = SystemXNetSerialization.RawSerialize(setHeader, Marshal.SizeOf(setHeader));

                    object getPacket = SystemXNetSerialization.SelectPacket(setHeader.uiDataLength);
                    if (getPacket == null) throw new Exception("SystemXNetSerialization.SelectPacket Failed.");
                    int iBodySize = Marshal.SizeOf(getPacket);
                    byte[] ucBodyArray = new byte[iBodySize];
                    getPacket = Convert.ChangeType(SystemXNetSerialization.RawDeSerialize(ucBodyArray, getPacket.GetType()), getPacket.GetType());
                    Array.Copy(getSendCompressFileByte, (byte[])getPacket.GetType().GetMethod("Data").Invoke(getPacket, null), getSendCompressFileByte.Count());

                    setHeader.uiBodyLength = (uint)iBodySize;
                    setHeader.uiPacketLength = (uint)(iHeaderSize + iBodySize);

                    byte[] SendBodyBuffer = SystemXNetSerialization.RawSerialize(getPacket, Marshal.SizeOf(getPacket));

                    ucGetStreamBytes = HeadTailMake(setHeader.uiPacketLength, SendHeadBuffer, SendBodyBuffer);

                    //string strMessage = Encoding.Unicode.GetString(ucGetFileBytes, 0, ucGetFileBytes.Length) + "\r\n";
                }
                catch (Exception e)
                {
                    //throw new CommonPacketMarshalException("General packet marshalling failed.[SystemX.Common : SystemXNetCommons.UserObjectToByteStream]");
                    MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"General packet marshalling failed.[SystemX.Common : SystemXNetCommons.UserObjectToByteStream]\r\n" + e.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG);
                }
            }
            finally
            {
                ;//Until unused
            }

            return ucGetStreamBytes;
        }
        public static byte[] SetSimpleResponsPacket(BASE_PROTOCOL SET_PROTOCOL, bool bState)
        {
            byte[] ucGetStreamBytes = new byte[1];

            try
            {
                try
                {
                    if (SET_PROTOCOL.GET_CURRENT_PROTOCOL == BASE_PROTOCOL.PROTOCOL_CODE.SIMPLE_RESPONSE)
                    {
                        ;//
                    }
                    else if (SET_PROTOCOL.GET_CURRENT_PROTOCOL == BASE_PROTOCOL.PROTOCOL_CODE.RAW_END)
                    {
                        ;//
                    }
                    else
                        return null;

                    var bytes = new byte[1];

                    HEADER_PACKET setHeader = new HEADER_PACKET();
                    int iHeaderSize = Marshal.SizeOf(setHeader);
                    byte[] ucHeaderMake = new byte[iHeaderSize];
                    setHeader = (HEADER_PACKET)SystemXNetSerialization.RawDeSerialize(ucHeaderMake, setHeader.GetType());
                    iHeaderSize = Marshal.SizeOf(setHeader);

                    setHeader.usCommand = SET_PROTOCOL.COMMAND;
                    setHeader.usSubCommand = SET_PROTOCOL.SUB_COMMAND;
                    setHeader.uiOptionCommand = SET_PROTOCOL.OPTION_COMMAND;

                    setHeader.bResponsState = bState;
                    setHeader.usHeader = 1;
                    setHeader.uiHeaderLength = (uint)iHeaderSize;
                    setHeader.uiDataLength = 512;

                    setHeader.objOptionName[0].Data = "-";
                    setHeader.objOptionExtension[0].Data = "-";

                    setHeader.uiSourDataNum = 0;
                    setHeader.uiDestDataNum = 0;

                    object getPacket = SystemXNetSerialization.SelectPacket(512);
                    if (getPacket == null) throw new Exception("SystemXNetSerialization.SelectPacket Failed.");
                    int iBodySize = Marshal.SizeOf(getPacket);
                    byte[] ucBodyArray = new byte[iBodySize];
                    getPacket = Convert.ChangeType(SystemXNetSerialization.RawDeSerialize(ucBodyArray, getPacket.GetType()), getPacket.GetType());

                    setHeader.uiBodyLength = (uint)iBodySize;
                    setHeader.uiPacketLength = (uint)(iHeaderSize + iBodySize);

                    byte[] SendHeadBuffer = SystemXNetSerialization.RawSerialize(setHeader, Marshal.SizeOf(setHeader));
                    byte[] SendBodyBuffer = SystemXNetSerialization.RawSerialize(getPacket, Marshal.SizeOf(getPacket));

                    ucGetStreamBytes = HeadTailMake(setHeader.uiPacketLength, SendHeadBuffer, SendBodyBuffer);

                    //string strMessage = Encoding.Unicode.GetString(ucGetFileBytes, 0, ucGetFileBytes.Length) + "\r\n";
                }
                catch (Exception e)
                {
                    //throw new CommonPacketMarshalException("General packet marshalling failed.[SystemX.Common : SystemXNetCommons.SimpleObjectToByteStream]");
                    MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"General packet marshalling failed.[SystemX.Common : SystemXNetCommons.SimpleObjectToByteStream]\r\n" + e.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG);
                }
            }
            finally
            {
                ;//Until unused
            }

            return ucGetStreamBytes;
        }
        public static byte[] ObjectToByteStream(BASE_PROTOCOL SET_PROTOCOL, object refObj, CP_PACKET? getCPPacket = null,
            ushort usPalletIndex = 0, int iRecordsAffected = 1, bool bHasRows = true, int iFieldCount = 1, params string[] strParameters)
        {
            byte[] ucGetStreamBytes = new byte[1];

            try
            {
                try
                {
                    string refObjGetPos = string.Empty;

                    string strGetFileName = string.Empty;
                    string strGetExtension = string.Empty;

                    MemoryStream ms = null;

                    //
                    SqlDataReader sqlReader = null;

                    DataSet ds = new DataSet();
                    DataTable dt = new DataTable();

                    bool bCheckPos = true;
                    bool bResponsSetState = true;
                    if (SET_PROTOCOL.GET_CURRENT_PROTOCOL == BASE_PROTOCOL.PROTOCOL_CODE.FILE_TRANSFER)
                    {
                        if (refObj is MemoryStream)
                        {
                            ms = (MemoryStream)refObj;

                            if (ms == null)
                            {
                                bCheckPos = false;

                                //throw new Exception("MemoryStream not initialize.. " + refObjGetPos);
                                MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"MemoryStream not initialize." + refObjGetPos + " [SystemX.Common : SystemXNetCommons.ObjectToByteStream]", ConsoleColor.Yellow, LogMessageLevel.DEBUG);
                            }
                        }
                        else
                        {
                            refObjGetPos = (string)refObj;

                            if (File.Exists(@refObjGetPos) == false)
                            {
                                bCheckPos = false;

                                //throw new Exception("The file cannot be found. " + refObjGetPos);
                                MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"The file cannot be found." + refObjGetPos + " [SystemX.Common : SystemXNetCommons.ObjectToByteStream]", ConsoleColor.Yellow, LogMessageLevel.DEBUG);
                            }
                            else
                            {
                                strGetFileName = Path.GetFileNameWithoutExtension(@refObjGetPos);
                                strGetExtension = Path.GetExtension(@refObjGetPos);
                            }
                        }
                    }
                    else if (SET_PROTOCOL.GET_CURRENT_PROTOCOL == BASE_PROTOCOL.PROTOCOL_CODE.DATASET_TRANSEFER)
                    {
                        if (refObj is SqlDataReader)
                        {
                            sqlReader = (SqlDataReader)refObj;

                            bResponsSetState = false;

                            if (sqlReader != null)
                            {
                                if (sqlReader.RecordsAffected > 0)
                                    bResponsSetState = true;
                                else if (sqlReader.HasRows == true)
                                    bResponsSetState = true;
                                else if (sqlReader.FieldCount > 0)
                                    bResponsSetState = true;
                                else
                                    bResponsSetState = false;

                                dt.Load(sqlReader);
                                ds.Tables.Add(dt);
                            }
                        }
                        else if (refObj is DataTable)
                        {
                            dt = (DataTable)refObj;

                            bResponsSetState = false;

                            if (dt != null)
                            {
                                if (iRecordsAffected > 0)
                                    bResponsSetState = true;
                                else if (bHasRows == true)
                                    bResponsSetState = true;
                                else if (iFieldCount > 0)
                                    bResponsSetState = true;
                                else
                                    bResponsSetState = false;

                                ds.Tables.Add(dt);
                            }
                        }
                        else if (refObj is DataSet)
                        {
                            ds = (DataSet)refObj;

                            bResponsSetState = false;

                            if (ds != null)
                            {
                                if (iRecordsAffected > 0)
                                    bResponsSetState = true;
                                else if (bHasRows == true)
                                    bResponsSetState = true;
                                else if (iFieldCount > 0)
                                    bResponsSetState = true;
                                else
                                    bResponsSetState = false;
                            }
                        }
                        else
                        {
                            ;//
                        }
                    }
                    if (bCheckPos)
                    {
                        var bytes = new byte[1];

                        if (SET_PROTOCOL.GET_CURRENT_PROTOCOL == BASE_PROTOCOL.PROTOCOL_CODE.FILE_TRANSFER)
                        {
                            if (ms != null)
                                bytes = ms.ToArray();
                            else
                                bytes = File.ReadAllBytes(@refObjGetPos);
                        }
                        else if (SET_PROTOCOL.GET_CURRENT_PROTOCOL == BASE_PROTOCOL.PROTOCOL_CODE.DATASET_TRANSEFER)
                            bytes = XDataArchive.CompressDatasetToByte(ds);

                        var memoryStream = new MemoryStream(bytes);
                        byte[] getSendOrgFileByte = memoryStream.ToArray();
                        byte[] getSendCompressFileByte = new byte[1];

                        if (SET_PROTOCOL.GET_CURRENT_PROTOCOL == BASE_PROTOCOL.PROTOCOL_CODE.FILE_TRANSFER)
                            getSendCompressFileByte = XDataArchive.CompressDeflateByteToByte(getSendOrgFileByte);
                        else if (SET_PROTOCOL.GET_CURRENT_PROTOCOL == BASE_PROTOCOL.PROTOCOL_CODE.DATASET_TRANSEFER)
                            getSendCompressFileByte = memoryStream.ToArray();

                        HEADER_PACKET setHeader = new HEADER_PACKET();
                        int iHeaderSize = Marshal.SizeOf(setHeader);
                        byte[] ucHeaderMake = new byte[iHeaderSize];
                        setHeader = (HEADER_PACKET)SystemXNetSerialization.RawDeSerialize(ucHeaderMake, setHeader.GetType());
                        iHeaderSize = Marshal.SizeOf(setHeader);

                        //Various Parameter
                        int nParamPos = 0;
                        foreach (string strParam in strParameters)
                        {
                            switch (nParamPos)
                            {
                                case 0: setHeader.objVarParam1[0].Data = strParam; break;
                                case 1: setHeader.objVarParam2[0].Data = strParam; break;
                                case 2: setHeader.objVarParam3[0].Data = strParam; break;
                            }

                            nParamPos++;
                        }

                        setHeader.bResponsState = bResponsSetState;

                        setHeader.usPalletIndex = usPalletIndex;

                        setHeader.usCommand = SET_PROTOCOL.COMMAND;
                        setHeader.usSubCommand = SET_PROTOCOL.SUB_COMMAND;
                        setHeader.uiOptionCommand = SET_PROTOCOL.OPTION_COMMAND;

                        setHeader.uiHeaderLength = (uint)iHeaderSize;
                        setHeader.uiDataLength = (uint)getSendCompressFileByte.Count();

                        setHeader.objOptionName[0].Data = "-";
                        setHeader.objOptionExtension[0].Data = "-";

                        if (getCPPacket == null)
                        {
                            setHeader.objCP_Packet[0].objStationName[0].Data = "-";
                            setHeader.objCP_Packet[0].objProdNo_P[0].Data = "-";
                            setHeader.objCP_Packet[0].objProdNo_C[0].Data = "-";
                            setHeader.objCP_Packet[0].objTestType[0].Data = "-";
                            setHeader.objCP_Packet[0].objTestCode[0].Data = "-";
                            setHeader.objCP_Packet[0].objVersion[0].Data = "-";
                            setHeader.objCP_Packet[0].objProdCode[0].Data = "-";
                            setHeader.objCP_Packet[0].uiTestListNo = 0;
                        }
                        else
                        {
                            setHeader.objCP_Packet[0].objStationName[0].Data = getCPPacket.Value.objStationName[0].Data;
                            setHeader.objCP_Packet[0].objProdNo_P[0].Data = getCPPacket.Value.objProdNo_P[0].Data;
                            setHeader.objCP_Packet[0].objProdNo_C[0].Data = getCPPacket.Value.objProdNo_C[0].Data;
                            setHeader.objCP_Packet[0].objTestType[0].Data = getCPPacket.Value.objTestType[0].Data;
                            setHeader.objCP_Packet[0].objTestCode[0].Data = getCPPacket.Value.objTestCode[0].Data;
                            setHeader.objCP_Packet[0].objVersion[0].Data = getCPPacket.Value.objVersion[0].Data;
                            setHeader.objCP_Packet[0].objProdCode[0].Data = getCPPacket.Value.objProdCode[0].Data;
                            setHeader.objCP_Packet[0].uiTestListNo = getCPPacket.Value.uiTestListNo;
                        }

                        if (SET_PROTOCOL.GET_CURRENT_PROTOCOL == BASE_PROTOCOL.PROTOCOL_CODE.FILE_TRANSFER)
                        {
                            if (strGetFileName != string.Empty)
                            {
                                setHeader.objOptionName[0].Data = strGetFileName;
                                setHeader.objOptionExtension[0].Data = strGetExtension;
                            }
                        }

                        setHeader.uiSourDataNum = 0;
                        setHeader.uiDestDataNum = 0;

                        object getPacket = SystemXNetSerialization.SelectPacket(setHeader.uiDataLength);
                        if (getPacket == null) throw new Exception("SystemXNetSerialization.SelectPacket Failed.");
                        int iBodySize = Marshal.SizeOf(getPacket);
                        byte[] ucBodyArray = new byte[iBodySize];
                        getPacket = Convert.ChangeType(SystemXNetSerialization.RawDeSerialize(ucBodyArray, getPacket.GetType()), getPacket.GetType());
                        Array.Copy(getSendCompressFileByte, (byte[])getPacket.GetType().GetMethod("Data").Invoke(getPacket, null), getSendCompressFileByte.Count());

                        setHeader.uiBodyLength = (uint)iBodySize;
                        setHeader.uiPacketLength = (uint)(iHeaderSize + iBodySize);

                        byte[] SendHeadBuffer = SystemXNetSerialization.RawSerialize(setHeader, Marshal.SizeOf(setHeader));

                        byte[] SendBodyBuffer = SystemXNetSerialization.RawSerialize(getPacket, Marshal.SizeOf(getPacket));

                        ucGetStreamBytes = HeadTailMake(setHeader.uiPacketLength, SendHeadBuffer, SendBodyBuffer);

                        //string strMessage = Encoding.Unicode.GetString(ucGetFileBytes, 0, ucGetFileBytes.Length) + "\r\n";
                    }
                }
                catch (Exception e)
                {
                    //throw new CommonPacketMarshalException("General packet marshalling failed.[SystemX.Common : SystemXNetCommons.ObjectToByteStream]");
                    MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"General packet marshalling failed.[SystemX.Common : SystemXNetCommons.ObjectToByteStream]\r\n" + e.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG);
                }
            }
            finally
            {
                ;//Until unused
            }

            return ucGetStreamBytes;
        }
        public static List<byte[]> ObjectToByteStreamList(BASE_PROTOCOL SET_PROTOCOL, object refObj, CP_PACKET? getCPPacket = null,
            ushort usPalletIdx = 0, params string[] strParameters)
        {
            List<byte[]> listStreamRawData = new List<byte[]>();

            byte[] ucGetStreamBytes = new byte[1];

            try
            {
                try
                {
                    string refObjGetPos = string.Empty;

                    string strGetFileName = string.Empty;
                    string strGetExtension = string.Empty;

                    bool bCheckPos = true;

                    // 1 = File, 2 = DataSet
                    int iSelectType = 0;
                    if (refObj is string)
                    {
                        refObjGetPos = (string)refObj;

                        if (File.Exists(@refObjGetPos) == false)
                        {
                            bCheckPos = false;

                            //throw new Exception("The file cannot be found. " + refObjGetPos);
                            MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"The file cannot be found. " + refObjGetPos + " [SystemX.Common : SystemXNetCommons.ObjectToByteStreamList]", ConsoleColor.Yellow, LogMessageLevel.DEBUG);

                        }
                        else
                        {
                            strGetFileName = Path.GetFileNameWithoutExtension(@refObjGetPos);
                            strGetExtension = Path.GetExtension(@refObjGetPos);

                            iSelectType = 1;
                        }
                    }
                    else if (refObj is DataSet)
                    {
                        strGetFileName = "";
                        strGetExtension = "";

                        iSelectType = 2;
                    }
                    else
                        bCheckPos = false;

                    if (bCheckPos)
                    {
                        var bytes = new byte[1];

                        switch (iSelectType)
                        {
                            case 1:
                                bytes = File.ReadAllBytes(@refObjGetPos);
                                break;
                            case 2:
                                bytes = XDataArchive.CompressDatasetToByte(refObj as DataSet);
                                break;
                        }
                        /*
                        try
                        {
                            Bitmap xBitmap = new Bitmap(@refObjGetPos);

                            using (var stream = new MemoryStream())
                            {
                                stream.Seek(0, SeekOrigin.Begin);
                                xBitmap.Save(stream, xBitmap.RawFormat);
                                bytes = stream.ToArray();
                            }

                            //ImageConverter _imageConverter = new ImageConverter();
                            //bytes = (byte[])_imageConverter.ConvertTo(xBitmap, typeof(byte[]));
                        }
                        catch (Exception e)
                        {
                            bytes = File.ReadAllBytes(@refObjGetPos);
                        }
                        */

                        var memoryStream = new MemoryStream(bytes);
                        //moryStream.Seek(0, SeekOrigin.Begin);

                        //byte[] getSendOrgFileByte = memoryStream.ToArray();
                        //byte[] getSendCompressFileByte = new byte[1];
                        //getSendCompressFileByte = XNetArchive.CompressGZipByteToByte(getSendOrgFileByte);
                        //getSendCompressFileByte = bytes;

                        if (iSelectType == 1)
                        {
                            byte[] getSendCompressFileByte = XDataArchive.CompressGZipByteToByte(memoryStream.ToArray());
                            memoryStream = new MemoryStream(getSendCompressFileByte);
                        }

                        memoryStream.Seek(0, SeekOrigin.Begin);

                        /*
                        if (memoryStream.Length < SystemXNetSerialization.MAX_PACKET_SIZE)
                        {
                            //throw new Exception("The file use normal send mode. " + refObjGetPos);
                            MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"The file use normal send mode. " + refObjGetPos + " [SystemX.Common : SystemXNetCommons.ObjectToByteStreamList]", ConsoleColor.Yellow, LogMessageLevel.DEBUG);
                        }
                        */

                        int iFileSize = (int)memoryStream.Length;
                        int iRemainSize = (int)memoryStream.Length;

                        uint uiMakeCnt = (uint)(Math.Ceiling((double)((double)iFileSize / (double)SystemXNetSerialization.MAX_PACKET_SIZE)));

                        int iSendPos = 0;

                        for (uint i = 0; i < uiMakeCnt; i++)
                        {
                            byte[] ucSendData = null;

                            if (iRemainSize >= SystemXNetSerialization.MAX_PACKET_SIZE)
                            {
                                ucSendData = new byte[SystemXNetSerialization.MAX_PACKET_SIZE];

                                int iRead = memoryStream.Read(ucSendData, 0, SystemXNetSerialization.MAX_PACKET_SIZE);

                                //Array.Copy(getSendCompressFileByte, iSendPos, ucSendData, 0, SystemXNetSerialization.MAX_PACKET_SIZE);

                                iSendPos += SystemXNetSerialization.MAX_PACKET_SIZE;
                                iRemainSize -= SystemXNetSerialization.MAX_PACKET_SIZE;
                            }
                            else
                            {
                                ucSendData = new byte[iRemainSize];

                                int iRead = memoryStream.Read(ucSendData, 0, iRemainSize);

                                //Array.Copy(getSendCompressFileByte, iSendPos, ucSendData, 0, iRemainSize);

                                iSendPos += iRemainSize;
                                iRemainSize -= iRemainSize;
                            }

                            HEADER_PACKET setHeader = new HEADER_PACKET();
                            int iHeaderSize = Marshal.SizeOf(setHeader);
                            byte[] ucHeaderMake = new byte[iHeaderSize];
                            setHeader = (HEADER_PACKET)SystemXNetSerialization.RawDeSerialize(ucHeaderMake, setHeader.GetType());
                            iHeaderSize = Marshal.SizeOf(setHeader);

                            //Various Parameter
                            int nParamPos = 0;
                            foreach (string strParam in strParameters)
                            {
                                switch (nParamPos)
                                {
                                    case 0: setHeader.objVarParam1[0].Data = strParam; break;
                                    case 1: setHeader.objVarParam2[0].Data = strParam; break;
                                    case 2: setHeader.objVarParam3[0].Data = strParam; break;
                                }

                                nParamPos++;
                            }

                            if (iSelectType == 1)
                                setHeader.usPalletIndex = usPalletIdx;
                            else if (iSelectType == 2)
                                setHeader.usPalletIndex = ushort.MaxValue;

                            setHeader.usCommand = SET_PROTOCOL.COMMAND;
                            setHeader.usSubCommand = SET_PROTOCOL.SUB_COMMAND;
                            setHeader.uiOptionCommand = SET_PROTOCOL.OPTION_COMMAND;

                            setHeader.uiHeaderLength = (uint)iHeaderSize;
                            setHeader.uiDataLength = (uint)ucSendData.Count();

                            if (getCPPacket == null)
                            {
                                setHeader.objCP_Packet[0].objStationName[0].Data = "-";
                                setHeader.objCP_Packet[0].objProdNo_P[0].Data = "-";
                                setHeader.objCP_Packet[0].objProdNo_C[0].Data = "-";
                                setHeader.objCP_Packet[0].objTestType[0].Data = "-";
                                setHeader.objCP_Packet[0].objTestCode[0].Data = "-";
                                setHeader.objCP_Packet[0].objVersion[0].Data = "-";
                                setHeader.objCP_Packet[0].objProdCode[0].Data = "-";
                                setHeader.objCP_Packet[0].uiTestListNo = 0;
                            }
                            else
                            {
                                setHeader.objCP_Packet[0].objStationName[0].Data = getCPPacket.Value.objStationName[0].Data;
                                setHeader.objCP_Packet[0].objProdNo_P[0].Data = getCPPacket.Value.objProdNo_P[0].Data;
                                setHeader.objCP_Packet[0].objProdNo_C[0].Data = getCPPacket.Value.objProdNo_C[0].Data;
                                setHeader.objCP_Packet[0].objTestType[0].Data = getCPPacket.Value.objTestType[0].Data;
                                setHeader.objCP_Packet[0].objTestCode[0].Data = getCPPacket.Value.objTestCode[0].Data;
                                setHeader.objCP_Packet[0].objVersion[0].Data = getCPPacket.Value.objVersion[0].Data;
                                setHeader.objCP_Packet[0].objProdCode[0].Data = getCPPacket.Value.objProdCode[0].Data;
                                setHeader.objCP_Packet[0].uiTestListNo = getCPPacket.Value.uiTestListNo;
                            }

                            setHeader.objOptionName[0].Data = strGetFileName;
                            setHeader.objOptionExtension[0].Data = strGetExtension;

                            setHeader.uiSourDataNum = i + 1;
                            setHeader.uiDestDataNum = uiMakeCnt;

                            object getPacket = SystemXNetSerialization.SelectPacket(setHeader.uiDataLength);
                            if (getPacket == null) throw new Exception("SystemXNetSerialization.SelectPacket Failed.");
                            int iBodySize = Marshal.SizeOf(getPacket);
                            byte[] ucBodyArray = new byte[iBodySize];
                            getPacket = Convert.ChangeType(SystemXNetSerialization.RawDeSerialize(ucBodyArray, getPacket.GetType()), getPacket.GetType());
                            Array.Copy(ucSendData, (byte[])getPacket.GetType().GetMethod("Data").Invoke(getPacket, null), ucSendData.Count());

                            setHeader.uiBodyLength = (uint)iBodySize;
                            setHeader.uiPacketLength = (uint)(iHeaderSize + iBodySize);

                            byte[] SendHeadBuffer = SystemXNetSerialization.RawSerialize(setHeader, Marshal.SizeOf(setHeader));

                            byte[] SendBodyBuffer = SystemXNetSerialization.RawSerialize(getPacket, Marshal.SizeOf(getPacket));

                            ucGetStreamBytes = HeadTailMake(setHeader.uiPacketLength, SendHeadBuffer, SendBodyBuffer);

                            listStreamRawData.Add(ucGetStreamBytes);
                        }
                        //string strMessage = Encoding.Unicode.GetString(ucGetFileBytes, 0, ucGetFileBytes.Length) + "\r\n";
                    }
                }
                catch (Exception e)
                {
                    //throw new CommonPacketMarshalException("General packet marshalling failed.[SystemX.Common : COMMON.ObjectToByteStreamList]");
                    MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"General packet marshalling failed.[SystemX.Common : COMMON.ObjectToByteStreamList]\r\n" + e.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG);
                }
            }
            finally
            {
                ;//Until unused
            }

            return listStreamRawData;
        }
    }
}
