Skip to content

Commit be3fbd1

Browse files
committed
More socket refactoring
1 parent a03fcc9 commit be3fbd1

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/util/SocketClient.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.squareup.workflow1.traceviewer.util
22

3+
import kotlinx.coroutines.CoroutineScope
34
import kotlinx.coroutines.Dispatchers
45
import kotlinx.coroutines.awaitCancellation
6+
import kotlinx.coroutines.channels.Channel
57
import kotlinx.coroutines.coroutineScope
68
import kotlinx.coroutines.ensureActive
79
import kotlinx.coroutines.launch
@@ -10,6 +12,22 @@ import kotlinx.coroutines.withContext
1012
import okio.IOException
1113
import java.net.Socket
1214

15+
internal suspend fun streamRenderPassesFromDevice(parseOnNewRenderPass: (String) -> Unit) {
16+
val renderPassChannel: Channel<String> = Channel(Channel.BUFFERED)
17+
coroutineScope {
18+
try {
19+
pollSocket(onNewRenderPass = renderPassChannel::send)
20+
} finally {
21+
renderPassChannel.close()
22+
}
23+
}
24+
25+
// Since channel implements ChannelIterator, we can for-loop through on the receiver end.
26+
for (renderPass in renderPassChannel) {
27+
parseOnNewRenderPass(renderPass)
28+
}
29+
}
30+
1331
/**
1432
* Collects data from a server socket and serves them back to the caller via callback.
1533
*
@@ -20,7 +38,7 @@ import java.net.Socket
2038
* @param onNewRenderPass is called from an arbitrary thread, so it is important to ensure that the
2139
* caller is thread safe
2240
*/
23-
suspend fun pollSocket(onNewRenderPass: suspend (String) -> Unit) {
41+
private suspend fun pollSocket(onNewRenderPass: suspend (String) -> Unit) {
2442
withContext(Dispatchers.IO) {
2543
try {
2644
runForwardingPortThroughAdb { port ->

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/util/TraceParser.kt

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import com.squareup.moshi.JsonAdapter
1414
import com.squareup.workflow1.traceviewer.TraceMode
1515
import com.squareup.workflow1.traceviewer.model.Node
1616
import com.squareup.workflow1.traceviewer.ui.DrawTree
17-
import kotlinx.coroutines.channels.Channel
18-
import kotlinx.coroutines.launch
19-
import java.net.SocketException
2017

2118
/**
2219
* Handles parsing the trace's after JsonParser has turned all render passes into frames. Also calls
@@ -58,6 +55,7 @@ internal fun RenderTrace(
5855
is ParseResult.Failure -> {
5956
error = parseResult.error.toString()
6057
}
58+
6159
is ParseResult.Success -> {
6260
addToStates(
6361
frame = parseResult.trace,
@@ -80,23 +78,13 @@ internal fun RenderTrace(
8078
}
8179

8280
is TraceMode.Live -> {
83-
val renderPassChannel: Channel<String> = Channel(Channel.BUFFERED)
84-
launch {
85-
try {
86-
pollSocket(onNewRenderPass = renderPassChannel::send)
87-
error = "Socket has already been closed or is not available."
88-
} finally {
89-
renderPassChannel.close()
90-
}
91-
}
9281
val adapter: JsonAdapter<List<Node>> = createMoshiAdapter<Node>()
93-
94-
// Since channel implements ChannelIterator, we can for-loop through on the receiver end.
95-
for (renderPass in renderPassChannel) {
82+
streamRenderPassesFromDevice { renderPass ->
9683
val currentTree = fullTree.lastOrNull()
9784
val parseResult = parseLiveTrace(renderPass, adapter, currentTree)
9885
handleParseResult(parseResult, onNewFrame)
9986
}
87+
error = "Socket has already been closed or is not available."
10088
}
10189
}
10290
}

0 commit comments

Comments
 (0)