This commit is contained in:
2014-06-23 10:10:31 +04:00
parent 44c82ec71c
commit 11d4b1762d
7 changed files with 129 additions and 41 deletions

1
.gitignore vendored
View File

@@ -1,5 +1,6 @@
target/ target/
src-gen/ src-gen/
out/
*.iml *.iml
*.ipr *.ipr
*.iws *.iws

View File

@@ -1,15 +1,10 @@
package ru.m.armageddon.client; package ru.m.armageddon.client;
import haxe.io.BytesInput; import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import haxe.io.Bytes;
import flash.events.Event; import flash.events.Event;
import flash.events.SecurityErrorEvent; import ru.m.armageddon.core.connect.FlashConnection;
import flash.events.IOErrorEvent; import ru.m.armageddon.core.connect.IConnection;
import haxe.io.BytesOutput;
import haxework.log.TraceLogger; import haxework.log.TraceLogger;
import flash.net.Socket;
class Client { class Client {
@@ -22,47 +17,20 @@ class Client {
} }
private var socket:Socket; private var connection:IConnection;
public function new() { public function new() {
connection = new FlashConnection("localhost", 5000, onConnect);
socket = new Socket(); flash.Lib.current.addEventListener(MouseEvent.CLICK, function(_) {
socket.addEventListener(IOErrorEvent.IO_ERROR, function(error):Void { onConnect(_);
L.e("SocketLogger", "", error);
}); });
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(); var request = new LoginRequest();
request.login = "shmyga"; request.login = "shmyga";
request.password = "xkbp8jh9z2"; request.password = "xkbp8jh9z2";
var out = new BytesOutput(); connection.send(request);
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);
} }
} }

View 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 + "");
}
}

View 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;
}

View File

@@ -36,10 +36,16 @@ class Server extends ThreadServer<Client, Message> {
override function readClientMessage(c:Client, buf:Bytes, pos:Int, len:Int) { override function readClientMessage(c:Client, buf:Bytes, pos:Int, len:Int) {
Lib.println("client " + Std.string(c.id) + " message"); 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(); var request = new LoginRequest();
request.mergeFrom(buf); request.mergeFrom(buf);
Lib.println(request.login); Lib.println(request.login);
Lib.println(request.password); Lib.println(request.password);
var db = new Db(); var db = new Db();
var data = db.getUser(); var data = db.getUser();

View File

@@ -0,0 +1,12 @@
package ru.m.armageddon.server;
class ServerTest {
public static function main() {
}
public function new() {
}
}