This commit is contained in:
2014-06-23 18:06:10 +04:00
parent a4afd49eb9
commit bd6af04156
9 changed files with 244 additions and 162 deletions

View File

@@ -0,0 +1,41 @@
package ru.m.armageddon.core;
import protohx.Message;
import ru.m.armageddon.proto.LoginRequest;
import ru.m.armageddon.proto.LoginResponse;
typedef PacketMeta = {
var family:Int;
var id:Int;
}
class PacketBuilder {
private static var MAP:Map<Int, Map<Int, Class<Message>>> = [
0x01 => [
0x0001 => LoginRequest,
0x0002 => LoginResponse
]
];
public static function packetMeta(packet:Message):PacketMeta {
for (family in MAP.keys()) {
var subMap = MAP[family];
for (id in subMap.keys()) {
var packetClass = subMap[id];
if (Std.is(packet, packetClass)) {
return {
family:family,
id:id
}
}
}
}
return null;
}
public static function buildPacket(family:Int, id:Int):Message {
var packetClass = MAP[family][id];
return Type.createInstance(packetClass, []);
}
}

View File

@@ -1,5 +1,6 @@
package ru.m.armageddon.core.connect;
import protohx.MessageUtils;
import flash.utils.Endian;
import haxe.io.BytesOutput;
import protohx.Message;
@@ -10,6 +11,8 @@ import flash.events.Event;
import flash.events.SecurityErrorEvent;
import flash.events.IOErrorEvent;
import flash.net.Socket;
import ru.m.armageddon.proto.LoginRequest;
import ru.m.armageddon.proto.LoginResponse;
class FlashConnection implements IConnection {
@@ -22,7 +25,7 @@ class FlashConnection implements IConnection {
private var socket:Socket;
public function new(host:String, port:Int, onConnect:Event->Void) {
public function new(host:String, port:Int, onConnect:?Dynamic->Void) {
this.onConnect = onConnect;
socket = new Socket();
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
@@ -39,7 +42,7 @@ class FlashConnection implements IConnection {
}
public dynamic function onConnect(event:Event):Void {
public dynamic function onConnect(?event:Dynamic):Void {
}
@@ -54,39 +57,25 @@ class FlashConnection implements IConnection {
var b = new flash.utils.ByteArray();
socket.readBytes(b);
var bs = Bytes.ofData(cast b);
var packet = buildPacket(family, id);
var packet = PacketBuilder.buildPacket(family, id);
packet.mergeFrom(bs);
receive(packet);
}
private function buildPacket(family:Int, id:Int):Message {
var packetClass = MAP[family][id];
return Type.createInstance(packetClass, []);
}
public function send(packet:Message):Void {
L.d("XXX", "send:" + packet);
for (family in MAP.keys()) {
var subMap = MAP[family];
for (id in subMap.keys()) {
var packetClass = subMap[id];
if (Std.is(packet, packetClass)) {
L.d("XXX", family + ":" + id);
socket.writeByte(family);
socket.writeByte(id);
var out = new BytesOutput();
packet.writeTo(out);
var bytes = out.getBytes();
socket.writeShort(bytes.length);
socket.writeBytes(cast bytes.getData());
socket.flush();
}
}
}
L.d("Send", MessageUtils.toJson(packet));
var meta = PacketBuilder.packetMeta(packet);
socket.writeByte(meta.family);
socket.writeByte(meta.id);
var out = new BytesOutput();
packet.writeTo(out);
var bytes = out.getBytes();
socket.writeShort(bytes.length);
socket.writeBytes(cast bytes.getData());
socket.flush();
}
public dynamic function receive(packet:Message):Void {
L.d("Receive", protohx.MessageUtils.toJson(packet));
L.d("Receive", MessageUtils.toJson(packet));
}
}

View File

@@ -1,13 +1,12 @@
package ru.m.armageddon.core.connect;
import flash.events.Event;
import protohx.Message;
interface IConnection {
public function send(packet:Message):Void;
public dynamic function onConnect(event:Event):Void;
public dynamic function onConnect(?event:Dynamic):Void;
public dynamic function receive(packet:Message):Void;
}