-
This commit is contained in:
@@ -22,7 +22,7 @@ class Client {
|
|||||||
public function new() {
|
public function new() {
|
||||||
connection = new FlashConnection("localhost", 5000, onConnect);
|
connection = new FlashConnection("localhost", 5000, onConnect);
|
||||||
|
|
||||||
flash.Lib.current.addEventListener(MouseEvent.CLICK, function(_) {
|
flash.Lib.current.stage.addEventListener(MouseEvent.CLICK, function(_) {
|
||||||
onConnect(_);
|
onConnect(_);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.armageddon.core.connect;
|
package ru.m.armageddon.core.connect;
|
||||||
|
|
||||||
|
import flash.utils.Endian;
|
||||||
import haxe.io.BytesOutput;
|
import haxe.io.BytesOutput;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
import haxe.io.Bytes;
|
import haxe.io.Bytes;
|
||||||
@@ -14,7 +15,7 @@ class FlashConnection implements IConnection {
|
|||||||
|
|
||||||
private static var MAP:Map<Int, Map<Int, Class<Message>>> = [
|
private static var MAP:Map<Int, Map<Int, Class<Message>>> = [
|
||||||
0x01 => [
|
0x01 => [
|
||||||
0x0001 => LoginRequest,
|
0x0003 => LoginRequest,
|
||||||
0x0002 => LoginResponse
|
0x0002 => LoginResponse
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
@@ -29,7 +30,7 @@ class FlashConnection implements IConnection {
|
|||||||
socket.addEventListener(Event.CLOSE, onClose);
|
socket.addEventListener(Event.CLOSE, onClose);
|
||||||
socket.addEventListener(Event.CONNECT, onConnect);
|
socket.addEventListener(Event.CONNECT, onConnect);
|
||||||
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
|
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
|
||||||
//socket.endian = flash.utils.Endian.LITTLE_ENDIAN;
|
socket.endian = Endian.LITTLE_ENDIAN;
|
||||||
|
|
||||||
socket.connect(host, port);
|
socket.connect(host, port);
|
||||||
}
|
}
|
||||||
@@ -49,6 +50,7 @@ class FlashConnection implements IConnection {
|
|||||||
private function onSocketData(_):Void {
|
private function onSocketData(_):Void {
|
||||||
var family = socket.readByte();
|
var family = socket.readByte();
|
||||||
var id = socket.readByte();
|
var id = socket.readByte();
|
||||||
|
var length = socket.readShort();
|
||||||
var b = new flash.utils.ByteArray();
|
var b = new flash.utils.ByteArray();
|
||||||
socket.readBytes(b);
|
socket.readBytes(b);
|
||||||
var bs = Bytes.ofData(cast b);
|
var bs = Bytes.ofData(cast b);
|
||||||
@@ -64,11 +66,13 @@ class FlashConnection implements IConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function send(packet:Message):Void {
|
public function send(packet:Message):Void {
|
||||||
|
L.d("XXX", "send:" + packet);
|
||||||
for (family in MAP.keys()) {
|
for (family in MAP.keys()) {
|
||||||
var subMap = MAP[family];
|
var subMap = MAP[family];
|
||||||
for (id in MAP.keys()) {
|
for (id in subMap.keys()) {
|
||||||
var packetClass = subMap[id];
|
var packetClass = subMap[id];
|
||||||
if (Std.is(packet, packetClass)) {
|
if (Std.is(packet, packetClass)) {
|
||||||
|
L.d("XXX", family + ":" + id);
|
||||||
socket.writeByte(family);
|
socket.writeByte(family);
|
||||||
socket.writeByte(id);
|
socket.writeByte(id);
|
||||||
var out = new BytesOutput();
|
var out = new BytesOutput();
|
||||||
@@ -83,6 +87,6 @@ class FlashConnection implements IConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public dynamic function receive(packet:Message):Void {
|
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 + "");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,75 +1,134 @@
|
|||||||
package ru.m.armageddon.server;
|
package ru.m.armageddon.server;
|
||||||
|
|
||||||
import haxe.io.BytesOutput;
|
import haxe.io.BytesOutput;
|
||||||
|
import haxe.io.BytesInput;
|
||||||
import sys.db.Connection;
|
import sys.db.Connection;
|
||||||
import sys.db.Mysql;
|
import sys.db.Mysql;
|
||||||
|
|
||||||
import neko.Lib;
|
import neko.Lib;
|
||||||
|
import haxe.io.Bytes;
|
||||||
import sys.net.Socket;
|
import sys.net.Socket;
|
||||||
import neko.net.ThreadServer;
|
import neko.net.ThreadServer;
|
||||||
import haxe.io.Bytes;
|
|
||||||
|
|
||||||
typedef Client = {
|
/*class NativeSession extends Session {
|
||||||
var id:Int;
|
|
||||||
var socket:Socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef Message = {
|
public var socket:Socket;
|
||||||
var str:String;
|
|
||||||
}
|
|
||||||
|
|
||||||
class Server extends ThreadServer<Client, Message> {
|
public function new(socket:Socket) {
|
||||||
// create a Client
|
super();
|
||||||
override function clientConnected(s:Socket):Client {
|
this.socket = socket;
|
||||||
s.input.bigEndian = false;
|
socket.setFastSend(true);
|
||||||
s.output.bigEndian = false;
|
socket.output.bigEndian = false;
|
||||||
var num = Std.random(100);
|
socket.input.bigEndian = false;
|
||||||
Lib.println("client " + num + " is " + s.peer());
|
|
||||||
return { id: num, socket: s };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override function close():Void {
|
||||||
|
socket.close();
|
||||||
override function clientDisconnected(c:Client) {
|
|
||||||
Lib.println("client " + Std.string(c.id) + " disconnected");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override function readClientMessage(c:Client, buf:Bytes, pos:Int, len:Int) {
|
public override function bakeMsg(msg:protohx.Message):BakedMsg {
|
||||||
Lib.println("client " + Std.string(c.id) + " message");
|
return new BakedMsg(msg);
|
||||||
|
}
|
||||||
|
|
||||||
var family = buf.get(0);
|
public override function writeMsgBaked(msg:BakedMsg):Void {
|
||||||
var id = buf.get(1);
|
writeMsg(msg.msg);
|
||||||
Lib.println(family + " - " + id);
|
}
|
||||||
|
|
||||||
|
public override function writeMsg(msg:protohx.Message):Void {
|
||||||
|
try {
|
||||||
|
var bytes = msgToBytes(msg);
|
||||||
|
socket.output.writeUInt16(bytes.length);
|
||||||
|
socket.output.write(bytes);
|
||||||
|
socket.output.flush();
|
||||||
|
} catch (e:Dynamic) {
|
||||||
|
trace(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function msgToBytes(msg:protohx.Message):haxe.io.Bytes {
|
||||||
|
var b = new BytesOutput();
|
||||||
|
msg.writeTo(b);
|
||||||
|
return b.getBytes();
|
||||||
|
}
|
||||||
|
|
||||||
|
}*/
|
||||||
|
|
||||||
|
class Session {
|
||||||
|
|
||||||
|
public var id:Int;
|
||||||
|
public var socket:Socket;
|
||||||
|
|
||||||
|
public function new(socket:Socket) {
|
||||||
|
this.socket = socket;
|
||||||
|
socket.setFastSend(true);
|
||||||
|
socket.output.bigEndian = false;
|
||||||
|
socket.input.bigEndian = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Server extends ThreadServer<Session, Bytes> {
|
||||||
|
|
||||||
|
//var sr:SessionRegistry;
|
||||||
|
|
||||||
|
public function new() {
|
||||||
|
super();
|
||||||
|
//sr = new SessionRegistry();
|
||||||
|
}
|
||||||
|
|
||||||
|
override function clientConnected(s:Socket):Session {
|
||||||
|
var session = new Session(s);
|
||||||
|
//sr.sessionConnect(session);
|
||||||
|
Lib.println("client: " + session.id + " / " + s.peer());
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
|
override function clientDisconnected(session:Session) {
|
||||||
|
Lib.println("client " + Std.string(session.id) + " disconnected");
|
||||||
|
//sr.sessionDisconnect(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
override function readClientMessage(session:Session, buf:Bytes, pos:Int, len:Int) {
|
||||||
|
// trace("data " + buf.length + ":" + pos + ":" + len);
|
||||||
|
return {msg: buf.sub(pos, len), bytes: len};
|
||||||
|
}
|
||||||
|
|
||||||
|
override function clientMessage(session:Session, bytes:Bytes) {
|
||||||
|
var available = bytes.length;
|
||||||
|
var bi = new BytesInput(bytes);
|
||||||
|
bi.bigEndian = false;
|
||||||
|
var family = bi.readByte();
|
||||||
|
var id = bi.readByte();
|
||||||
|
trace("Packet("+family+","+id+")");
|
||||||
|
var packetSize = bi.readUInt16();
|
||||||
|
available -= 2;
|
||||||
|
available -= 2;
|
||||||
|
|
||||||
|
var msgBytes = bi.read(packetSize);
|
||||||
|
|
||||||
var request = new LoginRequest();
|
var request = new LoginRequest();
|
||||||
request.mergeFrom(buf);
|
request.mergeFrom(msgBytes);
|
||||||
Lib.println(request.login);
|
|
||||||
Lib.println(request.password);
|
trace(request);
|
||||||
|
//sr.sessionData(session, bytes);
|
||||||
|
|
||||||
var db = new Db();
|
var db = new Db();
|
||||||
var data = db.getUser();
|
var userData = db.getUser();
|
||||||
|
|
||||||
var user = new User();
|
|
||||||
user.login = data.login;
|
|
||||||
user.nickname = data.login;
|
|
||||||
|
|
||||||
var response = new LoginResponse();
|
var response = new LoginResponse();
|
||||||
|
var user = new User();
|
||||||
|
user.login = userData.login;
|
||||||
|
user.nickname = userData.login;
|
||||||
response.user = user;
|
response.user = user;
|
||||||
|
|
||||||
|
var socket = session.socket;
|
||||||
var b = new BytesOutput();
|
var b = new BytesOutput();
|
||||||
response.writeTo(b);
|
response.writeTo(b);
|
||||||
var bytes = b.getBytes();
|
var bytes = b.getBytes();
|
||||||
c.socket.output.writeUInt16(bytes.length);
|
|
||||||
c.socket.output.writeBytes(bytes, 0, bytes.length);
|
|
||||||
|
|
||||||
return {
|
|
||||||
msg : null,
|
|
||||||
bytes : len
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
override function clientMessage(c:Client, msg:Message) {
|
|
||||||
|
|
||||||
|
socket.output.writeByte(1);
|
||||||
|
socket.output.writeByte(2);
|
||||||
|
socket.output.writeUInt16(bytes.length);
|
||||||
|
socket.output.write(bytes);
|
||||||
|
socket.output.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function main() {
|
public static function main() {
|
||||||
@@ -78,6 +137,7 @@ class Server extends ThreadServer<Client, Message> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class Db {
|
class Db {
|
||||||
|
|
||||||
private var db:Connection;
|
private var db:Connection;
|
||||||
|
|||||||
Reference in New Issue
Block a user