This commit is contained in:
2014-07-07 12:35:20 +04:00
parent 4dfe7b64c4
commit 9e4b7b0556
10 changed files with 101 additions and 33 deletions

View File

@@ -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"
}
]
}
]
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}
}

View File

@@ -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 {}

View File

@@ -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());
}

View File

@@ -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;
}

View File

@@ -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, []);
}

View File

@@ -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;

View File

@@ -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 {

View File

@@ -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 {