-
This commit is contained in:
@@ -1,15 +1,10 @@
|
||||
package ru.m.armageddon.client;
|
||||
|
||||
import haxe.io.BytesInput;
|
||||
import flash.events.ProgressEvent;
|
||||
import haxe.io.Bytes;
|
||||
import flash.events.MouseEvent;
|
||||
import flash.events.Event;
|
||||
import flash.events.SecurityErrorEvent;
|
||||
import flash.events.IOErrorEvent;
|
||||
import haxe.io.BytesOutput;
|
||||
import ru.m.armageddon.core.connect.FlashConnection;
|
||||
import ru.m.armageddon.core.connect.IConnection;
|
||||
import haxework.log.TraceLogger;
|
||||
import flash.net.Socket;
|
||||
|
||||
|
||||
class Client {
|
||||
|
||||
@@ -22,47 +17,20 @@ class Client {
|
||||
}
|
||||
|
||||
|
||||
private var socket:Socket;
|
||||
private var connection:IConnection;
|
||||
|
||||
public function new() {
|
||||
connection = new FlashConnection("localhost", 5000, onConnect);
|
||||
|
||||
socket = new Socket();
|
||||
socket.addEventListener(IOErrorEvent.IO_ERROR, function(error):Void {
|
||||
L.e("SocketLogger", "", error);
|
||||
flash.Lib.current.addEventListener(MouseEvent.CLICK, function(_) {
|
||||
onConnect(_);
|
||||
});
|
||||
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(error):Void {
|
||||
L.e("SocketLogger", "", error);
|
||||
});
|
||||
//socket.addEventListener(Event.CLOSE, closeHandler);
|
||||
socket.addEventListener(Event.CONNECT, onConnect);
|
||||
//socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
|
||||
//socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
|
||||
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
|
||||
//socket.endian = flash.utils.Endian.LITTLE_ENDIAN;
|
||||
|
||||
socket.connect("localhost", 5000);
|
||||
}
|
||||
|
||||
private function onConnect(_) {
|
||||
public function onConnect(event:Event):Void {
|
||||
var request = new LoginRequest();
|
||||
request.login = "shmyga";
|
||||
request.password = "xkbp8jh9z2";
|
||||
var out = new BytesOutput();
|
||||
request.writeTo(out);
|
||||
var bytes = out.getBytes();
|
||||
socket.writeShort(bytes.length);
|
||||
socket.writeBytes(cast bytes.getData());
|
||||
}
|
||||
|
||||
private function onSocketData(_) {
|
||||
L.d(TAG, "onSocketData");
|
||||
var b = new flash.utils.ByteArray();
|
||||
L.d(TAG, "socket.bytesAvailable: " + socket.bytesAvailable);
|
||||
socket.readBytes(b);
|
||||
var bs = Bytes.ofData(cast b);
|
||||
L.d(TAG, "bs.length: " + bs.length);
|
||||
var response = new LoginResponse();
|
||||
response.mergeFrom(bs);
|
||||
L.d(TAG, response.user.login);
|
||||
connection.send(request);
|
||||
}
|
||||
}
|
||||
|
||||
88
src/common/haxe/ru/m/armageddon/core/connect/FlashConnection.hx
Executable file
88
src/common/haxe/ru/m/armageddon/core/connect/FlashConnection.hx
Executable file
@@ -0,0 +1,88 @@
|
||||
package ru.m.armageddon.core.connect;
|
||||
|
||||
import haxe.io.BytesOutput;
|
||||
import protohx.Message;
|
||||
import haxe.io.Bytes;
|
||||
import flash.events.ErrorEvent;
|
||||
import flash.events.ProgressEvent;
|
||||
import flash.events.Event;
|
||||
import flash.events.SecurityErrorEvent;
|
||||
import flash.events.IOErrorEvent;
|
||||
import flash.net.Socket;
|
||||
|
||||
class FlashConnection 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:String, port:Int, onConnect:Event->Void) {
|
||||
this.onConnect = onConnect;
|
||||
socket = new Socket();
|
||||
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
|
||||
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
|
||||
socket.addEventListener(Event.CLOSE, onClose);
|
||||
socket.addEventListener(Event.CONNECT, onConnect);
|
||||
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
|
||||
//socket.endian = flash.utils.Endian.LITTLE_ENDIAN;
|
||||
|
||||
socket.connect(host, port);
|
||||
}
|
||||
|
||||
private function onError(event:ErrorEvent):Void {
|
||||
|
||||
}
|
||||
|
||||
public dynamic function onConnect(event:Event):Void {
|
||||
|
||||
}
|
||||
|
||||
private function onClose(_):Void {
|
||||
|
||||
}
|
||||
|
||||
private function onSocketData(_):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.writeByte(family);
|
||||
socket.writeByte(id);
|
||||
var out = new BytesOutput();
|
||||
packet.writeTo(out);
|
||||
var bytes = out.getBytes();
|
||||
socket.writeShort(bytes.length);
|
||||
socket.writeBytes(cast bytes.getData());
|
||||
socket.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public dynamic function receive(packet:Message):Void {
|
||||
L.d("Receive", packet + "");
|
||||
}
|
||||
}
|
||||
13
src/common/haxe/ru/m/armageddon/core/connect/IConnection.hx
Executable file
13
src/common/haxe/ru/m/armageddon/core/connect/IConnection.hx
Executable file
@@ -0,0 +1,13 @@
|
||||
package ru.m.armageddon.core.connect;
|
||||
|
||||
import flash.events.Event;
|
||||
import protohx.Message;
|
||||
|
||||
interface IConnection {
|
||||
|
||||
public function send(packet:Message):Void;
|
||||
|
||||
public dynamic function onConnect(event:Event):Void;
|
||||
|
||||
public dynamic function receive(packet:Message):Void;
|
||||
}
|
||||
0
src/common/haxe/ru/m/armageddon/core/connect/NekoConnection.hx
Executable file
0
src/common/haxe/ru/m/armageddon/core/connect/NekoConnection.hx
Executable file
@@ -36,10 +36,16 @@ class Server extends ThreadServer<Client, Message> {
|
||||
|
||||
override function readClientMessage(c:Client, buf:Bytes, pos:Int, len:Int) {
|
||||
Lib.println("client " + Std.string(c.id) + " message");
|
||||
|
||||
var family = buf.get(0);
|
||||
var id = buf.get(1);
|
||||
Lib.println(family + " - " + id);
|
||||
|
||||
var request = new LoginRequest();
|
||||
request.mergeFrom(buf);
|
||||
Lib.println(request.login);
|
||||
Lib.println(request.password);
|
||||
|
||||
var db = new Db();
|
||||
var data = db.getUser();
|
||||
|
||||
|
||||
12
src/server/haxe/ru/m/armageddon/server/ServerTest.hx
Executable file
12
src/server/haxe/ru/m/armageddon/server/ServerTest.hx
Executable file
@@ -0,0 +1,12 @@
|
||||
package ru.m.armageddon.server;
|
||||
|
||||
class ServerTest {
|
||||
|
||||
public static function main() {
|
||||
|
||||
}
|
||||
|
||||
public function new() {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user