[server] update proto

This commit is contained in:
2018-02-28 22:25:29 +03:00
parent 34e5ac2b9e
commit 634f5ad2d0
7 changed files with 238 additions and 272 deletions

View File

@@ -24,7 +24,8 @@ const build = () => function build() {
'protohx',
'orm',
'haxework:git',
'haxe-crypto'
'haxe-crypto',
'hxsignal'
],
cp: [
'src/common/haxe',

View File

@@ -8,7 +8,8 @@ import protohx.Message;
class PacketQueue<P:Message> {
private var packetClass:Class<P>;
public var packetClass(default, null):Class<P>;
private var bytesBuff:Bytes;
private var msgs:List<P>;

View File

@@ -1,30 +1,26 @@
package ru.m.connect;
import haxe.io.Bytes;
import haxe.io.BytesOutput;
import ru.m.connect.IConnection;
import protohx.Message;
class WebSocketTools {
public static function packet2string(packet:Message, builder:IPacketBuilder):String {
var meta = builder.packetMeta(packet);
public static function packet2string(packet:Message):String {
var b = new BytesOutput();
packet.writeTo(b);
var data = b.getBytes();
var res = new BytesOutput();
res.writeByte(meta.family);
res.writeByte(meta.id);
//res.writeUInt16(data.length);
res.write(data);
return Base64.encodeBase64(res.getBytes());
}
public static function string2packet(data:String, builder:IPacketBuilder):Message {
public static function string2packet<P:Message>(data:String, packetClass:Class<P>):P {
var bytes = Base64.decodeBase64(data);
var family = bytes.get(0);
var id = bytes.get(1);
var packet = builder.buildPacket({family:family, id:id});
var packet:P = Type.createInstance(packetClass, []);
packet.mergeFrom(bytes.sub(2, bytes.length - 2));
return packet;
}

View File

@@ -2,31 +2,27 @@ package ru.m.connect.neko;
import haxe.io.BytesOutput;
import protohx.Message;
import haxe.io.Bytes;
import sys.net.Socket;
import ru.m.connect.IConnection;
class NekoConnection extends BaseConnection {
class NekoConnection<O:Message, I:Message> extends BaseConnection<O, I> {
public var socket(default, null):Socket;
public function new(socket:Socket) {
super();
public function new(socket:Socket, i:Class<I>) {
super(i);
this.socket = socket;
socket.setFastSend(true);
socket.output.bigEndian = false;
socket.input.bigEndian = false;
}
override public function send(packet:Message):Void {
override public function send(packet:O):Void {
super.send(packet);
try {
var meta = builder.packetMeta(packet);
var b = new BytesOutput();
packet.writeTo(b);
var bytes = b.getBytes();
socket.output.writeByte(meta.family);
socket.output.writeByte(meta.id);
socket.output.writeUInt16(bytes.length);
socket.output.write(bytes);
socket.output.flush();

View File

@@ -5,21 +5,21 @@ import haxe.crypto.Sha1;
import protohx.Message;
import haxe.io.Bytes;
import sys.net.Socket;
import ru.m.connect.IConnection;
class NekoWebConnection extends NekoConnection {
class NekoWebConnection<O:Message, I:Message> extends NekoConnection<O, I> {
private var opened:Bool;
public function new(socket:Socket) {
super(socket);
public function new(socket:Socket, i:Class<I>) {
super(socket, i);
opened = false;
}
override public function send(packet:Message):Void {
override public function send(packet:O):Void {
#if proto_debug L.d("Send", Type.getClassName(Type.getClass(packet)).split(".").pop()); #end
try {
var data = WebSocketTools.packet2string(packet, builder);
var data = WebSocketTools.packet2string(packet);
writeData(data, socket);
} catch (e:Dynamic) {
trace(e);
@@ -38,8 +38,8 @@ class NekoWebConnection extends NekoConnection {
} else {
var data = parseData(bytes);
if (data != null) {
var packet = WebSocketTools.string2packet(data, builder);
receive(packet);
var packet:I = WebSocketTools.string2packet(data, queue.packetClass);
packetHandler.emit(packet);
}
}
}
@@ -85,15 +85,12 @@ class NekoWebConnection extends NekoConnection {
socket.output.writeByte(len | (!isServer ? 0x80 : 0x00));
if (data.length >= 126)
{
if (data.length < 65536)
{
if (data.length >= 126) {
if (data.length < 65536) {
socket.output.writeByte((data.length >> 8) & 0xFF);
socket.output.writeByte(data.length & 0xFF);
}
else
{
else {
socket.output.writeByte((data.length >> 24) & 0xFF);
socket.output.writeByte((data.length >> 16) & 0xFF);
socket.output.writeByte((data.length >> 8) & 0xFF);
@@ -101,20 +98,17 @@ class NekoWebConnection extends NekoConnection {
}
}
if (isServer)
{
if (isServer) {
socket.output.writeString(data);
}
else
{
else {
var mask = [ Std.random(256), Std.random(256), Std.random(256), Std.random(256) ];
socket.output.writeByte(mask[0]);
socket.output.writeByte(mask[1]);
socket.output.writeByte(mask[2]);
socket.output.writeByte(mask[3]);
var maskedData = new StringBuf();
for (i in 0...data.length)
{
for (i in 0...data.length) {
maskedData.addChar(data.charCodeAt(i) ^ mask[i % 4]);
}
socket.output.writeString(maskedData.toString());
@@ -125,34 +119,29 @@ class NekoWebConnection extends NekoConnection {
var p = 0;
var opcode = bytes.get(p++);
if (opcode == 0x00)
{
if (opcode == 0x00) {
var s = "";
var b : Int;
while ((b = bytes.get(p++)) != 0xFF)
{
var b:Int;
while ((b = bytes.get(p++)) != 0xFF) {
s += String.fromCharCode(b);
}
return s;
}
if (opcode == 0x81) // 0x81 = fin & text
{
// 0x81 = fin & text
if (opcode == 0x81) {
var len = bytes.get(p++);
if (len & 0x80 != 0) // mask
{
// mask
if (len & 0x80 != 0) {
len &= 0x7F;
if (len == 126)
{
if (len == 126) {
var b2 = bytes.get(p++);
var b3 = bytes.get(p++);
len = (b2 << 8) + b3;
}
else
if (len == 127)
{
else if (len == 127) {
var b2 = bytes.get(p++);
var b3 = bytes.get(p++);
var b4 = bytes.get(p++);
@@ -172,16 +161,13 @@ class NekoWebConnection extends NekoConnection {
//Lib.println("mask = " + mask);
var data = new StringBuf();
for (i in 0...len)
{
for (i in 0...len) {
data.addChar(bytes.get(p++) ^ mask[i % 4]);
}
//Lib.println("readed = " + data.toString());
return data.toString();
}
else
{
} else {
throw "Expected masked data.";
}
}
@@ -190,10 +176,7 @@ class NekoWebConnection extends NekoConnection {
//socket.close();
opened = false;
return null;
}
else
{
} else {
throw "Unsupported websocket opcode: " + opcode;
}
return null;

View File

@@ -1,13 +1,13 @@
package ru.m.tankz.server;
import haxework.log.SocketLogger;
import ru.m.connect.IConnection.IPacketBuilder;
import haxework.provider.Provider;
import ru.m.connect.IConnection.ConnectionEvent;
import haxework.log.TraceLogger;
import ru.m.tankz.server.session.Session;
import haxe.io.Bytes;
import sys.net.Socket;
import neko.net.ThreadServer;
#if debug import haxework.log.SocketLogger; #end
class Server extends ThreadServer<Session, Bytes> {
@@ -25,7 +25,7 @@ class Server extends ThreadServer<Session, Bytes> {
override public function clientDisconnected(session:Session) {
L.d(TAG, "Client disconnected");
session.onDisconnected();
session.connection.handler.emit(ConnectionEvent.DISCONNECTED);
}
override public function readClientMessage(session:Session, buf:Bytes, pos:Int, len:Int) {
@@ -43,7 +43,6 @@ class Server extends ThreadServer<Session, Bytes> {
#end
L.d(TAG, "Running");
L.i(TAG, "Build: " + CompilationOption.get("build"));
Provider.set(IPacketBuilder, new PacketBuilder());
var wserver = new Server();
wserver.run("localhost", 5001);
}

View File

@@ -1,32 +1,37 @@
package ru.m.tankz.server.session;
import ru.m.tankz.proto.pack.LoginRequest;
import ru.m.tankz.proto.pack.LoginResponse;
import haxe.io.Bytes;
import protohx.Message;
import ru.m.connect.IConnection;
import ru.m.connect.neko.NekoConnection;
import ru.m.connect.neko.NekoWebConnection;
import ru.m.tankz.proto.core.User;
import ru.m.tankz.proto.pack.LoginRequest;
import ru.m.tankz.proto.pack.LoginResponse;
import ru.m.tankz.proto.pack.Request;
import ru.m.tankz.proto.pack.Response;
import sys.net.Socket;
import Type;
class Session implements IConnectionHandler implements IPacketHandler {
typedef ServerConnection = IConnection<Response, Request>;
class Session {
public static var sessions:Map<Int, Session> = new Map<Int, Session>();
public var user(default, null):User;
public var gameId(default, null):Int = -1;
private var connection(default, null):IConnection;
public var connection(default, null):ServerConnection;
private var socket:Socket;
public function new(socket:Socket) {
this.socket = socket;
}
public function send(packet:Message):Void {
public function send(packet:Response):Void {
connection.send(packet);
}
@@ -36,47 +41,32 @@ class Session implements IConnectionHandler implements IPacketHandler {
} else {
var str:String = bytes.getString(0, bytes.length);
if (StringTools.startsWith(str, "GET")) {
connection = new NekoWebConnection(socket);
connection = new NekoWebConnection<Response, Request>(socket, Request);
} else {
connection = new NekoConnection(socket);
connection = new NekoConnection<Response, Request>(socket, Request);
}
connection.handler.addListener(this);
connection.packetHandler.addListener(this);
connection.handler.connect(onConnectionEvent);
connection.packetHandler.connect(onRequest);
connection.pushData(bytes);
}
}
/**
* Connection handlers
**/
public function onConnected():Void {
private function onConnectionEvent(event:ConnectionEvent):Void {
L.d('Session', '${this}, ${event}');
}
public function onDisconnected():Void {
/*if (person != null) {
var game = GameManager.byPersonId.get(person.id);
if (game != null) game.leave(person);
}*/
public function onRequest(request:Request):Void {
if (request.hasLogin()) {
connection.send(new Response().setLogin(login(request.login)));
}
}
public function onError(error:Dynamic):Void {
/*connection.send(
new ErrorResponse()
.setCode(0)
.setMessage(Std.string(error))
);*/
}
/**
* Packets handlers
**/
public function onLoginRequest(packet:LoginRequest):Void {
connection.send(new LoginResponse().setUser(
private function login(request:LoginRequest):LoginResponse {
return new LoginResponse().setUser(
new User()
.setUuid(packet.uuid != null ? packet.uuid : 'xxx')
.setName(packet.name)
));
.setUuid(request.uuid != null ? request.uuid : 'xxx')
.setName(request.name)
);
}
/*public function onPersonSelectRequest(packet:PersonSelectRequest):Void {