-
This commit is contained in:
@@ -22,7 +22,7 @@ class Client {
|
||||
public function new() {
|
||||
connection = new FlashConnection("localhost", 5000, onConnect);
|
||||
|
||||
flash.Lib.current.addEventListener(MouseEvent.CLICK, function(_) {
|
||||
flash.Lib.current.stage.addEventListener(MouseEvent.CLICK, function(_) {
|
||||
onConnect(_);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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 + "");
|
||||
}
|
||||
}
|
||||
@@ -1,75 +1,134 @@
|
||||
package ru.m.armageddon.server;
|
||||
|
||||
import haxe.io.BytesOutput;
|
||||
import haxe.io.BytesInput;
|
||||
import sys.db.Connection;
|
||||
import sys.db.Mysql;
|
||||
|
||||
import neko.Lib;
|
||||
import haxe.io.Bytes;
|
||||
import sys.net.Socket;
|
||||
import neko.net.ThreadServer;
|
||||
import haxe.io.Bytes;
|
||||
|
||||
typedef Client = {
|
||||
var id:Int;
|
||||
var socket:Socket;
|
||||
}
|
||||
/*class NativeSession extends Session {
|
||||
|
||||
typedef Message = {
|
||||
var str:String;
|
||||
}
|
||||
public var socket:Socket;
|
||||
|
||||
class Server extends ThreadServer<Client, Message> {
|
||||
// create a Client
|
||||
override function clientConnected(s:Socket):Client {
|
||||
s.input.bigEndian = false;
|
||||
s.output.bigEndian = false;
|
||||
var num = Std.random(100);
|
||||
Lib.println("client " + num + " is " + s.peer());
|
||||
return { id: num, socket: s };
|
||||
public function new(socket:Socket) {
|
||||
super();
|
||||
this.socket = socket;
|
||||
socket.setFastSend(true);
|
||||
socket.output.bigEndian = false;
|
||||
socket.input.bigEndian = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
override function clientDisconnected(c:Client) {
|
||||
Lib.println("client " + Std.string(c.id) + " disconnected");
|
||||
public override function close():Void {
|
||||
socket.close();
|
||||
}
|
||||
|
||||
override function readClientMessage(c:Client, buf:Bytes, pos:Int, len:Int) {
|
||||
Lib.println("client " + Std.string(c.id) + " message");
|
||||
public override function bakeMsg(msg:protohx.Message):BakedMsg {
|
||||
return new BakedMsg(msg);
|
||||
}
|
||||
|
||||
var family = buf.get(0);
|
||||
var id = buf.get(1);
|
||||
Lib.println(family + " - " + id);
|
||||
public override function writeMsgBaked(msg:BakedMsg):Void {
|
||||
writeMsg(msg.msg);
|
||||
}
|
||||
|
||||
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();
|
||||
request.mergeFrom(buf);
|
||||
Lib.println(request.login);
|
||||
Lib.println(request.password);
|
||||
request.mergeFrom(msgBytes);
|
||||
|
||||
trace(request);
|
||||
//sr.sessionData(session, bytes);
|
||||
|
||||
var db = new Db();
|
||||
var data = db.getUser();
|
||||
|
||||
var user = new User();
|
||||
user.login = data.login;
|
||||
user.nickname = data.login;
|
||||
|
||||
var userData = db.getUser();
|
||||
var response = new LoginResponse();
|
||||
var user = new User();
|
||||
user.login = userData.login;
|
||||
user.nickname = userData.login;
|
||||
response.user = user;
|
||||
|
||||
var socket = session.socket;
|
||||
var b = new BytesOutput();
|
||||
response.writeTo(b);
|
||||
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() {
|
||||
@@ -78,6 +137,7 @@ class Server extends ThreadServer<Client, Message> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Db {
|
||||
|
||||
private var db:Connection;
|
||||
|
||||
Reference in New Issue
Block a user