-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSocketBasedPluginFrontend.scala
57 lines (47 loc) · 1.62 KB
/
SocketBasedPluginFrontend.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package protocbridge.frontend
import protocbridge.{ExtraEnv, ProtocCodeGenerator}
import java.net.{InetAddress, ServerSocket}
import java.nio.file.{Files, Path}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future, blocking}
/** PluginFrontend for Windows and macOS where a server socket is used.
*/
abstract class SocketBasedPluginFrontend extends PluginFrontend {
case class InternalState(serverSocket: ServerSocket, shellScript: Path)
override def prepare(
plugin: ProtocCodeGenerator,
env: ExtraEnv
): (Path, InternalState) = {
/** we need to specify an address, otherwise it uses ANY (*.* in netstat)
* which does not conflict with existing 'localhost' sockets on macos,
* resulting in a conflict later on in MacPluginFrontend
*/
val ss = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1"))
val sh = createShellScript(ss.getLocalPort)
Future {
blocking {
// Accept a single client connection from the shell script.
val client = ss.accept()
try {
val response =
PluginFrontend.runWithInputStream(
plugin,
client.getInputStream,
env
)
client.getOutputStream.write(response)
} finally {
client.close()
}
}
}
(sh, InternalState(ss, sh))
}
override def cleanup(state: InternalState): Unit = {
state.serverSocket.close()
if (sys.props.get("protocbridge.debug") != Some("1")) {
Files.delete(state.shellScript)
}
}
protected def createShellScript(port: Int): Path
}