mirror of
https://github.com/moonlight-stream/moonlight-android.git
synced 2025-07-22 04:22:45 +00:00
Finish packet reading for RTSP and control streams
This commit is contained in:
parent
c06a4ab76d
commit
b191425112
@ -186,6 +186,7 @@ public class ControlStream implements ConnectionStatusListener, InputPacketSende
|
|||||||
// Prevent multiple clients from writing to the stream at the same time
|
// Prevent multiple clients from writing to the stream at the same time
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (context.serverGeneration >= ConnectionContext.SERVER_GENERATION_5) {
|
if (context.serverGeneration >= ConnectionContext.SERVER_GENERATION_5) {
|
||||||
|
enetConnection.pumpSocket();
|
||||||
packet.write(enetConnection);
|
packet.write(enetConnection);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -200,7 +201,7 @@ public class ControlStream implements ConnectionStatusListener, InputPacketSende
|
|||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
sendPacket(packet);
|
sendPacket(packet);
|
||||||
if (context.serverGeneration >= ConnectionContext.SERVER_GENERATION_5) {
|
if (context.serverGeneration >= ConnectionContext.SERVER_GENERATION_5) {
|
||||||
enetConnection.readPacket(128, CONTROL_TIMEOUT);
|
enetConnection.readPacket(0, CONTROL_TIMEOUT);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
new NvCtlResponse(in);
|
new NvCtlResponse(in);
|
||||||
@ -574,8 +575,10 @@ public class ControlStream implements ConnectionStatusListener, InputPacketSende
|
|||||||
// Use the class's serialization buffer to construct the wireform to send
|
// Use the class's serialization buffer to construct the wireform to send
|
||||||
synchronized (serializationBuffer) {
|
synchronized (serializationBuffer) {
|
||||||
serializationBuffer.rewind();
|
serializationBuffer.rewind();
|
||||||
|
serializationBuffer.limit(serializationBuffer.capacity());
|
||||||
serializationBuffer.putShort(type);
|
serializationBuffer.putShort(type);
|
||||||
serializationBuffer.put(payload);
|
serializationBuffer.put(payload);
|
||||||
|
serializationBuffer.limit(serializationBuffer.position());
|
||||||
|
|
||||||
conn.writePacket(serializationBuffer);
|
conn.writePacket(serializationBuffer);
|
||||||
}
|
}
|
||||||
|
@ -37,21 +37,54 @@ public class EnetConnection implements Closeable {
|
|||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ByteBuffer readPacket(int maxSize, int timeout) throws IOException {
|
public void pumpSocket() throws IOException {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(maxSize);
|
int ret;
|
||||||
|
while ((ret = readPacket(enetClient, null, 0, 0)) > 0);
|
||||||
int readLength = readPacket(enetClient, buffer.array(), buffer.limit(), timeout);
|
if (ret < 0) {
|
||||||
if (readLength <= 0) {
|
throw new IOException("ENet connection failed");
|
||||||
throw new IOException("Failed to receive ENet packet");
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.limit(readLength);
|
public ByteBuffer readPacket(int maxSize, int timeout) throws IOException {
|
||||||
|
ByteBuffer buffer;
|
||||||
|
byte[] array;
|
||||||
|
int length;
|
||||||
|
|
||||||
|
if (maxSize != 0) {
|
||||||
|
buffer = ByteBuffer.allocate(maxSize);
|
||||||
|
array = buffer.array();
|
||||||
|
length = buffer.limit();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// The caller doesn't want the packet back
|
||||||
|
buffer = null;
|
||||||
|
array = null;
|
||||||
|
length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int readLength = readPacket(enetClient, array, length, timeout);
|
||||||
|
if (readLength > length && length != 0) {
|
||||||
|
// This is a packet that was unexpectedly large compared to
|
||||||
|
// what the caller was expected.
|
||||||
|
throw new IOException("Received ENet packet too large: "+readLength);
|
||||||
|
}
|
||||||
|
else if (readLength <= 0) {
|
||||||
|
// We either got nothing or a socket error
|
||||||
|
throw new IOException("Failed to receive ENet packet");
|
||||||
|
}
|
||||||
|
else if (length == 0) {
|
||||||
|
// We received a packet but the caller didn't want it back
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// A packet was received which matched the caller's expectations
|
||||||
|
buffer.limit(readLength);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void writePacket(ByteBuffer buffer) throws IOException {
|
public void writePacket(ByteBuffer buffer) throws IOException {
|
||||||
if (!writePacket(enetClient, enetPeer, buffer.array(), buffer.position(), ENET_PACKET_FLAG_RELIABLE)) {
|
if (!writePacket(enetClient, enetPeer, buffer.array(), buffer.limit(), ENET_PACKET_FLAG_RELIABLE)) {
|
||||||
throw new IOException("Failed to send ENet packet");
|
throw new IOException("Failed to send ENet packet");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user