| /* |
| * Copyright 2016-2017 JetBrains s.r.o. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package kotlinx.coroutines.experimental.nio |
| |
| import kotlinx.coroutines.experimental.CancellableContinuation |
| import kotlinx.coroutines.experimental.CancellationException |
| import kotlinx.coroutines.experimental.Job |
| import kotlinx.coroutines.experimental.suspendCancellableCoroutine |
| import java.net.SocketAddress |
| import java.nio.ByteBuffer |
| import java.nio.channels.* |
| import java.util.concurrent.TimeUnit |
| |
| /** |
| * Performs [AsynchronousFileChannel.lock] without blocking a thread and resumes when asynchronous operation completes. |
| * This suspending function is cancellable. |
| * If the [Job] of the current coroutine is completed while this suspending function is suspended, this function |
| * *closes the underlying channel* and immediately resumes with [CancellationException]. |
| */ |
| suspend fun AsynchronousFileChannel.aLock() = suspendCancellableCoroutine<FileLock> { cont -> |
| lock(cont, asyncIOHandler()) |
| closeOnCancel(cont) |
| } |
| |
| /** |
| * Performs [AsynchronousFileChannel.lock] without blocking a thread and resumes when asynchronous operation completes. |
| * This suspending function is cancellable. |
| * If the [Job] of the current coroutine is completed while this suspending function is suspended, this function |
| * *closes the underlying channel* and immediately resumes with [CancellationException]. |
| */ |
| suspend fun AsynchronousFileChannel.aLock( |
| position: Long, |
| size: Long, |
| shared: Boolean |
| ) = suspendCancellableCoroutine<FileLock> { cont -> |
| lock(position, size, shared, cont, asyncIOHandler()) |
| closeOnCancel(cont) |
| } |
| |
| /** |
| * Performs [AsynchronousFileChannel.read] without blocking a thread and resumes when asynchronous operation completes. |
| * This suspending function is cancellable. |
| * If the [Job] of the current coroutine is completed while this suspending function is suspended, this function |
| * *closes the underlying channel* and immediately resumes with [CancellationException]. |
| */ |
| suspend fun AsynchronousFileChannel.aRead( |
| buf: ByteBuffer, |
| position: Long |
| ) = suspendCancellableCoroutine<Int> { cont -> |
| read(buf, position, cont, asyncIOHandler()) |
| closeOnCancel(cont) |
| } |
| |
| /** |
| * Performs [AsynchronousFileChannel.write] without blocking a thread and resumes when asynchronous operation completes. |
| * This suspending function is cancellable. |
| * If the [Job] of the current coroutine is completed while this suspending function is suspended, this function |
| * *closes the underlying channel* and immediately resumes with [CancellationException]. |
| */ |
| suspend fun AsynchronousFileChannel.aWrite( |
| buf: ByteBuffer, |
| position: Long |
| ) = suspendCancellableCoroutine<Int> { cont -> |
| write(buf, position, cont, asyncIOHandler()) |
| closeOnCancel(cont) |
| } |
| |
| /** |
| * Performs [AsynchronousServerSocketChannel.accept] without blocking a thread and resumes when asynchronous operation completes. |
| * This suspending function is cancellable. |
| * If the [Job] of the current coroutine is completed while this suspending function is suspended, this function |
| * *closes the underlying channel* and immediately resumes with [CancellationException]. |
| */ |
| suspend fun AsynchronousServerSocketChannel.aAccept() = suspendCancellableCoroutine<AsynchronousSocketChannel> { cont -> |
| accept(cont, asyncIOHandler()) |
| closeOnCancel(cont) |
| } |
| |
| /** |
| * Performs [AsynchronousServerSocketChannel.connect] without blocking a thread and resumes when asynchronous operation completes. |
| * This suspending function is cancellable. |
| * If the [Job] of the current coroutine is completed while this suspending function is suspended, this function |
| * *closes the underlying channel* and immediately resumes with [CancellationException]. |
| */ |
| suspend fun AsynchronousSocketChannel.aConnect( |
| socketAddress: SocketAddress |
| ) = suspendCancellableCoroutine<Unit> { cont -> |
| connect(socketAddress, cont, AsyncVoidIOHandler) |
| closeOnCancel(cont) |
| } |
| |
| /** |
| * Performs [AsynchronousServerSocketChannel.read] without blocking a thread and resumes when asynchronous operation completes. |
| * This suspending function is cancellable. |
| * If the [Job] of the current coroutine is completed while this suspending function is suspended, this function |
| * *closes the underlying channel* and immediately resumes with [CancellationException]. |
| */ |
| suspend fun AsynchronousSocketChannel.aRead( |
| buf: ByteBuffer, |
| timeout: Long = 0L, |
| timeUnit: TimeUnit = TimeUnit.MILLISECONDS |
| ) = suspendCancellableCoroutine<Int> { cont -> |
| read(buf, timeout, timeUnit, cont, asyncIOHandler()) |
| closeOnCancel(cont) |
| } |
| |
| /** |
| * Performs [AsynchronousServerSocketChannel.write] without blocking a thread and resumes when asynchronous operation completes. |
| * This suspending function is cancellable. |
| * If the [Job] of the current coroutine is completed while this suspending function is suspended, this function |
| * *closes the underlying channel* and immediately resumes with [CancellationException]. |
| */ |
| suspend fun AsynchronousSocketChannel.aWrite( |
| buf: ByteBuffer, |
| timeout: Long = 0L, |
| timeUnit: TimeUnit = TimeUnit.MILLISECONDS |
| ) = suspendCancellableCoroutine<Int> { cont -> |
| write(buf, timeout, timeUnit, cont, asyncIOHandler()) |
| closeOnCancel(cont) |
| } |
| |
| // ---------------- private details ---------------- |
| |
| private fun Channel.closeOnCancel(cont: CancellableContinuation<*>) { |
| cont.onCompletion { |
| if (cont.isCancelled) |
| try { |
| close() |
| } catch (ex: Throwable) { |
| // Specification says that it is Ok to call it any time, but reality is different, |
| // so we have just to ignore exception |
| } |
| } |
| } |
| |
| @Suppress("UNCHECKED_CAST") |
| private fun <T> asyncIOHandler(): CompletionHandler<T, CancellableContinuation<T>> = |
| AsyncIOHandlerAny as CompletionHandler<T, CancellableContinuation<T>> |
| |
| private object AsyncIOHandlerAny : CompletionHandler<Any, CancellableContinuation<Any>> { |
| override fun completed(result: Any, cont: CancellableContinuation<Any>) { |
| cont.resume(result) |
| } |
| |
| override fun failed(ex: Throwable, cont: CancellableContinuation<Any>) { |
| // just return if already cancelled and got an expected exception for that case |
| if (ex is AsynchronousCloseException && cont.isCancelled) return |
| cont.resumeWithException(ex) |
| } |
| } |
| |
| private object AsyncVoidIOHandler : CompletionHandler<Void?, CancellableContinuation<Unit>> { |
| override fun completed(result: Void?, cont: CancellableContinuation<Unit>) { |
| cont.resume(Unit) |
| } |
| |
| override fun failed(ex: Throwable, cont: CancellableContinuation<Unit>) { |
| // just return if already cancelled and got an expected exception for that case |
| if (ex is AsynchronousCloseException && cont.isCancelled) return |
| cont.resumeWithException(ex) |
| } |
| } |
| |
| |