This commit is contained in:
2014-07-05 18:16:54 +04:00
parent 6cd5eaee75
commit e207de6eb6
5 changed files with 45 additions and 46 deletions

View File

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

View File

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

View File

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

View File

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

View File

@@ -35,12 +35,9 @@ class Session implements IConnectionHandler {
);
}
public function onPacket(packet:Message):Void {
if (Std.is(packet, LoginRequest)) {
var request = cast(packet, LoginRequest);
public function onLoginRequest(packet:LoginRequest):Void {
var db = new Db();
var userData = db.getUser(request.login, request.password);
var userData = db.getUser(packet.login, packet.password);
if (userData != null) {
var user = new User();
user.login = userData.login;
@@ -56,5 +53,8 @@ class Session implements IConnectionHandler {
connection.send(response);
}
}
public function onPacket(packet:Message):Void {
trace("Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop());
}
}