diff --git a/res/layout/main.json b/res/layout/main.json index 89cf194..10a4083 100755 --- a/res/layout/main.json +++ b/res/layout/main.json @@ -70,9 +70,23 @@ }, { - "id":"game", "type":"haxework.gui.SpriteView", + "id":"game", "type":"ru.m.armageddon.client.frames.GameFrame", "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xa0a0a0"}, - "pWidth":100, "pHeight":100 + "pWidth":100, "pHeight":100, + "views":[ + { + "id":"name", "type":"haxework.gui.LabelView", + "pWidth":100, "height":25, "text":"" + }, + { + "id":"logout", + "type":"haxework.gui.ButtonView", + "width":100, + "height":30, + "skin":{"type":"haxework.gui.skin.ButtonColorSkin"}, + "text":"Logout" + } + ] } ] } \ No newline at end of file diff --git a/src/client/haxe/ru/m/armageddon/client/Client.hx b/src/client/haxe/ru/m/armageddon/client/Client.hx index 0a4bf2a..1930999 100755 --- a/src/client/haxe/ru/m/armageddon/client/Client.hx +++ b/src/client/haxe/ru/m/armageddon/client/Client.hx @@ -13,7 +13,7 @@ import ru.m.armageddon.core.connect.flash.FlashConnection; import ru.m.armageddon.core.connect.IConnection; import haxework.log.TraceLogger; -class Client { +class Client implements IConnectionHandler { private static inline var TAG = "Armageddon"; @@ -34,8 +34,19 @@ class Client { Provider.setFactory(GameData, GameData); Provider.set(IFrameSwitcher, switcher); - Provider.set(IConnection, new FlashConnection("localhost", 5000)); + Provider.set(IConnection, new FlashConnection("localhost", 5000, this)); switcher.change(AuthFrame.ID); } + + public function onConnected():Void {} + + public function onDisconnected():Void { + switcher.change(AuthFrame.ID); + } + + public function onError(error:Dynamic):Void { + L.e(TAG, "", error); + switcher.change(AuthFrame.ID); + } } diff --git a/src/client/haxe/ru/m/armageddon/client/frames/GameFrame.hx b/src/client/haxe/ru/m/armageddon/client/frames/GameFrame.hx new file mode 100755 index 0000000..47861ee --- /dev/null +++ b/src/client/haxe/ru/m/armageddon/client/frames/GameFrame.hx @@ -0,0 +1,35 @@ +package ru.m.armageddon.client.frames; + +import ru.m.armageddon.core.connect.IConnection; +import haxework.gui.LabelView; +import haxework.gui.ButtonView; +import haxework.provider.Provider; +import ru.m.armageddon.client.data.GameData; +import haxework.gui.VGroupView; + +class GameFrame extends VGroupView { + + private static inline var TAG = "GameFrame"; + + public static inline var ID = "game"; + + public function new() { + super(); + } + + public function init():Void { + findViewById("logout", ButtonView).onPress = this; + } + + public function onShow():Void { + var person = Provider.get(GameData).person; + findViewById("name", LabelView).text = person.name; + } + + public function onPress(view:ButtonView):Void { + switch (view.id) { + case "logout": + Provider.get(IConnection).disconnect(); + } + } +} diff --git a/src/client/haxe/ru/m/armageddon/client/frames/PersonFrame.hx b/src/client/haxe/ru/m/armageddon/client/frames/PersonFrame.hx index 8bb6354..7c02d81 100755 --- a/src/client/haxe/ru/m/armageddon/client/frames/PersonFrame.hx +++ b/src/client/haxe/ru/m/armageddon/client/frames/PersonFrame.hx @@ -43,7 +43,7 @@ class PersonFrame extends HGroupView implements IPacketHandler implements ListVi public function onPersonSelectResponse(packet:PersonSelectResponse):Void { Provider.get(GameData).person = packet.person; - Provider.get(IFrameSwitcher).change("game"); + Provider.get(IFrameSwitcher).change(GameFrame.ID); } public function onPacket(packet:Message):Void {} diff --git a/src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx b/src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx index 2edac50..b0a6bb1 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx @@ -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()); } diff --git a/src/common/haxe/ru/m/armageddon/core/connect/IConnection.hx b/src/common/haxe/ru/m/armageddon/core/connect/IConnection.hx index f51dc0b..84bcf44 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/IConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/IConnection.hx @@ -12,8 +12,10 @@ interface IConnection { private var builder:IPacketBuilder; public function connect():ICallback; + public function disconnect():Void; public function send(packet:Message):Void; public function pushData(bytes:Bytes):Void; + private function receive(packet:Message):Void; } diff --git a/src/common/haxe/ru/m/armageddon/core/connect/PacketBuilder.hx b/src/common/haxe/ru/m/armageddon/core/connect/PacketBuilder.hx index 3998bd6..9cd0b27 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/PacketBuilder.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/PacketBuilder.hx @@ -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, []); } diff --git a/src/common/haxe/ru/m/armageddon/core/connect/neko/PacketQueue.hx b/src/common/haxe/ru/m/armageddon/core/connect/PacketQueue.hx similarity index 97% rename from src/common/haxe/ru/m/armageddon/core/connect/neko/PacketQueue.hx rename to src/common/haxe/ru/m/armageddon/core/connect/PacketQueue.hx index d685974..065b212 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/neko/PacketQueue.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/PacketQueue.hx @@ -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; diff --git a/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx b/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx index 668cc73..71da32a 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx @@ -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 { diff --git a/src/common/haxe/ru/m/armageddon/core/connect/neko/NekoConnection.hx b/src/common/haxe/ru/m/armageddon/core/connect/neko/NekoConnection.hx index 7ad04d7..5425130 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/neko/NekoConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/neko/NekoConnection.hx @@ -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 {