[connect] web-socket connection fixes

This commit is contained in:
2020-03-30 19:00:02 +03:00
parent 88b4459023
commit a4ca9a924c
5 changed files with 21 additions and 26 deletions

View File

@@ -21,6 +21,7 @@ class Animate implements IAnimate {
public function new(view:IView<Dynamic>, duration:Int = -1) { public function new(view:IView<Dynamic>, duration:Int = -1) {
this.view = view; this.view = view;
this.duration = duration > -1 ? duration : defaultDuraion; this.duration = duration > -1 ? duration : defaultDuraion;
this.startTime = 0;
} }
private inline function get_object():DisplayObject { private inline function get_object():DisplayObject {
@@ -28,8 +29,10 @@ class Animate implements IAnimate {
} }
public function start():Promise<IAnimate> { public function start():Promise<IAnimate> {
startTime = runner.run(this); startTime = Date.now().getTime();
deferred = new Deferred(); deferred = new Deferred();
runner.push(this);
this.update(startTime);
return deferred.promise(); return deferred.promise();
} }

View File

@@ -19,10 +19,7 @@ import flash.Lib;
} }
} }
public function run(animate:IAnimate):Float { public function push(animate:IAnimate):Void {
animates.push(animate); animates.push(animate);
var time = Date.now().getTime();
animate.update(time);
return time;
} }
} }

View File

@@ -17,4 +17,13 @@ class PacketUtil {
packet.writeTo(out); packet.writeTo(out);
return out.getBytes(); return out.getBytes();
} }
public static function toBytesWithSize<P:Message>(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;
}
} }

View File

@@ -71,22 +71,14 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
} }
private function onSocketData(event:Dynamic):Void { private function onSocketData(event:Dynamic):Void {
var packet:I = null;
try {
var bytes = Bytes.ofData(event.data); var bytes = Bytes.ofData(event.data);
packet = PacketUtil.fromBytes(bytes, queue.packetClass); pushData(bytes);
} catch (error:Dynamic) {
handler.emit(ConnectionEvent.ERROR(error));
}
if (packet != null) {
receive(packet);
}
} }
override public function send(packet:O):Void { override public function send(packet:O):Void {
if (connected) { if (connected) {
super.send(packet); super.send(packet);
var bytes = PacketUtil.toBytes(packet); var bytes = PacketUtil.toBytesWithSize(packet);
socket.send(bytes.getData()); socket.send(bytes.getData());
} else { } else {
L.w("Connection", "closed"); L.w("Connection", "closed");

View File

@@ -10,6 +10,7 @@ import sys.net.Socket;
class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> { class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
private var opened:Bool; private var opened:Bool;
private var buffer:Bytes;
public function new(socket:Socket, i:Class<I>) { public function new(socket:Socket, i:Class<I>) {
super(socket, i); super(socket, i);
@@ -17,8 +18,8 @@ class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
} }
override private function sendPacket(packet:O):Void { override private function sendPacket(packet:O):Void {
var data = PacketUtil.toBytes(packet); var bytes = PacketUtil.toBytesWithSize(packet);
writeData(data, socket); writeData(bytes, socket);
} }
override public function pushData(bytes:Bytes):Void { override public function pushData(bytes:Bytes):Void {
@@ -33,8 +34,7 @@ class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
} else { } else {
var data = parseData(bytes); var data = parseData(bytes);
if (data != null) { if (data != null) {
var packet:I = PacketUtil.fromBytes(data, queue.packetClass); super.pushData(data);
receive(packet);
} }
} }
} }
@@ -126,23 +126,17 @@ class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
len = (b2 << 24) + (b3 << 16) + (b4 << 8) + b5; len = (b2 << 24) + (b3 << 16) + (b4 << 8) + b5;
} }
//Lib.println("len = " + len);
// direct array init not work corectly!
var mask = []; var mask = [];
mask.push(bytes.get(p++)); mask.push(bytes.get(p++));
mask.push(bytes.get(p++)); mask.push(bytes.get(p++));
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(); var data = new BytesBuffer();
for (i in 0...len) { for (i in 0...len) {
data.addByte(bytes.get(p++) ^ mask[i % 4]); data.addByte(bytes.get(p++) ^ mask[i % 4]);
} }
//Lib.println("readed = " + data.toString());
return data.getBytes(); return data.getBytes();
} else { } else {
throw "Expected masked data."; throw "Expected masked data.";