-
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package ru.m.armageddon.client;
|
||||
|
||||
import protohx.Message;
|
||||
import haxe.crypto.Md5;
|
||||
import flash.Lib;
|
||||
import flash.events.MouseEvent;
|
||||
import ru.m.armageddon.core.connect.FlashConnection;
|
||||
@@ -7,7 +9,7 @@ import ru.m.armageddon.core.connect.IConnection;
|
||||
import haxework.log.TraceLogger;
|
||||
import ru.m.armageddon.proto.LoginRequest;
|
||||
|
||||
class Client {
|
||||
class Client implements IConnectionHandler {
|
||||
|
||||
private static inline var TAG = "Armageddon";
|
||||
|
||||
@@ -21,17 +23,29 @@ class Client {
|
||||
private var connection:IConnection;
|
||||
|
||||
public function new() {
|
||||
connection = new FlashConnection("localhost", 5000, onConnect);
|
||||
connection = new FlashConnection("localhost", 5000, this);
|
||||
|
||||
Lib.current.stage.addEventListener(MouseEvent.CLICK, function(_) {
|
||||
onConnect(_);
|
||||
onConnected();
|
||||
});
|
||||
}
|
||||
|
||||
public function onConnect(?event:Dynamic):Void {
|
||||
public function onConnected():Void {
|
||||
var request = new LoginRequest();
|
||||
request.login = "shmyga";
|
||||
request.password = "xkbp8jh9z2";
|
||||
request.password = Md5.encode("xkbp8jh9z2");
|
||||
connection.send(request);
|
||||
}
|
||||
|
||||
public function onDisconnected():Void {
|
||||
|
||||
}
|
||||
|
||||
public function onError(error:Dynamic):Void {
|
||||
|
||||
}
|
||||
|
||||
public function onPacket(packet:Message):Void {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
17
src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx
Executable file
17
src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx
Executable file
@@ -0,0 +1,17 @@
|
||||
package ru.m.armageddon.core.connect;
|
||||
|
||||
import protohx.Message;
|
||||
import ru.m.armageddon.core.connect.IConnection;
|
||||
|
||||
class BaseConnection implements IConnection {
|
||||
|
||||
private var builder:IPacketBuilder;
|
||||
private var handler:IConnectionHandler;
|
||||
|
||||
public function new(handler:IConnectionHandler) {
|
||||
this.builder = new PacketBuilder();
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
public function send(packet:Message):Void {}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.armageddon.core.connect;
|
||||
|
||||
import ru.m.armageddon.core.connect.IConnection.IConnectionHandler;
|
||||
import protohx.MessageUtils;
|
||||
import flash.utils.Endian;
|
||||
import haxe.io.BytesOutput;
|
||||
@@ -14,19 +15,12 @@ import flash.net.Socket;
|
||||
import ru.m.armageddon.proto.LoginRequest;
|
||||
import ru.m.armageddon.proto.LoginResponse;
|
||||
|
||||
class FlashConnection implements IConnection {
|
||||
|
||||
private static var MAP:Map<Int, Map<Int, Class<Message>>> = [
|
||||
0x01 => [
|
||||
0x0003 => LoginRequest,
|
||||
0x0002 => LoginResponse
|
||||
]
|
||||
];
|
||||
class FlashConnection extends BaseConnection {
|
||||
|
||||
private var socket:Socket;
|
||||
|
||||
public function new(host:String, port:Int, onConnect:?Dynamic->Void) {
|
||||
this.onConnect = onConnect;
|
||||
public function new(host:String, port:Int, handler:IConnectionHandler) {
|
||||
super(handler);
|
||||
socket = new Socket();
|
||||
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
|
||||
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
|
||||
@@ -34,20 +28,19 @@ class FlashConnection implements IConnection {
|
||||
socket.addEventListener(Event.CONNECT, onConnect);
|
||||
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
|
||||
socket.endian = Endian.LITTLE_ENDIAN;
|
||||
|
||||
socket.connect(host, port);
|
||||
}
|
||||
|
||||
private function onError(event:ErrorEvent):Void {
|
||||
|
||||
handler.onError(event);
|
||||
}
|
||||
|
||||
public dynamic function onConnect(?event:Dynamic):Void {
|
||||
|
||||
private function onConnect(_):Void {
|
||||
handler.onConnected();
|
||||
}
|
||||
|
||||
private function onClose(_):Void {
|
||||
|
||||
handler.onDisconnected();
|
||||
}
|
||||
|
||||
private function onSocketData(_):Void {
|
||||
@@ -57,14 +50,14 @@ class FlashConnection implements IConnection {
|
||||
var b = new flash.utils.ByteArray();
|
||||
socket.readBytes(b);
|
||||
var bs = Bytes.ofData(cast b);
|
||||
var packet = PacketBuilder.buildPacket(family, id);
|
||||
var packet = builder.buildPacket({family:family, id:id});
|
||||
packet.mergeFrom(bs);
|
||||
receive(packet);
|
||||
}
|
||||
|
||||
public function send(packet:Message):Void {
|
||||
override public function send(packet:Message):Void {
|
||||
L.d("Send", MessageUtils.toJson(packet));
|
||||
var meta = PacketBuilder.packetMeta(packet);
|
||||
var meta = builder.packetMeta(packet);
|
||||
socket.writeByte(meta.family);
|
||||
socket.writeByte(meta.id);
|
||||
var out = new BytesOutput();
|
||||
@@ -75,7 +68,9 @@ class FlashConnection implements IConnection {
|
||||
socket.flush();
|
||||
}
|
||||
|
||||
public dynamic function receive(packet:Message):Void {
|
||||
private function receive(packet:Message):Void {
|
||||
L.d("Receive", MessageUtils.toJson(packet));
|
||||
var name = Type.getClassName(Type.getClass(packet)).split("::")[1];
|
||||
L.d("xxx", name);
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,24 @@ package ru.m.armageddon.core.connect;
|
||||
import protohx.Message;
|
||||
|
||||
interface IConnection {
|
||||
|
||||
private var builder:IPacketBuilder;
|
||||
private var handler:IConnectionHandler;
|
||||
public function send(packet:Message):Void;
|
||||
}
|
||||
|
||||
public dynamic function onConnect(?event:Dynamic):Void;
|
||||
interface IConnectionHandler {
|
||||
public function onConnected():Void;
|
||||
public function onDisconnected():Void;
|
||||
public function onError(error:Dynamic):Void;
|
||||
public function onPacket(packet:Message):Void;
|
||||
}
|
||||
|
||||
public dynamic function receive(packet:Message):Void;
|
||||
typedef PacketMeta = {
|
||||
var family:Int;
|
||||
var id:Int;
|
||||
}
|
||||
|
||||
interface IPacketBuilder {
|
||||
public function packetMeta(packet:Message):PacketMeta;
|
||||
public function buildPacket(meta:PacketMeta):Message;
|
||||
}
|
||||
@@ -1,24 +1,26 @@
|
||||
package ru.m.armageddon.core;
|
||||
package ru.m.armageddon.core.connect;
|
||||
|
||||
import ru.m.armageddon.core.connect.IConnection;
|
||||
import protohx.Message;
|
||||
import ru.m.armageddon.proto.LoginRequest;
|
||||
import ru.m.armageddon.proto.LoginResponse;
|
||||
import ru.m.armageddon.proto.ErrorResponse;
|
||||
|
||||
typedef PacketMeta = {
|
||||
var family:Int;
|
||||
var id:Int;
|
||||
}
|
||||
|
||||
class PacketBuilder {
|
||||
class PacketBuilder implements IPacketBuilder {
|
||||
|
||||
private static var MAP:Map<Int, Map<Int, Class<Message>>> = [
|
||||
0x01 => [
|
||||
0x0001 => LoginRequest,
|
||||
0x0002 => LoginResponse
|
||||
],
|
||||
0x10 => [
|
||||
0x0001 => ErrorResponse
|
||||
]
|
||||
];
|
||||
|
||||
public static function packetMeta(packet:Message):PacketMeta {
|
||||
public function new() {}
|
||||
|
||||
public function packetMeta(packet:Message):PacketMeta {
|
||||
for (family in MAP.keys()) {
|
||||
var subMap = MAP[family];
|
||||
for (id in subMap.keys()) {
|
||||
@@ -34,8 +36,8 @@ class PacketBuilder {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function buildPacket(family:Int, id:Int):Message {
|
||||
var packetClass = MAP[family][id];
|
||||
public function buildPacket(meta:PacketMeta):Message {
|
||||
var packetClass = MAP[meta.family][meta.id];
|
||||
return Type.createInstance(packetClass, []);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ class Server extends ThreadServer<Session, Bytes> {
|
||||
}
|
||||
|
||||
override function clientConnected(s:Socket):Session {
|
||||
var session = new Session(s);
|
||||
var session = new Session(s, null);
|
||||
Lib.println("client: " + session.id + " / " + s.peer());
|
||||
return session;
|
||||
}
|
||||
@@ -31,6 +31,7 @@ class Server extends ThreadServer<Session, Bytes> {
|
||||
}
|
||||
|
||||
public static function main() {
|
||||
Lib.println("Running");
|
||||
var server = new Server();
|
||||
server.run("localhost", 5000);
|
||||
}
|
||||
|
||||
@@ -9,17 +9,18 @@ class Db {
|
||||
|
||||
public function new() {
|
||||
db = Mysql.connect({
|
||||
host : "localhost",
|
||||
port : 3306,
|
||||
user : "shmyga",
|
||||
pass : "xkbp8jh9z2",
|
||||
socket : null,
|
||||
database : "armageddon"
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
user: "shmyga",
|
||||
pass: "xkbp8jh9z2",
|
||||
socket: null,
|
||||
database: "armageddon"
|
||||
});
|
||||
}
|
||||
|
||||
public function getUser() {
|
||||
var rset = db.request("SELECT * FROM Users");
|
||||
public function getUser(login:String, password:String) {
|
||||
var rset = db.request("SELECT * FROM Users WHERE login='" + login + "' AND password='" + password + "'");
|
||||
if (!rset.hasNext()) return null;
|
||||
var user = rset.next();
|
||||
return user;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package ru.m.armageddon.server.session;
|
||||
|
||||
import ru.m.armageddon.core.PacketBuilder;
|
||||
import ru.m.armageddon.core.connect.IConnection.IPacketBuilder;
|
||||
import protohx.Message;
|
||||
import haxe.io.BytesInput;
|
||||
import haxe.io.BytesBuffer;
|
||||
@@ -10,10 +10,12 @@ class PacketQueue {
|
||||
|
||||
public static inline var HEADER_SIZE:Int = 4;
|
||||
|
||||
private var builder:IPacketBuilder;
|
||||
private var bytesBuff:Bytes;
|
||||
private var msgs:List<Message>;
|
||||
|
||||
public function new() {
|
||||
public function new(builder:IPacketBuilder) {
|
||||
this.builder = builder;
|
||||
msgs = new List<Message>();
|
||||
}
|
||||
|
||||
@@ -55,7 +57,7 @@ class PacketQueue {
|
||||
if (packetSize <= available) {
|
||||
available -= packetSize;
|
||||
var msgBytes = bi.read(packetSize);
|
||||
var packet = PacketBuilder.buildPacket(family, id);
|
||||
var packet = builder.buildPacket({family:family, id:id});
|
||||
packet.mergeFrom(msgBytes);
|
||||
addMsg(packet);
|
||||
} else {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package ru.m.armageddon.server.session;
|
||||
|
||||
import ru.m.armageddon.core.connect.BaseConnection;
|
||||
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 ru.m.armageddon.core.PacketBuilder;
|
||||
import protohx.MessageUtils;
|
||||
import haxe.io.Bytes;
|
||||
import protohx.Message;
|
||||
@@ -12,18 +13,19 @@ import ru.m.armageddon.core.connect.IConnection;
|
||||
import haxe.io.BytesOutput;
|
||||
import sys.net.Socket;
|
||||
|
||||
class Session implements IConnection {
|
||||
class Session extends BaseConnection {
|
||||
|
||||
public var id:Int;
|
||||
public var socket:Socket;
|
||||
public var queue:PacketQueue;
|
||||
|
||||
public function new(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();
|
||||
queue = new PacketQueue(builder);
|
||||
}
|
||||
|
||||
public function pushData(bytes:Bytes):Void {
|
||||
@@ -34,12 +36,10 @@ class Session implements IConnection {
|
||||
}
|
||||
}
|
||||
|
||||
public dynamic function onConnect(?event:Dynamic):Void {}
|
||||
|
||||
public function send(packet:Message):Void {
|
||||
override public function send(packet:Message):Void {
|
||||
trace("Send: " + MessageUtils.toJson(packet));
|
||||
try {
|
||||
var meta = PacketBuilder.packetMeta(packet);
|
||||
var meta = builder.packetMeta(packet);
|
||||
var b = new BytesOutput();
|
||||
packet.writeTo(b);
|
||||
var bytes = b.getBytes();
|
||||
@@ -53,20 +53,26 @@ class Session implements IConnection {
|
||||
}
|
||||
}
|
||||
|
||||
public dynamic function receive(packet:Message):Void {
|
||||
private function receive(packet:Message):Void {
|
||||
trace("Receive: " + MessageUtils.toJson(packet));
|
||||
if (Std.is(packet, LoginRequest)) {
|
||||
var request = cast(packet, LoginRequest);
|
||||
|
||||
var db = new Db();
|
||||
var userData = db.getUser();
|
||||
|
||||
var user = new User();
|
||||
user.login = userData.login;
|
||||
user.nickname = userData.login;
|
||||
var response = new LoginResponse();
|
||||
response.user = user;
|
||||
send(response);
|
||||
var userData = db.getUser(request.login, request.password);
|
||||
if (userData != null) {
|
||||
var user = new User();
|
||||
user.login = userData.login;
|
||||
user.nickname = userData.login;
|
||||
var response = new LoginResponse();
|
||||
response.user = user;
|
||||
send(response);
|
||||
} else {
|
||||
var response = new ErrorResponse();
|
||||
response.code = 403;
|
||||
response.message = "User not found";
|
||||
send(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user