-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(network): commonize raw socket interface #3221
Draft
ArcticLampyrid
wants to merge
3
commits into
ktorio:main
Choose a base branch
from
ArcticLampyrid:main
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ kotlin { | |
} | ||
|
||
sourceSets { | ||
jvmAndNixMain { | ||
commonMain { | ||
dependencies { | ||
api(project(":ktor-utils")) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
ktor-network/common/src/io/ktor/network/selector/SelectorManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.ktor.network.selector | ||
|
||
import io.ktor.utils.io.core.* | ||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.* | ||
|
||
/** | ||
* Creates the selector manager for current platform. | ||
*/ | ||
@Suppress("FunctionName") | ||
public expect fun SelectorManager( | ||
dispatcher: CoroutineContext = EmptyCoroutineContext | ||
): SelectorManager | ||
|
||
public expect interface SelectorManager : CoroutineScope, Closeable { | ||
|
||
} |
File renamed without changes.
32 changes: 32 additions & 0 deletions
32
ktor-network/common/src/io/ktor/network/sockets/ConnectUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.sockets | ||
|
||
import io.ktor.network.selector.* | ||
|
||
internal expect suspend fun connect( | ||
selector: SelectorManager, | ||
remoteAddress: SocketAddress, | ||
socketOptions: SocketOptions.TCPClientSocketOptions | ||
): Socket | ||
|
||
internal expect fun bind( | ||
selector: SelectorManager, | ||
localAddress: SocketAddress?, | ||
socketOptions: SocketOptions.AcceptorOptions | ||
): ServerSocket | ||
|
||
internal expect fun UDPSocketBuilder.Companion.connectUDP( | ||
selector: SelectorManager, | ||
remoteAddress: SocketAddress, | ||
localAddress: SocketAddress?, | ||
options: SocketOptions.UDPSocketOptions | ||
): ConnectedDatagramSocket | ||
|
||
internal expect fun UDPSocketBuilder.Companion.bindUDP( | ||
selector: SelectorManager, | ||
localAddress: SocketAddress?, | ||
options: SocketOptions.UDPSocketOptions | ||
): BoundDatagramSocket |
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...ix/src/io/ktor/network/sockets/Sockets.kt → ...on/src/io/ktor/network/sockets/Sockets.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
ktor-network/js/src/io/ktor/network/selector/SelectorManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.selector | ||
|
||
import io.ktor.utils.io.core.* | ||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.* | ||
|
||
/** | ||
* Creates the selector manager for current platform. | ||
*/ | ||
@Suppress("FunctionName") | ||
public actual fun SelectorManager(dispatcher: kotlin.coroutines.CoroutineContext): SelectorManager { | ||
TODO("SelectorManager is not yet implemented on Javascript platform") | ||
} | ||
|
||
public actual interface SelectorManager : CoroutineScope, Closeable { | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
ktor-network/js/src/io/ktor/network/sockets/ConnectUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.sockets | ||
|
||
import io.ktor.network.selector.* | ||
|
||
internal actual suspend fun connect( | ||
selector: SelectorManager, | ||
remoteAddress: SocketAddress, | ||
socketOptions: SocketOptions.TCPClientSocketOptions | ||
): Socket = TODO("Not yet implemented") | ||
|
||
internal actual fun bind( | ||
selector: SelectorManager, | ||
localAddress: SocketAddress?, | ||
socketOptions: SocketOptions.AcceptorOptions | ||
): ServerSocket = TODO("Not yet implemented") | ||
|
||
internal actual fun UDPSocketBuilder.Companion.connectUDP( | ||
selector: SelectorManager, | ||
remoteAddress: SocketAddress, | ||
localAddress: SocketAddress?, | ||
options: SocketOptions.UDPSocketOptions | ||
): ConnectedDatagramSocket = TODO("Not yet implemented") | ||
|
||
internal actual fun UDPSocketBuilder.Companion.bindUDP( | ||
selector: SelectorManager, | ||
localAddress: SocketAddress?, | ||
options: SocketOptions.UDPSocketOptions | ||
): BoundDatagramSocket = TODO("Not yet implemented") |
12 changes: 12 additions & 0 deletions
12
ktor-network/js/src/io/ktor/network/sockets/SocketAddress.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.ktor.network.sockets | ||
|
||
public actual sealed class SocketAddress | ||
|
||
public actual data class InetSocketAddress actual constructor( | ||
public actual val hostname: String, | ||
public actual val port: Int | ||
) : SocketAddress() | ||
|
||
public actual data class UnixSocketAddress actual constructor( | ||
public actual val path: String | ||
) : SocketAddress() |
15 changes: 15 additions & 0 deletions
15
ktor-network/js/src/io/ktor/network/sockets/SocketTimeoutException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.sockets | ||
|
||
import io.ktor.utils.io.errors.* | ||
|
||
@Suppress("EXPECT_WITHOUT_ACTUAL") | ||
public actual class SocketTimeoutException( | ||
message: String, | ||
cause: Throwable? | ||
) : IOException(message, cause) { | ||
public actual constructor(message: String) : this(message, null) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
ktor-network/jvmAndNix/src/io/ktor/network/selector/SelectorManagerCommon.kt
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
ktor-network/jvmAndNix/src/io/ktor/network/sockets/ConnectUtils.kt
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
ktor-network/mingwX64/src/io/ktor/network/selector/SelectorManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.selector | ||
|
||
import io.ktor.utils.io.core.* | ||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.* | ||
|
||
/** | ||
* Creates the selector manager for current platform. | ||
*/ | ||
@Suppress("FunctionName") | ||
public actual fun SelectorManager(dispatcher: kotlin.coroutines.CoroutineContext): SelectorManager { | ||
TODO("SelectorManager is not yet implemented on Mingw x64 platform") | ||
} | ||
|
||
public actual interface SelectorManager : CoroutineScope, Closeable { | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
ktor-network/mingwX64/src/io/ktor/network/sockets/ConnectUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.sockets | ||
|
||
import io.ktor.network.selector.* | ||
|
||
internal actual suspend fun connect( | ||
selector: SelectorManager, | ||
remoteAddress: SocketAddress, | ||
socketOptions: SocketOptions.TCPClientSocketOptions | ||
): Socket = TODO("Not yet implemented") | ||
|
||
internal actual fun bind( | ||
selector: SelectorManager, | ||
localAddress: SocketAddress?, | ||
socketOptions: SocketOptions.AcceptorOptions | ||
): ServerSocket = TODO("Not yet implemented") | ||
|
||
internal actual fun UDPSocketBuilder.Companion.connectUDP( | ||
selector: SelectorManager, | ||
remoteAddress: SocketAddress, | ||
localAddress: SocketAddress?, | ||
options: SocketOptions.UDPSocketOptions | ||
): ConnectedDatagramSocket = TODO("Not yet implemented") | ||
|
||
internal actual fun UDPSocketBuilder.Companion.bindUDP( | ||
selector: SelectorManager, | ||
localAddress: SocketAddress?, | ||
options: SocketOptions.UDPSocketOptions | ||
): BoundDatagramSocket = TODO("Not yet implemented") |
12 changes: 12 additions & 0 deletions
12
ktor-network/mingwX64/src/io/ktor/network/sockets/SocketAddress.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.ktor.network.sockets | ||
|
||
public actual sealed class SocketAddress | ||
|
||
public actual data class InetSocketAddress actual constructor( | ||
public actual val hostname: String, | ||
public actual val port: Int | ||
) : SocketAddress() | ||
|
||
public actual data class UnixSocketAddress actual constructor( | ||
public actual val path: String | ||
) : SocketAddress() |
15 changes: 15 additions & 0 deletions
15
ktor-network/mingwX64/src/io/ktor/network/sockets/SocketTimeoutException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.sockets | ||
|
||
import io.ktor.utils.io.errors.* | ||
|
||
@Suppress("EXPECT_WITHOUT_ACTUAL") | ||
public actual class SocketTimeoutException( | ||
message: String, | ||
cause: Throwable? | ||
) : IOException(message, cause) { | ||
public actual constructor(message: String) : this(message, null) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What it the reason at all to provide this empty
SelectorManager
in common API, when f.e. nodejs sockets, can be implemented without it, as there is only single global event loop there?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to support the platforms without default event loop, so we need a "manager". (maybe not
selector manager
. Actually I'm just following the origin name here since I didn't define any member related toSelect
on common)For those platforms that only single global event loop is avaliable, it makes no sence itself. But if we want to write codes on common, we need a placeholder or we'll be hard on commonizing the user code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my question is more about: why at all expose an interface in common, which has no sense on JS?
We can create another
aSocket
functions, which will not useselector manager
in multiplatform code f.e.Overall, I don't understand, why to start with this?
Exposing Socket classes - ok
Exposing Select classes - not ok, as for now, it's more of an internal detail, rather then something, that could be used outside of ktor-network independent from ktor-network itself.
IMO: better to start trying to implement nodejs/mingw sockets, and see, if it's feasible to use
SelectorManager
abstraction, or may be it's better to create something newLike instead of
aSocket(selectorManager)
expose something likeaSocket(socketEngine)
or something fully different.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I did not treat it as a Selector (maybe I need to rename it), but a
SocketDispatcher
orNetworkEventLoopContext
. (I believe such a context is needed for platforms that supports running several isolated event loops, and such interfaces need commonizing)I'll think twice about whether Interface Driven Development is suitable in this case.