2013-07-17 03:52:27 +02:00
|
|
|
package com.comphenix.protocol.events;
|
|
|
|
|
2022-07-24 17:02:56 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import java.util.*;
|
|
|
|
|
2013-12-06 18:02:26 +01:00
|
|
|
import com.comphenix.protocol.PacketType;
|
2014-04-25 02:55:17 +02:00
|
|
|
import com.comphenix.protocol.ProtocolManager;
|
2013-07-17 03:52:27 +02:00
|
|
|
import com.google.common.base.Preconditions;
|
2022-07-24 17:02:56 +02:00
|
|
|
|
2022-03-08 04:09:04 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2013-07-17 03:52:27 +02:00
|
|
|
|
2013-07-17 19:49:00 +02:00
|
|
|
/**
|
2022-03-08 04:09:04 +01:00
|
|
|
* Marker containing the serialized packet data seen from the network, or output handlers that will serialize the
|
|
|
|
* current packet.
|
|
|
|
*
|
2013-07-17 19:49:00 +02:00
|
|
|
* @author Kristian
|
|
|
|
*/
|
2022-03-08 04:09:04 +01:00
|
|
|
public class NetworkMarker {
|
2013-12-06 18:02:26 +01:00
|
|
|
|
2022-03-08 04:09:04 +01:00
|
|
|
// The input data
|
|
|
|
private final PacketType type;
|
|
|
|
private final ConnectionSide side;
|
2013-12-06 18:02:26 +01:00
|
|
|
|
2022-03-08 04:09:04 +01:00
|
|
|
// Post-processing of the packet
|
|
|
|
private Set<PacketPostListener> postListeners;
|
2022-05-26 05:24:59 +02:00
|
|
|
private Deque<ScheduledPacket> scheduledPackets;
|
2013-12-06 18:02:26 +01:00
|
|
|
|
2013-07-17 03:52:27 +02:00
|
|
|
/**
|
|
|
|
* Construct a new network marker.
|
|
|
|
* <p>
|
|
|
|
* The input buffer is only non-null for client-side packets.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2015-06-17 20:25:39 +02:00
|
|
|
* @param side - which side this marker belongs to.
|
|
|
|
* @param type - packet type
|
2013-07-17 03:52:27 +02:00
|
|
|
*/
|
2022-03-08 04:09:04 +01:00
|
|
|
public NetworkMarker(@Nonnull ConnectionSide side, PacketType type) {
|
2013-07-17 03:52:27 +02:00
|
|
|
this.side = Preconditions.checkNotNull(side, "side cannot be NULL.");
|
2013-12-06 18:10:22 +01:00
|
|
|
this.type = type;
|
2013-07-17 03:52:27 +02:00
|
|
|
}
|
|
|
|
|
2013-07-17 20:07:02 +02:00
|
|
|
/**
|
2022-03-08 04:09:04 +01:00
|
|
|
* Determine if the given marker has any post listeners.
|
|
|
|
*
|
|
|
|
* @param marker - the marker to check.
|
|
|
|
* @return TRUE if it does, FALSE otherwise.
|
2013-07-17 19:49:00 +02:00
|
|
|
*/
|
2022-03-08 04:09:04 +01:00
|
|
|
public static boolean hasPostListeners(NetworkMarker marker) {
|
|
|
|
return marker != null && !marker.getPostListeners().isEmpty();
|
2013-12-06 18:02:26 +01:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-12-06 18:02:26 +01:00
|
|
|
/**
|
2022-03-08 04:09:04 +01:00
|
|
|
* Retrieve the network marker of a particular event without creating it.
|
2013-12-06 18:02:26 +01:00
|
|
|
* <p>
|
2022-03-08 04:09:04 +01:00
|
|
|
* This is an internal method that should not be used by API users.
|
|
|
|
*
|
|
|
|
* @param event - the event.
|
|
|
|
* @return The network marker.
|
2013-12-06 18:02:26 +01:00
|
|
|
*/
|
2022-03-08 04:09:04 +01:00
|
|
|
public static NetworkMarker getNetworkMarker(PacketEvent event) {
|
|
|
|
return event.networkMarker;
|
2013-07-17 19:49:00 +02:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-17 03:52:27 +02:00
|
|
|
/**
|
2022-03-08 04:09:04 +01:00
|
|
|
* Retrieve the scheduled packets of a particular network marker without constructing the list.
|
2013-07-17 03:52:27 +02:00
|
|
|
* <p>
|
2022-03-08 04:09:04 +01:00
|
|
|
* This is an internal method that should not be used by API users.
|
|
|
|
*
|
|
|
|
* @param marker - the marker.
|
|
|
|
* @return The list, or NULL if not found or initialized.
|
2013-07-17 03:52:27 +02:00
|
|
|
*/
|
2022-05-26 05:24:59 +02:00
|
|
|
public static Deque<ScheduledPacket> readScheduledPackets(NetworkMarker marker) {
|
2022-03-08 04:09:04 +01:00
|
|
|
return marker.scheduledPackets;
|
2013-07-17 03:52:27 +02:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-17 03:52:27 +02:00
|
|
|
/**
|
2022-03-08 04:09:04 +01:00
|
|
|
* Retrieve whether or not this marker belongs to a client or a server side packet.
|
|
|
|
*
|
|
|
|
* @return The side the parent packet belongs to.
|
2013-07-17 03:52:27 +02:00
|
|
|
*/
|
2022-03-08 04:09:04 +01:00
|
|
|
public ConnectionSide getSide() {
|
|
|
|
return this.side;
|
2013-07-17 03:52:27 +02:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
|
|
|
public PacketType getType() {
|
|
|
|
return this.type;
|
2013-07-17 03:52:27 +02:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2014-04-25 02:55:17 +02:00
|
|
|
/**
|
2022-03-08 04:09:04 +01:00
|
|
|
* Add a listener that is invoked after a packet has been successfully sent to the client, or received by the server.
|
2014-04-25 02:55:17 +02:00
|
|
|
* <p>
|
2022-03-08 04:09:04 +01:00
|
|
|
* Received packets are not guarenteed to have been fully processed, but packets passed to {@link
|
|
|
|
* ProtocolManager#receiveClientPacket(Player, PacketContainer)} will be processed after the current packet event.
|
2014-04-25 02:55:17 +02:00
|
|
|
* <p>
|
2022-03-08 04:09:04 +01:00
|
|
|
* Note that post listeners will be executed asynchronously off the main thread. They are not executed in any defined
|
|
|
|
* order.
|
|
|
|
*
|
2014-04-25 02:55:17 +02:00
|
|
|
* @param listener - the listener that will be invoked.
|
|
|
|
* @return TRUE if it was added.
|
|
|
|
*/
|
|
|
|
public boolean addPostListener(PacketPostListener listener) {
|
2022-03-08 04:09:04 +01:00
|
|
|
if (this.postListeners == null) {
|
|
|
|
this.postListeners = new HashSet<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.postListeners.add(listener);
|
2014-04-25 02:55:17 +02:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2014-04-25 02:55:17 +02:00
|
|
|
/**
|
|
|
|
* Remove the first instance of the given listener.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2014-04-25 02:55:17 +02:00
|
|
|
* @param listener - listener to remove.
|
|
|
|
* @return TRUE if it was removed, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
public boolean removePostListener(PacketPostListener listener) {
|
2022-03-08 04:09:04 +01:00
|
|
|
if (this.postListeners != null) {
|
|
|
|
return this.postListeners.remove(listener);
|
2014-04-25 02:55:17 +02:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2014-04-25 02:55:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2014-04-25 02:55:17 +02:00
|
|
|
/**
|
|
|
|
* Retrieve an immutable view of all the listeners that will be invoked once the packet has been sent or received.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2014-04-25 02:55:17 +02:00
|
|
|
* @return Every post packet listener. Never NULL.
|
|
|
|
*/
|
2022-03-08 04:09:04 +01:00
|
|
|
public Set<PacketPostListener> getPostListeners() {
|
|
|
|
return this.postListeners != null ? this.postListeners : Collections.emptySet();
|
2014-04-25 02:55:17 +02:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2014-04-26 00:20:50 +02:00
|
|
|
/**
|
2022-05-26 05:24:59 +02:00
|
|
|
* Retrieve a modifiable queue of packets that will be schedule (in-order) when the current packet has been
|
|
|
|
* successfully transmitted.
|
2022-03-08 04:09:04 +01:00
|
|
|
*
|
2022-05-26 05:24:59 +02:00
|
|
|
* @return the queue of packets to schedule after this packet, in order.
|
2014-04-26 00:20:50 +02:00
|
|
|
*/
|
2022-05-26 05:24:59 +02:00
|
|
|
public Deque<ScheduledPacket> getScheduledPackets() {
|
2022-03-08 04:09:04 +01:00
|
|
|
if (this.scheduledPackets == null) {
|
2022-05-26 05:24:59 +02:00
|
|
|
this.scheduledPackets = new LinkedList<>();
|
2022-03-08 04:09:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.scheduledPackets;
|
2014-04-26 00:20:50 +02:00
|
|
|
}
|
2022-03-08 04:09:04 +01:00
|
|
|
|
2013-07-17 03:52:27 +02:00
|
|
|
/**
|
|
|
|
* Ensure that the packet event is server side.
|
|
|
|
*/
|
|
|
|
private void checkServerSide() {
|
2022-03-08 04:09:04 +01:00
|
|
|
if (this.side.isForClient()) {
|
2013-07-17 03:52:27 +02:00
|
|
|
throw new IllegalStateException("Must be a server side packet.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|