Skip to content

Commit a159419

Browse files
Merge pull request #2 from alexarchambault/develop
Add helpers to convert channels to java.net.*Socket
2 parents dfc1ae4 + bb56bb3 commit a159419

File tree

6 files changed

+65
-2
lines changed

6 files changed

+65
-2
lines changed

.scalafmt.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ project.excludeFilters = [
3535
".metals"
3636
"out"
3737
]
38+
fileOverride {
39+
"glob:**/manual/client/src/**" {
40+
runner.dialect = scala3
41+
}
42+
"glob:**/manual/server/src/**" {
43+
runner.dialect = scala3
44+
}
45+
}

library/src/libdaemonjvm/Util.scala

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package libdaemonjvm
2+
3+
import java.net.{ServerSocket, Socket}
4+
import java.nio.ByteBuffer
5+
import java.nio.channels.{Channels, ReadableByteChannel, ServerSocketChannel, SocketChannel}
6+
7+
object Util {
8+
9+
def socketFromChannel(channel: SocketChannel): Socket =
10+
new Socket {
11+
override def getInputStream() =
12+
Channels.newInputStream(
13+
// Passing a custom ReadableByteChannel rather than channel,
14+
// so that ChannelInputStream doesn't try to acquire the "blocking lock",
15+
// that's also acquired by output stream stuff below (which causes reads and
16+
// writes to block each other).
17+
new ReadableByteChannel {
18+
override def read(dst: ByteBuffer) =
19+
channel.read(dst)
20+
override def close() =
21+
channel.close()
22+
override def isOpen() =
23+
channel.isOpen()
24+
}
25+
)
26+
override def getOutputStream() =
27+
Channels.newOutputStream(channel)
28+
// override def getLocalAddress() =
29+
// channel.getLocalAddress()
30+
override def isConnected() =
31+
channel.isConnected()
32+
override def getRemoteSocketAddress() =
33+
channel.getRemoteAddress()
34+
override def close() =
35+
channel.close()
36+
override def shutdownInput() =
37+
channel.shutdownInput()
38+
override def shutdownOutput() =
39+
channel.shutdownOutput()
40+
}
41+
42+
def serverSocketFromChannel(serverChannel: ServerSocketChannel): ServerSocket =
43+
new ServerSocket {
44+
override def accept() =
45+
socketFromChannel(serverChannel.accept())
46+
override def close() =
47+
serverChannel.close()
48+
override def getLocalPort() = -1
49+
}
50+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package libdaemonjvm.errors
2+
3+
class ConnectExceptionLike(cause: Throwable) extends SocketExceptionLike(cause)

library/src/libdaemonjvm/internal/SocketExceptionLike.scala renamed to library/src/libdaemonjvm/errors/SocketExceptionLike.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package libdaemonjvm.internal
1+
package libdaemonjvm.errors
22

33
import java.io.IOException
44

library/src/libdaemonjvm/internal/SocketFile.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import java.nio.channels.SocketChannel
55
import java.nio.file.Path
66
import java.net.Socket
77

8+
import libdaemonjvm.errors._
89
import libdaemonjvm.SocketPaths
910

1011
object SocketFile {

library/src/libdaemonjvm/internal/SocketHandler.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import scala.util.Properties
1212
import org.scalasbt.ipcsocket.Win32NamedPipeSocket
1313
import org.scalasbt.ipcsocket.Win32NamedPipeServerSocket
1414

15+
import libdaemonjvm.errors._
1516
import libdaemonjvm.SocketPaths
1617

1718
object SocketHandler {
@@ -21,7 +22,7 @@ object SocketHandler {
2122
if Option(ex.getCause)
2223
.collect { case e: NativeErrorException => e }
2324
.exists(e => connectionRelatedCodes(e.returnCode)) =>
24-
throw new SocketExceptionLike(ex)
25+
throw new ConnectExceptionLike(ex)
2526
case ex: IOException if ex.getMessage.contains("error code 2") =>
2627
throw new SocketExceptionLike(ex)
2728
case ex: IOException if ex.getMessage.contains("Couldn't open pipe for") =>

0 commit comments

Comments
 (0)