2017-08-22 15:02:23 +02:00
package net.ME1312.SubServers.Sync.Network ;
2018-04-15 03:53:51 +02:00
import com.google.gson.Gson ;
import com.google.gson.JsonParseException ;
2017-09-24 05:19:22 +02:00
import net.ME1312.SubServers.Sync.Event.SubNetworkConnectEvent ;
2017-08-22 15:02:23 +02:00
import net.ME1312.SubServers.Sync.Event.SubNetworkDisconnectEvent ;
2018-04-15 03:53:51 +02:00
import net.ME1312.SubServers.Sync.Library.Config.YAMLSection ;
2017-08-22 15:02:23 +02:00
import net.ME1312.SubServers.Sync.Library.Exception.IllegalPacketException ;
import net.ME1312.SubServers.Sync.Library.NamedContainer ;
import net.ME1312.SubServers.Sync.Library.Util ;
import net.ME1312.SubServers.Sync.Library.Version.Version ;
2018-01-05 23:30:01 +01:00
import net.ME1312.SubServers.Sync.Network.Encryption.AES ;
2017-08-22 15:02:23 +02:00
import net.ME1312.SubServers.Sync.Network.Packet.* ;
import net.ME1312.SubServers.Sync.SubPlugin ;
2018-04-15 03:53:51 +02:00
import org.yaml.snakeyaml.error.YAMLException ;
2017-08-22 15:02:23 +02:00
import java.io.BufferedReader ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.io.PrintWriter ;
import java.lang.reflect.InvocationTargetException ;
import java.net.InetAddress ;
import java.net.Socket ;
import java.net.SocketException ;
2017-11-22 22:58:33 +01:00
import java.nio.charset.StandardCharsets ;
2017-08-22 15:02:23 +02:00
import java.util.* ;
import java.util.concurrent.TimeUnit ;
/ * *
* SubData Direct Client Class
* /
public final class SubDataClient {
2018-07-29 20:39:42 +02:00
private static HashMap < Class < ? extends PacketOut > , NamedContainer < String , String > > pOut = new HashMap < Class < ? extends PacketOut > , NamedContainer < String , String > > ( ) ;
private static HashMap < String , HashMap < String , List < PacketIn > > > pIn = new HashMap < String , HashMap < String , List < PacketIn > > > ( ) ;
2018-01-05 21:37:23 +01:00
private static HashMap < String , Cipher > ciphers = new HashMap < String , Cipher > ( ) ;
2017-08-22 15:02:23 +02:00
private static boolean defaults = false ;
private PrintWriter writer ;
2017-09-24 05:19:22 +02:00
private NamedContainer < Boolean , Socket > socket ;
2018-01-22 16:01:33 +01:00
private String name = null ;
2018-01-05 21:37:23 +01:00
private Cipher cipher ;
2017-08-22 15:02:23 +02:00
private SubPlugin plugin ;
private LinkedList < NamedContainer < String , PacketOut > > queue ;
/ * *
* SubServers Client Instance
*
* @param plugin SubPlugin
* @param address Address
* @param port Port
2018-01-05 21:37:23 +01:00
* @param cipher Cipher
2017-08-22 15:02:23 +02:00
* @throws IOException
* /
2018-01-22 16:01:33 +01:00
public SubDataClient ( SubPlugin plugin , String name , InetAddress address , int port , Cipher cipher ) throws IOException {
2017-08-22 15:02:23 +02:00
if ( Util . isNull ( plugin , address , port ) ) throw new NullPointerException ( ) ;
2017-09-24 05:19:22 +02:00
socket = new NamedContainer < > ( false , new Socket ( address , port ) ) ;
2017-08-22 15:02:23 +02:00
this . plugin = plugin ;
2018-01-22 16:01:33 +01:00
this . name = name ;
2017-09-24 05:19:22 +02:00
this . writer = new PrintWriter ( socket . get ( ) . getOutputStream ( ) , true ) ;
2018-01-05 21:37:23 +01:00
this . cipher = ( cipher ! = null ) ? cipher : new Cipher ( ) {
@Override
public String getName ( ) {
return " NONE " ;
}
@Override
2018-04-15 03:53:51 +02:00
public byte [ ] encrypt ( String key , YAMLSection data ) {
return data . toJSON ( ) . getBytes ( StandardCharsets . UTF_8 ) ;
2018-01-05 21:37:23 +01:00
}
@Override
2018-04-15 03:53:51 +02:00
@SuppressWarnings ( " unchecked " )
public YAMLSection decrypt ( String key , byte [ ] data ) {
return new YAMLSection ( new Gson ( ) . fromJson ( new String ( data , StandardCharsets . UTF_8 ) , Map . class ) ) ;
2018-01-05 21:37:23 +01:00
}
} ;
2017-08-22 15:02:23 +02:00
this . queue = new LinkedList < NamedContainer < String , PacketOut > > ( ) ;
if ( ! defaults ) loadDefaults ( ) ;
loop ( ) ;
2017-09-24 05:19:22 +02:00
sendPacket ( new NamedContainer < > ( null , new PacketAuthorization ( plugin ) ) ) ;
}
private void init ( ) {
plugin . subdata . sendPacket ( new PacketDownloadLang ( plugin ) ) ;
2018-01-22 16:01:33 +01:00
plugin . subdata . sendPacket ( new PacketLinkProxy ( plugin ) ) ;
2018-04-15 03:53:51 +02:00
plugin . subdata . sendPacket ( new PacketDownloadProxyInfo ( proxy - > plugin . subdata . sendPacket ( new PacketDownloadServerList ( null , null , data - > {
if ( plugin . lastReload ! = proxy . getSection ( " subservers " ) . getLong ( " last-reload " ) ) {
2017-12-10 01:30:06 +01:00
System . out . println ( " SubServers > Resetting Server Data " ) ;
plugin . servers . clear ( ) ;
2018-04-15 03:53:51 +02:00
plugin . lastReload = proxy . getSection ( " subservers " ) . getLong ( " last-reload " ) ;
2017-12-10 01:30:06 +01:00
}
2018-04-15 03:53:51 +02:00
for ( String host : data . getSection ( " hosts " ) . getKeys ( ) ) {
for ( String subserver : data . getSection ( " hosts " ) . getSection ( host ) . getSection ( " servers " ) . getKeys ( ) ) {
plugin . merge ( subserver , data . getSection ( " hosts " ) . getSection ( host ) . getSection ( " servers " ) . getSection ( subserver ) , true ) ;
2017-09-24 05:19:22 +02:00
}
}
2018-04-15 03:53:51 +02:00
for ( String server : data . getSection ( " servers " ) . getKeys ( ) ) {
plugin . merge ( server , data . getSection ( " servers " ) . getSection ( server ) , false ) ;
2017-08-22 15:02:23 +02:00
}
2017-12-10 01:30:06 +01:00
} ) ) ) ) ;
2017-09-24 05:19:22 +02:00
while ( queue . size ( ) ! = 0 ) {
sendPacket ( queue . get ( 0 ) ) ;
queue . remove ( 0 ) ;
}
socket . rename ( true ) ;
plugin . getPluginManager ( ) . callEvent ( new SubNetworkConnectEvent ( this ) ) ;
2017-08-22 15:02:23 +02:00
}
2018-01-05 21:37:23 +01:00
static {
addCipher ( " AES " , new AES ( 128 ) ) ;
addCipher ( " AES_128 " , new AES ( 128 ) ) ;
addCipher ( " AES_192 " , new AES ( 192 ) ) ;
addCipher ( " AES_256 " , new AES ( 256 ) ) ;
} private void loadDefaults ( ) {
2017-08-22 15:02:23 +02:00
defaults = true ;
2018-07-29 20:39:42 +02:00
registerPacket ( new PacketAuthorization ( plugin ) , " SubData " , " Authorization " ) ;
registerPacket ( new PacketCommandServer ( ) , " SubServers " , " CommandServer " ) ;
registerPacket ( new PacketCreateServer ( ) , " SubServers " , " CreateServer " ) ;
registerPacket ( new PacketDownloadHostInfo ( ) , " SubServers " , " DownloadHostInfo " ) ;
registerPacket ( new PacketDownloadLang ( plugin ) , " SubServers " , " DownloadLang " ) ;
registerPacket ( new PacketDownloadNetworkList ( ) , " SubServers " , " DownloadNetworkList " ) ;
registerPacket ( new PacketDownloadPlayerList ( ) , " SubServers " , " DownloadPlayerList " ) ;
registerPacket ( new PacketDownloadProxyInfo ( ) , " SubServers " , " DownloadProxyInfo " ) ;
registerPacket ( new PacketDownloadServerInfo ( ) , " SubServers " , " DownloadServerInfo " ) ;
registerPacket ( new PacketDownloadServerList ( ) , " SubServers " , " DownloadServerList " ) ;
registerPacket ( new PacketInRunEvent ( ) , " SubServers " , " RunEvent " ) ;
registerPacket ( new PacketInReset ( ) , " SubServers " , " Reset " ) ;
registerPacket ( new PacketLinkProxy ( plugin ) , " SubServers " , " LinkProxy " ) ;
registerPacket ( new PacketStartServer ( ) , " SubServers " , " StartServer " ) ;
registerPacket ( new PacketStopServer ( ) , " SubServers " , " StopServer " ) ;
2017-08-22 15:02:23 +02:00
2018-07-29 20:39:42 +02:00
registerPacket ( PacketAuthorization . class , " SubData " , " Authorization " ) ;
registerPacket ( PacketCommandServer . class , " SubServers " , " CommandServer " ) ;
registerPacket ( PacketCreateServer . class , " SubServers " , " CreateServer " ) ;
registerPacket ( PacketDownloadHostInfo . class , " SubServers " , " DownloadHostInfo " ) ;
registerPacket ( PacketDownloadLang . class , " SubServers " , " DownloadLang " ) ;
registerPacket ( PacketDownloadNetworkList . class , " SubServers " , " DownloadNetworkList " ) ;
registerPacket ( PacketDownloadPlayerList . class , " SubServers " , " DownloadPlayerList " ) ;
registerPacket ( PacketDownloadProxyInfo . class , " SubServers " , " DownloadProxyInfo " ) ;
registerPacket ( PacketDownloadServerInfo . class , " SubServers " , " DownloadServerInfo " ) ;
registerPacket ( PacketDownloadServerList . class , " SubServers " , " DownloadServerList " ) ;
registerPacket ( PacketLinkProxy . class , " SubServers " , " LinkProxy " ) ;
registerPacket ( PacketStartServer . class , " SubServers " , " StartServer " ) ;
registerPacket ( PacketStopServer . class , " SubServers " , " StopServer " ) ;
2017-08-22 15:02:23 +02:00
}
private void loop ( ) {
new Thread ( ( ) - > {
try {
2017-09-24 05:19:22 +02:00
BufferedReader in = new BufferedReader ( new InputStreamReader ( socket . get ( ) . getInputStream ( ) ) ) ;
2017-08-22 15:02:23 +02:00
String input ;
while ( ( input = in . readLine ( ) ) ! = null ) {
try {
2018-04-15 03:53:51 +02:00
YAMLSection data = cipher . decrypt ( plugin . config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Password " ) , Base64 . getDecoder ( ) . decode ( input ) ) ;
for ( PacketIn packet : decodePacket ( data ) ) {
2017-08-22 15:02:23 +02:00
try {
2018-04-15 03:53:51 +02:00
packet . execute ( ( data . contains ( " c " ) ) ? data . getSection ( " c " ) : null ) ;
2017-08-22 15:02:23 +02:00
} catch ( Throwable e ) {
new InvocationTargetException ( e , " Exception while executing PacketIn " ) . printStackTrace ( ) ;
}
}
2018-04-15 03:53:51 +02:00
} catch ( JsonParseException | YAMLException e ) {
2018-01-05 21:37:23 +01:00
new IllegalPacketException ( " Unknown Packet Format: " + input ) . printStackTrace ( ) ;
2017-08-22 15:02:23 +02:00
} catch ( IllegalPacketException e ) {
e . printStackTrace ( ) ;
} catch ( Exception e ) {
new InvocationTargetException ( e , " Exception while decoding packet " ) . printStackTrace ( ) ;
}
}
try {
destroy ( plugin . config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getInt ( " Reconnect " , 30 ) ) ;
} catch ( IOException e1 ) {
e1 . printStackTrace ( ) ;
}
} catch ( Exception e ) {
if ( ! ( e instanceof SocketException ) ) e . printStackTrace ( ) ;
try {
destroy ( plugin . config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getInt ( " Reconnect " , 30 ) ) ;
} catch ( IOException e1 ) {
e1 . printStackTrace ( ) ;
}
}
} ) . start ( ) ;
}
2018-01-22 16:01:33 +01:00
/ * *
* Gets the Assigned Proxy Name
*
* @return Host Name
* /
2018-04-15 03:53:51 +02:00
@SuppressWarnings ( " unchecked " )
2018-01-22 16:01:33 +01:00
public String getName ( ) {
if ( name ! = null ) {
return name ;
} else if ( plugin . redis ) {
try {
return ( String ) plugin . redis ( " getServerId " ) ;
} catch ( Exception e ) {
return null ;
}
} else {
return null ;
}
}
2017-08-22 15:02:23 +02:00
/ * *
* Gets the Server Socket
*
* @return Server Socket
* /
public Socket getClient ( ) {
2017-09-24 05:19:22 +02:00
return socket . get ( ) ;
2017-08-22 15:02:23 +02:00
}
/ * *
2018-01-05 21:37:23 +01:00
* Add a Cipher for use by SubData
*
* @param cipher Cipher to Add
* @param handle Handle to Bind
* /
public static void addCipher ( String handle , Cipher cipher ) {
if ( Util . isNull ( cipher ) ) throw new NullPointerException ( ) ;
2018-01-12 22:56:22 +01:00
if ( ciphers . keySet ( ) . contains ( handle . toUpperCase ( ) . replace ( '-' , '_' ) . replace ( ' ' , '_' ) ) ) throw new IllegalStateException ( " Cipher already exists: " + handle ) ;
ciphers . put ( handle . toUpperCase ( ) . replace ( '-' , '_' ) . replace ( ' ' , '_' ) , cipher ) ;
2018-01-05 21:37:23 +01:00
}
/ * *
* Gets the Ciphers
2017-08-22 15:02:23 +02:00
*
2018-01-05 21:37:23 +01:00
* @return Cipher Map
2017-08-22 15:02:23 +02:00
* /
2018-01-05 21:37:23 +01:00
public static Map < String , Cipher > getCiphers ( ) {
return new TreeMap < > ( ciphers ) ;
}
/ * *
* Gets the Client ' s Cipher
*
* @return Cipher
* /
public Cipher getCipher ( ) {
return cipher ;
}
/ * *
* Gets a Cipher by Handle
*
* @param handle Handle
* @return Cipher
* /
public static Cipher getCipher ( String handle ) {
2018-01-12 22:56:22 +01:00
return getCiphers ( ) . get ( handle . toUpperCase ( ) . replace ( '-' , '_' ) . replace ( ' ' , '_' ) ) ;
2017-08-22 15:02:23 +02:00
}
/ * *
* Register PacketIn to the Network
*
* @param packet PacketIn to register
2018-07-29 20:39:42 +02:00
* @param channel Packet Channel
2017-08-22 15:02:23 +02:00
* @param handle Handle to Bind
* /
2018-07-29 20:39:42 +02:00
public static void registerPacket ( PacketIn packet , String channel , String handle ) {
2017-08-22 15:02:23 +02:00
if ( Util . isNull ( packet , handle ) ) throw new NullPointerException ( ) ;
2018-07-29 20:39:42 +02:00
HashMap < String , List < PacketIn > > map = ( pIn . keySet ( ) . contains ( channel . toLowerCase ( ) ) ) ? pIn . get ( channel . toLowerCase ( ) ) : new HashMap < String , List < PacketIn > > ( ) ;
List < PacketIn > list = ( map . keySet ( ) . contains ( handle ) ) ? map . get ( handle ) : new ArrayList < PacketIn > ( ) ;
2017-08-22 15:02:23 +02:00
if ( ! list . contains ( packet ) ) {
list . add ( packet ) ;
2018-07-29 20:39:42 +02:00
map . put ( handle , list ) ;
pIn . put ( channel . toLowerCase ( ) , map ) ;
2017-08-22 15:02:23 +02:00
}
}
/ * *
* Unregister PacketIn from the Network
*
2018-07-29 20:39:42 +02:00
* @param channel Packet Channel
2017-08-22 15:02:23 +02:00
* @param packet PacketIn to unregister
* /
2018-07-29 20:39:42 +02:00
public static void unregisterPacket ( String channel , PacketIn packet ) {
2017-08-22 15:02:23 +02:00
if ( Util . isNull ( packet ) ) throw new NullPointerException ( ) ;
2018-07-29 20:39:42 +02:00
if ( pIn . keySet ( ) . contains ( channel . toLowerCase ( ) ) ) {
List < String > search = new ArrayList < String > ( ) ;
search . addAll ( pIn . get ( channel . toLowerCase ( ) ) . keySet ( ) ) ;
for ( String handle : search ) if ( pIn . get ( channel . toLowerCase ( ) ) . get ( handle ) . contains ( packet ) ) {
List < PacketIn > list = pIn . get ( channel . toLowerCase ( ) ) . get ( handle ) ;
list . remove ( packet ) ;
if ( list . isEmpty ( ) ) {
pIn . get ( channel . toLowerCase ( ) ) . remove ( handle ) ;
if ( pIn . get ( channel . toLowerCase ( ) ) . isEmpty ( ) ) pIn . remove ( channel . toLowerCase ( ) ) ;
} else {
pIn . get ( channel . toLowerCase ( ) ) . put ( handle , list ) ;
}
2017-08-22 15:02:23 +02:00
}
}
}
/ * *
* Register PacketOut to the Network
*
* @param packet PacketOut to register
2018-07-29 20:39:42 +02:00
* @param channel Packet Channel
2017-08-22 15:02:23 +02:00
* @param handle Handle to bind
* /
2018-07-29 20:39:42 +02:00
public static void registerPacket ( Class < ? extends PacketOut > packet , String channel , String handle ) {
2017-08-22 15:02:23 +02:00
if ( Util . isNull ( packet , handle ) ) throw new NullPointerException ( ) ;
2018-07-29 20:39:42 +02:00
pOut . put ( packet , new NamedContainer < String , String > ( channel . toLowerCase ( ) , handle ) ) ;
2017-08-22 15:02:23 +02:00
}
/ * *
* Unregister PacketOut to the Network
*
2018-07-29 20:39:42 +02:00
* @param channel Packet Channel
2017-08-22 15:02:23 +02:00
* @param packet PacketOut to unregister
* /
2018-07-29 20:39:42 +02:00
public static void unregisterPacket ( String channel , Class < ? extends PacketOut > packet ) {
2017-08-22 15:02:23 +02:00
if ( Util . isNull ( packet ) ) throw new NullPointerException ( ) ;
2018-07-29 20:39:42 +02:00
if ( pOut . keySet ( ) . contains ( packet ) & & pOut . get ( packet ) . name ( ) . equalsIgnoreCase ( channel ) ) pOut . remove ( packet ) ;
2017-08-22 15:02:23 +02:00
}
/ * *
* Grab PacketIn Instances via handle
*
2018-07-29 20:39:42 +02:00
* @param channel Packet Channel
2017-08-22 15:02:23 +02:00
* @param handle Handle
* @return PacketIn
* /
2018-07-29 20:39:42 +02:00
public static List < ? extends PacketIn > getPacket ( String channel , String handle ) {
2017-08-22 15:02:23 +02:00
if ( Util . isNull ( handle ) ) throw new NullPointerException ( ) ;
2018-07-29 20:39:42 +02:00
return new ArrayList < PacketIn > ( pIn . get ( channel . toLowerCase ( ) ) . get ( handle ) ) ;
2017-08-22 15:02:23 +02:00
}
/ * *
* Send Packet to Server
*
* @param packet Packet to send
* /
public void sendPacket ( PacketOut packet ) {
if ( Util . isNull ( packet ) ) throw new NullPointerException ( ) ;
2017-09-24 05:19:22 +02:00
if ( socket . get ( ) = = null | | ! socket . name ( ) ) {
2017-08-22 15:02:23 +02:00
queue . add ( new NamedContainer < > ( null , packet ) ) ;
} else {
2017-09-24 05:19:22 +02:00
sendPacket ( new NamedContainer < > ( null , packet ) ) ;
}
}
private void sendPacket ( NamedContainer < String , PacketOut > packet ) {
try {
2018-04-15 03:53:51 +02:00
YAMLSection data = encodePacket ( packet . get ( ) ) ;
if ( packet . name ( ) ! = null ) data . set ( " f " , packet . name ( ) ) ;
writer . println ( Base64 . getEncoder ( ) . encodeToString ( cipher . encrypt ( plugin . config . get ( ) . getSection ( " Settings " ) . getSection ( " SubData " ) . getRawString ( " Password " ) , data ) ) ) ;
2017-09-24 05:19:22 +02:00
} catch ( Throwable e ) {
e . printStackTrace ( ) ;
2017-08-22 15:02:23 +02:00
}
}
/ * *
* Forward Packet to Client
*
* @param packet Packet to send
* @param location Where to send
* /
public void forwardPacket ( PacketOut packet , String location ) {
if ( Util . isNull ( packet , location ) ) throw new NullPointerException ( ) ;
2017-09-24 05:19:22 +02:00
if ( socket . get ( ) = = null | | ! socket . name ( ) ) {
2017-08-22 15:02:23 +02:00
queue . add ( new NamedContainer < > ( location , packet ) ) ;
} else {
2017-09-24 05:19:22 +02:00
sendPacket ( new NamedContainer < > ( null , packet ) ) ;
2017-08-22 15:02:23 +02:00
}
}
/ * *
* Broadcast packet to all Clients
*
* @param packet Packet to send
* /
public void broadcastPacket ( PacketOut packet ) {
forwardPacket ( packet , " " ) ;
}
/ * *
2018-04-15 03:53:51 +02:00
* Encode PacketOut
2017-08-22 15:02:23 +02:00
*
* @param packet PacketOut
* @return JSON Formatted Packet
* @throws IllegalPacketException
* /
2018-04-15 03:53:51 +02:00
private static YAMLSection encodePacket ( PacketOut packet ) throws IllegalPacketException , InvocationTargetException {
YAMLSection json = new YAMLSection ( ) ;
2017-08-22 15:02:23 +02:00
if ( ! pOut . keySet ( ) . contains ( packet . getClass ( ) ) ) throw new IllegalPacketException ( " Unknown PacketOut Channel: " + packet . getClass ( ) . getCanonicalName ( ) ) ;
if ( packet . getVersion ( ) . toString ( ) = = null ) throw new NullPointerException ( " PacketOut Version cannot be null: " + packet . getClass ( ) . getCanonicalName ( ) ) ;
try {
2018-04-15 03:53:51 +02:00
YAMLSection contents = packet . generate ( ) ;
2018-07-29 20:39:42 +02:00
json . set ( " n " , pOut . get ( packet . getClass ( ) ) . name ( ) ) ;
json . set ( " h " , pOut . get ( packet . getClass ( ) ) . get ( ) ) ;
2018-04-15 03:53:51 +02:00
json . set ( " v " , packet . getVersion ( ) . toString ( ) ) ;
if ( contents ! = null ) json . set ( " c " , contents ) ;
2017-08-22 15:02:23 +02:00
return json ;
} catch ( Throwable e ) {
throw new InvocationTargetException ( e , " Exception while encoding packet " ) ;
}
}
/ * *
2018-04-15 03:53:51 +02:00
* Decode PacketIn
2017-08-22 15:02:23 +02:00
*
2018-04-15 03:53:51 +02:00
* @param data Data to Decode
2017-08-22 15:02:23 +02:00
* @return PacketIn
* @throws IllegalPacketException
* @throws InvocationTargetException
* /
2018-04-15 03:53:51 +02:00
private static List < PacketIn > decodePacket ( YAMLSection data ) throws IllegalPacketException , InvocationTargetException {
if ( ! data . contains ( " h " ) | | ! data . contains ( " v " ) ) throw new IllegalPacketException ( " Unknown Packet Format: " + data . toString ( ) ) ;
2018-07-29 20:39:42 +02:00
if ( ! pIn . keySet ( ) . contains ( data . getRawString ( " n " ) ) | | ! pIn . get ( data . getRawString ( " n " ) ) . keySet ( ) . contains ( data . getRawString ( " h " ) ) ) throw new IllegalPacketException ( " Unknown PacketIn Channel: " + data . getRawString ( " n " ) + ':' + data . getRawString ( " h " ) ) ;
2017-08-22 15:02:23 +02:00
List < PacketIn > list = new ArrayList < PacketIn > ( ) ;
2018-07-29 20:39:42 +02:00
for ( PacketIn packet : pIn . get ( data . getRawString ( " n " ) ) . get ( data . getRawString ( " h " ) ) ) {
2018-04-15 03:53:51 +02:00
if ( packet . isCompatible ( new Version ( data . getRawString ( " v " ) ) ) ) {
2017-08-22 15:02:23 +02:00
list . add ( packet ) ;
} else {
2018-04-15 03:53:51 +02:00
new IllegalPacketException ( " Packet Version Mismatch in " + data . getRawString ( " h " ) + " : " + data . getRawString ( " v " ) + " -> " + packet . getVersion ( ) . toString ( ) ) . printStackTrace ( ) ;
2017-08-22 15:02:23 +02:00
}
}
return list ;
}
/ * *
* Drops All Connections and Stops the SubData Listener
*
* @throws IOException
* /
public void destroy ( int reconnect ) throws IOException {
if ( Util . isNull ( reconnect ) ) throw new NullPointerException ( ) ;
2017-09-24 05:19:22 +02:00
if ( socket . get ( ) ! = null ) {
final Socket socket = this . socket . get ( ) ;
this . socket . set ( null ) ;
2017-08-22 15:02:23 +02:00
if ( ! socket . isClosed ( ) ) socket . close ( ) ;
plugin . getPluginManager ( ) . callEvent ( new SubNetworkDisconnectEvent ( ) ) ;
System . out . println ( " SubServers > The SubData Connection was closed " ) ;
if ( reconnect > 0 ) {
System . out . println ( " SubServers > Attempting to reconnect in " + reconnect + " seconds " ) ;
Timer timer = new Timer ( ) ;
timer . scheduleAtFixedRate ( new TimerTask ( ) {
@Override
public void run ( ) {
try {
2018-01-22 16:01:33 +01:00
plugin . subdata = new SubDataClient ( plugin , name , socket . getInetAddress ( ) , socket . getPort ( ) , cipher ) ;
2017-08-22 15:02:23 +02:00
timer . cancel ( ) ;
while ( queue . size ( ) ! = 0 ) {
if ( queue . get ( 0 ) . name ( ) ! = null ) {
plugin . subdata . forwardPacket ( queue . get ( 0 ) . get ( ) , queue . get ( 0 ) . name ( ) ) ;
} else {
plugin . subdata . sendPacket ( queue . get ( 0 ) . get ( ) ) ;
}
queue . remove ( 0 ) ;
}
} catch ( IOException e ) {
System . out . println ( " SubServers > Connection was unsuccessful, retrying in " + reconnect + " seconds " ) ;
}
}
} , TimeUnit . SECONDS . toMillis ( reconnect ) , TimeUnit . SECONDS . toMillis ( reconnect ) ) ;
}
}
}
}