Exemple #1
0
        public CruMessageTransfer()
        {
            int[] dataSizeList = CruNetworkProtocol.GetMessageSizeList(CruNetworkProtocol.MSG_TYPE.HEAD);
            HEADSIZE_DATACOUNT = dataSizeList[0];
            HEADSIZE_TYPE      = dataSizeList[1];

            //Init();
        }
Exemple #2
0
        // 데이터 사이즈는 버퍼의 최종 사이즈보다 같거나 작다. 왜냐하면, 보낼 때, buffer사이즈보다 작은 사이즈를 보내기 때문!
        // 만약 데이터 사이즈와 버퍼 사이즈가 같다면... 그건 추가적으로 오는 데이터가 있을 수 있다는거니... 추가적인 작업이 있어야 함.
        public CruMessage[] ReceiveByteToMsgs(byte[] buffer, int receiveDatasize)
        {
            CruMessage[] result;
            CruMessage   temp;
            int          headsize;
            int          transferdDataSize = 0;
            short        dataCnt;
            short        type;
            int          idx = 0;

            // 데이터 수 확인
            headsize = CruNetworkProtocol.Head.HEADSIZE_DATACOUNT;
            dataCnt  = BitConverter.ToInt16(buffer, 0);
            result   = new CruMessage[dataCnt];
            idx     += headsize;

            // 개별 데이터 확인
            headsize = CruNetworkProtocol.Head.HEADSIZE_TYPE;

            for (int i = 0; i < dataCnt; ++i)
            {
                // type 확인
                type = BitConverter.ToInt16(buffer, idx);
                idx += headsize;

                // data 확인
                switch (type)
                {
                case (short)CruNetworkProtocol.MSG_TYPE.RESULT_PLAYERS_LOCATION:
                    temp = ExtractMutableDatas(type, buffer, idx, receiveDatasize, out transferdDataSize);
                    idx += transferdDataSize;
                    transferdDataSize = 0;
                    break;

                default:
                    temp = ExtractDatas(type, buffer, idx, receiveDatasize);
                    idx += CruNetworkProtocol.GetMessageTotalSIze(type);
                    break;
                }

                result[i] = temp;
            }

            if (idx > receiveDatasize)
            {
                return(null);
            }

            return(result);
        }
Exemple #3
0
        // CruMessage를 만드는 곳
        public CruMessage ExtractDatas(short type, byte[] buffer, int offset, int totalmsgssize)
        {
            CruMessage result;

            object[] datas;
            int[]    datasizelist;
            CruNetworkProtocol.DATA_TYPE[] typelist;
            int bufferIdx   = 0;
            int setDataSize = 0;

            datasizelist = CruNetworkProtocol.GetMessageSizeList(type);
            typelist     = CruNetworkProtocol.GetMessageTypeList(type);

            datas = new object[datasizelist.Length - 1];

            for (int i = 1; i < datasizelist.Length; ++i)
            {
                // 실제 버퍼 입력받을 위치
                bufferIdx = offset + setDataSize;

                // 다음에 처리할 사이즈가 총 버퍼의 사이즈를 넘어간다면,
                if (bufferIdx + datasizelist[i] > totalmsgssize)
                {
                    throw new SystemException("버퍼 사이즈를 넘겼습니다.");
                }

                // 데이터 Get
                switch (typelist[i])
                {
                case CruNetworkProtocol.DATA_TYPE.BOOLEAN:
                    datas[i - 1] = BitConverter.ToBoolean(buffer, bufferIdx);
                    break;

                case CruNetworkProtocol.DATA_TYPE.SHORT:
                    datas[i - 1] = BitConverter.ToInt16(buffer, bufferIdx);
                    break;

                case CruNetworkProtocol.DATA_TYPE.INT:
                    datas[i - 1] = BitConverter.ToInt32(buffer, bufferIdx);
                    break;

                case CruNetworkProtocol.DATA_TYPE.FLOAT:
                    datas[i - 1] = BitConverter.ToSingle(buffer, bufferIdx);
                    break;

                case CruNetworkProtocol.DATA_TYPE.DOUBLE:
                    datas[i - 1] = BitConverter.ToDouble(buffer, bufferIdx);
                    break;

                case CruNetworkProtocol.DATA_TYPE.STRING:
                {
                    int stringsize = BitConverter.ToInt16(buffer, bufferIdx);
                    datas[i - 1] = Encoding.ASCII.GetString(buffer, bufferIdx + CruNetworkProtocol.Head.HEADSIZE_STRING, stringsize);
                }
                break;

                default:
                    break;
                }

                setDataSize += datasizelist[i];
            }

            // 변화한 데이터 사이즈가, 총량을 넘어갔을 때 ㅇㅇ
            if (setDataSize > datasizelist[0])
            {
                return(null);
            }

            result = new CruMessage(type, datas);

            return(result);
        }
Exemple #4
0
        public int ExtractMutableSendDatas(CruMessage msg, byte[] buffer, int offset)
        {
            /*
             * 일반적으로 가변데이터 형식은 똑같다.
             * type + data갯수 + (data 정보들) + (data 정보들) + ...
             */
            byte[] temp;
            short  type = msg.Type;

            object[] datas         = msg.Datas;
            int      headSize      = 0;
            int      dataCnt       = datas.Length;
            int      dataSize      = 0;
            int      totalDataSize = 0;

            int[] datasizelist;
            CruNetworkProtocol.DATA_TYPE[] typelist;

            headSize     = CruNetworkProtocol.Head.HEADSIZE_TYPE;
            datasizelist = CruNetworkProtocol.GetMessageSizeList(type);
            typelist     = CruNetworkProtocol.GetMessageTypeList(type);

            // type 처리
            temp = BitConverter.GetBytes(type);
            Array.Copy(temp, 0, buffer, offset + dataSize, headSize);
            dataSize += headSize;

            // data count 처리
            temp = BitConverter.GetBytes((short)datas[0]);
            Array.Copy(temp, 0, buffer, offset + dataSize, datasizelist[1]);
            dataSize += datasizelist[1];

            // data 처리
            for (int i = 1; i < dataCnt; ++i)
            {
                for (int j = 2; j < datasizelist.Length; ++j)
                {
                    // 버퍼 사이즈 넘어가는지 체크
                    if (offset + dataSize + datasizelist[j] > buffer.Length)
                    {
                        throw new SystemException("버퍼 사이즈를 넘어갑니다.");
                    }

                    switch (typelist[j])
                    {
                    case CruNetworkProtocol.DATA_TYPE.BOOLEAN:
                        temp = BitConverter.GetBytes((bool)datas[i]);
                        break;

                    case CruNetworkProtocol.DATA_TYPE.SHORT:
                        temp = BitConverter.GetBytes((short)datas[i]);
                        break;

                    case CruNetworkProtocol.DATA_TYPE.INT:
                        temp = BitConverter.GetBytes((int)datas[i]);
                        break;

                    case CruNetworkProtocol.DATA_TYPE.FLOAT:
                        temp = BitConverter.GetBytes((float)datas[i]);
                        break;

                    case CruNetworkProtocol.DATA_TYPE.DOUBLE:
                        temp = BitConverter.GetBytes((double)datas[i]);
                        break;

                    case CruNetworkProtocol.DATA_TYPE.STRING:
                    {
                        short stringsize;
                        temp = new byte[datasizelist[j]];

                        if ((string)datas[i] == null)
                        {
                            stringsize = 0;
                        }
                        else
                        {
                            stringsize = (short)((string)datas[i]).Length;
                        }

                        // Set string size
                        Array.Copy(BitConverter.GetBytes(stringsize), temp, CruNetworkProtocol.Head.HEADSIZE_STRING);

                        // Set string data
                        if (datas[i] != null)
                        {
                            Encoding.ASCII.GetBytes((string)datas[i], 0, ((string)datas[i]).Length, temp, CruNetworkProtocol.Head.HEADSIZE_STRING);
                        }
                    }
                    break;

                    default:
                        break;
                    }

                    Array.Copy(temp, 0, buffer, offset + dataSize, datasizelist[j]);
                    dataSize += datasizelist[j];
                }
            }

            // 총 데이터 사이즈 = 헤드사이즈 + 데이터묶음 수 사이즈 + ((데이터 묶음 사이즈) * 데이터 묶음 수)
            totalDataSize = headSize + datasizelist[1] + ((datasizelist[0] - datasizelist[1]) * (short)datas[0]);

            if (dataSize != totalDataSize)
            {
                return(0);
            }

            return(dataSize);
        }
Exemple #5
0
        public int ExtractSendDatas(CruMessage msg, byte[] buffer, int offset)
        {
            byte[] temp;
            short  type = msg.Type;

            object[] datas    = msg.Datas;
            int      headSize = 0;
            int      dataSize = 0;

            int[] datasizelist;
            CruNetworkProtocol.DATA_TYPE[] typelist;

            headSize     = CruNetworkProtocol.Head.HEADSIZE_TYPE;
            datasizelist = CruNetworkProtocol.GetMessageSizeList(type);
            typelist     = CruNetworkProtocol.GetMessageTypeList(type);

            temp = BitConverter.GetBytes(type);
            Array.Copy(temp, 0, buffer, offset + dataSize, headSize);
            dataSize += headSize;

            for (int i = 0; i < msg.Datas.Length; ++i)
            {
                // 버퍼 사이즈 넘어가는지 체크
                if (offset + dataSize + datasizelist[i + 1] > buffer.Length)
                {
                    throw new SystemException("버퍼 사이즈를 넘어갑니다.");
                }

                switch (typelist[i + 1])
                {
                case CruNetworkProtocol.DATA_TYPE.BOOLEAN:
                    temp = BitConverter.GetBytes((bool)datas[i]);
                    break;

                case CruNetworkProtocol.DATA_TYPE.SHORT:
                    temp = BitConverter.GetBytes((short)datas[i]);
                    break;

                case CruNetworkProtocol.DATA_TYPE.INT:
                    temp = BitConverter.GetBytes((int)datas[i]);
                    break;

                case CruNetworkProtocol.DATA_TYPE.FLOAT:
                    temp = BitConverter.GetBytes((float)datas[i]);
                    break;

                case CruNetworkProtocol.DATA_TYPE.DOUBLE:
                    temp = BitConverter.GetBytes((double)datas[i]);
                    break;

                case CruNetworkProtocol.DATA_TYPE.STRING:
                {
                    short stringsize;
                    temp = new byte[datasizelist[i + 1]];

                    if ((string)datas[i] == null)
                    {
                        stringsize = 0;
                    }
                    else
                    {
                        stringsize = (short)((string)datas[i]).Length;
                    }

                    // Set string size
                    Array.Copy(BitConverter.GetBytes(stringsize), temp, CruNetworkProtocol.Head.HEADSIZE_STRING);

                    // Set string data
                    if (datas[i] != null)
                    {
                        Encoding.ASCII.GetBytes((string)datas[i], 0, ((string)datas[i]).Length, temp, CruNetworkProtocol.Head.HEADSIZE_STRING);
                    }
                }
                break;

                default:
                    break;
                }

                Array.Copy(temp, 0, buffer, offset + dataSize, datasizelist[i + 1]);
                dataSize += datasizelist[i + 1];
            }

            if (dataSize != datasizelist[0] + headSize)
            {
                return(0);
            }

            return(dataSize);
        }