diff --git a/.gitignore b/.gitignore index 32437c4..d6f5527 100755 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ out/ *.iml *.ipr *.iws -*.ids \ No newline at end of file +*.ids +*.stackdump \ 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 9d14dfa..5f71c68 100755 --- a/src/client/haxe/ru/m/armageddon/client/Client.hx +++ b/src/client/haxe/ru/m/armageddon/client/Client.hx @@ -4,7 +4,7 @@ import protohx.Message; import haxe.crypto.Md5; import flash.Lib; import flash.events.MouseEvent; -import ru.m.armageddon.core.connect.FlashConnection; +import ru.m.armageddon.core.connect.flash.FlashConnection; import ru.m.armageddon.core.connect.IConnection; import haxework.log.TraceLogger; import ru.m.armageddon.proto.LoginRequest; 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 fb4aaad..355c6b7 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx @@ -1,5 +1,6 @@ package ru.m.armageddon.core.connect; +import haxe.io.Bytes; import protohx.Message; import ru.m.armageddon.core.connect.IConnection; @@ -13,5 +14,7 @@ class BaseConnection implements IConnection { this.handler = handler; } + public function pushData(bytes:Bytes):Void {} public function send(packet:Message):Void {} + private function receive(packet:Message):Void {} } \ No newline at end of file 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 d882106..d71edf2 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/IConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/IConnection.hx @@ -1,11 +1,14 @@ package ru.m.armageddon.core.connect; +import haxe.io.Bytes; import protohx.Message; interface IConnection { private var builder:IPacketBuilder; private var handler:IConnectionHandler; public function send(packet:Message):Void; + public function pushData(bytes:Bytes):Void; + private function receive(packet:Message):Void; } interface IConnectionHandler { diff --git a/src/common/haxe/ru/m/armageddon/core/connect/NekoConnection.hx b/src/common/haxe/ru/m/armageddon/core/connect/NekoConnection.hx deleted file mode 100755 index 7c1e4d6..0000000 --- a/src/common/haxe/ru/m/armageddon/core/connect/NekoConnection.hx +++ /dev/null @@ -1,99 +0,0 @@ -package ru.m.armageddon.core.connect; - -import sys.net.Socket; -import haxe.io.BytesOutput; -import protohx.Message; -import haxe.io.Bytes; - -class NekoConnection implements IConnection { - - private static var MAP:Map>> = [ - 0x01 => [ - 0x0001 = > LoginRequest, - 0x0002 = > LoginResponse - ] - ]; - - private var socket:Socket; - - public function new(host, port) { - this.onConnect = onConnect; - this.addBytes = addBytes; - this.onClose = onClose; - try { - socket.connect(new sys.net.Host(host), port); - } catch (e:Dynamic) { - trace(e); - //onClose(); - return; - } - onConnect(); - - var buffer = Bytes.alloc(1024); - var socks = [socket]; - var timer = new haxe.Timer(100); - timer.run = function() { - try { - var r:Array; - do { - r = sys.net.Socket.select(socks, null, null, 0.001).read; - for (s in r) { - var size = s.input.readBytes(buffer, 0, buffer.length); - onSocketData(buffer.sub(0, size)); - } - } while (r.length > 0); - } catch (e:haxe.io.Eof) { - timer.stop(); - //onClose(); - socket.close(); - } catch (e:Dynamic) { - trace(e); - //onClose(); - } - }; - } - - public dynamic function onConnect():Void { - - } - - private function onSocketData(data):Void { - var family = socket.readByte(); - var id = socket.readByte(); - var b = new flash.utils.ByteArray(); - socket.readBytes(b); - var bs = Bytes.ofData(cast b); - var packet = buildPacket(family, id); - packet.mergeFrom(bs); - receive(packet); - } - - - private function buildPacket(family:Int, id:Int):Message { - var packetClass = MAP[family][id]; - return Type.createInstance(packetClass, []); - } - - public function send(packet:Message):Void { - for (family in MAP.keys()) { - var subMap = MAP[family]; - for (id in MAP.keys()) { - var packetClass = subMap[id]; - if (Std.is(packet, packetClass)) { - socket.output.writeByte(family); - socket.output.writeByte(id); - var b = new BytesOutput(); - packet.writeTo(b); - var bytes = b.getBytes(); - socket.output.writeUInt16(bytes.length); - socket.output.writeBytes(bytes, 0, bytes.length); - socket.output.flush(); - } - } - } - } - - public dynamic function receive(packet:Message):Void { - L.d("Receive", packet + ""); - } -} \ No newline at end of file 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 6ef1300..228a25c 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/PacketBuilder.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/PacketBuilder.hx @@ -9,12 +9,12 @@ import ru.m.armageddon.proto.ErrorResponse; class PacketBuilder implements IPacketBuilder { private static var MAP:Map>> = [ + 0x00 => [ + 0x0001 => ErrorResponse + ], 0x01 => [ 0x0001 => LoginRequest, 0x0002 => LoginResponse - ], - 0x10 => [ - 0x0001 => ErrorResponse ] ]; diff --git a/src/common/haxe/ru/m/armageddon/core/connect/FlashConnection.hx b/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx similarity index 91% rename from src/common/haxe/ru/m/armageddon/core/connect/FlashConnection.hx rename to src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx index 567727d..634f491 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/FlashConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx @@ -1,4 +1,4 @@ -package ru.m.armageddon.core.connect; +package ru.m.armageddon.core.connect.flash; import ru.m.armageddon.core.connect.IConnection.IConnectionHandler; import protohx.MessageUtils; @@ -68,9 +68,10 @@ class FlashConnection extends BaseConnection { socket.flush(); } - private function receive(packet:Message):Void { + override private function receive(packet:Message):Void { L.d("Receive", MessageUtils.toJson(packet)); - var name = Type.getClassName(Type.getClass(packet)).split("::")[1]; + 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 new file mode 100755 index 0000000..69b340c --- /dev/null +++ b/src/common/haxe/ru/m/armageddon/core/connect/neko/NekoConnection.hx @@ -0,0 +1,60 @@ +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; +import ru.m.armageddon.core.connect.IConnection.IConnectionHandler; + +class NekoConnection extends BaseConnection { + + public var queue(default, null):PacketQueue; + + private var socket:Socket; + + public function new(socket:Socket, handler:IConnectionHandler) { + super(handler); + this.socket = socket; + 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) { + handler.onError(error); + } + } + } + + override public function send(packet:Message):Void { + trace("Send: " + MessageUtils.toJson(packet)); + try { + var meta = builder.packetMeta(packet); + var b = new BytesOutput(); + packet.writeTo(b); + var bytes = b.getBytes(); + socket.output.writeByte(meta.family); + socket.output.writeByte(meta.id); + socket.output.writeUInt16(bytes.length); + socket.output.write(bytes); + socket.output.flush(); + } catch (e:Dynamic) { + 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/PacketQueue.hx b/src/common/haxe/ru/m/armageddon/core/connect/neko/PacketQueue.hx similarity index 97% rename from src/server/haxe/ru/m/armageddon/server/session/PacketQueue.hx rename to src/common/haxe/ru/m/armageddon/core/connect/neko/PacketQueue.hx index 66e2174..d685974 100755 --- a/src/server/haxe/ru/m/armageddon/server/session/PacketQueue.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/neko/PacketQueue.hx @@ -1,4 +1,4 @@ -package ru.m.armageddon.server.session; +package ru.m.armageddon.core.connect.neko; import ru.m.armageddon.core.connect.IConnection.IPacketBuilder; import protohx.Message; diff --git a/src/server/haxe/ru/m/armageddon/server/Server.hx b/src/server/haxe/ru/m/armageddon/server/Server.hx index 1d34d17..3de4391 100755 --- a/src/server/haxe/ru/m/armageddon/server/Server.hx +++ b/src/server/haxe/ru/m/armageddon/server/Server.hx @@ -13,13 +13,13 @@ class Server extends ThreadServer { } override function clientConnected(s:Socket):Session { - var session = new Session(s, null); - Lib.println("client: " + session.id + " / " + s.peer()); + var session = new Session(s); + Lib.println("client: " + session.user + " / " + s.peer()); return session; } override function clientDisconnected(session:Session) { - Lib.println("client " + Std.string(session.id) + " disconnected"); + Lib.println("client " + Std.string(session.user) + " disconnected"); } override function readClientMessage(session:Session, buf:Bytes, pos:Int, len:Int) { @@ -27,7 +27,7 @@ class Server extends ThreadServer { } override function clientMessage(session:Session, bytes:Bytes) { - session.pushData(bytes); + session.connection.pushData(bytes); } public static function main() { 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 01031fd..a958956 100755 --- a/src/server/haxe/ru/m/armageddon/server/session/Session.hx +++ b/src/server/haxe/ru/m/armageddon/server/session/Session.hx @@ -1,60 +1,41 @@ package ru.m.armageddon.server.session; -import ru.m.armageddon.core.connect.BaseConnection; +import ru.m.armageddon.core.connect.neko.NekoConnection; import ru.m.armageddon.proto.ErrorResponse; import ru.m.armageddon.server.db.Db; import ru.m.armageddon.proto.User; import ru.m.armageddon.proto.LoginResponse; import ru.m.armageddon.proto.LoginRequest; -import protohx.MessageUtils; -import haxe.io.Bytes; import protohx.Message; import ru.m.armageddon.core.connect.IConnection; -import haxe.io.BytesOutput; import sys.net.Socket; -class Session extends BaseConnection { +class Session implements IConnectionHandler { - public var id:Int; - public var socket:Socket; - public var queue:PacketQueue; + public var user(default, null):User; + public var connection(default, null):IConnection; - public function new(socket:Socket, handler:IConnectionHandler) { - super(handler); - this.socket = socket; - socket.setFastSend(true); - socket.output.bigEndian = false; - socket.input.bigEndian = false; - queue = new PacketQueue(builder); + public function new(socket:Socket) { + connection = new NekoConnection(socket, this); } - public function pushData(bytes:Bytes):Void { - queue.addBytes(bytes); - while (queue.hasMsg()) { - var packet:Message = queue.popMsg(); - receive(packet); - } + public function onConnected():Void { + } - override public function send(packet:Message):Void { - trace("Send: " + MessageUtils.toJson(packet)); - try { - var meta = builder.packetMeta(packet); - var b = new BytesOutput(); - packet.writeTo(b); - var bytes = b.getBytes(); - socket.output.writeByte(meta.family); - socket.output.writeByte(meta.id); - socket.output.writeUInt16(bytes.length); - socket.output.write(bytes); - socket.output.flush(); - } catch (e:Dynamic) { - trace(e); - } + public function onDisconnected():Void { + } - private function receive(packet:Message):Void { - trace("Receive: " + MessageUtils.toJson(packet)); + public function onError(error:Dynamic):Void { + connection.send( + new ErrorResponse() + .setCode(0) + .setMessage(Std.string(error)) + ); + } + + public function onPacket(packet:Message):Void { if (Std.is(packet, LoginRequest)) { var request = cast(packet, LoginRequest); @@ -64,14 +45,15 @@ class Session extends BaseConnection { var user = new User(); user.login = userData.login; user.nickname = userData.login; + this.user = user; var response = new LoginResponse(); response.user = user; - send(response); + connection.send(response); } else { var response = new ErrorResponse(); response.code = 403; response.message = "User not found"; - send(response); + connection.send(response); } } }