コード例 #1
0
    /// <summary>
    /// Encode each item in message.
    /// </summary>
    private int EncodeProp(object value, string type, int offset, byte[] buffer, MessageObject proto)
    {
        switch (type)
        {
        case "uInt32":
            this.WriteUInt32(buffer, ref offset, value);
            break;

        case "int32":
        case "sInt32":
            this.WriteInt32(buffer, ref offset, value);
            break;

        case "float":
            this.WriteFloat(buffer, ref offset, value);
            break;

        case "double":
            this.WriteDouble(buffer, ref offset, value);
            break;

        case "string":
            this.WriteString(buffer, ref offset, value);
            break;

        default:
            object __messages;
            object __message_type;

            if (proto.TryGetValue("__messages", out __messages))
            {
                if (((MessageObject)__messages).TryGetValue(type, out __message_type) || protos.TryGetValue("message " + type, out __message_type))
                {
                    byte[] tembuff = new byte[Encoder.ByteLength(value.ToString()) * 3];
                    int    length  = 0;
                    length = this.EncodeMsg(tembuff, length, (MessageObject)__message_type, (MessageObject)value);
                    offset = WriteBytes(buffer, offset, Encoder.EncodeUInt32((uint)length));
                    for (int i = 0; i < length; i++)
                    {
                        buffer[offset] = tembuff[i];
                        offset++;
                    }
                }
            }
            break;
        }
        return(offset);
    }
コード例 #2
0
    /// <summary>
    /// Encode the message from server.
    /// </summary>
    /// <param name='route'>
    /// Route.
    /// </param>
    /// <param name='msg'>
    /// Message.
    /// </param>
    public byte[] Encode(string route, MessageObject msg)
    {
        byte[] returnByte = null;
        object proto;

        if (this.protos.TryGetValue(route, out proto))
        {
            if (!CheckMsg(msg, (MessageObject)proto))
            {
                return(null);
            }
            int    length = Encoder.ByteLength(msg.ToString()) * 2;
            int    offset = 0;
            byte[] buff   = new byte[length];
            offset     = EncodeMsg(buff, offset, (MessageObject)proto, msg);
            returnByte = new byte[offset];
            for (int i = 0; i < offset; i++)
            {
                returnByte[i] = buff[i];
            }
        }
        return(returnByte);
    }