Skip to content

Commit

Permalink
Move proto method implementations to a separate class and extend Prot…
Browse files Browse the repository at this point in the history
…o_v2 from Proto
  • Loading branch information
thevindu-w committed Feb 15, 2024
1 parent 35542c7 commit b4ab5df
Show file tree
Hide file tree
Showing 4 changed files with 454 additions and 388 deletions.
92 changes: 3 additions & 89 deletions app/src/main/java/com/tw/clipshare/protocol/Proto.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,102 +27,16 @@
import com.tw.clipshare.netConnection.ServerConnection;
import com.tw.clipshare.platformUtils.AndroidUtils;
import com.tw.clipshare.platformUtils.StatusNotifier;
import java.nio.charset.StandardCharsets;

public abstract class Proto {
protected static final byte GET_TEXT = 1;
protected static final byte SEND_TEXT = 2;
protected static final byte GET_FILE = 3;
protected static final byte SEND_FILE = 4;
protected static final byte GET_IMAGE = 5;
protected static final byte INFO = 125;

protected static final byte STATUS_OK = 1;
protected static final int BUF_SZ = 65536;

protected final ServerConnection serverConnection;
protected final AndroidUtils utils;
protected final StatusNotifier notifier;

protected final ProtoMethods protoMethods;
protected Proto(ServerConnection serverConnection, AndroidUtils utils, StatusNotifier notifier) {
this.serverConnection = serverConnection;
this.utils = utils;
this.notifier = notifier;
}

protected long readSize() {
byte[] data = new byte[8];
if (this.serverConnection.receive(data)) {
return -1;
}
long size = 0;
for (byte b : data) {
size = (size << 8) | (b & 0xFF);
}
return size;
}

protected boolean sendSize(long size) {
byte[] data = new byte[8];
for (int i = data.length - 1; i >= 0; i--) {
data[i] = (byte) (size & 0xFF);
size >>= 8;
}
return !this.serverConnection.send(data);
}

protected boolean methodInit(byte method) {
byte[] methodArr = {method};
if (!this.serverConnection.send(methodArr)) {
return true;
}
byte[] status = new byte[1];
return (this.serverConnection.receive(status) || status[0] != STATUS_OK);
}

/**
* Reads a String encoded with UTF-8 from server
*
* @param maxSize maximum size to read
* @return read string or null on error
*/
protected String readString(int maxSize) {
long size = this.readSize();
if (size < 0 || size > maxSize) {
return null;
}
byte[] data = new byte[(int) size];
if (this.serverConnection.receive(data)) {
return null;
}
return new String(data, StandardCharsets.UTF_8);
}

/**
* Sends a String encoded with UTF-8 to server
*
* @param data String to be sent
* @return true on success or false on error
*/
protected boolean sendString(String data) {
if (data == null) return false;
final byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
final int len = bytes.length;
if (len >= 16777216) {
return false;
}
if (this.sendSize(len)) {
return false;
}
return this.serverConnection.send(bytes);
this.protoMethods = new ProtoMethods(serverConnection, utils, notifier);
}

/** Close the connection used for communicating with the server */
public void close() {
try {
if (this.serverConnection != null) this.serverConnection.close();
} catch (Exception ignored) {
}
this.protoMethods.close();
}

public abstract String getText();
Expand Down
Loading

0 comments on commit b4ab5df

Please sign in to comment.