All Downloads are FREE. Search and download functionalities are using the official Maven repository.

library.DoipTcpSocket.kt Maven / Gradle / Ivy

Go to download

This is a kotlin based domain specific language (dsl), to quickly and intuitively write custom DoIP ECU simulations.

There is a newer version: 0.15.1
Show newest version
package library

import io.ktor.network.sockets.*
import io.ktor.util.network.*
import io.ktor.utils.io.*
import io.ktor.utils.io.jvm.javaio.*
import java.io.Closeable
import java.io.OutputStream
import javax.net.ssl.SSLSocket

enum class SocketType {
    TCP_DATA,
    TLS_DATA,
}

interface DoipTcpSocket : AutoCloseable, Closeable {
    val remoteAddress: SocketAddress
        get() = getSocketRemoteAddress()

    val isClosed: Boolean
        get() = isSocketClosed()

    val socketType: SocketType

    fun isSocketClosed(): Boolean
    fun getSocketRemoteAddress(): SocketAddress
    fun openReadChannel(): ByteReadChannel
    fun openOutputStream(): OutputStream
    override fun close()
}

class DelegatedKtorSocket(private val socket: Socket) : DoipTcpSocket {
    override fun isSocketClosed(): Boolean =
        socket.isClosed

    override fun getSocketRemoteAddress(): SocketAddress =
        socket.remoteAddress

    override fun openReadChannel(): ByteReadChannel =
        socket.openReadChannel()

    override fun openOutputStream(): OutputStream =
        socket.openWriteChannel(true).toOutputStream()

    override fun close() =
        socket.close()

    override val socketType: SocketType
        get() = SocketType.TCP_DATA
}

class SSLDoipTcpSocket(private val socket: SSLSocket) : DoipTcpSocket {
    private val _remoteAddress = InetSocketAddress(socket.remoteSocketAddress.hostname, socket.remoteSocketAddress.port)

    override fun isSocketClosed(): Boolean =
        socket.isClosed

    override fun getSocketRemoteAddress(): SocketAddress =
        _remoteAddress

    override fun openReadChannel(): ByteReadChannel =
        socket.inputStream.toByteReadChannel()

    override fun openOutputStream(): OutputStream =
        socket.outputStream

    override fun close() =
        socket.close()

    override val socketType: SocketType
        get() = SocketType.TLS_DATA
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy