Skip to content

Commit

Permalink
fix: connection issues on Flatpak and Snap installations
Browse files Browse the repository at this point in the history
fix: connection issues on Flatpak and Snap installations
  • Loading branch information
vyfor committed May 6, 2024
2 parents 6a0c6d3 + bfb5df3 commit 202bce0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 54 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Offers DSL support
- Provides both JVM and Native implementations
- Validates the activity fields before sending them
- Supports Flatpak and Snap installations

## 🔌 Requirements
- **Java**: `16 or later` (only for use within the JVM environment)
Expand All @@ -16,7 +17,7 @@

```gradle
dependencies {
implementation("io.github.vyfor:kpresence:0.6.0")
implementation("io.github.vyfor:kpresence:0.6.1")
}
```

Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
}

group = "io.github.vyfor"
version = "0.6.0"
version = "0.6.1"

repositories {
mavenCentral()
Expand Down Expand Up @@ -44,7 +44,7 @@ kotlin {
dependencies {
implementation(kotlin("stdlib"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
}
}
val commonTest by getting {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PipeNotFoundException : Exception()
*
* @param exception The underlying exception that caused the connection issue.
*/
class ConnectionException(val exception: Exception) : Exception()
class ConnectionException(val exception: Exception) : Exception(exception)

/**
* Exception thrown when a connection is unexpectedly closed.
Expand Down
39 changes: 25 additions & 14 deletions src/jvmMain/kotlin/io/github/vyfor/kpresence/ipc/Connection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.github.vyfor.kpresence.exception.*
import io.github.vyfor.kpresence.utils.putInt
import io.github.vyfor.kpresence.utils.reverseBytes
import java.lang.System.getenv
import java.net.SocketException
import java.net.UnixDomainSocketAddress
import java.nio.ByteBuffer
import java.nio.channels.AsynchronousFileChannel
Expand Down Expand Up @@ -118,21 +119,31 @@ actual class Connection {
private var pipe: SocketChannel? = null

override fun open() {
val dir =
(getenv("XDG_RUNTIME_DIR") ?:
getenv("TMPDIR") ?:
getenv("TMP") ?:
getenv("TEMP")) ?:
"/tmp"
val dirs =
listOf("XDG_RUNTIME_DIR", "TMPDIR", "TMP", "TEMP")
.mapNotNull(::getenv)
.plus("/tmp")
.flatMap { base ->
listOf(
base,
"$base/app/com.discordapp.Discord",
"$base/snap.discord"
)
}

for (i in 0..9) {
try {
pipe = SocketChannel.open(UnixDomainSocketAddress.of("$dir/discord-ipc-$i"))
return
} catch (_: InvalidPathException) {
} catch (_: IllegalArgumentException) {
} catch (e: Exception) {
throw ConnectionException(e)
dirs.forEach { dir ->
for (i in 0..9) {
try {
pipe = SocketChannel.open(UnixDomainSocketAddress.of("$dir/discord-ipc-$i"))
return
} catch (_: InvalidPathException) {
} catch (_: IllegalArgumentException) {
} catch (e: SocketException) {
if (e.message == "No such file or directory") continue
throw ConnectionException(e)
} catch (e: Exception) {
throw ConnectionException(e)
}
}
}

Expand Down
42 changes: 24 additions & 18 deletions src/linuxMain/kotlin/io/github/vyfor/kpresence/ipc/Connection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ actual class Connection {
private var pipe = -1

actual fun open() {
val dir =
(getenv("XDG_RUNTIME_DIR") ?:
getenv("TMPDIR") ?:
getenv("TMP") ?:
getenv("TEMP"))?.toKString() ?:
"/tmp"
val dirs =
listOf("XDG_RUNTIME_DIR", "TMPDIR", "TMP", "TEMP")
.mapNotNull { getenv(it)?.toKString() }
.plus("/tmp")
.flatMap { base ->
listOf(
base,
"$base/app/com.discordapp.Discord",
"$base/snap.discord"
)
}

val socket = socket(AF_UNIX, SOCK_STREAM, 0)
if (socket == -1) throw ConnectionException(Exception(strerror(errno)?.toKString().orEmpty()))
Expand All @@ -27,18 +32,19 @@ actual class Connection {
if (fcntl(socket, F_SETFL, flags or O_NONBLOCK) == -1) throw ConnectionException(Exception(strerror(errno)?.toKString().orEmpty()))

memScoped {
for (i in 0..9) {
val pipeAddr = alloc<sockaddr_un>().apply {
sun_family = AF_UNIX.convert()
snprintf(sun_path, PATH_MAX.toULong(), "$dir/discord-ipc-$i")
}

val err = connect(socket, pipeAddr.ptr.reinterpret(), sizeOf<sockaddr_un>().convert())
if (err == 0) {
pipe = socket
return
} else if (errno != ENOENT) {
throw ConnectionException(Exception(strerror(errno)?.toKString().orEmpty()))
dirs.forEach { dir ->
for (i in 0..9) {
val pipeAddr = alloc<sockaddr_un>().apply {
sun_family = AF_UNIX.convert()
snprintf(sun_path, PATH_MAX.toULong(), "$dir/discord-ipc-$i")
}
val err = connect(socket, pipeAddr.ptr.reinterpret(), sizeOf<sockaddr_un>().convert())
if (err == 0) {
pipe = socket
return
} else if (errno != ENOENT) {
throw ConnectionException(Exception(strerror(errno)?.toKString().orEmpty()))
}
}
}
}
Expand Down
42 changes: 24 additions & 18 deletions src/macosMain/kotlin/io/github/vyfor/kpresence/ipc/Connection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ actual class Connection {
private var pipe = -1

actual fun open() {
val dir =
(getenv("XDG_RUNTIME_DIR") ?:
getenv("TMPDIR") ?:
getenv("TMP") ?:
getenv("TEMP"))?.toKString() ?:
"/tmp"
val dirs =
listOf("XDG_RUNTIME_DIR", "TMPDIR", "TMP", "TEMP")
.mapNotNull { getenv(it)?.toKString() }
.plus("/tmp")
.flatMap { base ->
listOf(
base,
"$base/app/com.discordapp.Discord",
"$base/snap.discord"
)
}

val socket = socket(AF_UNIX, SOCK_STREAM, 0)
if (socket == -1) throw ConnectionException(Exception(strerror(errno)?.toKString().orEmpty()))
Expand All @@ -27,18 +32,19 @@ actual class Connection {
if (fcntl(socket, F_SETFL, flags or O_NONBLOCK) == -1) throw ConnectionException(Exception(strerror(errno)?.toKString().orEmpty()))

memScoped {
for (i in 0..9) {
val pipeAddr = alloc<sockaddr_un>().apply {
sun_family = AF_UNIX.convert()
snprintf(sun_path, PATH_MAX.toULong(), "$dir/discord-ipc-$i")
}

val err = connect(socket, pipeAddr.ptr.reinterpret(), sizeOf<sockaddr_un>().convert())
if (err == 0) {
pipe = socket
return
} else if (errno != ENOENT) {
throw ConnectionException(Exception(strerror(errno)?.toKString().orEmpty()))
dirs.forEach { dir ->
for (i in 0..9) {
val pipeAddr = alloc<sockaddr_un>().apply {
sun_family = AF_UNIX.convert()
snprintf(sun_path, PATH_MAX.toULong(), "$dir/discord-ipc-$i")
}
val err = connect(socket, pipeAddr.ptr.reinterpret(), sizeOf<sockaddr_un>().convert())
if (err == 0) {
pipe = socket
return
} else if (errno != ENOENT) {
throw ConnectionException(Exception(strerror(errno)?.toKString().orEmpty()))
}
}
}
}
Expand Down

0 comments on commit 202bce0

Please sign in to comment.