Merge branch 'feat/server-transport-rawpacket' into 'main'
Encapsulate read data by transport layer into RawPacket See merge request cs108-fs26/Gruppe-13!30
This commit was merged in pull request #186.
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.casono.server.network.transport;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transport object, created by the transport layer to store raw data about the request
|
||||||
|
* prior to any processing
|
||||||
|
*/
|
||||||
|
public record RawPacket(int requestId, String payload) {}
|
||||||
+14
-8
@@ -27,24 +27,30 @@ public class TcpTransport implements TransportLayer {
|
|||||||
/**
|
/**
|
||||||
* Reads a string from the socket.
|
* Reads a string from the socket.
|
||||||
*
|
*
|
||||||
* @return the read string
|
* @return the read RawPacket
|
||||||
* @throws IOException if an I/O error occurs
|
* @throws IOException if an I/O error occurs
|
||||||
*/
|
*/
|
||||||
public String read() throws IOException {
|
public RawPacket read() throws IOException {
|
||||||
int length = in.readInt();
|
int length = in.readInt();
|
||||||
byte[] payload = new byte[length];
|
int requestId = in.readInt();
|
||||||
in.readFully(payload);
|
byte[] rawPayload = new byte[length];
|
||||||
return new String(payload, StandardCharsets.UTF_8);
|
in.readFully(rawPayload);
|
||||||
|
|
||||||
|
String payload = new String(rawPayload, StandardCharsets.UTF_8);
|
||||||
|
return new RawPacket(requestId, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a string to the socket.
|
* Writes a string to the socket.
|
||||||
*
|
*
|
||||||
* @param payload the string to write
|
* @param data the RawPacket's data to write
|
||||||
* @throws IOException if an I/O error occurs
|
* @throws IOException if an I/O error occurs
|
||||||
*/
|
*/
|
||||||
public void write(String payload) throws IOException {
|
public void write(RawPacket data) throws IOException {
|
||||||
byte[] rawPayload = payload.getBytes(StandardCharsets.UTF_8);
|
int requestId = data.requestId();
|
||||||
|
byte[] rawPayload = data.payload().getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
out.writeInt(requestId);
|
||||||
out.writeInt(rawPayload.length);
|
out.writeInt(rawPayload.length);
|
||||||
out.write(rawPayload);
|
out.write(rawPayload);
|
||||||
out.flush();
|
out.flush();
|
||||||
|
|||||||
+3
-3
@@ -7,10 +7,10 @@ public interface TransportLayer {
|
|||||||
/**
|
/**
|
||||||
* Reads data from the transport layer.
|
* Reads data from the transport layer.
|
||||||
*
|
*
|
||||||
* @return the read data as a string
|
* @return the read data as a RawPacket
|
||||||
* @throws IOException if an I/O error occurs
|
* @throws IOException if an I/O error occurs
|
||||||
*/
|
*/
|
||||||
String read() throws IOException;
|
RawPacket read() throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes data to the transport layer.
|
* Writes data to the transport layer.
|
||||||
@@ -18,7 +18,7 @@ public interface TransportLayer {
|
|||||||
* @param data the data to write
|
* @param data the data to write
|
||||||
* @throws IOException if an I/O error occurs
|
* @throws IOException if an I/O error occurs
|
||||||
*/
|
*/
|
||||||
void write(String data) throws IOException;
|
void write(RawPacket data) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes the transport layer.
|
* Closes the transport layer.
|
||||||
|
|||||||
Reference in New Issue
Block a user