-
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package ru.m.armageddon.core.connect;
|
||||
|
||||
import flash.utils.Endian;
|
||||
import haxe.io.BytesOutput;
|
||||
import protohx.Message;
|
||||
import haxe.io.Bytes;
|
||||
@@ -14,7 +15,7 @@ class FlashConnection implements IConnection {
|
||||
|
||||
private static var MAP:Map<Int, Map<Int, Class<Message>>> = [
|
||||
0x01 => [
|
||||
0x0001 => LoginRequest,
|
||||
0x0003 => LoginRequest,
|
||||
0x0002 => LoginResponse
|
||||
]
|
||||
];
|
||||
@@ -29,7 +30,7 @@ class FlashConnection implements IConnection {
|
||||
socket.addEventListener(Event.CLOSE, onClose);
|
||||
socket.addEventListener(Event.CONNECT, onConnect);
|
||||
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
|
||||
//socket.endian = flash.utils.Endian.LITTLE_ENDIAN;
|
||||
socket.endian = Endian.LITTLE_ENDIAN;
|
||||
|
||||
socket.connect(host, port);
|
||||
}
|
||||
@@ -49,6 +50,7 @@ class FlashConnection implements IConnection {
|
||||
private function onSocketData(_):Void {
|
||||
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);
|
||||
@@ -64,11 +66,13 @@ class FlashConnection implements IConnection {
|
||||
}
|
||||
|
||||
public function send(packet:Message):Void {
|
||||
L.d("XXX", "send:" + packet);
|
||||
for (family in MAP.keys()) {
|
||||
var subMap = MAP[family];
|
||||
for (id in MAP.keys()) {
|
||||
for (id in subMap.keys()) {
|
||||
var packetClass = subMap[id];
|
||||
if (Std.is(packet, packetClass)) {
|
||||
L.d("XXX", family + ":" + id);
|
||||
socket.writeByte(family);
|
||||
socket.writeByte(id);
|
||||
var out = new BytesOutput();
|
||||
@@ -83,6 +87,6 @@ class FlashConnection implements IConnection {
|
||||
}
|
||||
|
||||
public dynamic function receive(packet:Message):Void {
|
||||
L.d("Receive", packet + "");
|
||||
L.d("Receive", protohx.MessageUtils.toJson(packet));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
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<Int, Map<Int, Class<Message>>> = [
|
||||
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<sys.net.Socket>;
|
||||
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 + "");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user