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
490e0432
Commit
490e0432
authored
Feb 08, 2019
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix ClosedChannelException
parent
00dfb925
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
41 deletions
+58
-41
core/src/main/java/com/github/shadowsocks/bg/BaseService.kt
core/src/main/java/com/github/shadowsocks/bg/BaseService.kt
+1
-1
core/src/main/java/com/github/shadowsocks/net/ChannelMonitor.kt
...rc/main/java/com/github/shadowsocks/net/ChannelMonitor.kt
+55
-38
core/src/main/java/com/github/shadowsocks/net/LocalDnsServer.kt
...rc/main/java/com/github/shadowsocks/net/LocalDnsServer.kt
+2
-2
No files found.
core/src/main/java/com/github/shadowsocks/bg/BaseService.kt
View file @
490e0432
...
@@ -310,7 +310,7 @@ object BaseService {
...
@@ -310,7 +310,7 @@ object BaseService {
data
.
changeState
(
CONNECTING
)
data
.
changeState
(
CONNECTING
)
data
.
connectingJob
=
GlobalScope
.
launch
(
Dispatchers
.
Main
)
{
data
.
connectingJob
=
GlobalScope
.
launch
(
Dispatchers
.
Main
)
{
try
{
try
{
killProcesses
()
Executable
.
killAll
()
// clean up old processes
preInit
()
preInit
()
proxy
.
init
(
this
@Interface
::
resolver
)
proxy
.
init
(
this
@Interface
::
resolver
)
data
.
udpFallback
?.
init
(
this
@Interface
::
resolver
)
data
.
udpFallback
?.
init
(
this
@Interface
::
resolver
)
...
...
core/src/main/java/com/github/shadowsocks/net/ChannelMonitor.kt
View file @
490e0432
...
@@ -21,22 +21,28 @@
...
@@ -21,22 +21,28 @@
package
com.github.shadowsocks.net
package
com.github.shadowsocks.net
import
com.github.shadowsocks.utils.printLog
import
com.github.shadowsocks.utils.printLog
import
kotlinx.coroutines.suspendCancellableCoroutine
import
kotlinx.coroutines.*
import
kotlinx.coroutines.channels.Channel
import
java.io.IOException
import
java.io.IOException
import
java.lang.IllegalStateException
import
java.nio.ByteBuffer
import
java.nio.ByteBuffer
import
java.nio.channels.*
import
java.nio.channels.*
import
java.util.concurrent.ConcurrentLinkedQueue
import
java.util.concurrent.Executors
import
kotlin.coroutines.resume
class
ChannelMonitor
:
Thread
(
"ChannelMonitor"
),
AutoCloseable
{
class
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
selector
=
Selector
.
open
()
private
val
registrationPipe
=
Pipe
.
open
()
private
val
registrationPipe
=
Pipe
.
open
()
private
val
pendingRegistrations
=
C
oncurrentLinkedQueue
<
Triple
<
SelectableChannel
,
Int
,
(
SelectionKey
)
-
>
Unit
>
>()
private
val
pendingRegistrations
=
C
hannel
<
Registration
>()
@Volatile
@Volatile
private
var
running
=
true
private
var
running
=
true
private
fun
registerInternal
(
channel
:
SelectableChannel
,
ops
:
Int
,
block
:
(
SelectionKey
)
->
Unit
)
=
private
fun
registerInternal
(
channel
:
SelectableChannel
,
ops
:
Int
,
block
:
suspend
(
SelectionKey
)
->
Unit
)
=
channel
.
register
(
selector
,
ops
,
block
)
channel
.
register
(
selector
,
ops
,
block
)
init
{
init
{
...
@@ -45,52 +51,63 @@ class ChannelMonitor : Thread("ChannelMonitor"), AutoCloseable {
...
@@ -45,52 +51,63 @@ class ChannelMonitor : Thread("ChannelMonitor"), AutoCloseable {
registerInternal
(
this
,
SelectionKey
.
OP_READ
)
{
registerInternal
(
this
,
SelectionKey
.
OP_READ
)
{
val
junk
=
ByteBuffer
.
allocateDirect
(
1
)
val
junk
=
ByteBuffer
.
allocateDirect
(
1
)
while
(
read
(
junk
)
>
0
)
{
while
(
read
(
junk
)
>
0
)
{
val
(
channel
,
ops
,
block
)
=
pendingRegistrations
.
remove
()
pendingRegistrations
.
receive
().
apply
{
registerInternal
(
channel
,
ops
,
block
)
try
{
result
.
complete
(
registerInternal
(
channel
,
ops
,
listener
))
}
catch
(
e
:
ClosedChannelException
)
{
result
.
completeExceptionally
(
e
)
}
}
junk
.
clear
()
junk
.
clear
()
}
}
}
}
}
}
start
()
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
)
}
}
}
}
}
fun
register
(
channel
:
SelectableChannel
,
ops
:
Int
,
block
:
(
SelectionKey
)
->
Unit
)
{
suspend
fun
register
(
channel
:
SelectableChannel
,
ops
:
Int
,
block
:
suspend
(
SelectionKey
)
->
Unit
):
SelectionKey
{
pendingRegistrations
.
add
(
Triple
(
channel
,
ops
,
block
))
ByteBuffer
.
allocateDirect
(
1
).
also
{
junk
->
val
junk
=
ByteBuffer
.
allocateDirect
(
1
)
loop@
while
(
running
)
when
(
registrationPipe
.
sink
().
write
(
junk
))
{
while
(
running
&&
registrationPipe
.
sink
().
write
(
junk
)
==
0
);
0
->
yield
()
1
->
break
@loop
else
->
throw
IOException
(
"Failed to register in the channel"
)
}
}
if
(!
running
)
throw
ClosedChannelException
()
return
Registration
(
channel
,
ops
,
block
).
run
{
pendingRegistrations
.
send
(
this
)
result
.
await
()
}
}
}
suspend
fun
wait
(
channel
:
SelectableChannel
,
ops
:
Int
)
=
suspendCancellableCoroutine
<
Unit
>
{
cont
->
suspend
fun
wait
(
channel
:
SelectableChannel
,
ops
:
Int
)
=
CompletableDeferred
<
SelectionKey
>().
run
{
register
(
channel
,
ops
)
{
register
(
channel
,
ops
)
{
if
(
it
.
isValid
)
it
.
interestOps
(
0
)
// stop listening
if
(
it
.
isValid
)
it
.
interestOps
(
0
)
// stop listening
try
{
complete
(
it
)
cont
.
resume
(
Unit
)
}
catch
(
_
:
IllegalStateException
)
{
}
// already resumed by a timeout, maybe should use tryResume?
}
}
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
())
{
val
key
=
iterator
.
next
()
iterator
.
remove
()
(
key
.
attachment
()
as
(
SelectionKey
)
->
Unit
)(
key
)
}
}
}
await
()
}
}
override
fun
close
()
{
suspend
fun
close
()
{
running
=
false
running
=
false
selector
.
wakeup
()
selector
.
wakeup
()
join
()
jo
b
.
jo
in
()
selector
.
keys
().
forEach
{
it
.
channel
().
close
()
}
selector
.
keys
().
forEach
{
it
.
channel
().
close
()
}
selector
.
close
()
selector
.
close
()
}
}
...
...
core/src/main/java/com/github/shadowsocks/net/LocalDnsServer.kt
View file @
490e0432
...
@@ -67,14 +67,14 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
...
@@ -67,14 +67,14 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
private
val
job
=
SupervisorJob
()
private
val
job
=
SupervisorJob
()
override
val
coroutineContext
=
Dispatchers
.
Default
+
job
+
CoroutineExceptionHandler
{
_
,
t
->
printLog
(
t
)
}
override
val
coroutineContext
=
Dispatchers
.
Default
+
job
+
CoroutineExceptionHandler
{
_
,
t
->
printLog
(
t
)
}
fun
start
(
listen
:
SocketAddress
)
=
DatagramChannel
.
open
().
apply
{
suspend
fun
start
(
listen
:
SocketAddress
)
=
DatagramChannel
.
open
().
apply
{
configureBlocking
(
false
)
configureBlocking
(
false
)
socket
().
bind
(
listen
)
socket
().
bind
(
listen
)
monitor
.
register
(
this
,
SelectionKey
.
OP_READ
)
{
monitor
.
register
(
this
,
SelectionKey
.
OP_READ
)
{
val
buffer
=
ByteBuffer
.
allocateDirect
(
UDP_PACKET_SIZE
)
val
buffer
=
ByteBuffer
.
allocateDirect
(
UDP_PACKET_SIZE
)
val
source
=
receive
(
buffer
)
!!
val
source
=
receive
(
buffer
)
!!
buffer
.
flip
()
buffer
.
flip
()
launch
{
this
@LocalDnsServer
.
launch
{
val
reply
=
resolve
(
buffer
)
val
reply
=
resolve
(
buffer
)
while
(
send
(
reply
,
source
)
<=
0
)
monitor
.
wait
(
this
@apply
,
SelectionKey
.
OP_WRITE
)
while
(
send
(
reply
,
source
)
<=
0
)
monitor
.
wait
(
this
@apply
,
SelectionKey
.
OP_WRITE
)
}
}
...
...
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