RuneScape 2 Development > Snippets

Building Messages

<<< (3/3)

sini:

--- Quote from: Davidi2 on January 13, 2016, 06:53:25 PM ---
--- Quote from: Davidi2 on January 13, 2016, 04:09:12 PM ---
--- Code: Java ---parseName(FIVE_THOUSAND_FIFTY_FOUR.name())
--- End quote ---

--- Quote from: sini on January 13, 2016, 06:50:32 PM ---I cant tell if you're joking

--- End quote ---

--- End quote ---

but its you...you may do that...

Vix:
If anyone was interested this was the rest of the system. Not including all of the code, just enough to give an idea how i personally implemented this. Once again, this is old code I haven't worked with in a while.

Two methods pulled from the ChannelContext class. Prepare is called when the outgoing packet is first created. Execute just executes an event.


--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---        /**         * Prepares the channel's internal buffer for the writing of a message.         *          * @param packet The message of interest.         *          * @param buffer The internal buffer.         */        public final void prepare(OutgoingPacket packet, ChannelBuffer buffer) {                 execute(new PrepareChannelEvent(packet.getHeader(), buffer, packet.getOpcode()));        }         /**         * Executes a single channel pertinent event.         *          * @param event The event receiving the execution.         */        public final void execute(ChannelEvent event) {                 try {                         event.execute(this);                 } catch (IOException exception) {                         close();                }        }
An event pertinent to channel operation. Reading/Writing/Accepting connections.


--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---package com.runescape.oceanus.net.channel; import java.io.IOException; public abstract class ChannelEvent {         /**         * Executes an event pertinent to channel operation.         *          * @param context The context of the channel that is executing the event.         *          * @throws IOException The exception thrown if an error occurs while performing         * an input or output operation involving the channel.         */        public abstract void execute(ChannelContext context) throws IOException;}
An event to prepare the channel to send an outgoing packet.


--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---package com.runescape.oceanus.net.channel.events; import java.io.IOException; import com.runescape.oceanus.net.ByteValue;import com.runescape.oceanus.net.channel.ChannelBuffer;import com.runescape.oceanus.net.channel.ChannelContext;import com.runescape.oceanus.net.channel.ChannelEvent;import com.runescape.oceanus.net.channel.protocol.packet.PacketHeader; public final class PrepareChannelEvent extends ChannelEvent {         /**         * The message's heading definition.         */        private final PacketHeader definition;         /**         * The internal buffer.         */        private final ChannelBuffer buffer;         /**         * The numerical opcode.         */        private final int opcode;         /**         * The overloaded class constructor used for the instantiation of this         * class file.         *          * @param definition The message's heading definition.         *          * @param buffer The internal buffer.         *          * @param opcode The numerical opcode.         */        public PrepareChannelEvent(PacketHeader definition, ChannelBuffer buffer, int opcode) {                 this.definition = definition;                 this.buffer = buffer;                 this.opcode = opcode;        }         @Override        public void execute(ChannelContext context) throws IOException {                 /*                 * If a packet does not have an empty heading then the opcode must be encrypted                 * with a cryptographic cipher. After the encryption if the packet's heading is                 * defined as a byte or a short the length must be indicated by that primitive.                 */                if (!definition.equals(PacketHeader.EMPTY_PACKET_HEADER)) {                         buffer.writeByte(opcode + context.getPlayer().getCryptographyPair().getDecoder().getNextValue(), ByteValue.STANDARD);                         if (definition.equals(PacketHeader.VARIABLE_BYTE_PACKET_HEADER)) {                                 buffer.setLength(buffer.getInternal().position());                                 buffer.writeByte(0, ByteValue.STANDARD);                         } else if (definition.equals(PacketHeader.VARIABLE_SHORT_PACKET_HEADER)) {                                 buffer.setLength(buffer.getInternal().position());                                 buffer.writeShort(0, ByteValue.STANDARD);                        }                }        }}
An event to finish writing the packet.


--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---package com.runescape.oceanus.net.channel.events; import java.io.IOException; import com.runescape.oceanus.net.channel.ChannelBuffer;import com.runescape.oceanus.net.channel.ChannelContext;import com.runescape.oceanus.net.channel.ChannelEvent;import com.runescape.oceanus.net.channel.protocol.packet.PacketHeader; public final class WriteChannelEvent extends ChannelEvent {         /**         * The message's heading definition.         */        private final PacketHeader definition;         /**         * The internal buffer.         */        private final ChannelBuffer buffer;         /**         * The overloaded class constructor used for the instantiation of         * this class file.         *          * @param definition The message's heading definition.         *          * @param buffer The internal buffer.         */        public WriteChannelEvent(PacketHeader definition, ChannelBuffer buffer) {                 this.definition = definition;                 this.buffer = buffer;        }         @Override        public void execute(ChannelContext context) throws IOException {                 if (definition.equals(PacketHeader.VARIABLE_BYTE_PACKET_HEADER)) {                         buffer.getInternal().put(buffer.getLength(), (byte) (buffer.getInternal().position() - buffer.getLength() - 1));                 } else if (definition.equals(PacketHeader.VARIABLE_SHORT_PACKET_HEADER)) {                         buffer.getInternal().putShort(buffer.getLength(), (short) (buffer.getInternal().position() - buffer.getLength() - 2));                }                 /*                 * The limit is set to the current position and then the position is set to zero.                 */                buffer.getInternal().flip();                 /*                 * Writes a sequence of bytes to the specified channel.                 */                context.getChannel().write(buffer.getInternal());                 /*                 * Clears this buffer. The position is set to zero, the limit is set to the capacity.                 */                buffer.getInternal().clear();        }}
Base class for outgoing packets. Prepare event is triggered everytime a new outgoing packet is created.


