Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rudimentary PLH support #83

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion modules/core/src/main/java/com/illposed/osc/OSCParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.illposed.osc.argument.ArgumentHandler;
import com.illposed.osc.argument.handler.IntegerArgumentHandler;
import com.illposed.osc.argument.handler.TimeTag64ArgumentHandler;
import com.illposed.osc.transport.NetworkProtocol;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.Buffer;
Expand Down Expand Up @@ -223,7 +225,20 @@ private OSCBundle convertBundle(final ByteBuffer rawInput) throws OSCParseExcept
* Assumes that the byte array is a message.
* @return a message containing the data specified in the byte stream
*/
private OSCMessage convertMessage(final ByteBuffer rawInput) throws OSCParseException {
private OSCMessage convertMessage(final ByteBuffer rawInputWithLength) throws OSCParseException {

// remove the first four bytes, which is the frame length
// ideally we would use this to collect data from multiple incoming TCP packets, but for this will at least allow parsing to work for single packets
// we should only do this with TCP connections
ByteBuffer rawInput = null;
if ((properties.get("networkProtocol") == NetworkProtocol.TCP)&&(rawInputWithLength.limit() > 4)) {
rawInput = ByteBuffer.allocate(rawInputWithLength.limit() - 4);
for(int i = 4; i < rawInputWithLength.limit(); i++) {
rawInput.put(i - 4, rawInputWithLength.get(i));
}
} else {
rawInput = rawInputWithLength;
}

final String address = readString(rawInput);
final CharSequence typeIdentifiers = readTypes(rawInput);
Expand Down
22 changes: 22 additions & 0 deletions modules/core/src/main/java/com/illposed/osc/OSCSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,28 @@ private void writeArguments(final OSCMessage message) throws OSCSerializeExcepti
private void write(final OSCMessage message) throws OSCSerializeException {
writeAddress(message);
writeArguments(message);
if (properties.get("networkProtocol") == NetworkProtocol.TCP) {
writePLH();
}
}

/**
* Serializes a packet length header. Call this after writing the message and arguments.
* @throws OSCSerializeException if the message failed to serialize
*/
private void writePLH() throws OSCSerializeException {
byte[] rawOutput = output.toByteArray();
int length = rawOutput.length;
byte[] newOutput = new byte[4 + length];
ByteBuffer.wrap(newOutput).putInt(length); // add the length
ByteBuffer.wrap(newOutput, 4, length).put(rawOutput); // add the message content

output.clear();
output.put(newOutput);

if (false) { // what to check here?
throw new OSCSerializeException("Error adding packet length header");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public OSCSerializerAndParserBuilder() {
this.usingDefaultHandlers = true;
}

public void setNetworkProtocol(NetworkProtocol networkProtocol) {
this.properties.put("networkProtocol", networkProtocol);
}

// Public API
@SuppressWarnings("WeakerAccess")
public Map<Character, ArgumentHandler> getIdentifierToTypeMapping() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public OSCPortIn build() throws IOException {

if (parserBuilder == null) {
parserBuilder = new OSCSerializerAndParserBuilder();
parserBuilder.setNetworkProtocol(networkProtocol);
}

if (packetListeners == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public OSCPortOut build() throws IOException {

if (serializerBuilder == null) {
serializerBuilder = new OSCSerializerAndParserBuilder();
serializerBuilder.setNetworkProtocol(networkProtocol);
}

return new OSCPortOut(
Expand Down