Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Roman Elizarov | 3d9fb68 | 2017-01-23 21:02:43 +0300 | [diff] [blame] | 17 | package examples |
| 18 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.* |
| 20 | import kotlinx.coroutines.experimental.nio.* |
| 21 | import java.net.* |
| 22 | import java.nio.* |
| 23 | import java.nio.channels.* |
| 24 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 3d9fb68 | 2017-01-23 21:02:43 +0300 | [diff] [blame] | 25 | |
| 26 | val PORT = 12345 |
| 27 | val CLIENT_READ_TIMEOUT = 5000L // 5 sec |
| 28 | val CLIENT_WRITE_TIMEOUT = 1000L // 1 sec |
| 29 | val BUFFER_SIZE = 1024 |
| 30 | |
| 31 | fun main(args: Array<String>) = runBlocking { |
| 32 | val serverChannel = |
| 33 | AsynchronousServerSocketChannel |
| 34 | .open() |
| 35 | .bind(InetSocketAddress(PORT)) |
| 36 | log("Listening on port $PORT") |
| 37 | // loop and accept connections forever |
| 38 | while (true) { |
| 39 | val client = serverChannel.aAccept() |
| 40 | val address = try { |
| 41 | val ia = client.remoteAddress as InetSocketAddress |
| 42 | "${ia.address.hostAddress}:${ia.port}" |
| 43 | } catch (ex: Throwable) { |
| 44 | log("Accepted client connection but failed to get its address because of $ex") |
| 45 | continue /* accept next connection */ |
| 46 | } |
| 47 | log("Accepted client connection from $address") |
| 48 | // just start a new coroutine for each client connection |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 49 | launch(coroutineContext) { |
Roman Elizarov | 3d9fb68 | 2017-01-23 21:02:43 +0300 | [diff] [blame] | 50 | try { |
| 51 | handleClient(client) |
| 52 | log("Client connection from $address has terminated normally") |
| 53 | } catch (ex: Throwable) { |
| 54 | log("Client connection from $address has terminated because of $ex") |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
Roman Elizarov | 7cf452e | 2017-01-29 21:58:33 +0300 | [diff] [blame] | 60 | suspend fun handleClient(client: AsynchronousSocketChannel) { |
Roman Elizarov | 3d9fb68 | 2017-01-23 21:02:43 +0300 | [diff] [blame] | 61 | val buffer = ByteBuffer.allocate(BUFFER_SIZE) |
| 62 | while (true) { |
| 63 | val bytes = withTimeout(CLIENT_READ_TIMEOUT) { client.aRead(buffer) } |
| 64 | if (bytes < 0) break |
| 65 | buffer.flip() |
| 66 | withTimeout(CLIENT_WRITE_TIMEOUT) { client.aWrite(buffer) } |
| 67 | buffer.clear() |
| 68 | } |
| 69 | } |
| 70 | |