Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
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
MyCard
Mirai
Commits
22f2c508
Commit
22f2c508
authored
Feb 05, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reformat code
parent
aa7a3206
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
28 additions
and
30 deletions
+28
-30
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt
...-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt
+14
-17
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/DebugUtil.kt
...c/commonMain/kotlin/net.mamoe.mirai/utils/io/DebugUtil.kt
+1
-0
mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/GentleImage.kt
...o-gentleman/src/main/kotlin/demo/gentleman/GentleImage.kt
+9
-7
mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/Gentleman.kt
...emo-gentleman/src/main/kotlin/demo/gentleman/Gentleman.kt
+4
-6
No files found.
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt
View file @
22f2c508
...
...
@@ -9,7 +9,7 @@ import net.mamoe.mirai.message.MessagePacket
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.EmptyCoroutineContext
tailrec
fun
generateSessionKey
():
String
{
tailrec
fun
generateSessionKey
():
String
{
fun
generateRandomSessionKey
():
String
{
val
all
=
"QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm"
return
buildString
(
capacity
=
8
)
{
...
...
@@ -20,27 +20,27 @@ tailrec fun generateSessionKey():String{
}
val
key
=
generateRandomSessionKey
()
if
(!
SessionManager
.
allSession
.
containsKey
(
key
))
{
if
(!
SessionManager
.
allSession
.
containsKey
(
key
))
{
return
key
}
return
generateSessionKey
()
}
object
SessionManager
{
internal
object
SessionManager
{
val
allSession
:
MutableMap
<
String
,
Session
>
=
mutableMapOf
()
val
allSession
:
MutableMap
<
String
,
Session
>
=
mutableMapOf
()
lateinit
var
authKey
:
String
lateinit
var
authKey
:
String
fun
createTempSession
():
TempSession
=
TempSession
(
EmptyCoroutineContext
).
also
{
newTempSession
->
fun
createTempSession
():
TempSession
=
TempSession
(
EmptyCoroutineContext
).
also
{
newTempSession
->
allSession
[
newTempSession
.
key
]
=
newTempSession
//设置180000ms后检测并回收
newTempSession
.
launch
{
newTempSession
.
launch
{
delay
(
180000
)
allSession
[
newTempSession
.
key
]
?.
run
{
if
(
this
is
TempSession
)
if
(
this
is
TempSession
)
closeSession
(
newTempSession
.
key
)
}
}
...
...
@@ -50,15 +50,13 @@ object SessionManager {
fun
containSession
(
sessionKey
:
String
):
Boolean
=
allSession
.
containsKey
(
sessionKey
)
fun
closeSession
(
sessionKey
:
String
)
=
allSession
.
remove
(
sessionKey
)
?.
also
{
it
.
close
()
}
fun
closeSession
(
sessionKey
:
String
)
=
allSession
.
remove
(
sessionKey
)
?.
also
{
it
.
close
()
}
fun
closeSession
(
session
:
Session
)
=
closeSession
(
session
.
key
)
}
/**
* @author NaturalHG
* 这个用于管理不同Client与Mirai HTTP的会话
...
...
@@ -68,20 +66,19 @@ object SessionManager {
*/
abstract
class
Session
internal
constructor
(
coroutineContext
:
CoroutineContext
):
CoroutineScope
{
)
:
CoroutineScope
{
val
supervisorJob
=
SupervisorJob
(
coroutineContext
[
Job
])
final
override
val
coroutineContext
:
CoroutineContext
=
supervisorJob
+
coroutineContext
val
key
:
String
=
generateSessionKey
()
val
key
:
String
=
generateSessionKey
()
internal
open
fun
close
(){
internal
open
fun
close
()
{
supervisorJob
.
complete
()
}
}
/**
* 任何新链接建立后分配一个[TempSession]
*
...
...
@@ -93,10 +90,10 @@ class TempSession internal constructor(coroutineContext: CoroutineContext) : Ses
* 任何[TempSession]认证后转化为一个[AuthedSession]
* 在这一步[AuthedSession]应该已经有assigned的bot
*/
class
AuthedSession
internal
constructor
(
val
bot
:
Bot
,
coroutineContext
:
CoroutineContext
)
:
Session
(
coroutineContext
)
{
class
AuthedSession
internal
constructor
(
val
bot
:
Bot
,
coroutineContext
:
CoroutineContext
)
:
Session
(
coroutineContext
)
{
val
messageQueue
=
MessageQueue
()
private
val
_listener
:
Listener
<
MessagePacket
<
*
,
*
>>
private
val
_listener
:
Listener
<
MessagePacket
<
*
,
*
>>
init
{
bot
.
subscribeMessages
{
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/DebugUtil.kt
View file @
22f2c508
...
...
@@ -70,6 +70,7 @@ fun ByteReadPacket.debugPrintThis(name: String = ""): ByteReadPacket {
@MiraiDebugAPI
(
"Low efficiency"
)
@UseExperimental
(
ExperimentalContracts
::
class
)
inline
fun
<
R
>
Input
.
debugIfFail
(
name
:
String
=
""
,
onFail
:
(
ByteArray
)
->
ByteReadPacket
=
{
it
.
toReadPacket
()
},
block
:
ByteReadPacket
.()
->
R
):
R
{
contract
{
callsInPlace
(
block
,
InvocationKind
.
EXACTLY_ONCE
)
callsInPlace
(
onFail
,
InvocationKind
.
UNKNOWN
)
...
...
mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/GentleImage.kt
View file @
22f2c508
package
demo.gentleman
import
com.alibaba.fastjson.JSON
import
com.alibaba.fastjson.JSONObject
import
kotlinx.coroutines.*
import
net.mamoe.mirai.contact.Contact
import
net.mamoe.mirai.message.data.Image
import
net.mamoe.mirai.message.uploadAsImage
import
org.jsoup.Jsoup
import
kotlin.random.Random
class
GentleImage
{
lateinit
var
contact
:
Contact
// `Deferred<Image?>` causes a runtime ClassCastException
class
GentleImage
(
val
contact
:
Contact
,
val
keyword
:
String
)
{
val
image
:
Deferred
<
Image
>
by
lazy
{
getImage
(
0
)
}
...
...
@@ -18,18 +17,21 @@ class GentleImage {
fun
getImage
(
r18
:
Int
):
Deferred
<
Image
>
{
return
GlobalScope
.
async
{
withTimeoutOrNull
(
5
*
1000
)
{
withTimeoutOrNull
(
10
*
1000
)
{
withContext
(
Dispatchers
.
IO
)
{
val
result
=
JSON
.
parseObject
(
Jsoup
.
connect
(
"https://api.lolicon.app/setu/?r18=$r18"
).
ignoreContentType
(
true
).
timeout
(
Jsoup
.
connect
(
"https://api.lolicon.app/setu/?r18=$r18"
+
if
(
keyword
.
isNotBlank
())
"&keyword=$keyword&num=100"
else
""
).
ignoreContentType
(
true
).
timeout
(
10
_0000
).
get
().
body
().
text
()
)
val
url
:
String
val
pid
:
String
with
(
result
.
getJSONArray
(
"data"
).
getJSONObject
(
0
))
{
val
data
=
result
.
getJSONArray
(
"data"
)
with
(
JSONObject
(
data
.
getJSONObject
(
Random
.
nextInt
(
0
,
data
.
size
))))
{
url
=
this
.
getString
(
"url"
)
pid
=
this
.
getString
(
"pid"
)
}
...
...
mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/Gentleman.kt
View file @
22f2c508
...
...
@@ -18,22 +18,20 @@ private const val IMAGE_BUFFER_CAPACITY: Int = 5
@ExperimentalUnsignedTypes
@ExperimentalCoroutinesApi
object
Gentlemen
:
MutableMap
<
Long
,
Gentleman
>
by
mutableMapOf
()
{
fun
provide
(
key
:
Contact
):
Gentleman
=
this
.
getOrPut
(
key
.
id
)
{
Gentleman
(
key
)
}
fun
provide
(
key
:
Contact
,
keyword
:
String
=
""
):
Gentleman
=
this
.
getOrPut
(
key
.
id
)
{
Gentleman
(
key
,
keyword
)
}
}
/**
* 工作是缓存图片
*/
@ExperimentalCoroutinesApi
class
Gentleman
(
private
val
contact
:
Contact
)
:
Channel
<
GentleImage
>
by
Channel
(
IMAGE_BUFFER_CAPACITY
)
{
class
Gentleman
(
private
val
contact
:
Contact
,
private
val
keyword
:
String
)
:
Channel
<
GentleImage
>
by
Channel
(
IMAGE_BUFFER_CAPACITY
)
{
init
{
GlobalScope
.
launch
{
while
(!
isClosedForSend
)
{
send
(
GentleImage
().
apply
{
contact
=
this
@Gentleman
.
contact
image
// start downloading
send
(
GentleImage
(
contact
,
keyword
).
apply
{
seImage
// start downloading
})
}
}
...
...
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