Skip to content

Commit 10445dc

Browse files
author
anonymous
committed
[opt] Add some comments.
1 parent 62011cf commit 10445dc

File tree

8 files changed

+133
-4
lines changed

8 files changed

+133
-4
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ kotlin.code.style=official
2323
# disable for caching to work
2424
kapt.include.compile.classpath=false
2525

26-
ANDROID_COMPILE_SDK=35
26+
ANDROID_COMPILE_SDK=36
2727
ANDROID_MIN_SDK=26
28-
ANDROID_TARGET_SDK=35
28+
ANDROID_TARGET_SDK=36
2929

3030
VERSION_NAME=2.6.0
3131
VERSION_CODE=24080701

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ glide = "4.16.0"
3131
tuiutils = "1.1.1"
3232
tapm = "1.0.3"
3333
tlog = "1.0.1"
34+
tlrucache = "1.0.1"
3435

3536
[libraries]
3637
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -80,6 +81,7 @@ tapm-autoinit = { group = "io.github.tans5", name= "tapm-autoinit", version.ref
8081
tapm-log = { group = "io.github.tans5", name= "tapm-log", version.ref = "tapm" }
8182
tapm-breakpad = { group = "io.github.tans5", name= "tapm-breakpad", version.ref = "tapm" }
8283
tlog = { group = "io.github.tans5", name = "tlog", version.ref = "tlog" }
84+
tlrucache = { group = "io.github.tans5", name = "tlrucache", version.ref = "tlrucache" }
8385

8486
[plugins]
8587
androidApplication = { id = "com.android.application", version.ref = "agp" }

net/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ kotlin {
1313
}
1414

1515
dependencies {
16-
api(libs.coroutines.core)
16+
api(libs.coroutines.core)
1717

1818
// Moshi
1919
api(libs.moshi)
@@ -24,4 +24,5 @@ dependencies {
2424
api(libs.netty)
2525
api(libs.okio)
2626
api(libs.androidx.annotaion)
27+
api(libs.tlrucache)
2728
}

net/src/main/java/com/tans/tfiletransporter/transferproto/fileexplore/FileExplore.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class FileExplore(
7979
}
8080
private val heartbeatTaskFuture: AtomicReference<ScheduledFuture<*>?> = AtomicReference(null)
8181

82+
// 心跳服务,只有服务端注册
8283
private val heartbeatServer: IServer<Unit, Unit> by lazy {
8384
simplifyServer(
8485
requestType = FileExploreDataType.HeartbeatReq.type,
@@ -88,13 +89,15 @@ class FileExplore(
8889
)
8990
}
9091

92+
// 握手服务,只有服务端注册
9193
private val handshakeServer: IServer<HandshakeReq, HandshakeResp> by lazy {
9294
simplifyServer(
9395
requestType = FileExploreDataType.HandshakeReq.type,
9496
responseType = FileExploreDataType.HandshakeResp.type,
9597
log = log,
9698
onRequest = { _, _, r, isNew ->
9799
// Server receive client handshake.
100+
// 客户端请求握手,检查版本
98101
if (r.version == TransferProtoConstant.VERSION) {
99102
val currentState = getCurrentState()
100103
if (isNew && currentState is FileExploreState.Connected) {
@@ -108,6 +111,7 @@ class FileExplore(
108111
)
109112
}
110113

114+
// 请求浏览文件夹服务,服务端/客户端都注册.
111115
private val scanDirServer: IServer<ScanDirReq, ScanDirResp> by lazy {
112116
simplifyServer(
113117
requestType = FileExploreDataType.ScanDirReq.type,
@@ -120,6 +124,7 @@ class FileExplore(
120124
)
121125
}
122126

127+
// 请求发送文件服务,服务端/客户端都注册.
123128
private val sendFilesServer: IServer<SendFilesReq, SendFilesResp> by lazy {
124129
simplifyServer(
125130
requestType = FileExploreDataType.SendFilesReq.type,
@@ -132,6 +137,7 @@ class FileExplore(
132137
)
133138
}
134139

140+
// 请求下载文件服务,服务端/客户端都注册.
135141
private val downloadFilesServer: IServer<DownloadFilesReq, DownloadFilesResp> by lazy {
136142
simplifyServer(
137143
requestType = FileExploreDataType.DownloadFilesReq.type,
@@ -144,6 +150,7 @@ class FileExplore(
144150
)
145151
}
146152

153+
// 消息服务,服务端/客户端都注册.
147154
private val sendMsgServer: IServer<SendMsgReq, Unit> by lazy {
148155
simplifyServer(
149156
requestType = FileExploreDataType.SendMsgReq.type,
@@ -165,6 +172,7 @@ class FileExplore(
165172

166173
/**
167174
* Server create connection.
175+
* 服务端
168176
*/
169177
fun bind(address: InetAddress, simpleCallback: SimpleCallback<Unit>) {
170178
if (getCurrentState() !is FileExploreState.NoConnection) {
@@ -184,6 +192,7 @@ class FileExplore(
184192
idleLimitDuration = heartbeatInterval * 3,
185193
newClientTaskCallback = { task ->
186194
// Client coming, only one client would be accepted.
195+
// 客户端请求链接
187196
if (hasChildConnection.compareAndSet(false, true)) {
188197
/**
189198
* Step2: handle client connection.
@@ -201,6 +210,7 @@ class FileExplore(
201210
nettyState is NettyTaskState.ConnectionClosed ||
202211
getCurrentState() !is FileExploreState.Requesting) {
203212
// Client connection fail.
213+
// 客户端链接失败
204214
val errorMsg = "Connect error: $nettyState, ${getCurrentState()}"
205215
log.e(TAG, errorMsg)
206216
if (hasInvokeCallback.compareAndSet(false, true)) {
@@ -212,16 +222,24 @@ class FileExplore(
212222
serverTask.set(null)
213223
newState(FileExploreState.NoConnection)
214224
} else {
225+
// 客户端链接成功
215226
// Client connection success.
216227
if (nettyState is NettyTaskState.ConnectionActive) {
217228
newState(FileExploreState.Connected)
218229
log.d(TAG, "Connect success.")
230+
// 链接断开监听
219231
exploreTask.addObserver(closeObserver)
232+
// 注册握手服务
220233
exploreTask.registerServer(handshakeServer)
234+
// 注册心跳服务
221235
exploreTask.registerServer(heartbeatServer)
236+
// 注册文件夹浏览服务
222237
exploreTask.registerServer(scanDirServer)
238+
// 注册请求发送文件服务
223239
exploreTask.registerServer(sendFilesServer)
240+
// 注册请求下载文件服务
224241
exploreTask.registerServer(downloadFilesServer)
242+
// 注册消息服务
225243
exploreTask.registerServer(sendMsgServer)
226244
exploreTask.removeObserver(this)
227245
if (hasInvokeCallback.compareAndSet(false, true)) {
@@ -250,6 +268,7 @@ class FileExplore(
250268
if (nettyState is NettyTaskState.Error ||
251269
nettyState is NettyTaskState.ConnectionClosed ||
252270
getCurrentState() !is FileExploreState.Requesting) {
271+
// 服务启动失败
253272
// Server task connection create fail.
254273
if (hasInvokeCallback.compareAndSet(false, true)) {
255274
simpleCallback.onError("Server bind error: $nettyState")
@@ -258,6 +277,7 @@ class FileExplore(
258277
serverTask.stopTask()
259278
log.e(TAG, "Bind server error: $nettyState")
260279
} else if (nettyState is NettyTaskState.ConnectionActive) {
280+
// 服务启动成功
261281
// Server task connection create success.
262282
serverTask.addObserver(closeObserver)
263283
serverTask.removeObserver(this)
@@ -275,13 +295,15 @@ class FileExplore(
275295
/**
276296
* Step1: Start server task.
277297
*/
298+
// 启动服务
278299
serverTask.startTask()
279300
this.serverTask.get()?.stopTask()
280301
this.serverTask.set(serverTask)
281302
}
282303

283304
/**
284305
* Client create connection.
306+
* 客户端
285307
*/
286308
fun connect(
287309
serverAddress: InetAddress,
@@ -307,6 +329,7 @@ class FileExplore(
307329
if (nettyState is NettyTaskState.Error ||
308330
nettyState is NettyTaskState.ConnectionClosed ||
309331
getCurrentState() !is FileExploreState.Requesting) {
332+
// 链接服务器失败
310333
// Create client connection fail.
311334
val errorMsg = "Connect error: $nettyState, ${getCurrentState()}"
312335
log.e(TAG, errorMsg)
@@ -319,20 +342,26 @@ class FileExplore(
319342
} else {
320343
// Create client connection success.
321344
if (nettyState is NettyTaskState.ConnectionActive) {
345+
// 链接服务器成功
322346
newState(FileExploreState.Connected)
323347
log.d(TAG, "Connect success.")
324348
exploreTask.addObserver(closeObserver)
325349
this@FileExplore.exploreTask.get()?.stopTask()
326350
this@FileExplore.exploreTask.set(exploreTask)
351+
// 注册请求浏览文件夹服务
327352
exploreTask.registerServer(scanDirServer)
353+
// 注册请求发送文件服务
328354
exploreTask.registerServer(sendFilesServer)
355+
// 注册请求下载文件服务
329356
exploreTask.registerServer(downloadFilesServer)
357+
// 注册发送消息服务
330358
exploreTask.registerServer(sendMsgServer)
331359
exploreTask.removeObserver(this)
332360
if (hasInvokeCallback.compareAndSet(false, true)) {
333361
simpleCallback.onSuccess(Unit)
334362
}
335363
// Start heartbeat task, send a heartbeat each 8000 milliseconds default.
364+
// 定时发送心跳任务,客户端发送,服务端接收
336365
val future = taskScheduleExecutor.scheduleWithFixedDelay(
337366
{
338367
sendHeartbeat()
@@ -356,6 +385,7 @@ class FileExplore(
356385
/**
357386
* Step1: Start client connection task.
358387
*/
388+
// 开启任务,请求链接服务器
359389
exploreTask.startTask()
360390
}
361391

@@ -372,6 +402,7 @@ class FileExplore(
372402

373403
/**
374404
* Client request handshake to server.
405+
* 客户端向服务端发送,请求握手
375406
*/
376407
fun requestHandshake(simpleCallback: SimpleCallback<Handshake>) {
377408
assertState(false, simpleCallback) { task, _ ->
@@ -535,6 +566,7 @@ class FileExplore(
535566

536567
/**
537568
* Client send heartbeat to server.
569+
* 发送客户端发送心跳到服务端
538570
*/
539571
private fun sendHeartbeat() {
540572
assertState<Unit>(false, null) { task, _ ->

0 commit comments

Comments
 (0)