threefoldtech / myceliumflut

0 stars 0 forks source link

android: handle network state changes #40

Open iwanbk opened 1 month ago

iwanbk commented 1 month ago

Mobile phone have certain behaviors that different with server or PC/laptop: connection lost (and connected again) is not uncommon, for many reasons.

While mycelium should already handle connection lost, there are more about it in mobile phone:

Android has builtin class for this : NetworkCallback. It can give notification for various network state changes events:

iwanbk commented 1 month ago

Some research has been done to other platform about how they handle network state event.

yggdrasil only handle onAvailable event

https://github.com/yggdrasil-network/yggdrasil-android/blob/aa94ccad26435de615cfbae4ae13740e12db1040/app/src/main/java/eu/neilalexander/yggdrasil/NetworkStateCallback.kt#L15-L35

If we follow the code, it will call their Go engine

https://github.com/yggdrasil-network/yggdrasil-go/blob/v0.5.5/contrib/mobile/mobile.go#L183-L186

// Retry resets the peer connection timer and tries to dial them immediately.
func (m *Yggdrasil) RetryPeersNow() {
    m.core.RetryPeersNow()
}

from the comment and quick read, looks like yggdrasil resets all the connections.

I think this way is quite simple and reliable

iwanbk commented 1 month ago

I also looked at wireguard-android and found nothing so far. But i found that wireguard-apple do have the handles as i mentioned in https://github.com/threefoldtech/myceliumflut/issues/35#issuecomment-2138686759

To summarize, wireguard-apple do these:

iwanbk commented 1 month ago

looks like yggdrasil resets all the connections.

network available when wireguard state is started: bump sockets:

  • close existing socket
  • re-open socket
  • send keepalive to peers

All of above things are done in their respective engine (yggdrasil-go and wireguard-go), so we might need to do that as well in mycelium

iwanbk commented 4 weeks ago

The NetworkCallback already added and below are my observations:

For now i will push the NetworkCallback code but only in listening mode, no specific actions taken when any events fired