Replace reflection usage with a supplier

This commit is contained in:
RednedEpic 2021-11-13 19:59:20 -06:00
parent ec983f1447
commit 1188c1486f
3 changed files with 29 additions and 38 deletions

View file

@ -3,23 +3,23 @@ package com.github.steveice10.packetlib;
import com.github.steveice10.packetlib.event.server.*;
import com.github.steveice10.packetlib.packet.PacketProtocol;
import java.lang.reflect.Constructor;
import java.util.*;
import java.util.function.Supplier;
public abstract class AbstractServer implements Server {
private final String host;
private final int port;
private final Class<? extends PacketProtocol> protocol;
private final Supplier<? extends PacketProtocol> protocolSupplier;
private final List<Session> sessions = new ArrayList<>();
private final Map<String, Object> flags = new HashMap<>();
private final List<ServerListener> listeners = new ArrayList<>();
public AbstractServer(String host, int port, Class<? extends PacketProtocol> protocol) {
public AbstractServer(String host, int port, Supplier<? extends PacketProtocol> protocolSupplier) {
this.host = host;
this.port = port;
this.protocol = protocol;
this.protocolSupplier = protocolSupplier;
}
@Override
@ -33,23 +33,12 @@ public abstract class AbstractServer implements Server {
}
@Override
public Class<? extends PacketProtocol> getPacketProtocol() {
return this.protocol;
public Supplier<? extends PacketProtocol> getPacketProtocol() {
return this.protocolSupplier;
}
protected PacketProtocol createPacketProtocol() {
try {
Constructor<? extends PacketProtocol> constructor = this.protocol.getDeclaredConstructor();
if(!constructor.isAccessible()) {
constructor.setAccessible(true);
}
return constructor.newInstance();
} catch(NoSuchMethodError e) {
throw new IllegalStateException("PacketProtocol \"" + this.protocol.getName() + "\" does not have a no-params constructor for instantiation.");
} catch(Exception e) {
throw new IllegalStateException("Failed to instantiate PacketProtocol " + this.protocol.getName() + ".", e);
}
return this.protocolSupplier.get();
}
@Override

View file

@ -5,6 +5,7 @@ import com.github.steveice10.packetlib.packet.PacketProtocol;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
/**
* Listens for new sessions to connect.
@ -15,35 +16,35 @@ public interface Server {
*
* @return The listening host.
*/
public String getHost();
String getHost();
/**
* Gets the port the session is listening on.
*
* @return The listening port.
*/
public int getPort();
int getPort();
/**
* Gets the packet protocol of the server.
*
* @return The server's packet protocol.
*/
public Class<? extends PacketProtocol> getPacketProtocol();
Supplier<? extends PacketProtocol> getPacketProtocol();
/**
* Returns true if the listener is listening.
*
* @return True if the listener is listening.
*/
public boolean isListening();
boolean isListening();
/**
* Gets this server's set flags.
*
* @return This server's flags.
*/
public Map<String, Object> getGlobalFlags();
Map<String, Object> getGlobalFlags();
/**
* Checks whether this server has a flag set.
@ -51,7 +52,7 @@ public interface Server {
* @param key Key of the flag to check for.
* @return Whether this server has a flag set.
*/
public boolean hasGlobalFlag(String key);
boolean hasGlobalFlag(String key);
/**
* Gets the value of the given flag as an instance of the given type.
@ -61,7 +62,7 @@ public interface Server {
* @return Value of the flag.
* @throws IllegalStateException If the flag's value isn't of the required type.
*/
public <T> T getGlobalFlag(String key);
<T> T getGlobalFlag(String key);
/**
* Gets the value of the given flag as an instance of the given type.
@ -74,7 +75,7 @@ public interface Server {
* @throws IllegalStateException If the flag's value isn't of the required type.
*/
@SuppressWarnings("unchecked")
public <T> T getGlobalFlag(String key, T def);
<T> T getGlobalFlag(String key, T def);
/**
* Sets the value of a flag. The flag will be used in sessions if a session does
@ -83,47 +84,47 @@ public interface Server {
* @param key Key of the flag.
* @param value Value to set the flag to.
*/
public void setGlobalFlag(String key, Object value);
void setGlobalFlag(String key, Object value);
/**
* Gets the listeners listening on this session.
*
* @return This server's listeners.
*/
public List<ServerListener> getListeners();
List<ServerListener> getListeners();
/**
* Adds a listener to this server.
*
* @param listener Listener to add.
*/
public void addListener(ServerListener listener);
void addListener(ServerListener listener);
/**
* Removes a listener from this server.
*
* @param listener Listener to remove.
*/
public void removeListener(ServerListener listener);
void removeListener(ServerListener listener);
/**
* Gets all sessions belonging to this server.
*
* @return Sessions belonging to this server.
*/
public List<Session> getSessions();
List<Session> getSessions();
/**
* Binds the listener to its host and port.
*/
public AbstractServer bind();
AbstractServer bind();
/**
* Binds the listener to its host and port.
*
* @param wait Whether to wait for the listener to finish binding.
*/
public AbstractServer bind(boolean wait);
AbstractServer bind(boolean wait);
/**
* Binds the listener to its host and port.
@ -131,19 +132,19 @@ public interface Server {
* @param wait Whether to wait for the listener to finish binding.
* @param callback Callback to call when the listener has finished binding.
*/
public AbstractServer bind(boolean wait, Runnable callback);
AbstractServer bind(boolean wait, Runnable callback);
/**
* Closes the listener.
*/
public void close();
void close();
/**
* Closes the listener.
*
* @param wait Whether to wait for the listener to finish closing.
*/
public void close(boolean wait);
void close(boolean wait);
/**
* Closes the listener.
@ -151,5 +152,5 @@ public interface Server {
* @param wait Whether to wait for the listener to finish closing.
* @param callback Callback to call when the listener has finished closing.
*/
public void close(boolean wait, Runnable callback);
void close(boolean wait, Runnable callback);
}

View file

@ -24,13 +24,14 @@ import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import java.net.InetSocketAddress;
import java.util.function.Supplier;
public class TcpServer extends AbstractServer {
private EventLoopGroup group;
private Class<? extends ServerSocketChannel> serverSocketChannel;
private Channel channel;
public TcpServer(String host, int port, Class<? extends PacketProtocol> protocol) {
public TcpServer(String host, int port, Supplier<? extends PacketProtocol> protocol) {
super(host, port, protocol);
}