Files
native-mdns/README.md
T

113 lines
2.4 KiB
Markdown
Raw Normal View History

2025-09-29 02:02:44 +08:00
# native-mdns
A Kotlin multiplatform library for mdns announcer(mdns server), only broadcast is supported,
2025-09-30 15:08:53 +08:00
Also, nmdns can be compiled to shared/static lib for other language calling, see [c++ example](#cpp)
2025-09-29 02:02:44 +08:00
2025-09-29 02:05:22 +08:00
Supported platforms:
1. windows64(mingwx64)
2. linuxX64
3. linuxArm64
4. macosArm64
2025-09-30 15:08:53 +08:00
5. macosX64
6. jvm(11 or later)
2025-09-29 02:05:22 +08:00
2025-09-29 06:30:39 +08:00
Supported srv type
1. A
2. PTR
> AAAA is not supported
2025-09-29 02:05:22 +08:00
AirPlay2(_airplay._tcp.local.) is tested
# How to use
Add maven repository
```kotlin
repositories {
mavenCentral()
maven("https://repo.maven.rtast.cn/releases")
}
```
Add dependency
```kotlin
kotlin {
linuxX64()
// Add other platforms
sourceSets {
commonMain.dependencies {
2025-09-30 15:10:51 +08:00
implementation("cn.rtast.nmdns:native-mdns:<version>")
2025-09-29 02:05:22 +08:00
}
}
}
2025-09-29 02:07:43 +08:00
```
There is a simple airplay2 service
```kotlin
2025-09-29 06:30:39 +08:00
val service = registerService(
serviceType = "_airplay._tcp.local.",
serviceName = "AP@RTAST",
hostname = "rtakland.local.",
ipAddress = "192.168.10.104",
port = 7001,
bindAddress = "192.168.10.104",
mdnsPort = 5356,
2025-09-29 06:35:13 +08:00
txtRecords = listOf("key1=value1", "key2=value2")
2025-09-29 06:30:39 +08:00
)
while (true) {
service.broadcast()
delay(2000L) // or use cn.rtast.nmdns.sleep1(2) sleep 2 seconds
}
```
2025-09-30 15:08:53 +08:00
# cpp
```cpp
#include <csignal>
#include <cstdio>
#include "libnmdns_api.h"
volatile sig_atomic_t keepRunning = 1;
void handle_sigint(int sig) {
std::printf("\nCaught signal %d, exiting...\n", sig);
keepRunning = 0;
}
int main() {
libnmdns_kref_kotlin_collections_List txt = create_txt_records();
add_txt_record(txt, "deviceid=ef:42:0d:76:f1:97");
add_txt_record(txt, "model=AppleTV3,2C");
add_txt_record(txt, "features=0x5A7FFFF7,0x1E");
add_txt_record(txt, "srcvers=220.68");
add_txt_record(txt, "pk=f3769a660475d27b4f6040381d784645e13e21c53e6d2da6a8c3d757086fc336");
libnmdns_kref_cn_rtast_nmdns_NMDNSAnnouncer service = register_service(
"_airplay._tcp.local.",
"AP@RTAST",
"rtakland.local.",
"192.168.10.104",
7001, 3652, "192.168.10.104",
txt
);
std::signal(SIGINT, handle_sigint);
std::signal(SIGTERM, handle_sigint);
std::signal(SIGHUP, handle_sigint);
std::signal(SIGQUIT, handle_sigint);
std::printf("Running... Press Ctrl+C to exit\n");
while (keepRunning) {
broadcast(service);
sleep1(2);
}
return 0;
}
2025-09-29 02:05:22 +08:00
```