From a4ca9a924c9d62cac02bbc4b9ff01c0f1be877c2 Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 30 Mar 2020 19:00:02 +0300 Subject: [PATCH] [connect] web-socket connection fixes --- src/main/hw/animate/Animate.hx | 5 ++++- src/main/hw/animate/AnimateRunner.hx | 5 +---- src/main/hw/connect/PacketUtil.hx | 9 +++++++++ src/main/hw/connect/js/JsConnection.hx | 14 +++----------- src/main/hw/connect/neko/NekoWSConnection.hx | 14 ++++---------- 5 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/main/hw/animate/Animate.hx b/src/main/hw/animate/Animate.hx index af0b4a1..c45c631 100644 --- a/src/main/hw/animate/Animate.hx +++ b/src/main/hw/animate/Animate.hx @@ -21,6 +21,7 @@ class Animate implements IAnimate { public function new(view:IView, duration:Int = -1) { this.view = view; this.duration = duration > -1 ? duration : defaultDuraion; + this.startTime = 0; } private inline function get_object():DisplayObject { @@ -28,8 +29,10 @@ class Animate implements IAnimate { } public function start():Promise { - startTime = runner.run(this); + startTime = Date.now().getTime(); deferred = new Deferred(); + runner.push(this); + this.update(startTime); return deferred.promise(); } diff --git a/src/main/hw/animate/AnimateRunner.hx b/src/main/hw/animate/AnimateRunner.hx index 7bd972e..3074c83 100644 --- a/src/main/hw/animate/AnimateRunner.hx +++ b/src/main/hw/animate/AnimateRunner.hx @@ -19,10 +19,7 @@ import flash.Lib; } } - public function run(animate:IAnimate):Float { + public function push(animate:IAnimate):Void { animates.push(animate); - var time = Date.now().getTime(); - animate.update(time); - return time; } } diff --git a/src/main/hw/connect/PacketUtil.hx b/src/main/hw/connect/PacketUtil.hx index dde687f..b4d5668 100644 --- a/src/main/hw/connect/PacketUtil.hx +++ b/src/main/hw/connect/PacketUtil.hx @@ -17,4 +17,13 @@ class PacketUtil { packet.writeTo(out); return out.getBytes(); } + + public static function toBytesWithSize(packet:P):Bytes { + var out = new BytesOutput(); + out.writeUInt16(0); + packet.writeTo(out); + var bytes = out.getBytes(); + bytes.setUInt16(0, bytes.length - 2); + return bytes; + } } diff --git a/src/main/hw/connect/js/JsConnection.hx b/src/main/hw/connect/js/JsConnection.hx index 866c2d2..03496a0 100644 --- a/src/main/hw/connect/js/JsConnection.hx +++ b/src/main/hw/connect/js/JsConnection.hx @@ -71,22 +71,14 @@ class JsConnection extends BaseConnection { } private function onSocketData(event:Dynamic):Void { - var packet:I = null; - try { - var bytes = Bytes.ofData(event.data); - packet = PacketUtil.fromBytes(bytes, queue.packetClass); - } catch (error:Dynamic) { - handler.emit(ConnectionEvent.ERROR(error)); - } - if (packet != null) { - receive(packet); - } + var bytes = Bytes.ofData(event.data); + pushData(bytes); } override public function send(packet:O):Void { if (connected) { super.send(packet); - var bytes = PacketUtil.toBytes(packet); + var bytes = PacketUtil.toBytesWithSize(packet); socket.send(bytes.getData()); } else { L.w("Connection", "closed"); diff --git a/src/main/hw/connect/neko/NekoWSConnection.hx b/src/main/hw/connect/neko/NekoWSConnection.hx index 733f429..8879376 100644 --- a/src/main/hw/connect/neko/NekoWSConnection.hx +++ b/src/main/hw/connect/neko/NekoWSConnection.hx @@ -10,6 +10,7 @@ import sys.net.Socket; class NekoWSConnection extends NekoConnection { private var opened:Bool; + private var buffer:Bytes; public function new(socket:Socket, i:Class) { super(socket, i); @@ -17,8 +18,8 @@ class NekoWSConnection extends NekoConnection { } override private function sendPacket(packet:O):Void { - var data = PacketUtil.toBytes(packet); - writeData(data, socket); + var bytes = PacketUtil.toBytesWithSize(packet); + writeData(bytes, socket); } override public function pushData(bytes:Bytes):Void { @@ -33,8 +34,7 @@ class NekoWSConnection extends NekoConnection { } else { var data = parseData(bytes); if (data != null) { - var packet:I = PacketUtil.fromBytes(data, queue.packetClass); - receive(packet); + super.pushData(data); } } } @@ -126,23 +126,17 @@ class NekoWSConnection extends NekoConnection { len = (b2 << 24) + (b3 << 16) + (b4 << 8) + b5; } - //Lib.println("len = " + len); - - // direct array init not work corectly! var mask = []; mask.push(bytes.get(p++)); mask.push(bytes.get(p++)); mask.push(bytes.get(p++)); mask.push(bytes.get(p++)); - //Lib.println("mask = " + mask); - var data = new BytesBuffer(); for (i in 0...len) { data.addByte(bytes.get(p++) ^ mask[i % 4]); } - //Lib.println("readed = " + data.toString()); return data.getBytes(); } else { throw "Expected masked data.";