diff --git a/src/client/haxe/ru/m/armageddon/client/Client.hx b/src/client/haxe/ru/m/armageddon/client/Client.hx index 5f71c68..e5e8032 100755 --- a/src/client/haxe/ru/m/armageddon/client/Client.hx +++ b/src/client/haxe/ru/m/armageddon/client/Client.hx @@ -1,5 +1,7 @@ package ru.m.armageddon.client; +import ru.m.armageddon.proto.User; +import ru.m.armageddon.proto.LoginResponse; import protohx.Message; import haxe.crypto.Md5; import flash.Lib; @@ -21,6 +23,7 @@ class Client implements IConnectionHandler { private var connection:IConnection; + private var user:User; public function new() { connection = new FlashConnection("localhost", 5000, this); @@ -31,10 +34,11 @@ class Client implements IConnectionHandler { } public function onConnected():Void { - var request = new LoginRequest(); - request.login = "shmyga"; - request.password = Md5.encode("xkbp8jh9z2"); - connection.send(request); + connection.send( + new LoginRequest() + .setLogin("shmyga") + .setPassword(Md5.encode("xkbp8jh9z2")) + ); } public function onDisconnected():Void { @@ -45,7 +49,12 @@ class Client implements IConnectionHandler { } - public function onPacket(packet:Message):Void { + public function onLoginResponse(packet:LoginResponse):Void { + this.user = packet.user; + L.d(TAG, "Loginned: " + user.nickname); + } + public function onPacket(packet:Message):Void { + L.d(TAG, "Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop()); } } 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 355c6b7..51931ef 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx @@ -15,6 +15,16 @@ class BaseConnection implements IConnection { } public function pushData(bytes:Bytes):Void {} + public function send(packet:Message):Void {} - private function receive(packet:Message):Void {} + + private function receive(packet:Message):Void { + var name = "on" + Type.getClassName(Type.getClass(packet)).split(".").pop(); + var method = Reflect.field(handler, name); + if (method != null && Reflect.isFunction(method)) { + Reflect.callMethod(handler, method, [packet]); + } else { + handler.onPacket(packet); + } + } } \ No newline at end of file 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 634f491..c86cd0e 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 @@ -1,7 +1,6 @@ package ru.m.armageddon.core.connect.flash; import ru.m.armageddon.core.connect.IConnection.IConnectionHandler; -import protohx.MessageUtils; import flash.utils.Endian; import haxe.io.BytesOutput; import protohx.Message; @@ -12,8 +11,6 @@ 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 extends BaseConnection { @@ -56,7 +53,6 @@ class FlashConnection extends BaseConnection { } override public function send(packet:Message):Void { - L.d("Send", MessageUtils.toJson(packet)); var meta = builder.packetMeta(packet); socket.writeByte(meta.family); socket.writeByte(meta.id); @@ -67,11 +63,4 @@ class FlashConnection extends BaseConnection { socket.writeBytes(cast bytes.getData()); socket.flush(); } - - override private function receive(packet:Message):Void { - L.d("Receive", MessageUtils.toJson(packet)); - var name = Type.getClassName(Type.getClass(packet)).split(".").pop(); - L.d("xxx", name); - handler.onPacket(packet); - } } \ No newline at end of file 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 69b340c..c932bbf 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 @@ -1,7 +1,6 @@ package ru.m.armageddon.core.connect.neko; import haxe.io.BytesOutput; -import protohx.MessageUtils; import protohx.Message; import haxe.io.Bytes; import sys.net.Socket; @@ -35,7 +34,6 @@ class NekoConnection extends BaseConnection { } override public function send(packet:Message):Void { - trace("Send: " + MessageUtils.toJson(packet)); try { var meta = builder.packetMeta(packet); var b = new BytesOutput(); @@ -50,11 +48,4 @@ class NekoConnection extends BaseConnection { trace(e); } } - - override private function receive(packet:Message):Void { - trace("Receive", MessageUtils.toJson(packet)); - var name = Type.getClassName(Type.getClass(packet)).split(".").pop(); - trace("xxx", name); - handler.onPacket(packet); - } } \ No newline at end of file diff --git a/src/server/haxe/ru/m/armageddon/server/session/Session.hx b/src/server/haxe/ru/m/armageddon/server/session/Session.hx index a958956..d50d8d4 100755 --- a/src/server/haxe/ru/m/armageddon/server/session/Session.hx +++ b/src/server/haxe/ru/m/armageddon/server/session/Session.hx @@ -35,26 +35,26 @@ class Session implements IConnectionHandler { ); } - public function onPacket(packet:Message):Void { - if (Std.is(packet, LoginRequest)) { - var request = cast(packet, LoginRequest); - - var db = new Db(); - var userData = db.getUser(request.login, request.password); - if (userData != null) { - var user = new User(); - user.login = userData.login; - user.nickname = userData.login; - this.user = user; - var response = new LoginResponse(); - response.user = user; - connection.send(response); - } else { - var response = new ErrorResponse(); - response.code = 403; - response.message = "User not found"; - connection.send(response); - } + public function onLoginRequest(packet:LoginRequest):Void { + var db = new Db(); + var userData = db.getUser(packet.login, packet.password); + if (userData != null) { + var user = new User(); + user.login = userData.login; + user.nickname = userData.login; + this.user = user; + var response = new LoginResponse(); + response.user = user; + connection.send(response); + } else { + var response = new ErrorResponse(); + response.code = 403; + response.message = "User not found"; + connection.send(response); } } + + public function onPacket(packet:Message):Void { + trace("Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop()); + } } \ No newline at end of file