-
This commit is contained in:
17
src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx
Executable file
17
src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx
Executable file
@@ -0,0 +1,17 @@
|
||||
package ru.m.armageddon.core.connect;
|
||||
|
||||
import protohx.Message;
|
||||
import ru.m.armageddon.core.connect.IConnection;
|
||||
|
||||
class BaseConnection implements IConnection {
|
||||
|
||||
private var builder:IPacketBuilder;
|
||||
private var handler:IConnectionHandler;
|
||||
|
||||
public function new(handler:IConnectionHandler) {
|
||||
this.builder = new PacketBuilder();
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
public function send(packet:Message):Void {}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.armageddon.core.connect;
|
||||
|
||||
import ru.m.armageddon.core.connect.IConnection.IConnectionHandler;
|
||||
import protohx.MessageUtils;
|
||||
import flash.utils.Endian;
|
||||
import haxe.io.BytesOutput;
|
||||
@@ -14,19 +15,12 @@ import flash.net.Socket;
|
||||
import ru.m.armageddon.proto.LoginRequest;
|
||||
import ru.m.armageddon.proto.LoginResponse;
|
||||
|
||||
class FlashConnection implements IConnection {
|
||||
|
||||
private static var MAP:Map<Int, Map<Int, Class<Message>>> = [
|
||||
0x01 => [
|
||||
0x0003 => LoginRequest,
|
||||
0x0002 => LoginResponse
|
||||
]
|
||||
];
|
||||
class FlashConnection extends BaseConnection {
|
||||
|
||||
private var socket:Socket;
|
||||
|
||||
public function new(host:String, port:Int, onConnect:?Dynamic->Void) {
|
||||
this.onConnect = onConnect;
|
||||
public function new(host:String, port:Int, handler:IConnectionHandler) {
|
||||
super(handler);
|
||||
socket = new Socket();
|
||||
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
|
||||
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
|
||||
@@ -34,20 +28,19 @@ class FlashConnection implements IConnection {
|
||||
socket.addEventListener(Event.CONNECT, onConnect);
|
||||
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
|
||||
socket.endian = Endian.LITTLE_ENDIAN;
|
||||
|
||||
socket.connect(host, port);
|
||||
}
|
||||
|
||||
private function onError(event:ErrorEvent):Void {
|
||||
|
||||
handler.onError(event);
|
||||
}
|
||||
|
||||
public dynamic function onConnect(?event:Dynamic):Void {
|
||||
|
||||
private function onConnect(_):Void {
|
||||
handler.onConnected();
|
||||
}
|
||||
|
||||
private function onClose(_):Void {
|
||||
|
||||
handler.onDisconnected();
|
||||
}
|
||||
|
||||
private function onSocketData(_):Void {
|
||||
@@ -57,14 +50,14 @@ class FlashConnection implements IConnection {
|
||||
var b = new flash.utils.ByteArray();
|
||||
socket.readBytes(b);
|
||||
var bs = Bytes.ofData(cast b);
|
||||
var packet = PacketBuilder.buildPacket(family, id);
|
||||
var packet = builder.buildPacket({family:family, id:id});
|
||||
packet.mergeFrom(bs);
|
||||
receive(packet);
|
||||
}
|
||||
|
||||
public function send(packet:Message):Void {
|
||||
override public function send(packet:Message):Void {
|
||||
L.d("Send", MessageUtils.toJson(packet));
|
||||
var meta = PacketBuilder.packetMeta(packet);
|
||||
var meta = builder.packetMeta(packet);
|
||||
socket.writeByte(meta.family);
|
||||
socket.writeByte(meta.id);
|
||||
var out = new BytesOutput();
|
||||
@@ -75,7 +68,9 @@ class FlashConnection implements IConnection {
|
||||
socket.flush();
|
||||
}
|
||||
|
||||
public dynamic function receive(packet:Message):Void {
|
||||
private function receive(packet:Message):Void {
|
||||
L.d("Receive", MessageUtils.toJson(packet));
|
||||
var name = Type.getClassName(Type.getClass(packet)).split("::")[1];
|
||||
L.d("xxx", name);
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,24 @@ package ru.m.armageddon.core.connect;
|
||||
import protohx.Message;
|
||||
|
||||
interface IConnection {
|
||||
|
||||
private var builder:IPacketBuilder;
|
||||
private var handler:IConnectionHandler;
|
||||
public function send(packet:Message):Void;
|
||||
}
|
||||
|
||||
public dynamic function onConnect(?event:Dynamic):Void;
|
||||
interface IConnectionHandler {
|
||||
public function onConnected():Void;
|
||||
public function onDisconnected():Void;
|
||||
public function onError(error:Dynamic):Void;
|
||||
public function onPacket(packet:Message):Void;
|
||||
}
|
||||
|
||||
public dynamic function receive(packet:Message):Void;
|
||||
typedef PacketMeta = {
|
||||
var family:Int;
|
||||
var id:Int;
|
||||
}
|
||||
|
||||
interface IPacketBuilder {
|
||||
public function packetMeta(packet:Message):PacketMeta;
|
||||
public function buildPacket(meta:PacketMeta):Message;
|
||||
}
|
||||
@@ -1,24 +1,26 @@
|
||||
package ru.m.armageddon.core;
|
||||
package ru.m.armageddon.core.connect;
|
||||
|
||||
import ru.m.armageddon.core.connect.IConnection;
|
||||
import protohx.Message;
|
||||
import ru.m.armageddon.proto.LoginRequest;
|
||||
import ru.m.armageddon.proto.LoginResponse;
|
||||
import ru.m.armageddon.proto.ErrorResponse;
|
||||
|
||||
typedef PacketMeta = {
|
||||
var family:Int;
|
||||
var id:Int;
|
||||
}
|
||||
|
||||
class PacketBuilder {
|
||||
class PacketBuilder implements IPacketBuilder {
|
||||
|
||||
private static var MAP:Map<Int, Map<Int, Class<Message>>> = [
|
||||
0x01 => [
|
||||
0x0001 => LoginRequest,
|
||||
0x0002 => LoginResponse
|
||||
],
|
||||
0x10 => [
|
||||
0x0001 => ErrorResponse
|
||||
]
|
||||
];
|
||||
|
||||
public static function packetMeta(packet:Message):PacketMeta {
|
||||
public function new() {}
|
||||
|
||||
public function packetMeta(packet:Message):PacketMeta {
|
||||
for (family in MAP.keys()) {
|
||||
var subMap = MAP[family];
|
||||
for (id in subMap.keys()) {
|
||||
@@ -34,8 +36,8 @@ class PacketBuilder {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function buildPacket(family:Int, id:Int):Message {
|
||||
var packetClass = MAP[family][id];
|
||||
public function buildPacket(meta:PacketMeta):Message {
|
||||
var packetClass = MAP[meta.family][meta.id];
|
||||
return Type.createInstance(packetClass, []);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user