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
6b9f2245
Commit
6b9f2245
authored
Feb 08, 2019
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Join threads non-blockingly
parent
00376c2e
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
48 additions
and
50 deletions
+48
-50
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/bg/ProxyInstance.kt
.../src/main/java/com/github/shadowsocks/bg/ProxyInstance.kt
+3
-3
core/src/main/java/com/github/shadowsocks/bg/TrafficMonitor.kt
...src/main/java/com/github/shadowsocks/bg/TrafficMonitor.kt
+2
-4
core/src/main/java/com/github/shadowsocks/net/ChannelMonitor.kt
...rc/main/java/com/github/shadowsocks/net/ChannelMonitor.kt
+5
-2
core/src/main/java/com/github/shadowsocks/net/ConcurrentLocalSocketListener.kt
...m/github/shadowsocks/net/ConcurrentLocalSocketListener.kt
+2
-2
core/src/main/java/com/github/shadowsocks/net/LocalSocketListener.kt
...in/java/com/github/shadowsocks/net/LocalSocketListener.kt
+35
-9
core/src/main/java/com/github/shadowsocks/net/SocketListener.kt
...rc/main/java/com/github/shadowsocks/net/SocketListener.kt
+0
-29
No files found.
core/src/main/java/com/github/shadowsocks/bg/BaseService.kt
View file @
6b9f2245
...
...
@@ -261,7 +261,7 @@ object BaseService {
data
.
notification
=
null
val
ids
=
listOfNotNull
(
data
.
proxy
,
data
.
udpFallback
).
map
{
it
.
close
(
)
it
.
shutdown
(
this
)
it
.
profile
.
id
}
data
.
proxy
=
null
...
...
core/src/main/java/com/github/shadowsocks/bg/ProxyInstance.kt
View file @
6b9f2245
...
...
@@ -45,7 +45,7 @@ import java.security.MessageDigest
/**
* This class sets up environment for ss-local.
*/
class
ProxyInstance
(
val
profile
:
Profile
,
private
val
route
:
String
=
profile
.
route
)
:
AutoCloseable
{
class
ProxyInstance
(
val
profile
:
Profile
,
private
val
route
:
String
=
profile
.
route
)
{
private
var
configFile
:
File
?
=
null
var
trafficMonitor
:
TrafficMonitor
?
=
null
private
val
plugin
=
PluginConfiguration
(
profile
.
plugin
?:
""
).
selectedOptions
...
...
@@ -125,9 +125,9 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro
if
(
route
!
in
arrayOf
(
Acl
.
ALL
,
Acl
.
CUSTOM_RULES
))
AclSyncer
.
schedule
(
route
)
}
override
fun
close
(
)
{
fun
shutdown
(
scope
:
CoroutineScope
)
{
trafficMonitor
?.
apply
{
close
(
)
thread
.
shutdown
(
scope
)
// Make sure update total traffic when stopping the runner
try
{
// profile may have host, etc. modified and thus a re-fetch is necessary (possible race condition)
...
...
core/src/main/java/com/github/shadowsocks/bg/TrafficMonitor.kt
View file @
6b9f2245
...
...
@@ -29,8 +29,8 @@ import java.io.IOException
import
java.nio.ByteBuffer
import
java.nio.ByteOrder
class
TrafficMonitor
(
statFile
:
File
)
:
AutoCloseable
{
private
val
thread
=
object
:
LocalSocketListener
(
"TrafficMonitor-"
+
statFile
.
name
,
statFile
)
{
class
TrafficMonitor
(
statFile
:
File
)
{
val
thread
=
object
:
LocalSocketListener
(
"TrafficMonitor-"
+
statFile
.
name
,
statFile
)
{
private
val
buffer
=
ByteArray
(
16
)
private
val
stat
=
ByteBuffer
.
wrap
(
buffer
).
order
(
ByteOrder
.
LITTLE_ENDIAN
)
override
fun
acceptInternal
(
socket
:
LocalSocket
)
{
...
...
@@ -79,6 +79,4 @@ class TrafficMonitor(statFile: File) : AutoCloseable {
}
return
Pair
(
out
,
updated
)
}
override
fun
close
()
=
thread
.
close
()
}
core/src/main/java/com/github/shadowsocks/net/ChannelMonitor.kt
View file @
6b9f2245
...
...
@@ -23,6 +23,7 @@ package com.github.shadowsocks.net
import
com.github.shadowsocks.utils.printLog
import
kotlinx.coroutines.*
import
kotlinx.coroutines.channels.Channel
import
kotlinx.coroutines.channels.sendBlocking
import
java.io.IOException
import
java.nio.ByteBuffer
import
java.nio.channels.*
...
...
@@ -37,6 +38,7 @@ class ChannelMonitor : Thread("ChannelMonitor") {
private
val
selector
=
Selector
.
open
()
private
val
registrationPipe
=
Pipe
.
open
()
private
val
pendingRegistrations
=
Channel
<
Registration
>(
Channel
.
UNLIMITED
)
private
val
closeChannel
=
Channel
<
Unit
>(
1
)
@Volatile
private
var
running
=
true
...
...
@@ -101,13 +103,14 @@ class ChannelMonitor : Thread("ChannelMonitor") {
(
key
.
attachment
()
as
(
SelectionKey
)
->
Unit
)(
key
)
}
}
closeChannel
.
sendBlocking
(
Unit
)
}
fun
close
(
scope
:
CoroutineScope
)
{
running
=
false
selector
.
wakeup
()
scope
.
launch
(
Dispatchers
.
IO
)
{
// thread joining is a blocking operation
join
()
scope
.
launch
{
closeChannel
.
receive
()
selector
.
keys
().
forEach
{
it
.
channel
().
close
()
}
selector
.
close
()
}
...
...
core/src/main/java/com/github/shadowsocks/net/ConcurrentLocalSocketListener.kt
View file @
6b9f2245
...
...
@@ -34,10 +34,10 @@ abstract class ConcurrentLocalSocketListener(name: String, socketFile: File) : L
launch
{
super
.
accept
(
socket
)
}
}
fun
shutdown
(
scope
:
CoroutineScope
)
{
override
fun
shutdown
(
scope
:
CoroutineScope
)
{
running
=
false
job
.
cancel
()
close
(
)
super
.
shutdown
(
scope
)
scope
.
launch
{
job
.
join
()
}
}
}
core/src/main/java/com/github/shadowsocks/net/LocalSocketListener.kt
View file @
6b9f2245
...
...
@@ -23,31 +23,57 @@ package com.github.shadowsocks.net
import
android.net.LocalServerSocket
import
android.net.LocalSocket
import
android.net.LocalSocketAddress
import
android.system.ErrnoException
import
android.system.Os
import
android.system.OsConstants
import
com.github.shadowsocks.utils.printLog
import
kotlinx.coroutines.CoroutineScope
import
kotlinx.coroutines.channels.Channel
import
kotlinx.coroutines.channels.sendBlocking
import
kotlinx.coroutines.launch
import
java.io.File
import
java.io.IOException
abstract
class
LocalSocketListener
(
name
:
String
,
socketFile
:
File
)
:
SocketListener
(
name
)
{
abstract
class
LocalSocketListener
(
name
:
String
,
socketFile
:
File
)
:
Thread
(
name
)
{
private
val
localSocket
=
LocalSocket
().
apply
{
socketFile
.
delete
()
// It's a must-have to close and reuse previous local socket.
bind
(
LocalSocketAddress
(
socketFile
.
absolutePath
,
LocalSocketAddress
.
Namespace
.
FILESYSTEM
))
}
private
val
serverSocket
=
LocalServerSocket
(
localSocket
.
fileDescriptor
)
override
val
fileDescriptor
get
()
=
localSocket
.
fileDescriptor
private
val
closeChannel
=
Channel
<
Unit
>(
1
)
@Volatile
protected
var
running
=
true
/**
* Inherited class do not need to close input/output streams as they will be closed automatically.
*/
protected
open
fun
accept
(
socket
:
LocalSocket
)
=
socket
.
use
{
acceptInternal
(
socket
)
}
protected
abstract
fun
acceptInternal
(
socket
:
LocalSocket
)
final
override
fun
run
()
=
localSocket
.
use
{
while
(
running
)
{
try
{
accept
(
serverSocket
.
accept
())
}
catch
(
e
:
IOException
)
{
if
(
running
)
printLog
(
e
)
continue
final
override
fun
run
()
{
localSocket
.
use
{
while
(
running
)
{
try
{
accept
(
serverSocket
.
accept
())
}
catch
(
e
:
IOException
)
{
if
(
running
)
printLog
(
e
)
continue
}
}
}
closeChannel
.
sendBlocking
(
Unit
)
}
open
fun
shutdown
(
scope
:
CoroutineScope
)
{
running
=
false
localSocket
.
fileDescriptor
.
apply
{
// see also: https://issuetracker.google.com/issues/36945762#comment15
if
(
valid
())
try
{
Os
.
shutdown
(
this
,
OsConstants
.
SHUT_RDWR
)
}
catch
(
e
:
ErrnoException
)
{
// suppress fd inactive or already closed
if
(
e
.
errno
!=
OsConstants
.
EBADF
&&
e
.
errno
!=
OsConstants
.
ENOTCONN
)
throw
IOException
(
e
)
}
}
scope
.
launch
{
closeChannel
.
receive
()
}
}
}
core/src/main/java/com/github/shadowsocks/net/SocketListener.kt
deleted
100644 → 0
View file @
00376c2e
package
com.github.shadowsocks.net
import
android.system.ErrnoException
import
android.system.Os
import
android.system.OsConstants
import
java.io.FileDescriptor
import
java.io.IOException
abstract
class
SocketListener
(
name
:
String
)
:
Thread
(
name
),
AutoCloseable
{
protected
abstract
val
fileDescriptor
:
FileDescriptor
@Volatile
protected
var
running
=
true
private
fun
FileDescriptor
.
shutdown
()
{
// see also: https://issuetracker.google.com/issues/36945762#comment15
if
(
valid
())
try
{
Os
.
shutdown
(
this
,
OsConstants
.
SHUT_RDWR
)
}
catch
(
e
:
ErrnoException
)
{
// suppress fd inactive or already closed
if
(
e
.
errno
!=
OsConstants
.
EBADF
&&
e
.
errno
!=
OsConstants
.
ENOTCONN
)
throw
IOException
(
e
)
}
}
override
fun
close
()
{
running
=
false
fileDescriptor
.
shutdown
()
join
()
}
}
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