Exemplo n.º 1
0
    /// <summary>
    /// Read the value for an unknown key as bytes.
    /// Used to preserve unknown keys during deserialization.
    /// Requires the message option preserveunknown=true.
    /// </summary>
    public static byte[] ReadValueBytes(CitoStream stream, Key key)
    {
        byte[] b;
        int    offset = 0;

        switch (key.GetWireType())
        {
        case Wire.Fixed32:
            b = new byte[4];
            while (offset < 4)
            {
                offset += stream.Read(b, offset, 4 - offset);
            }
            return(b);

        case Wire.Fixed64:
            b = new byte[8];
            while (offset < 8)
            {
                offset += stream.Read(b, offset, 8 - offset);
            }
            return(b);

        case Wire.LengthDelimited:
            //Read and include length in value buffer
            int length          = ProtocolParser.ReadUInt32(stream);
            CitoMemoryStream ms = new CitoMemoryStream();
            {
                //TODO: pass b directly to MemoryStream constructor or skip usage of it completely
                ProtocolParser.WriteUInt32(ms, length);
                b = new byte[length + ms.Length()];
                byte[] arr = ms.ToArray();
                for (int i = 0; i < ProtoPlatform.ArrayLength(arr); i++)
                {
                    b[i] = arr[i];
                }
                offset = ms.Length();
            }

            //Read data into buffer
            while (offset < ProtoPlatform.ArrayLength(b))
            {
                offset += stream.Read(b, offset, ProtoPlatform.ArrayLength(b) - offset);
            }
            return(b);

        case Wire.Varint:
            return(ProtocolParser.ReadVarIntBytes(stream));

        default:
#if !CITO
            throw new NotImplementedException("Unknown wire type: " + key.GetWireType());
#else
            return(null);
#endif
        }
    }
Exemplo n.º 2
0
    public static byte[] Serialize(Packet_Server packet, IntRef retLength)
    {
        CitoMemoryStream ms = new CitoMemoryStream();

        Packet_ServerSerializer.Serialize(ms, packet);
        byte[] data = ms.ToArray();
        retLength.value = ms.Length();
        return(data);
    }
Exemplo n.º 3
0
    void SendRequest(NetClient client)
    {
        //Create request packet
        Packet_Client pp = ClientPackets.ServerQuery();

        //Serialize packet
        CitoMemoryStream ms = new CitoMemoryStream();

        Packet_ClientSerializer.Serialize(ms, pp);
        byte[] data = ms.ToArray();

        //Send packet to server
        INetOutgoingMessage msg = new INetOutgoingMessage();

        msg.Write(data, ms.Length());
        client.SendMessage(msg, MyNetDeliveryMethod.ReliableOrdered);
    }
Exemplo n.º 4
0
    /// <summary>
    /// Read the value for an unknown key as bytes.
    /// Used to preserve unknown keys during deserialization.
    /// Requires the message option preserveunknown=true.
    /// </summary>
    public static byte[] ReadValueBytes(CitoStream stream, Key key)
    {
        byte[] b;
        int offset = 0;

        switch (key.GetWireType())
        {
            case Wire.Fixed32:
                b = new byte[4];
                while (offset < 4)
                    offset += stream.Read(b, offset, 4 - offset);
                return b;
            case Wire.Fixed64:
                b = new byte[8];
                while (offset < 8)
                    offset += stream.Read(b, offset, 8 - offset);
                return b;
            case Wire.LengthDelimited:
                //Read and include length in value buffer
                int length = ProtocolParser.ReadUInt32(stream);
                CitoMemoryStream ms = new CitoMemoryStream();
                {
                    //TODO: pass b directly to MemoryStream constructor or skip usage of it completely
                    ProtocolParser.WriteUInt32(ms, length);
                    b = new byte[length + ms.Length()];
                    byte[] arr = ms.ToArray();
                    for (int i = 0; i < ProtoPlatform.ArrayLength(arr); i++)
                    {
                        b[i] = arr[i];
                    }
                    offset = ms.Length();
                }

                //Read data into buffer
                while (offset < ProtoPlatform.ArrayLength(b))
                    offset += stream.Read(b, offset, ProtoPlatform.ArrayLength(b) - offset);
                return b;
            case Wire.Varint:
                return ProtocolParser.ReadVarIntBytes(stream);
            default:
        #if !CITO
                throw new NotImplementedException("Unknown wire type: " + key.GetWireType());
        #else
                return null;
        #endif
        }
    }
Exemplo n.º 5
0
 public static byte[] Serialize(Packet_Server packet, IntRef retLength)
 {
     CitoMemoryStream ms = new CitoMemoryStream();
     Packet_ServerSerializer.Serialize(ms, packet);
     byte[] data = ms.ToArray();
     retLength.value = ms.Length();
     return data;
 }
Exemplo n.º 6
0
    void SendRequest(NetClient client)
    {
        //Create request packet
        Packet_Client pp = ClientPackets.ServerQuery();

        //Serialize packet
        CitoMemoryStream ms = new CitoMemoryStream();
        Packet_ClientSerializer.Serialize(ms, pp);
        byte[] data = ms.ToArray();

        //Send packet to server
        INetOutgoingMessage msg = new INetOutgoingMessage();
        msg.Write(data, ms.Length());
        client.SendMessage(msg, MyNetDeliveryMethod.ReliableOrdered);
    }