Move everything under kotlin-native folder
I was forced to manually do update the following files, because otherwise they would be ignored according .gitignore settings. Probably they should be deleted from repo. Interop/.idea/compiler.xml Interop/.idea/gradle.xml Interop/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_runtime_1_0_3.xml Interop/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_0_3.xml Interop/.idea/modules.xml Interop/.idea/modules/Indexer/Indexer.iml Interop/.idea/modules/Runtime/Runtime.iml Interop/.idea/modules/StubGenerator/StubGenerator.iml backend.native/backend.native.iml backend.native/bc.frontend/bc.frontend.iml backend.native/cli.bc/cli.bc.iml backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt backend.native/tests/link/lib/foo.kt backend.native/tests/link/lib/foo2.kt backend.native/tests/teamcity-test.property
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
# Sockets demo
|
||||
|
||||
To build use `../gradlew assemble`.
|
||||
|
||||
To run use `../gradlew runReleaseExecutableEchoServer` or execute the program directly:
|
||||
|
||||
./build/bin/echoServer/main/release/executable/echoServer.kexe 3000 &
|
||||
|
||||
Test the server by connecting to it, for example with telnet:
|
||||
|
||||
telnet localhost 3000
|
||||
|
||||
Write something to console and watch server echoing it back.
|
||||
|
||||
~~Quit telnet by pressing ctrl+] ctrl+D~~
|
||||
@@ -0,0 +1,52 @@
|
||||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetPreset
|
||||
|
||||
plugins {
|
||||
kotlin("multiplatform")
|
||||
}
|
||||
|
||||
// Add two additional presets for Raspberry Pi and Linux/ARM64.
|
||||
val raspberryPiPresets: List<KotlinNativeTargetPreset> = listOf("linuxArm32Hfp", "linuxArm64").map {
|
||||
kotlin.presets[it] as KotlinNativeTargetPreset
|
||||
}
|
||||
|
||||
kotlin {
|
||||
// Determine host preset.
|
||||
val hostOs = System.getProperty("os.name")
|
||||
|
||||
// Create a target for the host platform.
|
||||
val hostTarget = when {
|
||||
hostOs == "Mac OS X" -> macosX64("echoServer")
|
||||
hostOs == "Linux" -> linuxX64("echoServer")
|
||||
hostOs.startsWith("Windows") -> mingwX64("echoServer")
|
||||
else -> throw GradleException("Host OS '$hostOs' is not supported in Kotlin/Native $project.")
|
||||
}
|
||||
|
||||
// Create cross-targets.
|
||||
val raspberryPiTargets = raspberryPiPresets.map { preset ->
|
||||
val targetName = "echoServer${preset.name.capitalize()}"
|
||||
targetFromPreset(preset, targetName) {}
|
||||
}
|
||||
|
||||
// Configure executables for all targets.
|
||||
configure(raspberryPiTargets + listOf(hostTarget)) {
|
||||
binaries {
|
||||
executable {
|
||||
entryPoint = "sample.echoserver.main"
|
||||
runTask?.args(3000)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
val echoServerMain by getting
|
||||
raspberryPiPresets.forEach { preset ->
|
||||
val mainSourceSetName = "echoServer${preset.name.capitalize()}Main"
|
||||
getByName(mainSourceSetName).dependsOn(echoServerMain)
|
||||
}
|
||||
}
|
||||
|
||||
// Enable experimental stdlib API used by the sample.
|
||||
sourceSets.all {
|
||||
languageSettings.useExperimentalAnnotation("kotlin.ExperimentalStdlibApi")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
kotlin.code.style=official
|
||||
kotlin.import.noCommonSourceSets=true
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package sample.echoserver
|
||||
|
||||
import kotlinx.cinterop.*
|
||||
import platform.posix.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
if (args.isEmpty()) {
|
||||
println("Usage: echoserver.kexe <port>")
|
||||
return
|
||||
}
|
||||
|
||||
val port = args[0].toShort()
|
||||
|
||||
// Initialize sockets in platform-dependent way.
|
||||
init_sockets()
|
||||
|
||||
memScoped {
|
||||
|
||||
val buffer = ByteArray(1024)
|
||||
val prefixBuffer = "echo: ".encodeToByteArray()
|
||||
val serverAddr = alloc<sockaddr_in>()
|
||||
|
||||
val listenFd = socket(AF_INET, SOCK_STREAM, 0)
|
||||
.ensureUnixCallResult("socket") { !it.isMinusOne() }
|
||||
|
||||
with(serverAddr) {
|
||||
memset(this.ptr, 0, sockaddr_in.size.convert())
|
||||
sin_family = AF_INET.convert()
|
||||
sin_port = posix_htons(port).convert()
|
||||
}
|
||||
|
||||
bind(listenFd, serverAddr.ptr.reinterpret(), sockaddr_in.size.convert())
|
||||
.ensureUnixCallResult("bind") { it == 0 }
|
||||
|
||||
listen(listenFd, 10)
|
||||
.ensureUnixCallResult("listen") { it == 0 }
|
||||
|
||||
val commFd = accept(listenFd, null, null)
|
||||
.ensureUnixCallResult("accept") { !it.isMinusOne() }
|
||||
|
||||
buffer.usePinned { pinned ->
|
||||
while (true) {
|
||||
val length = recv(commFd, pinned.addressOf(0), buffer.size.convert(), 0).toInt()
|
||||
.ensureUnixCallResult("read") { it >= 0 }
|
||||
|
||||
if (length == 0) {
|
||||
break
|
||||
}
|
||||
|
||||
send(commFd, prefixBuffer.refTo(0), prefixBuffer.size.convert(), 0)
|
||||
.ensureUnixCallResult("write") { it >= 0 }
|
||||
send(commFd, pinned.addressOf(0), length.convert(), 0)
|
||||
.ensureUnixCallResult("write") { it >= 0 }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Int.ensureUnixCallResult(op: String, predicate: (Int) -> Boolean): Int {
|
||||
if (!predicate(this)) {
|
||||
throw Error("$op: ${strerror(posix_errno())!!.toKString()}")
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
inline fun Long.ensureUnixCallResult(op: String, predicate: (Long) -> Boolean): Long {
|
||||
if (!predicate(this)) {
|
||||
throw Error("$op: ${strerror(posix_errno())!!.toKString()}")
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
inline fun ULong.ensureUnixCallResult(op: String, predicate: (ULong) -> Boolean): ULong {
|
||||
if (!predicate(this)) {
|
||||
throw Error("$op: ${strerror(posix_errno())!!.toKString()}")
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
private fun Int.isMinusOne() = (this == -1)
|
||||
private fun Long.isMinusOne() = (this == -1L)
|
||||
private fun ULong.isMinusOne() = (this == ULong.MAX_VALUE)
|
||||
Reference in New Issue
Block a user