Exemplo n.º 1
0
        public static bool ItemsToPacket(List <CPacketItem> items, bool swap, Byte[] sendBuff, out int totalSendSize)
        {
            totalSendSize = 0;
            Boolean unsigned = false;

            for (int i = 0; i < items.Count; i++)
            {
                int    typeSize = 4;
                String type     = items[i].TypeString;
                Array  packets  = TypeHandling.GetArrayByTypeName(type, items[i].Length, out typeSize, out unsigned);

                if (packets == null || packets.Length < 1 || typeSize < 0)
                {
                    String error = "타입명세실패";
                    throw new Exception(error);
                }


                if (type.Equals("char") || type.Equals("string"))
                {
                    if (items[i].InitString == null || items[i].InitString.Length == 0)
                    {
                        continue;
                    }
                    packets = Encoding.UTF8.GetBytes(items[i].InitString);
                    Buffer.BlockCopy(packets, 0, sendBuff, totalSendSize, packets.Length);
                    totalSendSize += packets.Length;
                }
                else
                {
                    String[] units = items[i].InitValues;
                    for (int k = 0; k < units.Length; k++)
                    {
                        TypeHandling.TypeName typeName = TypeHandling.getTypeKind(units[k]);

                        if (typeName == TypeHandling.TypeName.Integer)
                        {
                            if (unsigned == false)
                            {
                                if (typeSize == 1)
                                {
                                    packets.SetValue(SByte.Parse(units[k]), k);
                                }
                                else if (typeSize == 2)
                                {
                                    packets.SetValue(Int16.Parse(units[k]), k);
                                }
                                else if (typeSize == 4)
                                {
                                    packets.SetValue(Int32.Parse(units[k]), k);
                                }
                                else
                                {
                                    packets.SetValue(Int64.Parse(units[k]), k);
                                }
                            }
                            else
                            {
                                if (typeSize == 1)
                                {
                                    packets.SetValue(Byte.Parse(units[k]), k);
                                }
                                else if (typeSize == 2)
                                {
                                    packets.SetValue(UInt16.Parse(units[k]), k);
                                }
                                else if (typeSize == 4)
                                {
                                    packets.SetValue(UInt32.Parse(units[k]), k);
                                }
                                else
                                {
                                    packets.SetValue(UInt64.Parse(units[k]), k);
                                }
                            }
                        }
                        else if (typeName == TypeHandling.TypeName.HexString)
                        {
                            if (unsigned)
                            {
                                if (typeSize == 1)
                                {
                                    packets.SetValue((byte)TypeHandling.getHexNumber <byte>(units[k]), k);
                                }
                                else if (typeSize == 2)
                                {
                                    packets.SetValue((ushort)TypeHandling.getHexNumber <ushort>(units[k]), k);
                                }
                                else if (typeSize == 4)
                                {
                                    packets.SetValue((uint)TypeHandling.getHexNumber <uint>(units[k]), k);
                                }
                                else
                                {
                                    packets.SetValue((UInt64)TypeHandling.getHexNumber <ulong>(units[k]), k);
                                }
                            }
                            else
                            {
                                if (typeSize == 1)
                                {
                                    packets.SetValue((sbyte)TypeHandling.getHexNumber <sbyte>(units[k]), k);
                                }
                                else if (typeSize == 2)
                                {
                                    packets.SetValue((short)TypeHandling.getHexNumber <short>(units[k]), k);
                                }
                                else if (typeSize == 4)
                                {
                                    packets.SetValue((int)TypeHandling.getHexNumber <int>(units[k]), k);
                                }
                                else
                                {
                                    packets.SetValue((Int64)TypeHandling.getHexNumber <long>(units[k]), k);
                                }
                            }
                        }
                        else if (typeName == TypeHandling.TypeName.Float)
                        {
                            if (typeSize == 4)
                            {
                                packets.SetValue(float.Parse(units[k]), k);
                            }
                            else
                            {
                                packets.SetValue(Double.Parse(units[k]), k);
                            }
                        }
                        else if (typeName == TypeHandling.TypeName.Bin)
                        {
                            if (typeSize == 1)
                            {
                                packets.SetValue((byte)TypeHandling.getBinNumber(units[k]), k);
                            }
                            else if (typeSize == 2)
                            {
                                packets.SetValue((short)TypeHandling.getBinNumber(units[k]), k);
                            }
                            else if (typeSize == 4)
                            {
                                packets.SetValue((int)TypeHandling.getBinNumber(units[k]), k);
                            }
                            else
                            {
                                packets.SetValue((Int64)TypeHandling.getBinNumber(units[k]), k);
                            }
                        }
                    }
                    //buff = new Byte[Marshal.SizeOf(packets.GetValue(0)) * packets.Length];
                    if (swap) //전체 swap
                    {
                        if (items[i].IsSwap == false)
                        {
                            Swaper.swapWithSize(packets, sendBuff, typeSize, Buffer.ByteLength(packets), 0, totalSendSize);
                        }
                        else
                        {
                            Buffer.BlockCopy(packets, 0, sendBuff, totalSendSize, Buffer.ByteLength(packets));
                        }
                    }
                    else //일부만 swap
                    {
                        if (items[i].IsSwap)
                        {
                            Swaper.swapWithSize(packets, sendBuff, typeSize, Buffer.ByteLength(packets), 0, totalSendSize);
                        }
                        else
                        {
                            Buffer.BlockCopy(packets, 0, sendBuff, totalSendSize, Buffer.ByteLength(packets));
                        }
                    }
                    totalSendSize += Buffer.ByteLength(packets);
                }
            }

            /*
             * if(_sendBuff.Equals(PacketBuffer)){ //만일 _sendBuff가 Packet버퍼와 같을 때, 사이즈를 조정한다.(초기사이즈->실제사이즈)
             *  Byte[] buff = new Byte[totalSendSize];
             *  Buffer.BlockCopy(_sendBuff, 0, buff, 0, totalSendSize);
             *  PacketBuffer = buff;
             * }
             */
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Items의 내용을 바꾸어 packet으로 변경한다.
        /// Send할 패킷을 만드는 데 사용된다.
        /// </summary>
        /// <param name="items"></param>
        /// <param name="funcList">사용될 function의 리스트. function은 String들을 argument로 받고, String[]을 리턴하는 function들이다.</param>
        /// <param name="varList">사용될 variables의 리스트. 같은 CPacketItem의 다른 item들을 찾아보고, 찾을 수 없다면 이 리스트에서 찾는다.</param>
        /// <param name="swap">전체적으로 swap을 할 것인지를 묻는다. 이 arguemnt가 true인데 단일 item이 IsSwap에 true가 되어있다면 다른 것과는 반대로 동작한다. 즉, swap되지 않는다</param>
        /// <param name="dataBuff">패킷데이터가 저장될 버퍼</param>
        /// <param name="dataSize">전체 크기</param>
        /// <returns></returns>
        public void ItemsToPacket(List <CPacketItem> items, Dictionary <String, InitVarFunc> funcList, Dictionary <String, VariableInfo> varList, bool swap, Byte[] dataBuff, out int dataSize)
        {
            dataSize = 0;
            Boolean unsigned = false;
            Dictionary <String, VariableInfo> vars = new Dictionary <string, VariableInfo>(); // = new Dictionary<string, string[]>(varList);

            for (int i = 0; i < items.Count; i++)                                             //static 하게 정의된 item의 요소는 변수로 사용가능하다.
            {
                if (items[i].InitValues.Length > 0)
                {
                    vars.Add(items[i].Name, new VariableInfo(items[i].Name, items[i].InitValues));
                }
            }
            for (int i = 0; i < varList.Count; i++)
            {
                if (vars.Keys.Contains(varList.Keys.ElementAt(i)))
                {
                    continue;                                               //같은 변수명이면 같은 패킷에 속한 것이 적용된다.
                }
                else
                {
                    vars.Add(varList.Keys.ElementAt(i), varList.Values.ElementAt(i));
                }
            }


            for (int i = 0; i < items.Count; i++)
            {
                int typeSize = 4;

                String type    = items[i].TypeString;
                Array  packets = TypeHandling.GetArrayByTypeName(type, items[i].Length, out typeSize, out unsigned);

                //Byte[] buff;
                if (items[i].Function.Exists)
                {
                    items[i].InitValues = runFunction(funcList, vars, items[i].Function);
                }
                else if (items[i].Var.Exists)
                {
                    String refName = items[i].Var.Name;
                    if (vars.Keys.Contains(refName))
                    {
                        if (vars[refName].Values != null && vars[refName].Values.Length > 0)
                        {
                            items[i].InitValues = vars[items[i].Var.Name].Values;
                        }
                        else
                        {
                            throw new Exception("변수" + refName + "가 존재하지 않거나 값이 설정되지 않았습니다.");
                        }
                    }
                }

                if (type.Equals("char") || type.Equals("string"))
                {
                    if (items[i].InitString == null || items[i].InitString.Length == 0)
                    {
                        continue;
                    }
                    packets = Encoding.UTF8.GetBytes(items[i].InitString);
                    Buffer.BlockCopy(packets, 0, dataBuff, dataSize, packets.Length);
                    dataSize += packets.Length;
                }
                else
                {
                    for (int k = 0; k < items[i].InitValues.Length; k++)
                    {
                        TypeHandling.TypeName typeName = TypeHandling.getTypeKind(items[i].InitValues[k]);
                        if (typeName == TypeHandling.TypeName.Integer)
                        {
                            if (unsigned == false)
                            {
                                if (typeSize == 1)
                                {
                                    packets.SetValue(SByte.Parse(items[i].InitValues[k]), k);
                                }
                                else if (typeSize == 2)
                                {
                                    packets.SetValue(Int16.Parse(items[i].InitValues[k]), k);
                                }
                                else if (typeSize == 4)
                                {
                                    packets.SetValue(Int32.Parse(items[i].InitValues[k]), k);
                                }
                                else
                                {
                                    packets.SetValue(Int64.Parse(items[i].InitValues[k]), k);
                                }
                            }
                            else
                            {
                                if (typeSize == 1)
                                {
                                    packets.SetValue(Byte.Parse(items[i].InitValues[k]), k);
                                }
                                else if (typeSize == 2)
                                {
                                    packets.SetValue(UInt16.Parse(items[i].InitValues[k]), k);
                                }
                                else if (typeSize == 4)
                                {
                                    packets.SetValue(UInt32.Parse(items[i].InitValues[k]), k);
                                }
                                else
                                {
                                    packets.SetValue(UInt64.Parse(items[i].InitValues[k]), k);
                                }
                            }
                        }
                        else if (typeName == TypeHandling.TypeName.HexString)
                        {
                            if (typeSize == 1)
                            {
                                packets.SetValue(TypeHandling.getHexNumber <byte>(items[i].InitValues[k]), k);
                            }
                            else if (typeSize == 2)
                            {
                                packets.SetValue(TypeHandling.getHexNumber <ushort>(items[i].InitValues[k]), k);
                            }
                            else if (typeSize == 4)
                            {
                                packets.SetValue(TypeHandling.getHexNumber <uint>(items[i].InitValues[k]), k);
                            }
                            else
                            {
                                packets.SetValue(TypeHandling.getHexNumber <ulong>(items[i].InitValues[k]), k);
                            }
                        }
                        else if (typeName == TypeHandling.TypeName.Float)
                        {
                            if (typeSize == 4)
                            {
                                packets.SetValue(float.Parse(items[i].InitValues[k]), k);
                            }
                            else
                            {
                                packets.SetValue(Double.Parse(items[i].InitValues[k]), k);
                            }
                        }
                        else if (typeName == TypeHandling.TypeName.Bin)
                        {
                            if (typeSize == 1)
                            {
                                packets.SetValue((byte)TypeHandling.getBinNumber(items[i].InitValues[k]), k);
                            }
                            else if (typeSize == 2)
                            {
                                packets.SetValue((short)TypeHandling.getBinNumber(items[i].InitValues[k]), k);
                            }
                            else if (typeSize == 4)
                            {
                                packets.SetValue((int)TypeHandling.getBinNumber(items[i].InitValues[k]), k);
                            }
                            else
                            {
                                packets.SetValue((Int64)TypeHandling.getBinNumber(items[i].InitValues[k]), k);
                            }
                        }
                    }
                    //buff = new Byte[Marshal.SizeOf(packets.GetValue(0)) * packets.Length];
                    if (swap)//전체 swap
                    {
                        if (items[i].IsSwap == false)
                        {
                            Swaper.swapWithSize(packets, dataBuff, typeSize, Buffer.ByteLength(packets), 0, dataSize);
                        }
                        else
                        {
                            Buffer.BlockCopy(packets, 0, dataBuff, dataSize, Buffer.ByteLength(packets));
                        }
                    }
                    else //각각 swap
                    {
                        if (items[i].IsSwap)
                        {
                            Swaper.swapWithSize(packets, dataBuff, typeSize, Buffer.ByteLength(packets), 0, dataSize);
                        }
                        else
                        {
                            Buffer.BlockCopy(packets, 0, dataBuff, dataSize, Buffer.ByteLength(packets));
                        }
                    }
                    dataSize += Buffer.ByteLength(packets);
                }
            }

            /*
             * if(_sendBuff.Equals(PacketBuffer)){ //만일 _sendBuff가 Packet버퍼와 같을 때, 사이즈를 조정한다.(초기사이즈->실제사이즈)
             *  Byte[] buff = new Byte[totalSendSize];
             *  Buffer.BlockCopy(_sendBuff, 0, buff, 0, totalSendSize);
             *  PacketBuffer = buff;
             * }
             */
        }