-
-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
290 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
server/desktop/src/main/java/dev/slimevr/desktop/platform/linux/UnixSocketConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package dev.slimevr.desktop.platform.linux; | ||
|
||
import dev.slimevr.protocol.ConnectionContext; | ||
import dev.slimevr.protocol.GenericConnection; | ||
import io.eiren.util.logging.LogManager; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
import java.nio.channels.SocketChannel; | ||
import java.util.Arrays; | ||
import java.util.UUID; | ||
|
||
public class UnixSocketConnection implements GenericConnection { | ||
public final UUID id; | ||
public final ConnectionContext context; | ||
private final ByteBuffer dst = ByteBuffer.allocate(2048).order(ByteOrder.LITTLE_ENDIAN); | ||
private final SocketChannel channel; | ||
|
||
public UnixSocketConnection(SocketChannel channel) { | ||
this.id = UUID.randomUUID(); | ||
this.context = new ConnectionContext(); | ||
this.channel = channel; | ||
} | ||
|
||
@Override | ||
public UUID getConnectionId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public ConnectionContext getContext() { | ||
return this.context; | ||
} | ||
|
||
public boolean isConnected() { | ||
return this.channel.isConnected(); | ||
} | ||
|
||
private void resetChannel() { | ||
try { | ||
this.channel.close(); | ||
} catch(IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void send(ByteBuffer bytes) { | ||
if (!this.channel.isConnected()) | ||
return; | ||
try { | ||
ByteBuffer[] src = new ByteBuffer[] { | ||
ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN), | ||
bytes.slice(), | ||
}; | ||
src[0].putInt(src[1].remaining() + 4); | ||
src[0].flip(); | ||
synchronized(this) { | ||
while (src[1].hasRemaining()) { | ||
this.channel.write(src); | ||
} | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public ByteBuffer read() { | ||
if(dst.position() < 4) { | ||
if (!this.channel.isConnected()) | ||
return null; | ||
try { | ||
int result = this.channel.read(dst); | ||
if (result == -1) { | ||
LogManager.info("[SolarXR Bridge] Reached end-of-stream on connection"); | ||
this.resetChannel(); | ||
return null; | ||
} | ||
if (result == 0 || dst.position() < 4) { | ||
return null; | ||
} | ||
} catch(IOException e) { | ||
e.printStackTrace(); | ||
this.resetChannel(); | ||
return null; | ||
} | ||
} | ||
int messageLength = dst.getInt(0); | ||
if (messageLength > 1024) { | ||
LogManager.severe("[SolarXR Bridge] Buffer overflow on socket. Message length: " + messageLength); | ||
this.resetChannel(); | ||
return null; | ||
} | ||
if (dst.position() < messageLength) { | ||
return null; | ||
} | ||
ByteBuffer message = dst.slice(); | ||
message.position(4); | ||
message.limit(messageLength); | ||
return message; | ||
} | ||
|
||
public void next() { | ||
int messageLength = dst.getInt(0); | ||
int originalpos = dst.position(); | ||
dst.position(messageLength); | ||
dst.compact(); | ||
dst.position(originalpos - messageLength); | ||
} | ||
} |
Oops, something went wrong.