package common.network;

public interface NetHandler {
	public static final class ThreadQuickExitException extends RuntimeException {
		private static final ThreadQuickExitException EXIT = new ThreadQuickExitException();
		
		private ThreadQuickExitException() {
			this.setStackTrace(new StackTraceElement[0]);
		}

		public synchronized Throwable fillInStackTrace() {
			this.setStackTrace(new StackTraceElement[0]);
			return this;
		}
	}

	void onDisconnect(String reason);
	
	default void update() {
	}

	public static <T> void checkThread(final Packet<T> packet, final T handler, IThreadListener listener) throws ThreadQuickExitException {
		if(!listener.isMainThread()) {
			listener.schedule(new Runnable() {
				public void run() {
					packet.processPacket(handler);
				}
			});
			throw ThreadQuickExitException.EXIT;
		}
	}

	public static <T> void checkThread(final Packet<T> packet, final T handler, IThreadListener listener, Object check) throws ThreadQuickExitException {
		if(check == null && listener.isMainThread())
			throw ThreadQuickExitException.EXIT;
		checkThread(packet, handler, listener);
	}
}
