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
0efe2e8a
Commit
0efe2e8a
authored
Feb 08, 2019
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert using a thread pool
parent
0d5834eb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
25 deletions
+27
-25
core/src/main/java/com/github/shadowsocks/net/ChannelMonitor.kt
...rc/main/java/com/github/shadowsocks/net/ChannelMonitor.kt
+26
-24
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 @
0efe2e8a
...
...
@@ -21,21 +21,22 @@
package
com.github.shadowsocks.net
import
com.github.shadowsocks.utils.printLog
import
kotlinx.coroutines.*
import
kotlinx.coroutines.CompletableDeferred
import
kotlinx.coroutines.CoroutineScope
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.channels.Channel
import
kotlinx.coroutines.launch
import
java.io.IOException
import
java.nio.ByteBuffer
import
java.nio.channels.*
import
java.util.concurrent.Executors
class
ChannelMonitor
{
class
ChannelMonitor
(
private
val
scope
:
CoroutineScope
)
:
Thread
(
"ChannelMonitor"
)
{
private
data class
Registration
(
val
channel
:
SelectableChannel
,
val
ops
:
Int
,
val
listener
:
suspend
(
SelectionKey
)
->
Unit
)
{
val
result
=
CompletableDeferred
<
SelectionKey
>()
}
private
val
job
:
Job
private
val
selector
=
Selector
.
open
()
private
val
registrationPipe
=
Pipe
.
open
()
private
val
pendingRegistrations
=
Channel
<
Registration
>()
...
...
@@ -62,29 +63,13 @@ class ChannelMonitor {
}
}
}
job
=
GlobalScope
.
launch
(
Executors
.
newSingleThreadExecutor
().
asCoroutineDispatcher
())
{
while
(
running
)
{
val
num
=
try
{
selector
.
select
()
}
catch
(
e
:
IOException
)
{
printLog
(
e
)
continue
}
if
(
num
<=
0
)
continue
val
iterator
=
selector
.
selectedKeys
().
iterator
()
while
(
iterator
.
hasNext
())
{
val
key
=
iterator
.
next
()
iterator
.
remove
()
(
key
.
attachment
()
as
suspend
(
SelectionKey
)
->
Unit
)(
key
)
}
}
}
start
()
}
suspend
fun
register
(
channel
:
SelectableChannel
,
ops
:
Int
,
block
:
suspend
(
SelectionKey
)
->
Unit
):
SelectionKey
{
ByteBuffer
.
allocateDirect
(
1
).
also
{
junk
->
loop@
while
(
running
)
when
(
registrationPipe
.
sink
().
write
(
junk
))
{
0
->
yield
()
0
->
kotlinx
.
coroutines
.
yield
()
1
->
break
@loop
else
->
throw
IOException
(
"Failed to register in the channel"
)
}
...
...
@@ -104,11 +89,28 @@ class ChannelMonitor {
await
()
}
override
fun
run
()
{
while
(
running
)
{
val
num
=
try
{
selector
.
select
()
}
catch
(
e
:
IOException
)
{
printLog
(
e
)
continue
}
if
(
num
<=
0
)
continue
val
iterator
=
selector
.
selectedKeys
().
iterator
()
while
(
iterator
.
hasNext
())
{
iterator
.
next
().
let
{
scope
.
launch
{
(
it
.
attachment
()
as
suspend
(
SelectionKey
)
->
Unit
)(
it
)
}
}
iterator
.
remove
()
}
}
}
fun
close
(
scope
:
CoroutineScope
)
{
running
=
false
selector
.
wakeup
()
scope
.
launch
{
jo
b
.
jo
in
()
scope
.
launch
(
Dispatchers
.
IO
)
{
// thread joining is a blocking operation
join
()
selector
.
keys
().
forEach
{
it
.
channel
().
close
()
}
selector
.
close
()
}
...
...
core/src/main/java/com/github/shadowsocks/net/LocalDnsServer.kt
View file @
0efe2e8a
...
...
@@ -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
()
private
val
monitor
=
ChannelMonitor
(
this
)
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