@@ -45,7 +45,6 @@ class ReporterServer : Service() {
45
45
serverSocket = LocalServerSocket (ABSTRACT_SOCKET_NAME )
46
46
Log .i(" ReporterServer" , " Server started on abstract address: localabstract:$ABSTRACT_SOCKET_NAME " )
47
47
48
- // Accept client connection (this is a blocking call)
49
48
clientSocket = serverSocket?.accept()
50
49
Log .i(" ReporterServer" , " Client connected" )
51
50
outputStream = clientSocket?.outputStream
@@ -83,9 +82,6 @@ class ReporterServer : Service() {
83
82
if (outputStream == null || clientSocket == null || ! clientSocket!! .isConnected) {
84
83
Log .e(" ReporterServer" , " Client is not connected or socket is closed." )
85
84
// If sending fails, assume connection is lost and stop/retry server
86
- if (isRunning.get()) {
87
- stopServer(true )
88
- }
89
85
return false
90
86
}
91
87
@@ -95,16 +91,10 @@ class ReporterServer : Service() {
95
91
} catch (e: IOException ) {
96
92
// Catch IOException for write/flush errors, indicating connection issue
97
93
Log .e(" ReporterServer" , " Server sending data error: ${e.message} " )
98
- if (isRunning.get()) {
99
- stopServer(true ) // Retry connection on send error
100
- }
101
94
return false
102
95
} catch (e: Exception ) {
103
96
// Catch other potential exceptions
104
97
Log .e(" ReporterServer" , " Unexpected sending data error: ${e.message} " )
105
- if (isRunning.get()) {
106
- stopServer(true ) // Retry connection on send error
107
- }
108
98
return false
109
99
}
110
100
return true
@@ -124,11 +114,25 @@ class ReporterServer : Service() {
124
114
break
125
115
}
126
116
}
117
+ stopServer(true )
127
118
Log .d(" ReporterServer" , " Heartbeat loop finished." )
128
119
}
129
120
}
130
121
131
122
private fun stopServer (retry : Boolean ) {
123
+ fun stopExecutor () {
124
+ executor.shutdownNow()
125
+ try {
126
+ // Wait a bit for tasks to terminate
127
+ if (! executor.awaitTermination(1 , TimeUnit .SECONDS )) {
128
+ Log .w(" ReporterServer" , " Executor did not terminate in time." )
129
+ }
130
+ } catch (e: InterruptedException ) {
131
+ Log .e(" ReporterServer" , " Interrupted while waiting for executor termination." )
132
+ Thread .currentThread().interrupt() // Restore interrupt flag
133
+ }
134
+ }
135
+
132
136
// Use compareAndSet to ensure only one thread stops the server
133
137
if (! isRunning.compareAndSet(true , false )) {
134
138
Log .w(" ReporterServer" , " Server is already stopping or stopped." )
@@ -138,6 +142,7 @@ class ReporterServer : Service() {
138
142
Log .i(" ReporterServer" , " Attempting to stop server..." )
139
143
try {
140
144
// Closing LocalSocket will likely cause read/write operations on it to throw IOException
145
+ outputStream?.close()
141
146
clientSocket?.close()
142
147
serverSocket?.close()
143
148
Log .i(" ReporterServer" , " Server socket resources closed." )
@@ -151,27 +156,18 @@ class ReporterServer : Service() {
151
156
outputStream = null
152
157
}
153
158
154
- // Shutdown executor gracefully
155
- executor.shutdownNow()
156
- try {
157
- // Wait a bit for tasks to terminate
158
- if (! executor.awaitTermination(1 , TimeUnit .SECONDS )) {
159
- Log .w(" ReporterServer" , " Executor did not terminate in time." )
160
- }
161
- } catch (e: InterruptedException ) {
162
- Log .e(" ReporterServer" , " Interrupted while waiting for executor termination." )
163
- Thread .currentThread().interrupt() // Restore interrupt flag
159
+ if (! retry) {
160
+ stopExecutor()
161
+ Log .i(" ReporterServer" , " Server fully stopped (no retry)." )
162
+ return
164
163
}
165
164
166
- if (! retry) Log .i(" ReporterServer" , " Server fully stopped (no retry)." )
167
- // when need to reconnect, show the disconnected message
168
165
uiHandler.post { Toast .makeText(this , I18n .choose(listOf (
169
166
" PC client disconnected." ,
170
167
" 电脑端已断开连接。" ,
171
168
)), Toast .LENGTH_SHORT ).show() }
172
-
173
169
uiHandler.postDelayed({
174
- Log .i(" ReporterServer" , " Attempting to restart server after delay ..." )
170
+ Log .i(" ReporterServer" , " Attempting to restart server..." )
175
171
startServer()
176
172
}, RETRY_INTERVAL_MS )
177
173
}
@@ -193,7 +189,8 @@ class ReporterServer : Service() {
193
189
inner class LocalBinder : Binder () {
194
190
fun sendEvent (event : Int ) {
195
191
executor.execute {
196
- this @ReporterServer.sendEvent(event)
192
+ val ret = this @ReporterServer.sendEvent(event)
193
+ if (! ret) stopServer(true )
197
194
}
198
195
}
199
196
}
0 commit comments