-
This commit is contained in:
@@ -9,11 +9,13 @@ class BaseConnection implements IConnection {
|
||||
public var handler(default,default):IConnectionHandler;
|
||||
public var packetHandler(default,default):IPacketHandler;
|
||||
public var connected(default, null):Bool;
|
||||
public var queue(default, null):PacketQueue;
|
||||
|
||||
private var builder:IPacketBuilder;
|
||||
|
||||
public function new(?handler:IConnectionHandler = null, ?packetHandler:IPacketHandler) {
|
||||
this.builder = new PacketBuilder();
|
||||
this.queue = new PacketQueue(builder);
|
||||
this.packetHandler = packetHandler;
|
||||
this.handler = handler;
|
||||
}
|
||||
@@ -22,8 +24,22 @@ class BaseConnection implements IConnection {
|
||||
throw "Not implemented";
|
||||
}
|
||||
|
||||
public function pushData(bytes:Bytes):Void {}
|
||||
public function disconnect():Void {
|
||||
throw "Not implemented";
|
||||
}
|
||||
|
||||
public function pushData(bytes:Bytes):Void {
|
||||
queue.addBytes(bytes);
|
||||
while (queue.hasMsg()) {
|
||||
var packet:Message = queue.popMsg();
|
||||
try {
|
||||
receive(packet);
|
||||
} catch (error:Dynamic) {
|
||||
trace(error);
|
||||
handler.onError(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
public function send(packet:Message):Void {
|
||||
L.d("Send", Type.getClassName(Type.getClass(packet)).split(".").pop());
|
||||
}
|
||||
|
||||
@@ -12,8 +12,10 @@ interface IConnection {
|
||||
private var builder:IPacketBuilder;
|
||||
|
||||
public function connect():ICallback<Dynamic>;
|
||||
public function disconnect():Void;
|
||||
public function send(packet:Message):Void;
|
||||
public function pushData(bytes:Bytes):Void;
|
||||
|
||||
private function receive(packet:Message):Void;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +41,8 @@ class PacketBuilder implements IPacketBuilder {
|
||||
}
|
||||
|
||||
public function buildPacket(meta:PacketMeta):Message {
|
||||
if (!MAP.exists(meta.family)) throw "Unsupported family(" + meta.family + ")";
|
||||
if (!MAP[meta.family].exists(meta.id)) throw "Unsupported family(" + meta.family + ") id(" + meta.id + ")";
|
||||
var packetClass = MAP[meta.family][meta.id];
|
||||
return Type.createInstance(packetClass, []);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package ru.m.armageddon.core.connect.neko;
|
||||
package ru.m.armageddon.core.connect;
|
||||
|
||||
import ru.m.armageddon.core.connect.IConnection.IPacketBuilder;
|
||||
import protohx.Message;
|
||||
@@ -42,6 +42,12 @@ class FlashConnection extends BaseConnection {
|
||||
return callback;
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
if (handler != null) handler.onDisconnected();
|
||||
}
|
||||
|
||||
private function onError(event:ErrorEvent):Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
@@ -70,16 +76,14 @@ class FlashConnection extends BaseConnection {
|
||||
}
|
||||
|
||||
private function onSocketData(_):Void {
|
||||
if (socket.bytesAvailable < 4) return;
|
||||
var family = socket.readByte();
|
||||
var id = socket.readByte();
|
||||
var length = socket.readShort();
|
||||
var b = new flash.utils.ByteArray();
|
||||
socket.readBytes(b);
|
||||
var bs = Bytes.ofData(cast b);
|
||||
var packet = builder.buildPacket({family:family, id:id});
|
||||
packet.mergeFrom(bs);
|
||||
receive(packet);
|
||||
try {
|
||||
var b = new flash.utils.ByteArray();
|
||||
socket.readBytes(b);
|
||||
var bs = Bytes.ofData(cast b);
|
||||
pushData(bs);
|
||||
} catch (error:Dynamic) {
|
||||
handler.onError(error);
|
||||
}
|
||||
}
|
||||
|
||||
override public function send(packet:Message):Void {
|
||||
|
||||
@@ -8,8 +8,6 @@ import ru.m.armageddon.core.connect.IConnection;
|
||||
|
||||
class NekoConnection extends BaseConnection {
|
||||
|
||||
public var queue(default, null):PacketQueue;
|
||||
|
||||
private var socket:Socket;
|
||||
|
||||
public function new(socket:Socket, ?handler:IConnectionHandler = null, ?packetHandler:IPacketHandler = null) {
|
||||
@@ -18,20 +16,6 @@ class NekoConnection extends BaseConnection {
|
||||
socket.setFastSend(true);
|
||||
socket.output.bigEndian = false;
|
||||
socket.input.bigEndian = false;
|
||||
queue = new PacketQueue(builder);
|
||||
}
|
||||
|
||||
override public function pushData(bytes:Bytes):Void {
|
||||
queue.addBytes(bytes);
|
||||
while (queue.hasMsg()) {
|
||||
var packet:Message = queue.popMsg();
|
||||
try {
|
||||
receive(packet);
|
||||
} catch (error:Dynamic) {
|
||||
trace(error);
|
||||
handler.onError(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override public function send(packet:Message):Void {
|
||||
|
||||
Reference in New Issue
Block a user