RawSocketWebSocketClient.read() blocked Thread #50
Comments
I experimented and found that read functions do not return control when bytes in the socket end |
Hello. Tell me, are you planning to solve this problem? And when can you wait for a decision? |
Yes, I want to fix this. Sorry for the delay. I have tons of bugs and issues reported and the amount of time I can invest is limited in my spare time. Contributions and PRs are welcome too. In the case nobody fixes it before me, I plan to do it ASAP. My current focus is on solving pending bugs. But I'm fixing first the ones directly related to Korge, so this will have to wait a bit more. |
Yes, I understand you. I'll wait. As soon as our first project starts
working, rest assured, we will thank you and will support your project
with finances. Because We plan to use your libraries in future
projects.
ср, 29 апр. 2020 г. в 12:31, Carlos Ballesteros Velasco <
notifications@github.com>:
… Yes, I want to fix this. Sorry for the delay. I have tons of bugs and
issues reported and the amount of time I can invest is limited in my spare
time. Contributions and PRs are welcome too. In the case nobody fixes it
before me, I plan to do it ASAP. My current focus is on solving pending
bugs. But I'm fixing first the ones directly related to Korge, so this will
have to wait a bit more.
https://github.com/orgs/korlibs/projects/1
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#50 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AN7Z3N5MW7BIQCUEF5U7GS3RO7XV3ANCNFSM4LSZCZGQ>
.
|
Nice, thanks! I would like to close all the remaining bugs before the end of the week, but since they are bugs the amount of time required is uncertain, but I will try 👍 |
BTW. I have fixed the remaining issues already. In which platform do you have these blocks? |
Hello. In the JVM. But I already use
com.soywiz.korio.net.ws.WebSocketClient. So this problem is not
relevant for me.
пт, 1 мая 2020 г. в 17:11, Carlos Ballesteros Velasco <
notifications@github.com>:
… BTW. I have fixed the remaining issues already. In which platform do you
have these blocks?
JVM, Android, Node.JS, K/N Windows, K/N Linux, K/N Mac, K/N iOS*?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#50 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AN7Z3N3W4LWWD2HH73REKG3RPLKCRANCNFSM4LSZCZGQ>
.
|
But I have an insoluble problem that I’ve been thinking about for several
days.
As soon as I use the delay in the suspend function, an error is raised in
the browser
TypeError: this.resultContinuation_0 is undefined.
For instance:
@JsName("wait")
suspend fun wait(t: Long)
{
delay(2000)
console.log("this condition22")
}
This is a feature of compiling suspend functions:
https://discuss.kotlinlang.org/t/coroutines-and-the-js-compiler/6268
Maybe you have ideas on how to solve this.
The meaning of the code is that the current thread is waiting for the
object to appear in the queue.
пт, 1 мая 2020 г. в 17:15, Олег Лемко <lemkoleg82@gmail.com>:
… Hello. In the JVM. But I already use com.soywiz.korio.net.ws.WebSocketClient. So this problem is not relevant for me.
пт, 1 мая 2020 г. в 17:11, Carlos Ballesteros Velasco <
***@***.***>:
> BTW. I have fixed the remaining issues already. In which platform do you
> have these blocks?
> JVM, Android, Node.JS, K/N Windows, K/N Linux, K/N Mac, K/N iOS*?
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <#50 (comment)>, or
> unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AN7Z3N3W4LWWD2HH73REKG3RPLKCRANCNFSM4LSZCZGQ>
> .
>
|
Then I'm going to close this issue. Regarding to the problem you said. It is either a Kotlin.JS compiler problem or that you are calling it from the outside (JS) in a way that is not expected. @JsName("wait")
fun wait(t: Double) = GlobalScope.promise {
delay(2000)
console.log("this condition22")
} Also you shouldn't use Long on the signature of the functions either, use Double instead and cast inside the body. Long in JS are not normal variables, but there is a wrapper that holds the long as two 32-bit integers. |
Thank you. I'll try
пт, 1 мая 2020 г. в 19:35, Carlos Ballesteros Velasco <
notifications@github.com>:
… Then I'm going to close this issue.
Regarding to the problem you said. It is either a Kotlin.JS compiler
problem or that you are calling it from the outside (JS) in a way that is
not expected.
If you plan to call it from javascript. Consider not using a suspend fun
(that internally requires a continuation as last parameter) but a normal
function returning a Promise.
like:
@JsName("wait")fun wait(t: Double) = GlobalScope.promise {
delay(2000)
console.log("this condition22")
}
Also you shouldn't use Long on the signature of the functions either, use
Double instead and cast inside the body. Long in JS are not normal
variables, but there is a wrapper that holds the long as two 32-bit
integers.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#50 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AN7Z3N6DNUBFOJNL5SGFJIDRPL263ANCNFSM4LSZCZGQ>
.
|
Hello, Carlos. Forgive me my impudence. I know that you have a lot of your
work. But still
I decided to contact you for help. Here with such a problem. My
multi-platform project is almost ready.
But I ran into an unsolvable problem for me. The logic of the project is
implemented with maximum
JS and Native programming logic compatibility. Therefore, it is implemented
so that there is a moment,
when the next task (which, naturally, is performed in the new coroutine) is
waiting for a response from the server.
The listener sends requests and receives the next response, puts it in the
response queue.
After that, the listener sends a signal about the awakening of the task for
which he received an answer.
And the task for this time is in standby mode. This is how I tried to
implement a wait-wake class
for js:
package сrossPlatforms
import com.soywiz.klock.DateTime
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
import kotlin.time.ExperimentalTime
import kotlin.time.milliseconds
actual class MyCondition : CoroutineScope {
actual override val coroutineContext: CoroutineContext =
Dispatchers.Unconfined
actual val MyConditionContext = CoroutineScope(coroutineContext)
@JsName("isAwaited")
actual var isAwaited: Boolean = false
private set
@ExperimentalTime
@JsName("cAwait")
actual fun cAwait(t: Long): Boolean{
isAwaited = false
js("""this.cAwait2(10000);""")
console.log("this condition1")
return isAwaited
}
@ExperimentalTime
@JsName("cAwait2")
suspend fun cAwait2()
{
this.wait(10000).await()
console.log("this condition22")
}
@ExperimentalTime
@JsName("wait")
fun wait(t: Long) = promise {
val time = DateTime.nowUnixLong() + t
while(true) {
if(isAwaited || time <= DateTime.nowUnixLong()){
break
}
delay(100.milliseconds)
console.log("this condition3")
}
}
@JsName("cSignal")
actual fun cSignal() {
isAwaited = true
}
@ExperimentalTime
@JsName("cDestroy")
actual fun cDestroy() {
isAwaited = true
MyConditionContext.launch {
delay(100.milliseconds)
isAwaited = false
}
}
}
*Index*
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>Before script...</p>
<script src="JSOCKET.js"></script>
<script type="text/javascript">
var i = new avaclub.crossPlatforms.MyCondition();
async function l() {
setTimeout(() => i.cSignal(), 2000);
i.cAwait(10000); // here the cycle starts and works for 2 seconds
console.log("ggggggggggggg");} //this conclusion does not start anymore
l()
</script>
<p>...After script</p>
</body>
</html>
Your advice helped me a bit and the wait loop using delay () worked.
But, as soon as I run the code in the browser, I get this console output:
this condition0
*TypeError: this.context is undefined*
this condition3 4
this condition3
this condition3 11
The l () function starts a loop. The cycle is processed. And that’s all:
the loop does not return function control.
I don’t know what could be the problem. If you have time to look at it, I
would be very grateful to you. Thank.
… |
If I try to create this class in general code
package CrossPlatforms
import com.badoo.reaktive.utils.lock.Condition
import com.badoo.reaktive.utils.lock.Lock
import com.soywiz.klock.DateTime
import com.soywiz.korio.util.OS
import io.ktor.util.InternalAPI
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext
import kotlin.js.JsName
import kotlin.time.ExperimentalTime
import kotlin.time.milliseconds
@JsName("DEFAULT_AWAIT_TIMEOUT")
const val DEFAULT_AWAIT_TIMEOUT = 10000L
@JsName("MyCondition")
class MyCondition : CoroutineScope {
override val coroutineContext: CoroutineContext = Dispatchers.Unconfined
val MyConditionContext = CoroutineScope(coroutineContext)
var isAwaited: Boolean = false
private set
@internalapi
val lock: Lock? = if(!OS.isJs){Lock()}else null
@internalapi
val condition: Condition? = if(!OS.isJs){lock?.newCondition()} else null
@internalapi
@ExperimentalTime
@JsName("cAwait")
suspend fun cAwait(t: Long):Boolean{
isAwaited = false
if(OS.isJs){
val time = DateTime.nowUnixLong() + if(t== 0L)
{t}
else
{
DEFAULT_AWAIT_TIMEOUT
}
while(true) {
if(isAwaited || time <= DateTime.nowUnixLong()){
break
}
delay(100.milliseconds)
println("this condition3")
}
}
else{
condition!!.await(if(t== 0L)
{t}
else
{
DEFAULT_AWAIT_TIMEOUT
})
}
return isAwaited
}
@internalapi
@JsName("cSignal")
fun cSignal(){
isAwaited = true
if(!OS.isJs){
condition!!.signal()
}
}
@internalapi
@ExperimentalTime
@JsName("cDestroy")
fun cDestroy(){
if(OS.isJs){
isAwaited = true
MyConditionContext.launch {
delay(100.milliseconds)
isAwaited = false
}
}
else{
condition!!.destroy()
}
}
}
then I get such an error:
TypeError: this.resultContinuation_0 is undefined (Mozilla)
вс, 3 мая 2020 г. в 16:42, Олег Лемко <lemkoleg82@gmail.com>:
… Hello, Carlos. Forgive me my impudence. I know that you have a lot of your
work. But still
I decided to contact you for help. Here with such a problem. My
multi-platform project is almost ready.
But I ran into an unsolvable problem for me. The logic of the project is
implemented with maximum
JS and Native programming logic compatibility. Therefore, it is
implemented so that there is a moment,
when the next task (which, naturally, is performed in the new coroutine)
is waiting for a response from the server.
The listener sends requests and receives the next response, puts it in the
response queue.
After that, the listener sends a signal about the awakening of the task
for which he received an answer.
And the task for this time is in standby mode. This is how I tried to
implement a wait-wake class
for js:
package сrossPlatforms
import com.soywiz.klock.DateTime
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
import kotlin.time.ExperimentalTime
import kotlin.time.milliseconds
actual class MyCondition : CoroutineScope {
actual override val coroutineContext: CoroutineContext =
Dispatchers.Unconfined
actual val MyConditionContext = CoroutineScope(coroutineContext)
@JsName("isAwaited")
actual var isAwaited: Boolean = false
private set
@ExperimentalTime
@JsName("cAwait")
actual fun cAwait(t: Long): Boolean{
isAwaited = false
js("""this.cAwait2(10000);""")
console.log("this condition1")
return isAwaited
}
@ExperimentalTime
@JsName("cAwait2")
suspend fun cAwait2()
{
this.wait(10000).await()
console.log("this condition22")
}
@ExperimentalTime
@JsName("wait")
fun wait(t: Long) = promise {
val time = DateTime.nowUnixLong() + t
while(true) {
if(isAwaited || time <= DateTime.nowUnixLong()){
break
}
delay(100.milliseconds)
console.log("this condition3")
}
}
@JsName("cSignal")
actual fun cSignal() {
isAwaited = true
}
@ExperimentalTime
@JsName("cDestroy")
actual fun cDestroy() {
isAwaited = true
MyConditionContext.launch {
delay(100.milliseconds)
isAwaited = false
}
}
}
*Index*
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>Before script...</p>
<script src="JSOCKET.js"></script>
<script type="text/javascript">
var i = new avaclub.crossPlatforms.MyCondition();
async function l() {
setTimeout(() => i.cSignal(), 2000);
i.cAwait(10000); // here the cycle starts and works for 2 seconds
console.log("ggggggggggggg");} //this conclusion does not start anymore
l()
</script>
<p>...After script</p>
</body>
</html>
Your advice helped me a bit and the wait loop using delay () worked.
But, as soon as I run the code in the browser, I get this console output:
this condition0
*TypeError: this.context is undefined*
this condition3 4
this condition3
this condition3 11
The l () function starts a loop. The cycle is processed. And that’s all:
the loop does not return function control.
I don’t know what could be the problem. If you have time to look at it, I
would be very grateful to you. Thank.
>
|
The suspend functions internally in JVM and JS have a special signature like this:
-->
If you don't provide the continuation, it is undefined, and thus the error. So it works as expected. Also Long in JS uses an internal class since JS doesn't have longs so you would have an additional error. As said, if you plan to call it from javascript directly, your best bet is to return a promise instead that would be the equivalent to the suspend function (though in js, you have to use Is it totally necessary for you to have the same code there for JS and JVM? You can use
With a |
Thank you, I know about Long. This is still a pprototype. It is sad
that there is no synchronous code simulation implementation for JS.
пн, 4 мая 2020 г. в 21:35, Carlos Ballesteros Velasco <
notifications@github.com>:
… The suspend functions internally in JVM and JS have a special signature
like this:
suspend fun await(time: Long)
-->
function await(time, continuation)
If you don't provide the continuation, it is undefined, and thus the
error. So it works as expected. Also Long in JS uses an internal class
since JS doesn't have longs so you would have an additional error.
You can also put js("debugger;") and do step by step execution on the
browser to understand this.
As said, if you plan to call it from javascript directly, your best bet is
to return a promise instead that would be the equivalent to the suspend
function (though in js, you have to use .then or use the await keyword)
Is it totally necessary for you to have the same code there for JS and
JVM? You can use expect and actual to have different implementations
while having the same interface. And you can expose additional methods like
the one returning the promise in JS.
You can also make an:
expect MyPromise<T>...
With a actual typealias MyPromise<T> = Promise on JS, and an actual
implementation on the JVM. Then return MyPromise in your cAwait method.
You would have to call await or something on the JVM too to it actually
suspends.
Depending on what you are trying to do in the end, there might be better
ways to do this stuff than others.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#50 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AN7Z3N7MLSWNIIFS34UNNDLRP4DHLANCNFSM4LSZCZGQ>
.
|
The suspend implementation was not designed to be super interoperable, but to make code clean. You can also use this on the JS target: But think that Kotlin/common is not a panacea. Sometimes you have to write What I can do if you want is to add a common Promise to Korio as I suggested in the previous comment so you can use it directly in common / all the targets. I'll make a separate PR with that |
Here we are: you have to make your function to return |
Once deployed, Korio 1.11.1 includes the Promise class. So the functions you need to use from JS, make them: |
I don’t know why, but my gralde cannot download version 1.11.1
пн, 4 мая 2020 г. в 23:08, Carlos Ballesteros Velasco <
notifications@github.com>:
… Once deployed, Korio 1.11.1 includes the Promise class.
So the functions you need to use from JS, make them: fun myfunc(value:
Double): Promise<T> and in javascript you can do await obj.myfunc(10) and
in normal kotlin obj.myfunc(10).await() in a suspend function
7cd7274
<7cd7274>
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#50 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AN7Z3N6B6TVF2LLTVA5QMBDRP4ODXANCNFSM4LSZCZGQ>
.
|
Deploy is in progress: https://github.com/korlibs/korio/runs/644209072 If you want to try right now, you can download that commit and |
Oh, I'll wait. Nothing bad
пн, 4 мая 2020 г. в 23:44, Carlos Ballesteros Velasco <
notifications@github.com>:
… Deploy is in progress: https://github.com/korlibs/korio/runs/644209072
If you want to try right now, you can download that commit and ./gradlew
publishToMavenLocal
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#50 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AN7Z3NZSHMCKJ5DXSQO5AWTRP4SKVANCNFSM4LSZCZGQ>
.
|
Hello. When I try to read data from a socket,
my thread freezes. (data comes in - that's for sure). Here is my code:
private suspend fun conn(connectionDNSName :String, connPort :Int){
val cl = createTcpClient()
cl.connect(connectionDNSName, connPort)
val mySocket = RawSocketWebSocketClient(
GlobalScope.coroutineContext, cl, URL.invoke(""), null, false, null, "")
//val r = MySocketChannel.readBinary() -- freezes
//val r = MySocketChannel..client.readAll() -- freezes
//val r = MySocketChannel..client.readAvailable() -- freezes
And one more question:
can I get RawSocketWebSocketClient from WebSocketClient ? For instance:
val url = "ws://hostname:3456"
val c: RawSocketWebSocketClient = WebSocketClient(url) as RawSocketWebSocketClient
And work with him as with RawSocketWebSocketClient.
The text was updated successfully, but these errors were encountered: