Android Open Source - steamchat Proto Packet






From Project

Back to project page steamchat.

License

The source code is released under:

Apache License

If you think the Android project steamchat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kevelbreh.steamchat.steam2.packet;
/*from  w  ww. jav  a 2 s.c  o  m*/
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.GeneratedMessage;
import com.kevelbreh.steamchat.SteamChat;
import com.kevelbreh.steamchat.steam.language.Message;
import com.kevelbreh.steamchat.steam.proto.SteamMessagesBaseProto.CMsgProtoBufHeader;
import com.kevelbreh.steamchat.steam.util.BinaryReader;
import com.kevelbreh.steamchat.steam.util.BinaryWriter;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Protobuf backed packet for Steam network communication.
 */
public class ProtoPacket<T extends GeneratedMessage.Builder<T>> extends Packet {

    /**
     * The header protobuf builder. This is automatically generated by protobuf for the CMsgProtoBufHeader
     * defined proto.
     */
    private CMsgProtoBufHeader.Builder header = CMsgProtoBufHeader.newBuilder();

    /**
     * The body content of the packet. This is also automatically generated by a defined proto.
     */
    private T body;

    /**
     * @return the header of the packet.
     */
    public CMsgProtoBufHeader.Builder getHeader() {
        return header;
    }

    /**
     * @return the body of the packet.
     */
    public T getBody() {
        return body;
    }

    @SuppressWarnings("unchecked")
    public ProtoPacket(Class<? extends AbstractMessage> klass, int messageType) {
        this(klass);
        setMessageType(messageType);
    }

    @SuppressWarnings("unchecked")
    public ProtoPacket(Class<? extends AbstractMessage> klass) {
        try {
            final Method method = klass.getMethod("newBuilder");
            body = (T) method.invoke(null);
        }
        catch(final IllegalAccessException e) {
            SteamChat.debug(this, "Failed to create packet: " + e.toString());
        }
        catch(final NoSuchMethodException e) {
            SteamChat.debug(this, "Failed to create packet: " + e.toString());
        }
        catch(final SecurityException e) {
            SteamChat.debug(this, "Failed to create packet: " + e.toString());
        }
        catch(final IllegalArgumentException e) {
            SteamChat.debug(this, "Failed to create packet: " + e.toString());
        }
        catch(final InvocationTargetException e) {
            SteamChat.debug(this, "Failed to create packet: " + e.toString());
        }
    }

    @Override
    public byte[] serialize() throws IOException {
        final BinaryWriter stream = new BinaryWriter();

        header.setSteamid(getSteamId());
        header.setClientSessionid(getSessionId());
        header.setJobidSource(getSourceJobId());
        header.setJobidTarget(getTargetJobId());

    SteamChat.debug("Serializing message=" + getMessageType());

        final byte[] header_data = header.build().toByteArray();
        stream.write(Message.buff(getMessageType()));
        stream.write(header_data.length);
        stream.write(header_data);
        stream.write(body.build().toByteArray());

        return stream.toByteArray();
    }

    @Override
    public void deserialize() throws IOException {
        BinaryReader stream = new BinaryReader(getData());

        // Read packet type and header length.
        int messagetype = stream.readInt();
        int headerlength = stream.readInt();

        // Read proto header and merge into our builder.
        header = CMsgProtoBufHeader.newBuilder();
        final byte[] temp = stream.readBytes(headerlength);
        header.mergeFrom(temp);

        setTargetJobId(header.getJobidTarget());
        setSourceJobId(header.getJobidSource());

        // Read the body of the packet.
        body.mergeFrom(stream.getStream());
    }
}




Java Source Code List