--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---package com.runescape.oceanus.net.channel.protocol.packet; import java.io.IOException;import java.nio.ByteBuffer; import com.runescape.oceanus.net.channel.ChannelBuffer;import com.runescape.oceanus.net.channel.ChannelContext; public abstract class OutgoingPacket {         /**         * The opcode of the packet.         */        private final int opcode;         /**         * The heading of the packet.         */        private final PacketHeader header;         /**         * The context of the channel that is dispatching the packet.         */        protected ChannelContext context;         /**         * The packet's internal buffer.         */        protected ChannelBuffer buffer;         /**         * Dispatches the packet to the associated channel.         *          * @param player The distributor.         *          * @throws IOException The exception thrown if an error occurs while         * building or dispensing the packet.         */        public abstract ChannelBuffer dispatch() throws IOException;         /**         * The overloaded class constructor used for instantiation of this         * class file.         *          * @param opcode The opcode of the packet.         *          * @param header The heading of the packet.         *          * @param context The context of the channel that is dispatching the packet.         *          * @param allocation The allocation of the packet's internal buffer.         */        public OutgoingPacket(int opcode, PacketHeader header, ChannelContext context, int allocation) {                 this.opcode = opcode;                 this.header = header;                 this.context = context;                 this.buffer = new ChannelBuffer(ByteBuffer.allocate(allocation));                 context.prepare(this, buffer);        }         /**         * Returns the opcode of the packet.         *          * @return The returned opcode.         */        public final int getOpcode() {                 return opcode;        }         /**         * Returns the heading of the packet.         *          * @return The returned heading.         */        public final PacketHeader getHeader() {                 return header;        }}
An example packet.


--- Code: Java(TM) 2 Platform Standard Edition 5.0 ---package com.runescape.oceanus.net.channel.protocol.packet.out; import java.io.IOException;import java.util.Iterator; import com.runescape.oceanus.Oceanus;import com.runescape.oceanus.model.entity.mobile.player.Player;import com.runescape.oceanus.model.entity.mobile.player.update.UpdateBlock;import com.runescape.oceanus.model.entity.mobile.player.update.impl.PlayerMovementBlock;import com.runescape.oceanus.model.entity.mobile.player.update.impl.RegionalMovementBlock;import com.runescape.oceanus.model.entity.mobile.player.update.impl.StatefulUpdateBlock;import com.runescape.oceanus.net.ByteAccess;import com.runescape.oceanus.net.channel.ChannelBuffer;import com.runescape.oceanus.net.channel.ChannelContext;import com.runescape.oceanus.net.channel.protocol.packet.OutgoingPacket;import com.runescape.oceanus.net.channel.protocol.packet.PacketHeader; public final class PlayerUpdatePacket extends OutgoingPacket {         /**         * The internal buffer responsible for pooling the update block.         */        private final ChannelBuffer update;         /**         * The overloaded class constructor used for instantiation of this         * class file.         *          * @param context The context of the channel that is dispatching the packet.         */        public PlayerUpdatePacket(ChannelContext context, ChannelBuffer update) {                 super(81, PacketHeader.VARIABLE_SHORT_PACKET_HEADER, context, 16384);                 this.update = update;        }         /**         * Appends an individual piece of the main updating block.         *          * @param block The piece of the main updating block.         *          * @param buffer The internal buffer.         *          * @param player The player being updated.         */        public final void append(UpdateBlock block, ChannelBuffer buffer, Player player) {                 block.update(player, buffer);        }         @Override        public ChannelBuffer dispatch() throws IOException {                 final Player player = context.getPlayer();                 buffer.setAccessType(ByteAccess.BIT_ACCESS);                 append(new PlayerMovementBlock(), buffer, player);                 if (player.updateRequired()) {                         append(new StatefulUpdateBlock(false), update, player);                }                 buffer.writeBits(8, player.getLocalPlayers().size());                 for(Iterator<Player> iterator = player.getLocalPlayers().iterator(); iterator.hasNext();) {                         final Player other = iterator.next();                         if (other.getLocation().isWithinDistance(player.getLocation(), 15)) {                                 append(new RegionalMovementBlock(), buffer, other);                                 if (other.updateRequired()) {                                         append(new StatefulUpdateBlock(false), update, other);                                 }                         } else {                                 iterator.remove();                                 buffer.writeBits(1, 1);                                 buffer.writeBits(2, 3);                        }                }                 for (Player other : Oceanus.getUpdateProcessor().getPlayers().values()) {                         if (other.getLocalPlayers().size() >= 255) {                                 break;                        }                         if (other == player || player.getLocalPlayers().contains(other)) {                                 continue;                         }                        if (other.getLocation().isWithinDistance(player.getLocation(), 15)) {                                 player.getLocalPlayers().add(other);                                 buffer.writeBits(11, other.getIndex());                                 buffer.writeBits(1, 1);                                 buffer.writeBits(1, 1);                                 buffer.writeBits(5, other.getLocation().getY() - player.getLocation().getY());                                 buffer.writeBits(5, other.getLocation().getX() - player.getLocation().getX());                                 append(new StatefulUpdateBlock(true), update, other);                         }                }                 if (update.getInternal().position() > 0) {                         buffer.writeBits(11, 2047);                         buffer.setAccessType(ByteAccess.BYTE_ACCESS);                         buffer.writeBytes(update.getInternal());                 } else {                         buffer.setAccessType(ByteAccess.BYTE_ACCESS);                }                 return buffer;        }}

Navigation

[0] Message Index

[*] Previous page

Go to full version
Powered by SMFPacks SEO Pro Mod |