Code Monkey home page Code Monkey logo

async-coroutines-socket's Introduction

async-coroutines-socket

is a small kotlin library that provides an implementation for java.nio.channels.AsynchronousSocketChannel wrapped in kotlin coroutines.

Kotlin Linter Socks5 tests

preamble

Using java.net.Socket connect/read/write operations within kotlin coroutines causes blocking calls that blocks either current thread or other's dispatcher thread (in case you are run it on Dispatchers.IO).

For reason to have non-blocking socket operations java.nio have AsynchronousSocketChannel class that provides the same functional but with callbacks. So, it's connect/read/write functions returns right after it's called without waiting operation to be completed. When operation is actually finished, callback (java.nio.channels.CompletionHandler interface) is called with either success or failure method.

But using callback based socket is inconvenient after all. It's commonly causes lots of lambdas, interfaces and indentation levels.

Here's our library comes in. Using a suspendCoroutine and Continuation mechanism in kotlin coroutines we're wrapped a callback based functions into coroutine-blocking function.

It's just blocking a current coroutine on connect/read/write call and resume it on callback functions -- success or failure.

usage

Some examples of usage this library is stored in src/test/kotlin package.

Using a regular Socket
    suspend fun someFunction() {
        // Socket(InetAddress, int) actually calls connect(InetSocketAddress)
        // so this call is thread-blocking
        val socket = Socket(InetAddress.getByName("localhost"), 9999)
        val buffer = ByteArray(16)
        // this call will block entire thread 
        // and all coroutines that working on it right now
        val count = socket.getInputStream().read(buffer)
        // ... 
    }
Using a coroutine socket
    suspend fun someFunction(channel: AsynchronousSocketChannel) {
        val socket = CoroutineSocket(channel)
        // this call actually uses callback to resume coroutine
        // and other coroutines will working while this one 
        // is awaiting for connect() finish
        socket.connect(InetSocketAddress("localhost", 9999))
        val byteBuffer = ByteByffer.allocate(16)
        // same as connect(), read() will not block the thread
        // just a single coroutine until read operation is complete
        val count = socket.read(byteBuffer)
        // ...
    }

async-coroutines-socket's People

Contributors

dependabot[bot] avatar theevilroot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

siyovushdev

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.