com.kevelbreh.steamchat.SteamChat.java
com.kevelbreh.steamchat.account.AuthenticatorService.java
com.kevelbreh.steamchat.account.Authenticator.java
com.kevelbreh.steamchat.account.SteamAccount.java
com.kevelbreh.steamchat.activity.AuthenticationActivity.java
com.kevelbreh.steamchat.activity.ConversationActivity.java
com.kevelbreh.steamchat.activity.FriendsActivity.java
com.kevelbreh.steamchat.activity.InteractionsActivity.java
com.kevelbreh.steamchat.activity.LauncherActivity.java
com.kevelbreh.steamchat.activity.SettingsActivity.java
com.kevelbreh.steamchat.fragment.ConversationFragment.java
com.kevelbreh.steamchat.fragment.FriendsFragment.java
com.kevelbreh.steamchat.fragment.InteractionsFragment.java
com.kevelbreh.steamchat.fragment.SettingsFragment.java
com.kevelbreh.steamchat.provider.SteamProviderUtils.java
com.kevelbreh.steamchat.provider.SteamProvider.java
com.kevelbreh.steamchat.provider.content.InteractionContentItem.java
com.kevelbreh.steamchat.provider.content.PersonaContentItem.java
com.kevelbreh.steamchat.provider.content.UserContentItem.java
com.kevelbreh.steamchat.steam2.SteamConnection.java
com.kevelbreh.steamchat.steam2.SteamEventBus.java
com.kevelbreh.steamchat.steam2.SteamService.java
com.kevelbreh.steamchat.steam2.handler.ConnectionHandler.java
com.kevelbreh.steamchat.steam2.handler.FriendHandler.java
com.kevelbreh.steamchat.steam2.handler.MessageHandler.java
com.kevelbreh.steamchat.steam2.handler.UserHandler.java
com.kevelbreh.steamchat.steam2.packet.Packet.java
com.kevelbreh.steamchat.steam2.packet.ProtoPacket.java
com.kevelbreh.steamchat.steam.SteamClient.java
com.kevelbreh.steamchat.steam.SteamID.java
com.kevelbreh.steamchat.steam.SteamServiceHandler.java
com.kevelbreh.steamchat.steam.SteamService.java
com.kevelbreh.steamchat.steam.handler2.ConnectionHandler.java
com.kevelbreh.steamchat.steam.handler2.FriendHandler.java
com.kevelbreh.steamchat.steam.handler2.Handler.java
com.kevelbreh.steamchat.steam.handler2.UserHandler.java
com.kevelbreh.steamchat.steam.handler.AEventHandler.java
com.kevelbreh.steamchat.steam.handler.AuthenticationHandler.java
com.kevelbreh.steamchat.steam.handler.FriendsHandler.java
com.kevelbreh.steamchat.steam.handler.IEventHandler.java
com.kevelbreh.steamchat.steam.handler.IHandler.java
com.kevelbreh.steamchat.steam.handler.MessageDebugHandler.java
com.kevelbreh.steamchat.steam.language.Language.java
com.kevelbreh.steamchat.steam.language.Message.java
com.kevelbreh.steamchat.steam.network.TCPConnection.java
com.kevelbreh.steamchat.steam.network.packet.ChannelEncryptRequest.java
com.kevelbreh.steamchat.steam.network.packet.ChannelEncryptResponse.java
com.kevelbreh.steamchat.steam.network.packet.ChannelEncryptResult.java
com.kevelbreh.steamchat.steam.network.packet.ClientLogOnResponse.java
com.kevelbreh.steamchat.steam.network.packet.ClientLogOn.java
com.kevelbreh.steamchat.steam.network.packet.HeartBeat.java
com.kevelbreh.steamchat.steam.network.packet.MultiPacket.java
com.kevelbreh.steamchat.steam.network.packet.Packet.java
com.kevelbreh.steamchat.steam.network.packet.ProtoPacket.java
com.kevelbreh.steamchat.steam.proto.DescriptorsProto.java
com.kevelbreh.steamchat.steam.proto.EncryptedAppTicketProto.java
com.kevelbreh.steamchat.steam.proto.SteamMessagesBaseProto.java
com.kevelbreh.steamchat.steam.proto.SteamMessagesClientServerProto.java
com.kevelbreh.steamchat.steam.security.AsnKeyParser.java
com.kevelbreh.steamchat.steam.security.AsnParser.java
com.kevelbreh.steamchat.steam.security.BerDecodeException.java
com.kevelbreh.steamchat.steam.security.Cryptography.java
com.kevelbreh.steamchat.steam.security.NetEncryption.java
com.kevelbreh.steamchat.steam.security.PublicKey.java
com.kevelbreh.steamchat.steam.security.RSA.java
com.kevelbreh.steamchat.steam.util.BinaryReader.java
com.kevelbreh.steamchat.steam.util.BinaryWriter.java
com.kevelbreh.steamchat.steam.util.BitVector64.java
com.kevelbreh.steamchat.steam.util.JenkinsHash.java
com.kevelbreh.steamchat.util.AServiceActivity.java
com.kevelbreh.steamchat.util.Dump.java
com.kevelbreh.steamchat.util.MiscUtils.java
com.kevelbreh.steamchat.widget.adapter.ChatAdapter.java
com.kevelbreh.steamchat.widget.adapter.ConversationAdapter.java
com.kevelbreh.steamchat.widget.adapter.FriendAdapter.java
com.kevelbreh.steamchat.widget.view.AvatarView.java
com.kevelbreh.steamchat.widget.view.FriendInteractionsView.java