[server] update proto
This commit is contained in:
@@ -24,7 +24,8 @@ const build = () => function build() {
|
|||||||
'protohx',
|
'protohx',
|
||||||
'orm',
|
'orm',
|
||||||
'haxework:git',
|
'haxework:git',
|
||||||
'haxe-crypto'
|
'haxe-crypto',
|
||||||
|
'hxsignal'
|
||||||
],
|
],
|
||||||
cp: [
|
cp: [
|
||||||
'src/common/haxe',
|
'src/common/haxe',
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ import protohx.Message;
|
|||||||
|
|
||||||
class PacketQueue<P:Message> {
|
class PacketQueue<P:Message> {
|
||||||
|
|
||||||
private var packetClass:Class<P>;
|
public var packetClass(default, null):Class<P>;
|
||||||
|
|
||||||
private var bytesBuff:Bytes;
|
private var bytesBuff:Bytes;
|
||||||
private var msgs:List<P>;
|
private var msgs:List<P>;
|
||||||
|
|
||||||
|
|||||||
@@ -1,30 +1,26 @@
|
|||||||
package ru.m.connect;
|
package ru.m.connect;
|
||||||
|
|
||||||
import haxe.io.Bytes;
|
|
||||||
import haxe.io.BytesOutput;
|
import haxe.io.BytesOutput;
|
||||||
import ru.m.connect.IConnection;
|
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
|
|
||||||
|
|
||||||
class WebSocketTools {
|
class WebSocketTools {
|
||||||
|
|
||||||
public static function packet2string(packet:Message, builder:IPacketBuilder):String {
|
public static function packet2string(packet:Message):String {
|
||||||
var meta = builder.packetMeta(packet);
|
|
||||||
var b = new BytesOutput();
|
var b = new BytesOutput();
|
||||||
packet.writeTo(b);
|
packet.writeTo(b);
|
||||||
var data = b.getBytes();
|
var data = b.getBytes();
|
||||||
var res = new BytesOutput();
|
var res = new BytesOutput();
|
||||||
res.writeByte(meta.family);
|
|
||||||
res.writeByte(meta.id);
|
|
||||||
//res.writeUInt16(data.length);
|
//res.writeUInt16(data.length);
|
||||||
res.write(data);
|
res.write(data);
|
||||||
return Base64.encodeBase64(res.getBytes());
|
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 bytes = Base64.decodeBase64(data);
|
||||||
var family = bytes.get(0);
|
var family = bytes.get(0);
|
||||||
var id = bytes.get(1);
|
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));
|
packet.mergeFrom(bytes.sub(2, bytes.length - 2));
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,31 +2,27 @@ package ru.m.connect.neko;
|
|||||||
|
|
||||||
import haxe.io.BytesOutput;
|
import haxe.io.BytesOutput;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
import haxe.io.Bytes;
|
|
||||||
import sys.net.Socket;
|
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 var socket(default, null):Socket;
|
||||||
|
|
||||||
public function new(socket:Socket) {
|
public function new(socket:Socket, i:Class<I>) {
|
||||||
super();
|
super(i);
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
socket.setFastSend(true);
|
socket.setFastSend(true);
|
||||||
socket.output.bigEndian = false;
|
socket.output.bigEndian = false;
|
||||||
socket.input.bigEndian = false;
|
socket.input.bigEndian = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function send(packet:Message):Void {
|
override public function send(packet:O):Void {
|
||||||
super.send(packet);
|
super.send(packet);
|
||||||
try {
|
try {
|
||||||
var meta = builder.packetMeta(packet);
|
|
||||||
var b = new BytesOutput();
|
var b = new BytesOutput();
|
||||||
packet.writeTo(b);
|
packet.writeTo(b);
|
||||||
var bytes = b.getBytes();
|
var bytes = b.getBytes();
|
||||||
socket.output.writeByte(meta.family);
|
|
||||||
socket.output.writeByte(meta.id);
|
|
||||||
socket.output.writeUInt16(bytes.length);
|
socket.output.writeUInt16(bytes.length);
|
||||||
socket.output.write(bytes);
|
socket.output.write(bytes);
|
||||||
socket.output.flush();
|
socket.output.flush();
|
||||||
|
|||||||
@@ -5,21 +5,21 @@ import haxe.crypto.Sha1;
|
|||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
import haxe.io.Bytes;
|
import haxe.io.Bytes;
|
||||||
import sys.net.Socket;
|
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;
|
private var opened:Bool;
|
||||||
|
|
||||||
public function new(socket:Socket) {
|
public function new(socket:Socket, i:Class<I>) {
|
||||||
super(socket);
|
super(socket, i);
|
||||||
opened = false;
|
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
|
#if proto_debug L.d("Send", Type.getClassName(Type.getClass(packet)).split(".").pop()); #end
|
||||||
try {
|
try {
|
||||||
var data = WebSocketTools.packet2string(packet, builder);
|
var data = WebSocketTools.packet2string(packet);
|
||||||
writeData(data, socket);
|
writeData(data, socket);
|
||||||
} catch (e:Dynamic) {
|
} catch (e:Dynamic) {
|
||||||
trace(e);
|
trace(e);
|
||||||
@@ -38,8 +38,8 @@ class NekoWebConnection extends NekoConnection {
|
|||||||
} else {
|
} else {
|
||||||
var data = parseData(bytes);
|
var data = parseData(bytes);
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
var packet = WebSocketTools.string2packet(data, builder);
|
var packet:I = WebSocketTools.string2packet(data, queue.packetClass);
|
||||||
receive(packet);
|
packetHandler.emit(packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -85,15 +85,12 @@ class NekoWebConnection extends NekoConnection {
|
|||||||
|
|
||||||
socket.output.writeByte(len | (!isServer ? 0x80 : 0x00));
|
socket.output.writeByte(len | (!isServer ? 0x80 : 0x00));
|
||||||
|
|
||||||
if (data.length >= 126)
|
if (data.length >= 126) {
|
||||||
{
|
if (data.length < 65536) {
|
||||||
if (data.length < 65536)
|
|
||||||
{
|
|
||||||
socket.output.writeByte((data.length >> 8) & 0xFF);
|
socket.output.writeByte((data.length >> 8) & 0xFF);
|
||||||
socket.output.writeByte(data.length & 0xFF);
|
socket.output.writeByte(data.length & 0xFF);
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
socket.output.writeByte((data.length >> 24) & 0xFF);
|
socket.output.writeByte((data.length >> 24) & 0xFF);
|
||||||
socket.output.writeByte((data.length >> 16) & 0xFF);
|
socket.output.writeByte((data.length >> 16) & 0xFF);
|
||||||
socket.output.writeByte((data.length >> 8) & 0xFF);
|
socket.output.writeByte((data.length >> 8) & 0xFF);
|
||||||
@@ -101,20 +98,17 @@ class NekoWebConnection extends NekoConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isServer)
|
if (isServer) {
|
||||||
{
|
|
||||||
socket.output.writeString(data);
|
socket.output.writeString(data);
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
var mask = [ Std.random(256), Std.random(256), Std.random(256), Std.random(256) ];
|
var mask = [ Std.random(256), Std.random(256), Std.random(256), Std.random(256) ];
|
||||||
socket.output.writeByte(mask[0]);
|
socket.output.writeByte(mask[0]);
|
||||||
socket.output.writeByte(mask[1]);
|
socket.output.writeByte(mask[1]);
|
||||||
socket.output.writeByte(mask[2]);
|
socket.output.writeByte(mask[2]);
|
||||||
socket.output.writeByte(mask[3]);
|
socket.output.writeByte(mask[3]);
|
||||||
var maskedData = new StringBuf();
|
var maskedData = new StringBuf();
|
||||||
for (i in 0...data.length)
|
for (i in 0...data.length) {
|
||||||
{
|
|
||||||
maskedData.addChar(data.charCodeAt(i) ^ mask[i % 4]);
|
maskedData.addChar(data.charCodeAt(i) ^ mask[i % 4]);
|
||||||
}
|
}
|
||||||
socket.output.writeString(maskedData.toString());
|
socket.output.writeString(maskedData.toString());
|
||||||
@@ -125,34 +119,29 @@ class NekoWebConnection extends NekoConnection {
|
|||||||
var p = 0;
|
var p = 0;
|
||||||
var opcode = bytes.get(p++);
|
var opcode = bytes.get(p++);
|
||||||
|
|
||||||
if (opcode == 0x00)
|
if (opcode == 0x00) {
|
||||||
{
|
|
||||||
var s = "";
|
var s = "";
|
||||||
var b : Int;
|
var b:Int;
|
||||||
while ((b = bytes.get(p++)) != 0xFF)
|
while ((b = bytes.get(p++)) != 0xFF) {
|
||||||
{
|
|
||||||
s += String.fromCharCode(b);
|
s += String.fromCharCode(b);
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opcode == 0x81) // 0x81 = fin & text
|
// 0x81 = fin & text
|
||||||
{
|
if (opcode == 0x81) {
|
||||||
var len = bytes.get(p++);
|
var len = bytes.get(p++);
|
||||||
|
|
||||||
if (len & 0x80 != 0) // mask
|
// mask
|
||||||
{
|
if (len & 0x80 != 0) {
|
||||||
len &= 0x7F;
|
len &= 0x7F;
|
||||||
|
|
||||||
if (len == 126)
|
if (len == 126) {
|
||||||
{
|
|
||||||
var b2 = bytes.get(p++);
|
var b2 = bytes.get(p++);
|
||||||
var b3 = bytes.get(p++);
|
var b3 = bytes.get(p++);
|
||||||
len = (b2 << 8) + b3;
|
len = (b2 << 8) + b3;
|
||||||
}
|
}
|
||||||
else
|
else if (len == 127) {
|
||||||
if (len == 127)
|
|
||||||
{
|
|
||||||
var b2 = bytes.get(p++);
|
var b2 = bytes.get(p++);
|
||||||
var b3 = bytes.get(p++);
|
var b3 = bytes.get(p++);
|
||||||
var b4 = bytes.get(p++);
|
var b4 = bytes.get(p++);
|
||||||
@@ -172,16 +161,13 @@ class NekoWebConnection extends NekoConnection {
|
|||||||
//Lib.println("mask = " + mask);
|
//Lib.println("mask = " + mask);
|
||||||
|
|
||||||
var data = new StringBuf();
|
var data = new StringBuf();
|
||||||
for (i in 0...len)
|
for (i in 0...len) {
|
||||||
{
|
|
||||||
data.addChar(bytes.get(p++) ^ mask[i % 4]);
|
data.addChar(bytes.get(p++) ^ mask[i % 4]);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Lib.println("readed = " + data.toString());
|
//Lib.println("readed = " + data.toString());
|
||||||
return data.toString();
|
return data.toString();
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
throw "Expected masked data.";
|
throw "Expected masked data.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -190,10 +176,7 @@ class NekoWebConnection extends NekoConnection {
|
|||||||
//socket.close();
|
//socket.close();
|
||||||
opened = false;
|
opened = false;
|
||||||
return null;
|
return null;
|
||||||
}
|
} else {
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw "Unsupported websocket opcode: " + opcode;
|
throw "Unsupported websocket opcode: " + opcode;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
package ru.m.tankz.server;
|
package ru.m.tankz.server;
|
||||||
|
|
||||||
import haxework.log.SocketLogger;
|
import ru.m.connect.IConnection.ConnectionEvent;
|
||||||
import ru.m.connect.IConnection.IPacketBuilder;
|
|
||||||
import haxework.provider.Provider;
|
|
||||||
import haxework.log.TraceLogger;
|
import haxework.log.TraceLogger;
|
||||||
import ru.m.tankz.server.session.Session;
|
import ru.m.tankz.server.session.Session;
|
||||||
import haxe.io.Bytes;
|
import haxe.io.Bytes;
|
||||||
import sys.net.Socket;
|
import sys.net.Socket;
|
||||||
import neko.net.ThreadServer;
|
import neko.net.ThreadServer;
|
||||||
|
#if debug import haxework.log.SocketLogger; #end
|
||||||
|
|
||||||
|
|
||||||
class Server extends ThreadServer<Session, Bytes> {
|
class Server extends ThreadServer<Session, Bytes> {
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ class Server extends ThreadServer<Session, Bytes> {
|
|||||||
|
|
||||||
override public function clientDisconnected(session:Session) {
|
override public function clientDisconnected(session:Session) {
|
||||||
L.d(TAG, "Client disconnected");
|
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) {
|
override public function readClientMessage(session:Session, buf:Bytes, pos:Int, len:Int) {
|
||||||
@@ -43,7 +43,6 @@ class Server extends ThreadServer<Session, Bytes> {
|
|||||||
#end
|
#end
|
||||||
L.d(TAG, "Running");
|
L.d(TAG, "Running");
|
||||||
L.i(TAG, "Build: " + CompilationOption.get("build"));
|
L.i(TAG, "Build: " + CompilationOption.get("build"));
|
||||||
Provider.set(IPacketBuilder, new PacketBuilder());
|
|
||||||
var wserver = new Server();
|
var wserver = new Server();
|
||||||
wserver.run("localhost", 5001);
|
wserver.run("localhost", 5001);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +1,37 @@
|
|||||||
package ru.m.tankz.server.session;
|
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 haxe.io.Bytes;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
import ru.m.connect.IConnection;
|
import ru.m.connect.IConnection;
|
||||||
import ru.m.connect.neko.NekoConnection;
|
import ru.m.connect.neko.NekoConnection;
|
||||||
import ru.m.connect.neko.NekoWebConnection;
|
import ru.m.connect.neko.NekoWebConnection;
|
||||||
import ru.m.tankz.proto.core.User;
|
import ru.m.tankz.proto.core.User;
|
||||||
import ru.m.tankz.proto.pack.LoginRequest;
|
import ru.m.tankz.proto.pack.Request;
|
||||||
import ru.m.tankz.proto.pack.LoginResponse;
|
import ru.m.tankz.proto.pack.Response;
|
||||||
import sys.net.Socket;
|
import sys.net.Socket;
|
||||||
import Type;
|
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 static var sessions:Map<Int, Session> = new Map<Int, Session>();
|
||||||
|
|
||||||
public var user(default, null):User;
|
public var user(default, null):User;
|
||||||
public var gameId(default, null):Int = -1;
|
public var gameId(default, null):Int = -1;
|
||||||
|
|
||||||
private var connection(default, null):IConnection;
|
public var connection(default, null):ServerConnection;
|
||||||
|
|
||||||
private var socket:Socket;
|
private var socket:Socket;
|
||||||
|
|
||||||
public function new(socket:Socket) {
|
public function new(socket:Socket) {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function send(packet:Message):Void {
|
public function send(packet:Response):Void {
|
||||||
connection.send(packet);
|
connection.send(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,47 +41,32 @@ class Session implements IConnectionHandler implements IPacketHandler {
|
|||||||
} else {
|
} else {
|
||||||
var str:String = bytes.getString(0, bytes.length);
|
var str:String = bytes.getString(0, bytes.length);
|
||||||
if (StringTools.startsWith(str, "GET")) {
|
if (StringTools.startsWith(str, "GET")) {
|
||||||
connection = new NekoWebConnection(socket);
|
connection = new NekoWebConnection<Response, Request>(socket, Request);
|
||||||
} else {
|
} else {
|
||||||
connection = new NekoConnection(socket);
|
connection = new NekoConnection<Response, Request>(socket, Request);
|
||||||
}
|
}
|
||||||
connection.handler.addListener(this);
|
connection.handler.connect(onConnectionEvent);
|
||||||
connection.packetHandler.addListener(this);
|
connection.packetHandler.connect(onRequest);
|
||||||
connection.pushData(bytes);
|
connection.pushData(bytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function onConnectionEvent(event:ConnectionEvent):Void {
|
||||||
* Connection handlers
|
L.d('Session', '${this}, ${event}');
|
||||||
**/
|
|
||||||
public function onConnected():Void {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onDisconnected():Void {
|
public function onRequest(request:Request):Void {
|
||||||
/*if (person != null) {
|
if (request.hasLogin()) {
|
||||||
var game = GameManager.byPersonId.get(person.id);
|
connection.send(new Response().setLogin(login(request.login)));
|
||||||
if (game != null) game.leave(person);
|
}
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onError(error:Dynamic):Void {
|
private function login(request:LoginRequest):LoginResponse {
|
||||||
/*connection.send(
|
return new LoginResponse().setUser(
|
||||||
new ErrorResponse()
|
|
||||||
.setCode(0)
|
|
||||||
.setMessage(Std.string(error))
|
|
||||||
);*/
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Packets handlers
|
|
||||||
**/
|
|
||||||
public function onLoginRequest(packet:LoginRequest):Void {
|
|
||||||
connection.send(new LoginResponse().setUser(
|
|
||||||
new User()
|
new User()
|
||||||
.setUuid(packet.uuid != null ? packet.uuid : 'xxx')
|
.setUuid(request.uuid != null ? request.uuid : 'xxx')
|
||||||
.setName(packet.name)
|
.setName(request.name)
|
||||||
));
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public function onPersonSelectRequest(packet:PersonSelectRequest):Void {
|
/*public function onPersonSelectRequest(packet:PersonSelectRequest):Void {
|
||||||
|
|||||||
Reference in New Issue
Block a user