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
82a3a34b
Commit
82a3a34b
authored
Jul 24, 2019
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix useCancellable not cancellable
parent
44c4f79c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
5 deletions
+12
-5
core/src/main/java/com/github/shadowsocks/utils/Utils.kt
core/src/main/java/com/github/shadowsocks/utils/Utils.kt
+12
-5
No files found.
core/src/main/java/com/github/shadowsocks/utils/Utils.kt
View file @
82a3a34b
...
...
@@ -42,12 +42,13 @@ import androidx.core.view.updateLayoutParams
import
androidx.preference.Preference
import
com.crashlytics.android.Crashlytics
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.GlobalScope
import
kotlinx.coroutines.launch
import
kotlinx.coroutines.suspendCancellableCoroutine
import
kotlinx.coroutines.withContext
import
java.net.HttpURLConnection
import
java.net.InetAddress
import
kotlin.coroutines.resume
import
kotlin.coroutines.resumeWithException
fun
<
T
>
Iterable
<
T
>.
forEachTry
(
action
:
(
T
)
->
Unit
)
{
var
result
:
Exception
?
=
null
...
...
@@ -82,12 +83,18 @@ fun String?.parseNumericAddress(): InetAddress? = Os.inet_pton(OsConstants.AF_IN
fun
<
K
,
V
>
MutableMap
<
K
,
V
>.
computeIfAbsentCompat
(
key
:
K
,
value
:
()
->
V
)
=
if
(
Build
.
VERSION
.
SDK_INT
>=
24
)
computeIfAbsent
(
key
)
{
value
()
}
else
this
[
key
]
?:
value
().
also
{
put
(
key
,
it
)
}
suspend
fun
<
T
>
HttpURLConnection
.
useCancellable
(
block
:
HttpURLConnection
.()
->
T
)
=
withContext
(
Dispatchers
.
IO
)
{
suspendCancellableCoroutine
<
T
>
{
cont
->
suspend
fun
<
T
>
HttpURLConnection
.
useCancellable
(
block
:
suspend
HttpURLConnection
.()
->
T
):
T
{
return
suspendCancellableCoroutine
{
cont
->
cont
.
invokeOnCancellation
{
if
(
Build
.
VERSION
.
SDK_INT
>=
26
)
disconnect
()
else
launch
(
Dispatchers
.
IO
)
{
disconnect
()
}
if
(
Build
.
VERSION
.
SDK_INT
>=
26
)
disconnect
()
else
GlobalScope
.
launch
(
Dispatchers
.
IO
)
{
disconnect
()
}
}
GlobalScope
.
launch
(
Dispatchers
.
IO
)
{
try
{
cont
.
resume
(
block
())
}
catch
(
e
:
Throwable
)
{
cont
.
resumeWithException
(
e
)
}
}
cont
.
resume
(
block
())
}
}
...
...
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