Added missing option to connect to WebServer
This commit is contained in:
@@ -30,6 +30,11 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
*/
|
||||
private NetworkClient clientToINS;
|
||||
|
||||
/**
|
||||
* Handles everything with WebServer-Connection.
|
||||
*/
|
||||
private NetworkClient clientToWeb;
|
||||
|
||||
/**
|
||||
* Manages the folder structure for client certificates.
|
||||
*/
|
||||
@@ -41,9 +46,13 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
@Getter
|
||||
private ProtocolBridge protocolBridge;
|
||||
/**
|
||||
* Stores the protocol version of the connected server.
|
||||
* Stores the protocol version of the connected insserver.
|
||||
*/
|
||||
private ProtocolVersion serverVersion = null;
|
||||
private ProtocolVersion insServerVersion = null;
|
||||
/**
|
||||
* Stores the protocol version of the connected webserver.
|
||||
*/
|
||||
private ProtocolVersion webServerVersion = null;
|
||||
|
||||
/**
|
||||
* Initializes the ProtocolClient, setting up certificate folders and the INS client connection.
|
||||
@@ -64,6 +73,53 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to a WebServer.
|
||||
*
|
||||
* @param host WebServer host
|
||||
* @param port WebServer port
|
||||
*/
|
||||
public final void connectToWebServer(String host, int port) {
|
||||
if (!protocolBridge.isRunningAsClient())
|
||||
throw new IllegalStateException("Not running as client");
|
||||
|
||||
if (clientToWeb != null && clientToWeb.isConnected())
|
||||
return;
|
||||
|
||||
createWebClient(host, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the WebServer connection client.
|
||||
*
|
||||
* @return the NetworkClient handling the WebServer connection.
|
||||
*/
|
||||
public final NetworkClient getClientWebConnection() {
|
||||
return clientToWeb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize connection to WebServer
|
||||
*
|
||||
* @param host WebServer host
|
||||
* @param port WebServer port
|
||||
*/
|
||||
private final void createWebClient(String host, int port) {
|
||||
clientToWeb = new NetworkClient.ClientBuilder()
|
||||
.setLogger(protocolBridge.getLogger())
|
||||
.setProxy(protocolBridge.getProxy())
|
||||
.setHost(host)
|
||||
.setPort(port)
|
||||
.setPacketHandler(protocolBridge.getProtocolSettings().packetHandler)
|
||||
.setEventManager(protocolBridge.getProtocolSettings().eventManager)
|
||||
.setRootCAFolder(folderStructure.publicCAFolder)
|
||||
.setClientCertificatesFolder(
|
||||
folderStructure.publicClientFolder,
|
||||
folderStructure.privateClientFolder
|
||||
)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects the ProtocolBridge.
|
||||
* @param bridge the Bridge instance.
|
||||
@@ -125,148 +181,293 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
*
|
||||
* @return the ProtocolVersion of the server, or PV_1_0_0_CLASSIC if not set.
|
||||
*/
|
||||
public final ProtocolVersion getServerVersion() {
|
||||
return serverVersion == null ? ProtocolVersion.PV_1_0_0_CLASSIC : serverVersion;
|
||||
public final ProtocolVersion getWebServerVersion() {
|
||||
return webServerVersion == null ? ProtocolVersion.PV_1_0_0_CLASSIC : webServerVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the protocol version of the connected server.
|
||||
*
|
||||
* @param serverVersion the ProtocolVersion to set for the server.
|
||||
* @param webServerVersion the ProtocolVersion to set for the server.
|
||||
*/
|
||||
public final void setServerVersion(ProtocolVersion serverVersion) {
|
||||
if (serverVersion == null) this.serverVersion = serverVersion;
|
||||
public final void setWebServerVersion(ProtocolVersion webServerVersion) {
|
||||
if (webServerVersion == null) this.webServerVersion = insServerVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles INS disconnection events, resetting the server version and closing the web client connection if necessary.
|
||||
* Gets the protocol version of the connected server.
|
||||
*
|
||||
* @return the ProtocolVersion of the server, or PV_1_0_0_CLASSIC if not set.
|
||||
*/
|
||||
public final ProtocolVersion getInsServerVersion() {
|
||||
return insServerVersion == null ? ProtocolVersion.PV_1_0_0_CLASSIC : insServerVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the protocol version of the connected server.
|
||||
*
|
||||
* @param insServerVersion the ProtocolVersion to set for the server.
|
||||
*/
|
||||
public final void setInsServerVersion(ProtocolVersion insServerVersion) {
|
||||
if (insServerVersion == null) this.insServerVersion = insServerVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles disconnect events, resetting the server version and closing the web client connection if necessary.
|
||||
*
|
||||
* @param event the ClientDisconnectedEvent triggered on INS disconnection.
|
||||
*/
|
||||
public final void onINSDisconnect(ClientDisconnectedEvent event) {
|
||||
public final void onDisconnect(ClientDisconnectedEvent event) {
|
||||
// Reset server version on INS disconnect
|
||||
serverVersion = null;
|
||||
if (!clientToINS.isConnected()) {
|
||||
insServerVersion = null;
|
||||
disconnectFromWebServer();
|
||||
}
|
||||
|
||||
if (!clientToWeb.isConnected()) webServerVersion = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected server is a stable server.
|
||||
* Checks if the connected insserver is a stable server.
|
||||
*
|
||||
* @return true if the server is stable, false otherwise.
|
||||
*/
|
||||
public final boolean isStableServer() {
|
||||
public final boolean isINSStableServer() {
|
||||
// Check if the server version is stable
|
||||
return !isBetaServer() && !isClassicServer();
|
||||
return !isINSBetaServer() && !isINSClassicServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected server or its compatible versions support stable protocol.
|
||||
* Checks if the connected insserver or its compatible versions support stable protocol.
|
||||
*
|
||||
* @return true if stable protocol is supported, false otherwise.
|
||||
*/
|
||||
public final boolean supportServerStable() {
|
||||
public final boolean supportINSServerStable() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getServerVersion().getCompatibleVersions()) {
|
||||
for (ProtocolVersion compatibleVersion : getWebServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is stable
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.STABLE;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the server version is stable
|
||||
return isStableServer() || yes;
|
||||
return isINSStableServer() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected server is a beta server.
|
||||
* Checks if the connected insserver is a beta server.
|
||||
*
|
||||
* @return true if the server is beta, false otherwise.
|
||||
*/
|
||||
public final boolean isBetaServer() {
|
||||
public final boolean isINSBetaServer() {
|
||||
// Check if the server version is beta
|
||||
return getServerVersion().getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
return getInsServerVersion().getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected server or its compatible versions support beta protocol.
|
||||
* Checks if the connected insserver or its compatible versions support beta protocol.
|
||||
*
|
||||
* @return true if beta protocol is supported, false otherwise.
|
||||
*/
|
||||
public final boolean supportServerBeta() {
|
||||
public final boolean supportINSServerBeta() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getServerVersion().getCompatibleVersions()) {
|
||||
for (ProtocolVersion compatibleVersion : getInsServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is beta
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the server version is beta
|
||||
return isBetaServer() || yes;
|
||||
return isINSBetaServer() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected server is a classic server.
|
||||
* Checks if the connected insserver is a classic server.
|
||||
*
|
||||
* @return true if the server is classic, false otherwise.
|
||||
*/
|
||||
public final boolean isClassicServer() {
|
||||
public final boolean isINSClassicServer() {
|
||||
// Check if the server version is classic
|
||||
return getServerVersion().getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
return getInsServerVersion().getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected server or its compatible versions support classic protocol.
|
||||
* Checks if the connected insserver or its compatible versions support classic protocol.
|
||||
*
|
||||
* @return true if classic protocol is supported, false otherwise.
|
||||
*/
|
||||
public final boolean supportServerClassic() {
|
||||
public final boolean supportINSServerClassic() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getServerVersion().getCompatibleVersions()) {
|
||||
for (ProtocolVersion compatibleVersion : getInsServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is classic
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the server version is classic
|
||||
return isClassicServer() || yes;
|
||||
return isINSClassicServer() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected server supports the protocol version of the given packet.
|
||||
* Checks if the connected insserver supports the protocol version of the given packet.
|
||||
*
|
||||
* @param packet the OACPacket to check against the server's supported protocol version.
|
||||
* @return true if the server supports the packet's protocol version, false otherwise.
|
||||
*/
|
||||
public final boolean supportServerPacket(OACPacket packet) {
|
||||
public final boolean supportINSServerPacket(OACPacket packet) {
|
||||
// Check if the server supports the protocol version of the packet
|
||||
return supportServerVersion(packet.getProtocolVersion());
|
||||
return supportINSServerVersion(packet.getProtocolVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected server or its compatible versions support the specified protocol version.
|
||||
* Checks if the connected insserver or its compatible versions support the specified protocol version.
|
||||
*
|
||||
* @param targetVersion the ProtocolVersion to check for support.
|
||||
* @return true if the server or its compatible versions support the target version, false otherwise.
|
||||
*/
|
||||
public final boolean supportServerVersion(ProtocolVersion targetVersion) {
|
||||
public final boolean supportINSServerVersion(ProtocolVersion targetVersion) {
|
||||
// Directly check if the server version matches or is in the list of compatible versions
|
||||
return getServerVersion() == targetVersion || getServerVersion().getCompatibleVersions().contains(targetVersion);
|
||||
return getInsServerVersion() == targetVersion || getInsServerVersion().getCompatibleVersions().contains(targetVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected server or its compatible versions support the specified protocol.
|
||||
* Checks if the connected insserver or its compatible versions support the specified protocol.
|
||||
*
|
||||
* @param protocol the Protocol to check for support.
|
||||
* @return true if the server or its compatible versions support the protocol, false otherwise.
|
||||
*/
|
||||
public final boolean supportServerProtocol(ProtocolVersion.Protocol protocol) {
|
||||
public final boolean supportINSServerProtocol(ProtocolVersion.Protocol protocol) {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getServerVersion().getCompatibleVersions()) {
|
||||
for (ProtocolVersion compatibleVersion : getInsServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version supports the protocol
|
||||
yes = compatibleVersion.getSupportedProtocols().contains(protocol);
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the server version supports the protocol
|
||||
return getServerVersion().getSupportedProtocols().contains(protocol) || yes;
|
||||
return getInsServerVersion().getSupportedProtocols().contains(protocol) || yes;
|
||||
}
|
||||
|
||||
//fwfwef
|
||||
/**
|
||||
* Checks if the connected webserver is a stable server.
|
||||
*
|
||||
* @return true if the server is stable, false otherwise.
|
||||
*/
|
||||
public final boolean isWebStableServer() {
|
||||
// Check if the server version is stable
|
||||
return !isWebBetaServer() && !isWebClassicServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver or its compatible versions support stable protocol.
|
||||
*
|
||||
* @return true if stable protocol is supported, false otherwise.
|
||||
*/
|
||||
public final boolean supportWebServerStable() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getWebServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is stable
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.STABLE;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the server version is stable
|
||||
return isWebBetaServer() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver is a beta server.
|
||||
*
|
||||
* @return true if the server is beta, false otherwise.
|
||||
*/
|
||||
public final boolean isWebBetaServer() {
|
||||
// Check if the server version is beta
|
||||
return getWebServerVersion().getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver or its compatible versions support beta protocol.
|
||||
*
|
||||
* @return true if beta protocol is supported, false otherwise.
|
||||
*/
|
||||
public final boolean supportWebServerBeta() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getWebServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is beta
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.BETA;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the server version is beta
|
||||
return isWebStableServer() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver is a classic server.
|
||||
*
|
||||
* @return true if the server is classic, false otherwise.
|
||||
*/
|
||||
public final boolean isWebClassicServer() {
|
||||
// Check if the server version is classic
|
||||
return getWebServerVersion().getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver or its compatible versions support classic protocol.
|
||||
*
|
||||
* @return true if classic protocol is supported, false otherwise.
|
||||
*/
|
||||
public final boolean supportWebServerClassic() {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getWebServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version is classic
|
||||
yes = compatibleVersion.getProtocolType() == ProtocolVersion.ProtocolType.CLASSIC;
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the server version is classic
|
||||
return isWebClassicServer() || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver supports the protocol version of the given packet.
|
||||
*
|
||||
* @param packet the OACPacket to check against the server's supported protocol version.
|
||||
* @return true if the server supports the packet's protocol version, false otherwise.
|
||||
*/
|
||||
public final boolean supportWebServerPacket(OACPacket packet) {
|
||||
// Check if the server supports the protocol version of the packet
|
||||
return supportWebServerVersion(packet.getProtocolVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver or its compatible versions support the specified protocol version.
|
||||
*
|
||||
* @param targetVersion the ProtocolVersion to check for support.
|
||||
* @return true if the server or its compatible versions support the target version, false otherwise.
|
||||
*/
|
||||
public final boolean supportWebServerVersion(ProtocolVersion targetVersion) {
|
||||
// Directly check if the server version matches or is in the list of compatible versions
|
||||
return getWebServerVersion() == targetVersion || getWebServerVersion().getCompatibleVersions().contains(targetVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the connected webserver or its compatible versions support the specified protocol.
|
||||
*
|
||||
* @param protocol the Protocol to check for support.
|
||||
* @return true if the server or its compatible versions support the protocol, false otherwise.
|
||||
*/
|
||||
public final boolean supportWebServerProtocol(ProtocolVersion.Protocol protocol) {
|
||||
boolean yes = false;
|
||||
for (ProtocolVersion compatibleVersion : getWebServerVersion().getCompatibleVersions()) {
|
||||
// Check if compatible version supports the protocol
|
||||
yes = compatibleVersion.getSupportedProtocols().contains(protocol);
|
||||
if (yes) break;
|
||||
}
|
||||
|
||||
// Check if the server version supports the protocol
|
||||
return getWebServerVersion().getSupportedProtocols().contains(protocol) || yes;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -289,6 +490,17 @@ public abstract class ProtocolClient extends DefaultMethodsOverrider {
|
||||
onQuerySent(tln, name, sub, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects from the WebServer.
|
||||
*/
|
||||
public final void disconnectFromWebServer() {
|
||||
if (clientToWeb != null) {
|
||||
clientToWeb.disconnect();
|
||||
clientToWeb = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called when the client receives an INS response from the server.
|
||||
* <p>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.openautonomousconnection.protocol.side.client.events;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.event.impl.Event;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.side.client.ProtocolClient;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
|
||||
/**
|
||||
* Event triggered when a client successfully connects to a INS protocol server.
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.INS)
|
||||
public final class ConnectedToProtocolWebServerEvent extends Event {
|
||||
|
||||
/**
|
||||
* Reference to the ProtocolClient object.
|
||||
*/
|
||||
@Getter
|
||||
private final ProtocolClient client;
|
||||
|
||||
public ConnectedToProtocolWebServerEvent(ProtocolClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package org.openautonomousconnection.protocol.side.web;
|
||||
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.server.ConnectionHandler;
|
||||
import dev.unlegitdqrk.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
import lombok.Getter;
|
||||
import org.openautonomousconnection.protocol.packets.OACPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebRequestPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebResponsePacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebStreamChunkPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebStreamEndPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebStreamStartPacket;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
|
||||
import javax.net.ssl.SSLSocket;
|
||||
@@ -14,8 +16,7 @@ import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
/**
|
||||
* A connected web client using pure OAC packets.
|
||||
* No HTTP, no GET/POST, no header parsing.
|
||||
* A connected web client using pure protocol packets.
|
||||
*/
|
||||
public final class ConnectedWebClient {
|
||||
|
||||
@@ -28,13 +29,11 @@ public final class ConnectedWebClient {
|
||||
@Getter
|
||||
private ProtocolWebServer server;
|
||||
|
||||
/**
|
||||
* The protocol version of the connected client.
|
||||
*/
|
||||
private ObjectOutputStream out;
|
||||
private ObjectInputStream in;
|
||||
|
||||
private ProtocolVersion clientVersion = null;
|
||||
/**
|
||||
* Indicates if the client version has been loaded.
|
||||
*/
|
||||
|
||||
@Getter
|
||||
private boolean clientVersionLoaded = false;
|
||||
|
||||
@@ -44,46 +43,6 @@ public final class ConnectedWebClient {
|
||||
this.pipelineConnection = pipelineConnection;
|
||||
}
|
||||
|
||||
public void attachWebServer(SSLSocket socket, ProtocolWebServer server) {
|
||||
if (this.webSocket != null) return;
|
||||
|
||||
this.webSocket = socket;
|
||||
this.server = server;
|
||||
|
||||
this.receiveThread = new Thread(this::receiveLoop, "OAC-WebClient-Receiver");
|
||||
this.receiveThread.start();
|
||||
}
|
||||
|
||||
private void receiveLoop() {
|
||||
try {
|
||||
ObjectInputStream in = new ObjectInputStream(webSocket.getInputStream());
|
||||
ObjectOutputStream out = new ObjectOutputStream(webSocket.getOutputStream());
|
||||
|
||||
PacketHandler handler = server.getProtocolBridge().getProtocolSettings().packetHandler;
|
||||
|
||||
while (!webSocket.isClosed() && pipelineConnection.isConnected()) {
|
||||
|
||||
Object obj = in.readObject();
|
||||
if (!(obj instanceof OACPacket packet)) continue;
|
||||
|
||||
if (packet instanceof WebRequestPacket requestPacket) {
|
||||
|
||||
WebResponsePacket response =
|
||||
server.onWebRequest(this, requestPacket);
|
||||
|
||||
if (response != null) {
|
||||
out.writeObject(response);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception ignored) {
|
||||
} finally {
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return webSocket != null &&
|
||||
webSocket.isConnected() &&
|
||||
@@ -91,25 +50,72 @@ public final class ConnectedWebClient {
|
||||
pipelineConnection.isConnected();
|
||||
}
|
||||
|
||||
public void attachWebServer(SSLSocket socket, ProtocolWebServer server) throws IOException {
|
||||
if (this.webSocket != null) return;
|
||||
|
||||
this.webSocket = socket;
|
||||
this.server = server;
|
||||
|
||||
// IMPORTANT: ObjectOutputStream first, flush, then ObjectInputStream
|
||||
this.out = new ObjectOutputStream(webSocket.getOutputStream());
|
||||
this.out.flush();
|
||||
this.in = new ObjectInputStream(webSocket.getInputStream());
|
||||
|
||||
this.receiveThread = new Thread(this::receiveLoop, "OAC-WebClient-Receiver");
|
||||
this.receiveThread.start();
|
||||
}
|
||||
|
||||
private void receiveLoop() {
|
||||
try {
|
||||
while (isConnected()) {
|
||||
Object obj = in.readObject();
|
||||
if (!(obj instanceof OACPacket packet)) continue;
|
||||
|
||||
if (packet instanceof WebRequestPacket req) {
|
||||
// server decides whether it returns normal response or does streaming itself
|
||||
WebResponsePacket resp = server.onWebRequest(this, req);
|
||||
if (resp != null) send(resp);
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
} finally {
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void send(Object packet) throws IOException {
|
||||
if (!isConnected()) return;
|
||||
out.writeObject(packet);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
public synchronized void streamStart(WebStreamStartPacket start) throws IOException {
|
||||
send(start);
|
||||
}
|
||||
|
||||
public synchronized void streamChunk(WebStreamChunkPacket chunk) throws IOException {
|
||||
send(chunk);
|
||||
}
|
||||
|
||||
public synchronized void streamEnd(WebStreamEndPacket end) throws IOException {
|
||||
send(end);
|
||||
}
|
||||
|
||||
public synchronized void disconnect() {
|
||||
try {
|
||||
server.onDisconnect(this);
|
||||
clientVersionLoaded = false;
|
||||
clientVersion = null;
|
||||
} catch (Exception ignored) {}
|
||||
try { server.onDisconnect(this); } catch (Exception ignored) {}
|
||||
|
||||
try {
|
||||
pipelineConnection.disconnect();
|
||||
} catch (Exception ignored) {}
|
||||
try { pipelineConnection.disconnect(); } catch (Exception ignored) {}
|
||||
|
||||
try {
|
||||
if (webSocket != null) webSocket.close();
|
||||
} catch (IOException ignored) {}
|
||||
try { if (in != null) in.close(); } catch (Exception ignored) {}
|
||||
try { if (out != null) out.close(); } catch (Exception ignored) {}
|
||||
try { if (webSocket != null) webSocket.close(); } catch (Exception ignored) {}
|
||||
|
||||
in = null;
|
||||
out = null;
|
||||
webSocket = null;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Gets the protocol version of the connected client.
|
||||
*
|
||||
* @return The protocol version of the client, defaults to PV_1_0_0_CLASSIC if not set.
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.openautonomousconnection.protocol.ProtocolBridge;
|
||||
import org.openautonomousconnection.protocol.annotations.ProtocolInfo;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebRequestPacket;
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.WebResponsePacket;
|
||||
import org.openautonomousconnection.protocol.side.web.events.WebClientConnectedEvent;
|
||||
import org.openautonomousconnection.protocol.side.web.managers.AuthManager;
|
||||
import org.openautonomousconnection.protocol.side.web.managers.RuleManager;
|
||||
import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
@@ -252,6 +253,7 @@ public abstract class ProtocolWebServer {
|
||||
if (connectedWebClient.getPipelineConnection().getClientID() != -1 && connectedWebClient.isClientVersionLoaded()) {
|
||||
// Assign socket to an existing connected client
|
||||
connectedWebClient.attachWebServer(client, this);
|
||||
protocolBridge.getProtocolSettings().eventManager.executeEvent(new WebClientConnectedEvent(connectedWebClient));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -10,7 +10,7 @@ import org.openautonomousconnection.protocol.versions.ProtocolVersion;
|
||||
* Event triggered when a web client connects to the web server.
|
||||
*/
|
||||
@ProtocolInfo(protocolSide = ProtocolVersion.ProtocolSide.WEB)
|
||||
public final class ConnectedWebClientEvent extends Event {
|
||||
public final class WebClientConnectedEvent extends Event {
|
||||
|
||||
/**
|
||||
* The connected web client.
|
||||
@@ -18,7 +18,7 @@ public final class ConnectedWebClientEvent extends Event {
|
||||
@Getter
|
||||
private final ConnectedWebClient webClient;
|
||||
|
||||
public ConnectedWebClientEvent(ConnectedWebClient webClient) {
|
||||
public WebClientConnectedEvent(ConnectedWebClient webClient) {
|
||||
this.webClient = webClient;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user