From e04d497fe90910a9ec0eb397098e7532be1ecce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Wed, 28 May 2014 16:01:57 +0200 Subject: [PATCH] Fixed incorrect check leading to components not always being properly initialized before sent to clients. Exposing finishConnect on internet card for those who think they need it. --- .../li/cil/oc/common/component/ManagedComponent.scala | 6 ++++-- .../li/cil/oc/server/component/InternetCard.scala | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/scala/li/cil/oc/common/component/ManagedComponent.scala b/src/main/scala/li/cil/oc/common/component/ManagedComponent.scala index f340f1bfa..acc7f3c04 100644 --- a/src/main/scala/li/cil/oc/common/component/ManagedComponent.scala +++ b/src/main/scala/li/cil/oc/common/component/ManagedComponent.scala @@ -26,8 +26,10 @@ abstract class ManagedComponent extends ManagedEnvironment { // the address is embedded in the saved data that gets sent to the client, // so that that address can be used to associate components on server and // client (for example keyboard and screen/text buffer). - if (node == null) api.Network.joinNewNetwork(node) - if (node != null) nbt.setNewCompoundTag("node", node.save) + if (node != null) { + if (node.network == null) api.Network.joinNewNetwork(node) + nbt.setNewCompoundTag("node", node.save) + } } final protected def result(args: Any*): Array[AnyRef] = { diff --git a/src/main/scala/li/cil/oc/server/component/InternetCard.scala b/src/main/scala/li/cil/oc/server/component/InternetCard.scala index fcca83e48..18b0a4e64 100644 --- a/src/main/scala/li/cil/oc/server/component/InternetCard.scala +++ b/src/main/scala/li/cil/oc/server/component/InternetCard.scala @@ -75,6 +75,16 @@ class InternetCard extends ManagedComponent { result(handle) } + @Callback(doc = """function(handle:number):boolean -- Ensures a socket is connected. Errors if the connection failed.""") + def finishConnect(context: Context, args: Arguments): Array[AnyRef] = this.synchronized { + checkOwner(context) + val handle = args.checkInteger(0) + connections.get(handle) match { + case Some(socket) => result(socket.checkConnected()) + case _ => throw new IOException("bad connection descriptor") + } + } + @Callback(direct = true, doc = """function(handle:number) -- Closes an open socket stream.""") def close(context: Context, args: Arguments): Array[AnyRef] = this.synchronized { checkOwner(context)