예제 #1
0
        public static byte[] encoder(Message message)
        {
            int location = 0;
            byte[] finalMessage = new byte[getMessageSize(message) + 4];
            byte[] totalSizeMessage = BitConverter.GetBytes(getMessageSize(message));
            totalSizeMessage.CopyTo(finalMessage, location);
            location += 4;
            MessageItem mSource = new MessageItem(message.Source);
            mSource.getItemSizeAndContent().CopyTo(finalMessage, location);
            location += mSource.getItemLength();

            MessageItem mTarget = new MessageItem(message.Target);
            mTarget.getItemSizeAndContent().CopyTo(finalMessage, location);
            location += mTarget.getItemLength();

            MessageItem mOperation = new MessageItem(message.Operation);
            mOperation.getItemSizeAndContent().CopyTo(finalMessage, location);
            location += mOperation.getItemLength();

            MessageItem mStamp = new MessageItem(message.Stamp);
            mStamp.getItemSizeAndContent().CopyTo(finalMessage, location);
            location += mStamp.getItemLength();

            BitConverter.GetBytes(message.ListParams.Count).CopyTo(finalMessage, location);
            location += 4;

            foreach (string param in message.ListParams)
            {
                MessageItem mParam = new MessageItem(param);
                mParam.getItemSizeAndContent().CopyTo(finalMessage, location);
                location += mParam.getItemLength();
            }

            return finalMessage;
        }
예제 #2
0
        public static Message decoder(byte[] restMessage)
        {
            byte[] sizeSource = readNByte(restMessage, 4);
            restMessage = removeNByte(restMessage, 4);
            byte[] contentSource = readNByte(restMessage, byteToInt(sizeSource));
            restMessage = removeNByte(restMessage, byteToInt(sizeSource));

            MessageItem source = new MessageItem(sizeSource, contentSource);

            byte[] sizeTarget = readNByte(restMessage, 4);
            restMessage = removeNByte(restMessage, 4);
            byte[] contentTarget = readNByte(restMessage, byteToInt(sizeTarget));
            restMessage = removeNByte(restMessage, byteToInt(sizeTarget));
            MessageItem target = new MessageItem(sizeTarget, contentTarget);

            byte[] sizeOperation = readNByte(restMessage, 4);
            restMessage = removeNByte(restMessage, 4);
            byte[] contentOperation = readNByte(restMessage, byteToInt(sizeOperation));
            restMessage = removeNByte(restMessage, byteToInt(sizeOperation));
            MessageItem operation = new MessageItem(sizeOperation, contentOperation);

            byte[] sizeStamp = readNByte(restMessage, 4);
            restMessage = removeNByte(restMessage, 4);
            byte[] contentStamp = readNByte(restMessage, byteToInt(sizeStamp));
            restMessage = removeNByte(restMessage, byteToInt(sizeStamp));
            MessageItem stamp = new MessageItem(sizeStamp, contentStamp);

            byte[] paramsCount = readNByte(restMessage, 4);
            restMessage = removeNByte(restMessage, 4);

            List<string> listParams = new List<string>();

            for (int i = 0; i < byteToInt(paramsCount); i++)
            {
                byte[] sizeParam = readNByte(restMessage, 4);
                restMessage = removeNByte(restMessage, 4);
                byte[] contentParam = readNByte(restMessage, byteToInt(sizeParam));
                restMessage = removeNByte(restMessage, byteToInt(sizeParam));
                listParams.Add((new MessageItem(sizeParam, contentParam)).DecoderItem());
            }

            return new Message(source.DecoderItem(), target.DecoderItem(), operation.DecoderItem()
                , stamp.DecoderItem(), listParams);
        }