@@ -39,6 +39,7 @@ import kotlinx.coroutines.sync.withLock
39
39
import kotlinx.coroutines.withContext
40
40
import kotlinx.coroutines.withTimeoutOrNull
41
41
import sefirah.clipboard.ClipboardHandler
42
+ import sefirah.common.R
42
43
import sefirah.common.notifications.NotificationCenter
43
44
import sefirah.data.repository.AppRepository
44
45
import sefirah.domain.model.ConnectionState
@@ -50,7 +51,6 @@ import sefirah.domain.model.SocketType
50
51
import sefirah.domain.repository.PlaybackRepository
51
52
import sefirah.domain.repository.PreferencesRepository
52
53
import sefirah.domain.repository.SocketFactory
53
- import sefirah.media.MediaHandler
54
54
import sefirah.network.NetworkDiscovery.NetworkAction
55
55
import sefirah.network.extensions.handleDeviceInfo
56
56
import sefirah.network.extensions.handleMessage
@@ -60,6 +60,7 @@ import sefirah.network.util.ECDHHelper
60
60
import sefirah.network.util.MessageSerializer
61
61
import sefirah.network.util.getInstalledApps
62
62
import sefirah.notification.NotificationHandler
63
+ import sefirah.projection.media.MediaHandler
63
64
import javax.inject.Inject
64
65
65
66
@AndroidEntryPoint
@@ -148,31 +149,43 @@ class NetworkService : Service() {
148
149
sendDeviceInfo(remoteInfo)
149
150
150
151
// Wait for device info
151
- withTimeoutOrNull(60000 ) { // 30 seconds timeout
152
+ val state = withTimeoutOrNull(30000 ) { // 30 seconds timeout
152
153
readChannel?.readUTF8Line()?.let { jsonMessage ->
153
- // Log.d(TAG, "Raw received data: $jsonMessage")
154
- val deviceInfo = messageSerializer.deserialize(jsonMessage) as ? DeviceInfo
155
- if (deviceInfo == null ) {
154
+ val message = messageSerializer.deserialize(jsonMessage)
155
+ if (message == null ) {
156
156
Log .e(TAG , " Invalid device info received" )
157
- return @withTimeoutOrNull null
157
+ return @withTimeoutOrNull ConnectionState .Error (getString(R .string.connection_error))
158
+ }
159
+ when (message) {
160
+ is DeviceInfo -> {
161
+ connectedDevice = remoteInfo
162
+ handleDeviceInfo(message, remoteInfo, connectedIpAddress!! )
163
+ return @withTimeoutOrNull ConnectionState .Connected
164
+ }
165
+ else -> {
166
+ Log .w(TAG , " Authentication rejected" )
167
+ return @withTimeoutOrNull ConnectionState .Error (getString(R .string.connection_error))
168
+ }
158
169
}
159
- connectedDevice = remoteInfo
160
- handleDeviceInfo(deviceInfo, remoteInfo, connectedIpAddress!! )
161
- return @withTimeoutOrNull Unit
162
170
}
171
+ null
163
172
} ? : run {
164
173
Log .e(TAG , " Timeout waiting for device info" )
165
- _connectionState .value = ConnectionState .Disconnected ( )
174
+ _connectionState .value = ConnectionState .Error (getString( R .string.connection_error) )
166
175
stop(false )
167
176
return @launch
168
177
}
169
- _connectionState .value = ConnectionState .Connected
170
- startListening()
171
- if (remoteInfo.avatar == null ) {
172
- sendInstalledApps()
178
+ _connectionState .value = state
179
+ if (state == ConnectionState .Connected ) {
180
+ startListening()
181
+ if (remoteInfo.avatar == null ) {
182
+ sendInstalledApps()
183
+ }
184
+ // Setup complete
185
+ finalizeConnection()
186
+ } else {
187
+ stop(true )
173
188
}
174
- // Setup complete
175
- finalizeConnection()
176
189
} catch (e: Exception ) {
177
190
Log .e(TAG , " Error in connecting" , e)
178
191
_connectionState .value = ConnectionState .Disconnected ()
0 commit comments