|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 Functional Streams for Scala |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | + * this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to |
| 7 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 8 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 16 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 17 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 18 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package fs2 |
| 23 | +package io.net |
| 24 | + |
| 25 | +import cats.effect.LiftIO |
| 26 | +import cats.effect.Selector |
| 27 | +import cats.effect.kernel.Async |
| 28 | +import cats.effect.std.Mutex |
| 29 | +import cats.syntax.all._ |
| 30 | +import com.comcast.ip4s.IpAddress |
| 31 | +import com.comcast.ip4s.SocketAddress |
| 32 | + |
| 33 | +import java.nio.ByteBuffer |
| 34 | +import java.nio.channels.SelectionKey.OP_READ |
| 35 | +import java.nio.channels.SelectionKey.OP_WRITE |
| 36 | +import java.nio.channels.SocketChannel |
| 37 | + |
| 38 | +private final class SelectingSocket[F[_]: LiftIO] private ( |
| 39 | + selector: Selector, |
| 40 | + ch: SocketChannel, |
| 41 | + readMutex: Mutex[F], |
| 42 | + writeMutex: Mutex[F], |
| 43 | + val localAddress: F[SocketAddress[IpAddress]], |
| 44 | + val remoteAddress: F[SocketAddress[IpAddress]] |
| 45 | +)(implicit F: Async[F]) |
| 46 | + extends Socket.BufferedReads(readMutex) { |
| 47 | + |
| 48 | + protected def readChunk(buf: ByteBuffer): F[Int] = |
| 49 | + F.delay(ch.read(buf)).flatMap { readed => |
| 50 | + if (readed == 0) selector.select(ch, OP_READ).to *> readChunk(buf) |
| 51 | + else F.pure(readed) |
| 52 | + } |
| 53 | + |
| 54 | + def write(bytes: Chunk[Byte]): F[Unit] = { |
| 55 | + def go(buf: ByteBuffer): F[Unit] = |
| 56 | + F.delay { |
| 57 | + ch.write(buf) |
| 58 | + buf.remaining() |
| 59 | + }.flatMap { remaining => |
| 60 | + if (remaining > 0) { |
| 61 | + selector.select(ch, OP_WRITE).to *> go(buf) |
| 62 | + } else F.unit |
| 63 | + } |
| 64 | + writeMutex.lock.surround { |
| 65 | + F.delay(bytes.toByteBuffer).flatMap(go) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + def isOpen: F[Boolean] = F.delay(ch.isOpen) |
| 70 | + |
| 71 | + def endOfOutput: F[Unit] = |
| 72 | + F.delay { |
| 73 | + ch.shutdownOutput(); () |
| 74 | + } |
| 75 | + |
| 76 | + def endOfInput: F[Unit] = |
| 77 | + F.delay { |
| 78 | + ch.shutdownInput(); () |
| 79 | + } |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +private object SelectingSocket { |
| 84 | + def apply[F[_]: LiftIO]( |
| 85 | + selector: Selector, |
| 86 | + ch: SocketChannel, |
| 87 | + localAddress: F[SocketAddress[IpAddress]], |
| 88 | + remoteAddress: F[SocketAddress[IpAddress]] |
| 89 | + )(implicit F: Async[F]): F[Socket[F]] = |
| 90 | + (Mutex[F], Mutex[F]).flatMapN { (readMutex, writeMutex) => |
| 91 | + F.delay { |
| 92 | + new SelectingSocket[F]( |
| 93 | + selector, |
| 94 | + ch, |
| 95 | + readMutex, |
| 96 | + writeMutex, |
| 97 | + localAddress, |
| 98 | + remoteAddress |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments