Фиксы SocketHookManager

Travis.CI
This commit is contained in:
Gravit 2019-01-02 18:11:01 +07:00
parent b892d50e6d
commit 7d89dc1c70
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
4 changed files with 32 additions and 5 deletions

View file

@ -3,28 +3,35 @@
import ru.gravit.launcher.request.RequestException; import ru.gravit.launcher.request.RequestException;
import ru.gravit.launchserver.socket.SocketContext; import ru.gravit.launchserver.socket.SocketContext;
import java.net.Socket;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public class SocketHookManager { public class SocketHookManager {
@FunctionalInterface @FunctionalInterface
interface SocketPreHook public interface SocketPreHook
{ {
boolean preHook(SocketContext context); //Вернуть true если необходимо продолжть обработку, false если остановить обработку boolean preHook(SocketContext context); //Вернуть true если необходимо продолжть обработку, false если остановить обработку
} }
@FunctionalInterface @FunctionalInterface
interface SocketPostHook public interface SocketPostHook
{ {
void postHook(SocketContext context); void postHook(SocketContext context);
} }
@FunctionalInterface @FunctionalInterface
interface SocketErrorHook public interface SocketErrorHook
{ {
boolean errorHook(SocketContext context, RequestException e); //Вернуть true если необходимо продолжть обработку, false если остановить обработку boolean errorHook(SocketContext context, RequestException e); //Вернуть true если необходимо продолжть обработку, false если остановить обработку
} }
@FunctionalInterface
public interface SocketFatalErrorHook
{
boolean fatalErrorHook(Socket socket, Exception e); //Вернуть true если необходимо продолжть обработку, false если остановить обработку
}
private Set<SocketPostHook> POST_HOOKS; private Set<SocketPostHook> POST_HOOKS;
private Set<SocketPreHook> PRE_HOOKS; private Set<SocketPreHook> PRE_HOOKS;
private Set<SocketErrorHook> ERROR_HOOKS; private Set<SocketErrorHook> ERROR_HOOKS;
private Set<SocketFatalErrorHook> FATALERROR_HOOKS;
public void registerPostHook(SocketPostHook hook) public void registerPostHook(SocketPostHook hook)
{ {
if(POST_HOOKS == null) POST_HOOKS = new HashSet<>(); if(POST_HOOKS == null) POST_HOOKS = new HashSet<>();
@ -40,6 +47,11 @@ public void registerErrorHook(SocketErrorHook hook)
if(ERROR_HOOKS == null) ERROR_HOOKS = new HashSet<>(); if(ERROR_HOOKS == null) ERROR_HOOKS = new HashSet<>();
ERROR_HOOKS.add(hook); ERROR_HOOKS.add(hook);
} }
public void registerFatalErrorHook(SocketFatalErrorHook hook)
{
if(FATALERROR_HOOKS == null) FATALERROR_HOOKS = new HashSet<>();
FATALERROR_HOOKS.add(hook);
}
public boolean preHook(SocketContext context) public boolean preHook(SocketContext context)
{ {
if(PRE_HOOKS == null) return true; if(PRE_HOOKS == null) return true;
@ -66,4 +78,13 @@ public boolean errorHook(SocketContext context, RequestException e)
} }
return true; return true;
} }
public boolean fatalErrorHook(Socket socket, Exception e)
{
if(FATALERROR_HOOKS == null) return true;
for(SocketFatalErrorHook errorHook : FATALERROR_HOOKS)
{
if(!errorHook.fatalErrorHook(socket,e)) return false;
}
return true;
}
} }

View file

@ -133,7 +133,8 @@ public void run() {
} }
} catch (Exception e) { } catch (Exception e) {
savedError = e; savedError = e;
LogHelper.error(e); if(server.socketHookManager.fatalErrorHook(socket,e))
LogHelper.error(e);
} finally { } finally {
IOHelper.close(socket); IOHelper.close(socket);
if (!cancelled) if (!cancelled)

View file

@ -101,7 +101,11 @@ public void run() {
// Invoke pre-connect listener // Invoke pre-connect listener
long id = idCounter.incrementAndGet(); long id = idCounter.incrementAndGet();
if (listener != null && !listener.onConnect(socket.getInetAddress())) if (listener != null && !listener.onConnect(socket.getInetAddress()))
{
socket.close();
continue; // Listener didn't accepted this connection continue; // Listener didn't accepted this connection
}
// Reply in separate thread // Reply in separate thread
threadPool.execute(new ResponseThread(server, id, socket, sessionManager, server.socketHookManager)); threadPool.execute(new ResponseThread(server, id, socket, sessionManager, server.socketHookManager));

View file

@ -1,5 +1,6 @@
#Wed Jan 02 17:51:09 GMT+07:00 2019
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-all.zip