diff --git a/build/client.js b/build/client.js index b51b6e7..e338569 100755 --- a/build/client.js +++ b/build/client.js @@ -8,6 +8,7 @@ const Haxe = require('../tasks/haxe'); const FlashPlayer = require('../tasks/flashplayer'); const version = require('./version'); const prepare = require('./prepare'); +const generate = prepare.generate; const debug = require('../tasks/debug'); const webserver = require('gulp-webserver'); const run = require('gulp-run'); @@ -15,12 +16,6 @@ const tail = require('../tasks/tail'); const deb = require('gulp-debian'); -const generate = () => function generate() { - return new Haxe().haxelib([ - 'run', 'protohx', 'generate', 'protohx.json' - ]); -}; - const build = (platform, values) => function build() { const argv = yargs.argv; return gulp.src('.') @@ -131,10 +126,10 @@ const testLinux = gulp.series( -exports['client:flash:test'] = gulp.series(prepare(Haxe.ID, FlashPlayer.ID), testFlash); -exports['client:html5:test'] = gulp.series(prepare(Haxe.ID), testHtml5()); -exports['client:html5-dom:test'] = gulp.series(prepare(Haxe.ID), testHtml5(true)); -exports['client:linux:test'] = gulp.series(prepare(Haxe.ID), testLinux); +exports['client:flash:test'] = gulp.series(prepare(Haxe.ID, FlashPlayer.ID), generate(), testFlash); +exports['client:html5:test'] = gulp.series(prepare(Haxe.ID), generate(), testHtml5()); +exports['client:html5-dom:test'] = gulp.series(prepare(Haxe.ID), generate(), testHtml5(true)); +exports['client:linux:test'] = gulp.series(prepare(Haxe.ID), generate(), testLinux); exports['client:test'] = exports['client:flash:test']; diff --git a/build/prepare.js b/build/prepare.js index 91e0bf0..2603cb8 100755 --- a/build/prepare.js +++ b/build/prepare.js @@ -23,10 +23,16 @@ const prepare = (...targets) => function prepare() { return Promise.all(tasks); }; - const update = () => { return new Haxe().upgrade(); }; +const generate = () => function generate() { + return new Haxe().haxelib([ + 'run', 'protohx', 'generate', 'protohx.json' + ]); +}; + module.exports = prepare; module.exports.update = update; +module.exports.generate = generate; diff --git a/build/server.js b/build/server.js index d4fec9c..1ee668f 100755 --- a/build/server.js +++ b/build/server.js @@ -5,15 +5,10 @@ const FlashPlayer = require('../tasks/flashplayer'); const Neko = require('../tasks/neko'); const version = require('./version'); const prepare = require('./prepare'); +const generate = prepare.generate; const debug = require('../tasks/debug'); -const generate = () => function generate() { - return new Haxe().haxelib([ - 'run', 'protohx', 'generate', 'protohx.json' - ]); -}; - const build = () => function build() { const argv = yargs.argv; return gulp.src('.') @@ -25,7 +20,6 @@ const build = () => function build() { 'orm', 'haxework:git', 'haxe-crypto', - 'hxsignal' ], cp: [ 'src/common/haxe', diff --git a/package.json b/package.json index b84c8ee..6e48186 100755 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "haxework": "git@bitbucket.org:shmyga/haxework.git", "orm": "2.1.0", "yield": "1.1.2", - "haxe-crypto": "0.0.7", - "hxsignal": "0.9.2" + "haxe-crypto": "0.0.7" } } diff --git a/project.xml b/project.xml index e65b08c..dbf7251 100755 --- a/project.xml +++ b/project.xml @@ -13,7 +13,6 @@ - diff --git a/src/client/haxe/ru/m/tankz/network/NetworkManager.hx b/src/client/haxe/ru/m/tankz/network/NetworkManager.hx index 5854918..c0a7eef 100644 --- a/src/client/haxe/ru/m/tankz/network/NetworkManager.hx +++ b/src/client/haxe/ru/m/tankz/network/NetworkManager.hx @@ -1,8 +1,8 @@ package ru.m.tankz.network; import haxework.provider.Provider; -import hxsignal.impl.Signal1; import ru.m.connect.IConnection; +import ru.m.signal.Signal; import ru.m.tankz.proto.pack.LoginRequest; import ru.m.tankz.proto.pack.Request; import ru.m.tankz.proto.pack.Response; @@ -14,7 +14,7 @@ typedef ClientConnection = IConnection; class NetworkManager { public var state(default, null):String; - public var stateSignal:Signal1; + public var stateSignal:Signal; public var user(default, null):User; private var connection(get, never):ClientConnection; @@ -29,10 +29,10 @@ class NetworkManager { } public function new() { - stateSignal = new Signal1(); + stateSignal = new Signal(); updateState('offline'); connection.handler.connect(onConnectionEvent); - connection.packetHandler.connect(onResponse); + connection.receiveHandler.connect(onResponse); user = storage.read(); if (user == null) { user = {name: 'User', uuid: null}; @@ -58,7 +58,12 @@ class NetworkManager { } private function onConnectionEvent(event:ConnectionEvent):Void { - updateState(Std.string(event)); + L.d('Network', '${event}'); + updateState(switch (event) { + case ConnectionEvent.CONNECTED: 'connected'; + case ConnectionEvent.DISCONNECTED: 'offline'; + case ConnectionEvent.ERROR(error): 'error'; + }); } private function onResponse(packet:Response):Void { diff --git a/src/common/haxe/ru/m/connect/BaseConnection.hx b/src/common/haxe/ru/m/connect/BaseConnection.hx index a96e31e..31c19d7 100755 --- a/src/common/haxe/ru/m/connect/BaseConnection.hx +++ b/src/common/haxe/ru/m/connect/BaseConnection.hx @@ -1,16 +1,17 @@ package ru.m.connect; -import promhx.Deferred; -import hxsignal.impl.Signal1; import haxe.io.Bytes; +import promhx.Deferred; import promhx.Promise; import protohx.Message; import ru.m.connect.IConnection; +import ru.m.signal.Signal; class BaseConnection implements IConnection { - public var handler(default, default):Signal1; - public var packetHandler(default, default):Signal1; + public var handler(default, null):Signal; + public var sendHandler(default, null):Signal; + public var receiveHandler(default, null):Signal; public var connected(default, null):Bool; public var queue(default, null):PacketQueue; @@ -18,8 +19,9 @@ class BaseConnection implements IConnection { public function new(i:Class) { queue = new PacketQueue(i); - handler = new Signal1(); - packetHandler = new Signal1(); + handler = new Signal(); + sendHandler = new Signal(); + receiveHandler = new Signal(); connectDeferred = new Deferred(); } @@ -32,14 +34,21 @@ class BaseConnection implements IConnection { } public function pushData(bytes:Bytes):Void { + #if proto_debug L.d('Proto', 'pushData: ${bytes.length}'); #end queue.addBytes(bytes); while (queue.hasMsg()) { var packet:I = queue.popMsg(); - packetHandler.emit(packet); + receive(packet); } } public function send(packet:O):Void { - #if proto_debug L.d("Send", Type.getClassName(Type.getClass(packet)).split(".").pop()); #end + #if proto_debug L.d('Proto', 'send: ${packet}'); #end + sendHandler.emit(packet); + } + + public function receive(packet:I):Void { + #if proto_debug L.d('Proto', 'receive: ${packet}'); #end + receiveHandler.emit(packet); } } \ No newline at end of file diff --git a/src/common/haxe/ru/m/connect/IConnection.hx b/src/common/haxe/ru/m/connect/IConnection.hx index f4b9472..a2dbdd7 100755 --- a/src/common/haxe/ru/m/connect/IConnection.hx +++ b/src/common/haxe/ru/m/connect/IConnection.hx @@ -1,9 +1,9 @@ package ru.m.connect; import haxe.io.Bytes; -import hxsignal.impl.Signal1; import promhx.Promise; import protohx.Message; +import ru.m.signal.Signal; enum ConnectionEvent { @@ -14,8 +14,9 @@ enum ConnectionEvent { interface IConnection { public var connected(default, null):Bool; - public var handler(default, null):Signal1; - public var packetHandler(default, null):Signal1; + public var handler(default, null):Signal; + public var sendHandler(default, null):Signal; + public var receiveHandler(default, null):Signal; public function connect():Promise>; public function disconnect():Void; diff --git a/src/common/haxe/ru/m/connect/PacketQueue.hx b/src/common/haxe/ru/m/connect/PacketQueue.hx index 61ba56b..4c893e4 100755 --- a/src/common/haxe/ru/m/connect/PacketQueue.hx +++ b/src/common/haxe/ru/m/connect/PacketQueue.hx @@ -50,7 +50,7 @@ class PacketQueue { bi.bigEndian = false; while (available > 0) { var packetSize = bi.readUInt16(); - available -= 1; + available -= 2; if (packetSize <= available) { var msgBytes = bi.read(packetSize); var packet:P = Type.createInstance(packetClass, []); diff --git a/src/common/haxe/ru/m/connect/flash/FlashConnection.hx b/src/common/haxe/ru/m/connect/flash/FlashConnection.hx index 0590e1f..70bffcd 100755 --- a/src/common/haxe/ru/m/connect/flash/FlashConnection.hx +++ b/src/common/haxe/ru/m/connect/flash/FlashConnection.hx @@ -32,6 +32,7 @@ class FlashConnection extends BaseConnection { socket.addEventListener(Event.CONNECT, onConnect); socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData); socket.endian = Endian.LITTLE_ENDIAN; + sendHandler.connect(_send); } override public function connect():Promise> { @@ -77,8 +78,7 @@ class FlashConnection extends BaseConnection { } } - override public function send(packet:O):Void { - super.send(packet); + private function _send(packet:O):Void { var out = new BytesOutput(); packet.writeTo(out); var bytes = out.getBytes(); diff --git a/src/common/haxe/ru/m/connect/neko/NekoConnection.hx b/src/common/haxe/ru/m/connect/neko/NekoConnection.hx index 9f36189..b6b31b7 100755 --- a/src/common/haxe/ru/m/connect/neko/NekoConnection.hx +++ b/src/common/haxe/ru/m/connect/neko/NekoConnection.hx @@ -12,13 +12,13 @@ class NekoConnection extends BaseConnection { public function new(socket:Socket, i:Class) { super(i); this.socket = socket; - socket.setFastSend(true); - socket.output.bigEndian = false; - socket.input.bigEndian = false; + //socket.setFastSend(true); + //socket.output.bigEndian = false; + //socket.input.bigEndian = false; + sendHandler.connect(_send); } - override public function send(packet:O):Void { - super.send(packet); + private function _send(packet:O):Void { try { var b = new BytesOutput(); packet.writeTo(b); @@ -26,9 +26,8 @@ class NekoConnection extends BaseConnection { socket.output.writeUInt16(bytes.length); socket.output.write(bytes); socket.output.flush(); - } catch (e:Dynamic) { - trace("Error send packet: " + Type.getClassName(Type.getClass(packet))); - trace(e); + } catch (error:Dynamic) { + L.e('Proto', 'Error send packet: ${packet}', error); } } } \ No newline at end of file diff --git a/src/common/haxe/ru/m/connect/neko/NekoWebConnection.hx b/src/common/haxe/ru/m/connect/neko/NekoWebConnection.hx index 46c5405..cb67524 100644 --- a/src/common/haxe/ru/m/connect/neko/NekoWebConnection.hx +++ b/src/common/haxe/ru/m/connect/neko/NekoWebConnection.hx @@ -16,13 +16,12 @@ class NekoWebConnection extends NekoConnection { opened = false; } - override public function send(packet:O):Void { - #if proto_debug L.d("Send", Type.getClassName(Type.getClass(packet)).split(".").pop()); #end + override private function _send(packet:O):Void { try { var data = WebSocketTools.packet2string(packet); writeData(data, socket); - } catch (e:Dynamic) { - trace(e); + } catch (error:Dynamic) { + L.e('Proto', 'Error send packet: ${packet}', error); } } @@ -39,7 +38,7 @@ class NekoWebConnection extends NekoConnection { var data = parseData(bytes); if (data != null) { var packet:I = WebSocketTools.string2packet(data, queue.packetClass); - packetHandler.emit(packet); + receive(packet); } } } diff --git a/src/common/haxe/ru/m/signal/Signal.hx b/src/common/haxe/ru/m/signal/Signal.hx new file mode 100644 index 0000000..a60692a --- /dev/null +++ b/src/common/haxe/ru/m/signal/Signal.hx @@ -0,0 +1,27 @@ +package ru.m.signal; + + +typedef Receiver = T -> Void; + +class Signal { + + private var receivers:Array>; + + public function new() { + receivers = []; + } + + public function connect(receiver:Receiver):Void { + receivers.push(receiver); + } + + public function disconnect(receiver:Receiver):Void { + receivers.remove(receiver); + } + + public function emit(value:T):Void { + for (receiver in receivers) { + receiver(value); + } + } +} diff --git a/src/server/haxe/ru/m/tankz/server/Server.hx b/src/server/haxe/ru/m/tankz/server/Server.hx index 7dc8ed3..301d652 100755 --- a/src/server/haxe/ru/m/tankz/server/Server.hx +++ b/src/server/haxe/ru/m/tankz/server/Server.hx @@ -1,11 +1,11 @@ package ru.m.tankz.server; -import ru.m.connect.IConnection.ConnectionEvent; -import haxework.log.TraceLogger; -import ru.m.tankz.server.session.Session; import haxe.io.Bytes; -import sys.net.Socket; +import haxework.log.TraceLogger; import neko.net.ThreadServer; +import ru.m.connect.IConnection.ConnectionEvent; +import ru.m.tankz.server.session.Session; +import sys.net.Socket; #if debug import haxework.log.SocketLogger; #end @@ -33,7 +33,11 @@ class Server extends ThreadServer { } override public function clientMessage(session:Session, bytes:Bytes) { - session.pushData(bytes); + try { + session.pushData(bytes); + } catch (error:Dynamic) { + L.d(TAG, "_", error); + } } public static function main() { diff --git a/src/server/haxe/ru/m/tankz/server/session/Session.hx b/src/server/haxe/ru/m/tankz/server/session/Session.hx index f2d81a5..f9feba0 100755 --- a/src/server/haxe/ru/m/tankz/server/session/Session.hx +++ b/src/server/haxe/ru/m/tankz/server/session/Session.hx @@ -46,7 +46,7 @@ class Session { connection = new NekoConnection(socket, Request); } connection.handler.connect(onConnectionEvent); - connection.packetHandler.connect(onRequest); + connection.receiveHandler.connect(onRequest); connection.pushData(bytes); } } @@ -109,8 +109,4 @@ class Session { public function onGameActionRequest(packet:GameActionRequest):Void { GameManager.byPersonId.get(person.id).action(person, packet); }*/ - - public function onPacket(packet:Message):Void { - trace("Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop()); - } } \ No newline at end of file