Fire event before a packet is sent, allowing the operation to be modified or cancelled.

This commit is contained in:
Steveice10 2017-06-26 15:58:08 -07:00
parent a17d4769e8
commit e8a86dafcb
3 changed files with 111 additions and 15 deletions

View file

@ -0,0 +1,81 @@
package com.github.steveice10.packetlib.event.session;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.packet.Packet;
/**
* Called when the session is sending a packet.
*/
public class PacketSendingEvent implements SessionEvent {
private Session session;
private Packet packet;
private boolean cancelled = false;
/**
* Creates a new PacketSendingEvent instance.
*
* @param session Session sending the packet.
* @param packet Packet being sent.
*/
public PacketSendingEvent(Session session, Packet packet) {
this.session = session;
this.packet = packet;
}
/**
* Gets the session involved in this event.
*
* @return The event's session.
*/
public Session getSession() {
return this.session;
}
/**
* Gets the packet involved in this event as the required type.
*
* @param <T> Type of the packet.
* @return The event's packet as the required type.
* @throws IllegalStateException If the packet's value isn't of the required type.
*/
@SuppressWarnings("unchecked")
public <T extends Packet> T getPacket() {
try {
return (T) this.packet;
} catch(ClassCastException e) {
throw new IllegalStateException("Tried to get packet as the wrong type. Actual type: " + this.packet.getClass().getName());
}
}
/**
* Sets the packet that should be sent as a result of this event.
*
* @param packet The packet to send.
*/
public void setPacket(Packet packet) {
this.packet = packet;
}
/**
* Gets whether the event has been cancelled.
*
* @return Whether the event has been cancelled.
*/
public boolean isCancelled() {
return this.cancelled;
}
/**
* Sets whether the event should be cancelled.
*
* @param cancelled Whether the event should be cancelled.
*/
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
@Override
public void call(SessionListener listener) {
listener.packetSending(this);
}
}

View file

@ -11,6 +11,13 @@ public interface SessionListener {
*/
public void packetReceived(PacketReceivedEvent event);
/**
* Called when a session is sending a packet.
*
* @param event Data relating to the event.
*/
public void packetSending(PacketSendingEvent event);
/**
* Called when a session sends a packet.
*

View file

@ -2,6 +2,7 @@ package com.github.steveice10.packetlib.tcp;
import com.github.steveice10.packetlib.event.session.DisconnectingEvent;
import com.github.steveice10.packetlib.event.session.PacketReceivedEvent;
import com.github.steveice10.packetlib.event.session.PacketSendingEvent;
import com.github.steveice10.packetlib.event.session.SessionListener;
import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.packet.PacketProtocol;
@ -203,26 +204,33 @@ public abstract class TcpSession extends SimpleChannelInboundHandler<Packet> imp
}
@Override
public void send(final Packet packet) {
public void send(Packet packet) {
if(this.channel == null) {
return;
}
ChannelFuture future = this.channel.writeAndFlush(packet).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if(future.isSuccess()) {
callEvent(new PacketSentEvent(TcpSession.this, packet));
} else {
exceptionCaught(null, future.cause());
}
}
});
PacketSendingEvent sendingEvent = new PacketSendingEvent(this, packet);
this.callEvent(sendingEvent);
if(packet.isPriority()) {
try {
future.await();
} catch(InterruptedException e) {
if(!sendingEvent.isCancelled()) {
final Packet toSend = sendingEvent.getPacket();
ChannelFuture future = this.channel.writeAndFlush(toSend).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if(future.isSuccess()) {
callEvent(new PacketSentEvent(TcpSession.this, toSend));
} else {
exceptionCaught(null, future.cause());
}
}
});
if(toSend.isPriority()) {
try {
future.await();
} catch(InterruptedException e) {
}
}
}
}