@ -16,16 +16,20 @@ public final class DesktopConnection implements Closeable {
private static final String SOCKET_NAME = "scrcpy" ;
private static final String SOCKET_NAME = "scrcpy" ;
private final LocalSocket socket ;
private final LocalSocket videoSocket ;
private final InputStream inputStream ;
private final FileDescriptor videoFd ;
private final FileDescriptor fd ;
private final LocalSocket controlSocket ;
private final InputStream controlInputStream ;
private final ControlEventReader reader = new ControlEventReader ( ) ;
private final ControlEventReader reader = new ControlEventReader ( ) ;
private DesktopConnection ( LocalSocket socket ) throws IOException {
private DesktopConnection ( LocalSocket videoSocket , LocalSocket controlSocket ) throws IOException {
this . socket = socket ;
this . videoSocket = videoSocket ;
inputStream = socket . getInputStream ( ) ;
this . controlSocket = controlSocket ;
fd = socket . getFileDescriptor ( ) ;
controlInputStream = controlSocket . getInputStream ( ) ;
videoFd = videoSocket . getFileDescriptor ( ) ;
}
}
private static LocalSocket connect ( String abstractName ) throws IOException {
private static LocalSocket connect ( String abstractName ) throws IOException {
@ -34,35 +38,47 @@ public final class DesktopConnection implements Closeable {
return localSocket ;
return localSocket ;
}
}
private static LocalSocket listenAndAccept ( String abstractName ) throws IOException {
public static DesktopConnection open ( Device device , boolean tunnelForward ) throws IOException {
LocalServerSocket localServerSocket = new LocalServerSocket ( abstractName ) ;
LocalSocket videoSocket ;
LocalSocket controlSocket ;
if ( tunnelForward ) {
LocalServerSocket localServerSocket = new LocalServerSocket ( SOCKET_NAME ) ;
try {
try {
return localServerSocket . accept ( ) ;
videoSocket = localServerSocket . accept ( ) ;
// send one byte so the client may read() to detect a connection error
videoSocket . getOutputStream ( ) . write ( 0 ) ;
try {
controlSocket = localServerSocket . accept ( ) ;
} catch ( IOException | RuntimeException e ) {
videoSocket . close ( ) ;
throw e ;
}
} finally {
} finally {
localServerSocket . close ( ) ;
localServerSocket . close ( ) ;
}
}
}
public static DesktopConnection open ( Device device , boolean tunnelForward ) throws IOException {
LocalSocket socket ;
if ( tunnelForward ) {
socket = listenAndAccept ( SOCKET_NAME ) ;
// send one byte so the client may read() to detect a connection error
socket . getOutputStream ( ) . write ( 0 ) ;
} else {
} else {
socket = connect ( SOCKET_NAME ) ;
videoSocket = connect ( SOCKET_NAME ) ;
try {
controlSocket = connect ( SOCKET_NAME ) ;
} catch ( IOException | RuntimeException e ) {
videoSocket . close ( ) ;
throw e ;
}
}
}
DesktopConnection connection = new DesktopConnection ( socket ) ;
DesktopConnection connection = new DesktopConnection ( videoSocket, controlS ocket) ;
Size videoSize = device . getScreenInfo ( ) . getVideoSize ( ) ;
Size videoSize = device . getScreenInfo ( ) . getVideoSize ( ) ;
connection . send ( Device . getDeviceName ( ) , videoSize . getWidth ( ) , videoSize . getHeight ( ) ) ;
connection . send ( Device . getDeviceName ( ) , videoSize . getWidth ( ) , videoSize . getHeight ( ) ) ;
return connection ;
return connection ;
}
}
public void close ( ) throws IOException {
public void close ( ) throws IOException {
socket . shutdownInput ( ) ;
videoSocket . shutdownInput ( ) ;
socket . shutdownOutput ( ) ;
videoSocket . shutdownOutput ( ) ;
socket . close ( ) ;
videoSocket . close ( ) ;
controlSocket . shutdownInput ( ) ;
controlSocket . shutdownOutput ( ) ;
controlSocket . close ( ) ;
}
}
@SuppressWarnings ( "checkstyle:MagicNumber" )
@SuppressWarnings ( "checkstyle:MagicNumber" )
@ -78,17 +94,17 @@ public final class DesktopConnection implements Closeable {
buffer [ DEVICE_NAME_FIELD_LENGTH + 1 ] = ( byte ) width ;
buffer [ DEVICE_NAME_FIELD_LENGTH + 1 ] = ( byte ) width ;
buffer [ DEVICE_NAME_FIELD_LENGTH + 2 ] = ( byte ) ( height > > 8 ) ;
buffer [ DEVICE_NAME_FIELD_LENGTH + 2 ] = ( byte ) ( height > > 8 ) ;
buffer [ DEVICE_NAME_FIELD_LENGTH + 3 ] = ( byte ) height ;
buffer [ DEVICE_NAME_FIELD_LENGTH + 3 ] = ( byte ) height ;
IO . writeFully ( f d, buffer , 0 , buffer . length ) ;
IO . writeFully ( videoF d, buffer , 0 , buffer . length ) ;
}
}
public FileDescriptor get Fd( ) {
public FileDescriptor get Video Fd( ) {
return f d;
return videoF d;
}
}
public ControlEvent receiveControlEvent ( ) throws IOException {
public ControlEvent receiveControlEvent ( ) throws IOException {
ControlEvent event = reader . next ( ) ;
ControlEvent event = reader . next ( ) ;
while ( event = = null ) {
while ( event = = null ) {
reader . readFrom ( i nputStream) ;
reader . readFrom ( controlI nputStream) ;
event = reader . next ( ) ;
event = reader . next ( ) ;
}
}
return event ;
return event ;