예제 #1
0
        public static byte[] Construct(ushort header, HDestination destination, HProtocol protocol, params object[] chunks)
        {
            var  buffer    = new List <byte>();
            bool isAncient = (protocol == HProtocol.Ancient);

            if (isAncient && destination == HDestination.Server)
            {
                buffer.Add(64);
            }
            buffer.AddRange(protocol == HProtocol.Ancient ? Ancient.CypherShort(header) : Modern.CypherShort(header));

            buffer.AddRange(ConstructBody(destination, protocol, chunks));

            if (!isAncient || destination == HDestination.Server)
            {
                buffer.InsertRange(isAncient ? 1 : 0, isAncient ? Ancient.CypherShort((ushort)(buffer.Count - 1)) : Modern.CypherInt(buffer.Count));
            }
            else if (buffer[buffer.Count - 1] != 1)
            {
                buffer.Add(1);
            }

            return(buffer.ToArray());
        }
예제 #2
0
        public static byte[] ConstructBody(HDestination destination, HProtocol protocol, params object[] chunks)
        {
            var  buffer    = new List <byte>();
            bool isAncient = (protocol == HProtocol.Ancient);

            for (int i = 0; i < chunks.Length; i++)
            {
                object chunk = chunks[i];
                if (chunk == null)
                {
                    throw new NullReferenceException(string.Format("Unable to encode a null object. {{ Index = {0} }}", i));
                }

                var data = chunk as byte[];
                if (data != null)
                {
                    buffer.AddRange(data);
                }
                else
                {
                    switch (Type.GetTypeCode(chunk.GetType()))
                    {
                    case TypeCode.Int32:
                    {
                        var value = (int)chunk;
                        buffer.AddRange(protocol == HProtocol.Ancient ? Ancient.CypherInt(value) : Modern.CypherInt(value));
                        break;
                    }

                    case TypeCode.Boolean:
                    {
                        var value = (bool)chunk;
                        buffer.Add(isAncient ? (byte)(value ? 73 : 72) : Convert.ToByte(value));
                        break;
                    }

                    case TypeCode.Byte:
                    {
                        var value = (byte)chunk;
                        buffer.Add(value);
                        break;
                    }

                    default:
                    {
                        string value = chunk.ToString();
                        if (!isAncient || destination == HDestination.Server)
                        {
                            ushort valueLength = (ushort)value.Length;
                            buffer.AddRange(protocol == HProtocol.Ancient ? Ancient.CypherShort(valueLength) : Modern.CypherShort(valueLength));
                            buffer.AddRange(Encoding.Default.GetBytes(value));
                        }
                        else
                        {
                            buffer.AddRange(Encoding.Default.GetBytes(value));
                            buffer.Add(2);
                        }
                        break;
                    }
                    }
                }
            }
            return(buffer.ToArray());
        }