Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
com.ccwangluo.accelerator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sheteng
com.ccwangluo.accelerator
Commits
493b6e85
Commit
493b6e85
authored
Feb 08, 2019
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix register called in wrong thread
parent
0efe2e8a
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
16 deletions
+13
-16
core/src/main/java/com/github/shadowsocks/net/ChannelMonitor.kt
...rc/main/java/com/github/shadowsocks/net/ChannelMonitor.kt
+12
-15
core/src/main/java/com/github/shadowsocks/net/LocalDnsServer.kt
...rc/main/java/com/github/shadowsocks/net/LocalDnsServer.kt
+1
-1
No files found.
core/src/main/java/com/github/shadowsocks/net/ChannelMonitor.kt
View file @
493b6e85
...
...
@@ -21,29 +21,26 @@
package
com.github.shadowsocks.net
import
com.github.shadowsocks.utils.printLog
import
kotlinx.coroutines.CompletableDeferred
import
kotlinx.coroutines.CoroutineScope
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.*
import
kotlinx.coroutines.channels.Channel
import
kotlinx.coroutines.launch
import
java.io.IOException
import
java.nio.ByteBuffer
import
java.nio.channels.*
class
ChannelMonitor
(
private
val
scope
:
CoroutineScope
)
:
Thread
(
"ChannelMonitor"
)
{
class
ChannelMonitor
:
Thread
(
"ChannelMonitor"
)
{
private
data class
Registration
(
val
channel
:
SelectableChannel
,
val
ops
:
Int
,
val
listener
:
suspend
(
SelectionKey
)
->
Unit
)
{
val
listener
:
(
SelectionKey
)
->
Unit
)
{
val
result
=
CompletableDeferred
<
SelectionKey
>()
}
private
val
selector
=
Selector
.
open
()
private
val
registrationPipe
=
Pipe
.
open
()
private
val
pendingRegistrations
=
Channel
<
Registration
>()
private
val
pendingRegistrations
=
Channel
<
Registration
>(
Channel
.
UNLIMITED
)
@Volatile
private
var
running
=
true
private
fun
registerInternal
(
channel
:
SelectableChannel
,
ops
:
Int
,
block
:
suspend
(
SelectionKey
)
->
Unit
)
=
private
fun
registerInternal
(
channel
:
SelectableChannel
,
ops
:
Int
,
block
:
(
SelectionKey
)
->
Unit
)
=
channel
.
register
(
selector
,
ops
,
block
)
init
{
...
...
@@ -52,7 +49,7 @@ class ChannelMonitor(private val scope: CoroutineScope) : Thread("ChannelMonitor
registerInternal
(
this
,
SelectionKey
.
OP_READ
)
{
val
junk
=
ByteBuffer
.
allocateDirect
(
1
)
while
(
read
(
junk
)
>
0
)
{
pendingRegistrations
.
receive
()
.
apply
{
pendingRegistrations
.
poll
()
!!
.
apply
{
try
{
result
.
complete
(
registerInternal
(
channel
,
ops
,
listener
))
}
catch
(
e
:
ClosedChannelException
)
{
...
...
@@ -66,7 +63,9 @@ class ChannelMonitor(private val scope: CoroutineScope) : Thread("ChannelMonitor
start
()
}
suspend
fun
register
(
channel
:
SelectableChannel
,
ops
:
Int
,
block
:
suspend
(
SelectionKey
)
->
Unit
):
SelectionKey
{
suspend
fun
register
(
channel
:
SelectableChannel
,
ops
:
Int
,
block
:
(
SelectionKey
)
->
Unit
):
SelectionKey
{
val
registration
=
Registration
(
channel
,
ops
,
block
)
pendingRegistrations
.
send
(
registration
)
ByteBuffer
.
allocateDirect
(
1
).
also
{
junk
->
loop@
while
(
running
)
when
(
registrationPipe
.
sink
().
write
(
junk
))
{
0
->
kotlinx
.
coroutines
.
yield
()
...
...
@@ -75,10 +74,7 @@ class ChannelMonitor(private val scope: CoroutineScope) : Thread("ChannelMonitor
}
}
if
(!
running
)
throw
ClosedChannelException
()
return
Registration
(
channel
,
ops
,
block
).
run
{
pendingRegistrations
.
send
(
this
)
result
.
await
()
}
return
registration
.
result
.
await
()
}
suspend
fun
wait
(
channel
:
SelectableChannel
,
ops
:
Int
)
=
CompletableDeferred
<
SelectionKey
>().
run
{
...
...
@@ -100,8 +96,9 @@ class ChannelMonitor(private val scope: CoroutineScope) : Thread("ChannelMonitor
if
(
num
<=
0
)
continue
val
iterator
=
selector
.
selectedKeys
().
iterator
()
while
(
iterator
.
hasNext
())
{
iterator
.
next
().
let
{
scope
.
launch
{
(
it
.
attachment
()
as
suspend
(
SelectionKey
)
->
Unit
)(
it
)
}
}
val
key
=
iterator
.
next
()
iterator
.
remove
()
(
key
.
attachment
()
as
(
SelectionKey
)
->
Unit
)(
key
)
}
}
}
...
...
core/src/main/java/com/github/shadowsocks/net/LocalDnsServer.kt
View file @
493b6e85
...
...
@@ -62,7 +62,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
private
const
val
TTL
=
120L
private
const
val
UDP_PACKET_SIZE
=
512
}
private
val
monitor
=
ChannelMonitor
(
this
)
private
val
monitor
=
ChannelMonitor
()
private
val
job
=
SupervisorJob
()
override
val
coroutineContext
=
Dispatchers
.
Default
+
job
+
CoroutineExceptionHandler
{
_
,
t
->
printLog
(
t
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment