style: format code
This commit is contained in:
@@ -8,45 +8,45 @@ import protohx.Message;
|
||||
import ru.m.connect.IConnection;
|
||||
|
||||
class BaseConnection<O:Message, I:Message> implements IConnection<O, I> {
|
||||
public var handler(default, null):Signal<ConnectionEvent>;
|
||||
public var sendHandler(default, null):Signal<O>;
|
||||
public var receiveHandler(default, null):Signal<I>;
|
||||
public var connected(default, null):Bool;
|
||||
public var queue(default, null):PacketQueue<I>;
|
||||
public var handler(default, null):Signal<ConnectionEvent>;
|
||||
public var sendHandler(default, null):Signal<O>;
|
||||
public var receiveHandler(default, null):Signal<I>;
|
||||
public var connected(default, null):Bool;
|
||||
public var queue(default, null):PacketQueue<I>;
|
||||
|
||||
private var connectDeferred:Deferred<IConnection<O, I>>;
|
||||
private var connectDeferred:Deferred<IConnection<O, I>>;
|
||||
|
||||
public function new(inputFactory:Class<I>) {
|
||||
queue = new PacketQueue<I>(inputFactory);
|
||||
handler = new Signal<ConnectionEvent>();
|
||||
sendHandler = new Signal<O>();
|
||||
receiveHandler = new Signal<I>();
|
||||
public function new(inputFactory:Class<I>) {
|
||||
queue = new PacketQueue<I>(inputFactory);
|
||||
handler = new Signal<ConnectionEvent>();
|
||||
sendHandler = new Signal<O>();
|
||||
receiveHandler = new Signal<I>();
|
||||
}
|
||||
|
||||
public function connect():Promise<IConnection<O, I>> {
|
||||
throw "Not implemented";
|
||||
}
|
||||
|
||||
public function disconnect():Void {
|
||||
throw "Not implemented";
|
||||
}
|
||||
|
||||
public function pushData(bytes:Bytes):Void {
|
||||
#if proto_debug L.d('Proto', 'pushData: ${bytes.length}'); #end
|
||||
queue.addBytes(bytes);
|
||||
while (queue.hasMsg()) {
|
||||
var packet:I = queue.popMsg();
|
||||
receive(packet);
|
||||
}
|
||||
}
|
||||
|
||||
public function connect():Promise<IConnection<O, I>> {
|
||||
throw "Not implemented";
|
||||
}
|
||||
public function send(packet:O):Void {
|
||||
#if proto_debug L.d('Proto', 'send: ${packet}'); #end
|
||||
sendHandler.emit(packet);
|
||||
}
|
||||
|
||||
public function disconnect():Void {
|
||||
throw "Not implemented";
|
||||
}
|
||||
|
||||
public function pushData(bytes:Bytes):Void {
|
||||
#if proto_debug L.d('Proto', 'pushData: ${bytes.length}'); #end
|
||||
queue.addBytes(bytes);
|
||||
while (queue.hasMsg()) {
|
||||
var packet:I = queue.popMsg();
|
||||
receive(packet);
|
||||
}
|
||||
}
|
||||
|
||||
public function send(packet:O):Void {
|
||||
#if proto_debug L.d('Proto', 'send: ${packet}'); #end
|
||||
sendHandler.emit(packet);
|
||||
}
|
||||
|
||||
public function receive(packet:I):Void {
|
||||
#if proto_debug L.d('Proto', 'receive: ${packet}'); #end
|
||||
receiveHandler.emit(packet);
|
||||
}
|
||||
public function receive(packet:I):Void {
|
||||
#if proto_debug L.d('Proto', 'receive: ${packet}'); #end
|
||||
receiveHandler.emit(packet);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,19 +6,19 @@ import promhx.Promise;
|
||||
import protohx.Message;
|
||||
|
||||
enum ConnectionEvent {
|
||||
CONNECTED;
|
||||
DISCONNECTED;
|
||||
ERROR(error:Dynamic);
|
||||
CONNECTED;
|
||||
DISCONNECTED;
|
||||
ERROR(error:Dynamic);
|
||||
}
|
||||
|
||||
interface IConnection<O:Message, I:Message> {
|
||||
public var connected(default, null):Bool;
|
||||
public var handler(default, null):Signal<ConnectionEvent>;
|
||||
public var sendHandler(default, null):Signal<O>;
|
||||
public var receiveHandler(default, null):Signal<I>;
|
||||
public var connected(default, null):Bool;
|
||||
public var handler(default, null):Signal<ConnectionEvent>;
|
||||
public var sendHandler(default, null):Signal<O>;
|
||||
public var receiveHandler(default, null):Signal<I>;
|
||||
|
||||
public function connect():Promise<IConnection<O, I>>;
|
||||
public function disconnect():Void;
|
||||
public function send(packet:O):Void;
|
||||
public function pushData(bytes:Bytes):Void;
|
||||
public function connect():Promise<IConnection<O, I>>;
|
||||
public function disconnect():Void;
|
||||
public function send(packet:O):Void;
|
||||
public function pushData(bytes:Bytes):Void;
|
||||
}
|
||||
|
||||
@@ -7,65 +7,64 @@ import haxe.io.BytesInput;
|
||||
import protohx.Message;
|
||||
|
||||
class PacketQueue<P:Message> {
|
||||
public var packetClass(default, null):Class<P>;
|
||||
|
||||
public var packetClass(default, null):Class<P>;
|
||||
private var buffer:BytesBuffer;
|
||||
private var msgs:List<P>;
|
||||
|
||||
private var buffer:BytesBuffer;
|
||||
private var msgs:List<P>;
|
||||
public function new(packetClass:Class<P>) {
|
||||
this.packetClass = packetClass;
|
||||
msgs = new List<P>();
|
||||
buffer = new BytesBuffer();
|
||||
}
|
||||
|
||||
public function new(packetClass:Class<P>) {
|
||||
this.packetClass = packetClass;
|
||||
msgs = new List<P>();
|
||||
buffer = new BytesBuffer();
|
||||
}
|
||||
public inline function hasMsg():Bool {
|
||||
return !msgs.isEmpty();
|
||||
}
|
||||
|
||||
public inline function hasMsg():Bool {
|
||||
return !msgs.isEmpty();
|
||||
}
|
||||
public inline function popMsg():P {
|
||||
return msgs.pop();
|
||||
}
|
||||
|
||||
public inline function popMsg():P {
|
||||
return msgs.pop();
|
||||
}
|
||||
public inline function addMsg(msg:P):Void {
|
||||
msgs.add(msg);
|
||||
}
|
||||
|
||||
public inline function addMsg(msg:P):Void {
|
||||
msgs.add(msg);
|
||||
}
|
||||
|
||||
private function readPackage():Null<P> {
|
||||
var bytes = buffer.getBytes();
|
||||
var input = new BytesInput(bytes);
|
||||
input.bigEndian = false;
|
||||
if (input.length > 1) {
|
||||
var packetSize = input.readUInt16();
|
||||
if (input.length >= packetSize + 2) {
|
||||
var packet:P = Type.createInstance(packetClass, []);
|
||||
try {
|
||||
packet.mergeFrom(input.read(packetSize));
|
||||
buffer = new BytesBuffer();
|
||||
buffer.add(input.read(input.length - (packetSize + 2)));
|
||||
return packet;
|
||||
} catch (error:Dynamic) {
|
||||
L.w("PacketQueue", "readPackage ", error);
|
||||
buffer = new BytesBuffer();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private function readPackage():Null<P> {
|
||||
var bytes = buffer.getBytes();
|
||||
var input = new BytesInput(bytes);
|
||||
input.bigEndian = false;
|
||||
if (input.length > 1) {
|
||||
var packetSize = input.readUInt16();
|
||||
if (input.length >= packetSize + 2) {
|
||||
var packet:P = Type.createInstance(packetClass, []);
|
||||
try {
|
||||
packet.mergeFrom(input.read(packetSize));
|
||||
buffer = new BytesBuffer();
|
||||
buffer.add(input.read(input.length - (packetSize + 2)));
|
||||
return packet;
|
||||
} catch (error:Dynamic) {
|
||||
L.w("PacketQueue", "readPackage ", error);
|
||||
buffer = new BytesBuffer();
|
||||
return null;
|
||||
}
|
||||
buffer = new BytesBuffer();
|
||||
buffer.add(bytes);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
buffer = new BytesBuffer();
|
||||
buffer.add(bytes);
|
||||
return null;
|
||||
}
|
||||
|
||||
public function addBytes(bytes:Bytes):Void {
|
||||
buffer.add(bytes);
|
||||
var packet = readPackage();
|
||||
while (packet != null) {
|
||||
msgs.add(packet);
|
||||
packet = readPackage();
|
||||
}
|
||||
public function addBytes(bytes:Bytes):Void {
|
||||
buffer.add(bytes);
|
||||
var packet = readPackage();
|
||||
while (packet != null) {
|
||||
msgs.add(packet);
|
||||
packet = readPackage();
|
||||
}
|
||||
}
|
||||
|
||||
public function clean():Void {
|
||||
buffer = new BytesBuffer();
|
||||
}
|
||||
public function clean():Void {
|
||||
buffer = new BytesBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,16 +5,15 @@ import haxe.io.BytesOutput;
|
||||
import protohx.Message;
|
||||
|
||||
class PacketUtil {
|
||||
public static function fromBytes<P:Message>(bytes:Bytes, factory:Class<P>):P {
|
||||
var packet:P = Type.createInstance(factory, []);
|
||||
packet.mergeFrom(bytes);
|
||||
return packet;
|
||||
}
|
||||
|
||||
public static function fromBytes<P:Message>(bytes:Bytes, factory:Class<P>):P {
|
||||
var packet:P = Type.createInstance(factory, []);
|
||||
packet.mergeFrom(bytes);
|
||||
return packet;
|
||||
}
|
||||
|
||||
public static function toBytes<P:Message>(packet:P):Bytes {
|
||||
var out = new BytesOutput();
|
||||
packet.writeTo(out);
|
||||
return out.getBytes();
|
||||
}
|
||||
public static function toBytes<P:Message>(packet:P):Bytes {
|
||||
var out = new BytesOutput();
|
||||
packet.writeTo(out);
|
||||
return out.getBytes();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,23 +3,21 @@ package ru.m.connect;
|
||||
import haxe.io.BytesOutput;
|
||||
import protohx.Message;
|
||||
|
||||
|
||||
class WebSocketTools {
|
||||
public static function packet2string(packet:Message):String {
|
||||
var b = new BytesOutput();
|
||||
packet.writeTo(b);
|
||||
var data = b.getBytes();
|
||||
var res = new BytesOutput();
|
||||
// res.writeUInt16(data.length);
|
||||
res.write(data);
|
||||
return Base64.encodeBase64(res.getBytes());
|
||||
}
|
||||
|
||||
public static function packet2string(packet:Message):String {
|
||||
var b = new BytesOutput();
|
||||
packet.writeTo(b);
|
||||
var data = b.getBytes();
|
||||
var res = new BytesOutput();
|
||||
//res.writeUInt16(data.length);
|
||||
res.write(data);
|
||||
return Base64.encodeBase64(res.getBytes());
|
||||
}
|
||||
|
||||
public static function string2packet<P:Message>(data:String, packetClass:Class<P>):P {
|
||||
var bytes = Base64.decodeBase64(data);
|
||||
var packet:P = Type.createInstance(packetClass, []);
|
||||
packet.mergeFrom(bytes);
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
public static function string2packet<P:Message>(data:String, packetClass:Class<P>):P {
|
||||
var bytes = Base64.decodeBase64(data);
|
||||
var packet:P = Type.createInstance(packetClass, []);
|
||||
packet.mergeFrom(bytes);
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,67 +10,66 @@ import sys.net.Host;
|
||||
import sys.net.Socket;
|
||||
|
||||
class DesktopConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
private var host:String;
|
||||
private var port:Int;
|
||||
private var socket:Socket;
|
||||
private var reader:Thread;
|
||||
|
||||
private var host:String;
|
||||
private var port:Int;
|
||||
private var socket:Socket;
|
||||
private var reader:Thread;
|
||||
public function new(host:String, port:Int, inputFactory:Class<I>) {
|
||||
super(inputFactory);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
connected = false;
|
||||
socket = new Socket();
|
||||
socket.setFastSend(true);
|
||||
socket.output.bigEndian = false;
|
||||
socket.input.bigEndian = false;
|
||||
sendHandler.connect(_send);
|
||||
}
|
||||
|
||||
public function new(host:String, port:Int, inputFactory:Class<I>) {
|
||||
super(inputFactory);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
connected = false;
|
||||
socket = new Socket();
|
||||
socket.setFastSend(true);
|
||||
socket.output.bigEndian = false;
|
||||
socket.input.bigEndian = false;
|
||||
sendHandler.connect(_send);
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
connectDeferred = new Deferred();
|
||||
try {
|
||||
if (connected) {
|
||||
connectDeferred.resolve(this);
|
||||
} else {
|
||||
socket.connect(new Host(host), port);
|
||||
connected = true;
|
||||
reader = Thread.create(_read);
|
||||
connectDeferred.resolve(this);
|
||||
handler.emit(ConnectionEvent.CONNECTED);
|
||||
}
|
||||
} catch (error:Dynamic) {
|
||||
handler.emit(ConnectionEvent.ERROR(error));
|
||||
Timer.delay(function() connectDeferred.throwError(error), 1);
|
||||
}
|
||||
return connectDeferred.promise();
|
||||
}
|
||||
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
connectDeferred = new Deferred();
|
||||
try {
|
||||
if (connected) {
|
||||
connectDeferred.resolve(this);
|
||||
} else {
|
||||
socket.connect(new Host(host), port);
|
||||
connected = true;
|
||||
reader = Thread.create(_read);
|
||||
connectDeferred.resolve(this);
|
||||
handler.emit(ConnectionEvent.CONNECTED);
|
||||
}
|
||||
} catch (error:Dynamic) {
|
||||
handler.emit(ConnectionEvent.ERROR(error));
|
||||
Timer.delay(function() connectDeferred.throwError(error), 1);
|
||||
}
|
||||
return connectDeferred.promise();
|
||||
}
|
||||
override public function disconnect():Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.emit(DISCONNECTED);
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.emit(DISCONNECTED);
|
||||
private function _read():Void {
|
||||
try {
|
||||
while (connected) {
|
||||
socket.waitForRead();
|
||||
var size = socket.input.readUInt16();
|
||||
var data = socket.input.read(size);
|
||||
var packet:I = PacketUtil.fromBytes(data, queue.packetClass);
|
||||
receiveHandler.emit(packet);
|
||||
}
|
||||
} catch (error:Dynamic) {
|
||||
handler.emit(ERROR(error));
|
||||
}
|
||||
}
|
||||
|
||||
private function _read():Void {
|
||||
try {
|
||||
while (connected) {
|
||||
socket.waitForRead();
|
||||
var size = socket.input.readUInt16();
|
||||
var data = socket.input.read(size);
|
||||
var packet:I = PacketUtil.fromBytes(data, queue.packetClass);
|
||||
receiveHandler.emit(packet);
|
||||
}
|
||||
} catch (error:Dynamic) {
|
||||
handler.emit(ERROR(error));
|
||||
}
|
||||
}
|
||||
|
||||
private function _send(packet:O):Void {
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.output.writeUInt16(bytes.length);
|
||||
socket.output.write(bytes);
|
||||
socket.output.flush();
|
||||
}
|
||||
private function _send(packet:O):Void {
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.output.writeUInt16(bytes.length);
|
||||
socket.output.write(bytes);
|
||||
socket.output.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,14 +5,13 @@ import promhx.Promise;
|
||||
import protohx.Message;
|
||||
|
||||
class FakeConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
handler.emit(ConnectionEvent.CONNECTED);
|
||||
var promise:Promise<IConnection<O, I>> = cast Promise.promise(this);
|
||||
return promise;
|
||||
}
|
||||
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
handler.emit(ConnectionEvent.CONNECTED);
|
||||
var promise:Promise<IConnection<O, I>> = cast Promise.promise(this);
|
||||
return promise;
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
}
|
||||
override public function disconnect():Void {
|
||||
handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,76 +14,75 @@ import protohx.Message;
|
||||
import ru.m.connect.IConnection;
|
||||
|
||||
class FlashConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
private var host:String;
|
||||
private var port:Int;
|
||||
private var socket:Socket;
|
||||
|
||||
private var host:String;
|
||||
private var port:Int;
|
||||
private var socket:Socket;
|
||||
public function new(host:String, port:Int, inputFactory:Class<I>) {
|
||||
super(inputFactory);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
connected = false;
|
||||
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 = Endian.LITTLE_ENDIAN;
|
||||
sendHandler.connect(_send);
|
||||
}
|
||||
|
||||
public function new(host:String, port:Int, inputFactory:Class<I>) {
|
||||
super(inputFactory);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
connected = false;
|
||||
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 = Endian.LITTLE_ENDIAN;
|
||||
sendHandler.connect(_send);
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
socket.connect(host, port);
|
||||
connectDeferred = new Deferred();
|
||||
return connectDeferred.promise();
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
if (socket.connected) {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
}
|
||||
}
|
||||
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
socket.connect(host, port);
|
||||
connectDeferred = new Deferred();
|
||||
return connectDeferred.promise();
|
||||
private function onError(event:ErrorEvent):Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.ERROR(event));
|
||||
if (connectDeferred != null) {
|
||||
connectDeferred.throwError(event);
|
||||
connectDeferred = null;
|
||||
}
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
if (socket.connected) {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
}
|
||||
private function onConnect(_):Void {
|
||||
connected = true;
|
||||
handler.emit(ConnectionEvent.CONNECTED);
|
||||
if (connectDeferred != null) {
|
||||
connectDeferred.resolve(this);
|
||||
connectDeferred = null;
|
||||
}
|
||||
}
|
||||
|
||||
private function onError(event:ErrorEvent):Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.ERROR(event));
|
||||
if (connectDeferred != null) {
|
||||
connectDeferred.throwError(event);
|
||||
connectDeferred = null;
|
||||
}
|
||||
}
|
||||
private function onClose(_):Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
}
|
||||
|
||||
private function onConnect(_):Void {
|
||||
connected = true;
|
||||
handler.emit(ConnectionEvent.CONNECTED);
|
||||
if (connectDeferred != null) {
|
||||
connectDeferred.resolve(this);
|
||||
connectDeferred = null;
|
||||
}
|
||||
}
|
||||
private function onSocketData(_):Void {
|
||||
var data = new flash.utils.ByteArray();
|
||||
socket.readBytes(data);
|
||||
var bytes = Bytes.ofData(data);
|
||||
pushData(bytes);
|
||||
}
|
||||
|
||||
private function onClose(_):Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
}
|
||||
|
||||
private function onSocketData(_):Void {
|
||||
var data = new flash.utils.ByteArray();
|
||||
socket.readBytes(data);
|
||||
var bytes = Bytes.ofData(data);
|
||||
pushData(bytes);
|
||||
}
|
||||
|
||||
private function _send(packet:O):Void {
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.writeShort(bytes.length);
|
||||
socket.writeBytes(bytes.getData());
|
||||
socket.flush();
|
||||
}
|
||||
private function _send(packet:O):Void {
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.writeShort(bytes.length);
|
||||
socket.writeBytes(bytes.getData());
|
||||
socket.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,82 +10,81 @@ import protohx.Message;
|
||||
import ru.m.connect.IConnection;
|
||||
|
||||
class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
private var host:String;
|
||||
private var port:Int;
|
||||
private var socket:WebSocket;
|
||||
|
||||
private var host:String;
|
||||
private var port:Int;
|
||||
private var socket:WebSocket;
|
||||
public function new(host:String, port:Int, inputFactory:Class<I>) {
|
||||
super(inputFactory);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
connected = false;
|
||||
}
|
||||
|
||||
public function new(host:String, port:Int, inputFactory:Class<I>) {
|
||||
super(inputFactory);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
connected = false;
|
||||
public static function isSecured():Bool {
|
||||
return Browser.location.protocol == "https:";
|
||||
}
|
||||
|
||||
private function buildSocket(host:String, port:Int):WebSocket {
|
||||
var protocol = isSecured() ? "wss:" : "ws:";
|
||||
return new WebSocket('$protocol//$host:$port');
|
||||
}
|
||||
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
var self = this;
|
||||
socket = buildSocket(host, port);
|
||||
socket.binaryType = BinaryType.ARRAYBUFFER;
|
||||
socket.onopen = this.onConnect;
|
||||
socket.onclose = this.onClose;
|
||||
socket.onerror = this.onError;
|
||||
socket.onmessage = this.onSocketData;
|
||||
connectDeferred = new Deferred();
|
||||
return connectDeferred.promise();
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
socket.close(1000);
|
||||
connected = false;
|
||||
}
|
||||
|
||||
private function onError(event:Dynamic):Void {
|
||||
socket.close(1000);
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.ERROR(event));
|
||||
}
|
||||
|
||||
private function onConnect(_):Void {
|
||||
connected = true;
|
||||
handler.emit(ConnectionEvent.CONNECTED);
|
||||
connectDeferred.resolve(this);
|
||||
}
|
||||
|
||||
private function onClose(_):Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
}
|
||||
|
||||
private function onSocketData(event:Dynamic):Void {
|
||||
var packet:I = null;
|
||||
try {
|
||||
var bytes = Bytes.ofData(event.data);
|
||||
packet = PacketUtil.fromBytes(bytes, queue.packetClass);
|
||||
} catch (error:Dynamic) {
|
||||
handler.emit(ConnectionEvent.ERROR(error));
|
||||
}
|
||||
|
||||
public static function isSecured():Bool {
|
||||
return Browser.location.protocol == "https:";
|
||||
if (packet != null) {
|
||||
receive(packet);
|
||||
}
|
||||
}
|
||||
|
||||
private function buildSocket(host:String, port:Int):WebSocket {
|
||||
var protocol = isSecured() ? "wss:" : "ws:";
|
||||
return new WebSocket('$protocol//$host:$port');
|
||||
}
|
||||
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
var self = this;
|
||||
socket = buildSocket(host, port);
|
||||
socket.binaryType = BinaryType.ARRAYBUFFER;
|
||||
socket.onopen = this.onConnect;
|
||||
socket.onclose = this.onClose;
|
||||
socket.onerror = this.onError;
|
||||
socket.onmessage = this.onSocketData;
|
||||
connectDeferred = new Deferred();
|
||||
return connectDeferred.promise();
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
socket.close(1000);
|
||||
connected = false;
|
||||
}
|
||||
|
||||
private function onError(event:Dynamic):Void {
|
||||
socket.close(1000);
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.ERROR(event));
|
||||
}
|
||||
|
||||
private function onConnect(_):Void {
|
||||
connected = true;
|
||||
handler.emit(ConnectionEvent.CONNECTED);
|
||||
connectDeferred.resolve(this);
|
||||
}
|
||||
|
||||
private function onClose(_):Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
}
|
||||
|
||||
private function onSocketData(event:Dynamic):Void {
|
||||
var packet:I = null;
|
||||
try {
|
||||
var bytes = Bytes.ofData(event.data);
|
||||
packet = PacketUtil.fromBytes(bytes, queue.packetClass);
|
||||
} catch (error:Dynamic) {
|
||||
handler.emit(ConnectionEvent.ERROR(error));
|
||||
}
|
||||
if (packet != null) {
|
||||
receive(packet);
|
||||
}
|
||||
}
|
||||
|
||||
override public function send(packet:O):Void {
|
||||
if (connected) {
|
||||
super.send(packet);
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.send(bytes.getData());
|
||||
} else {
|
||||
L.w("Connection", "closed");
|
||||
}
|
||||
override public function send(packet:O):Void {
|
||||
if (connected) {
|
||||
super.send(packet);
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.send(bytes.getData());
|
||||
} else {
|
||||
L.w("Connection", "closed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,53 +4,52 @@ import protohx.Message;
|
||||
import sys.net.Socket;
|
||||
|
||||
class NekoConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
public var socket(default, null):Socket;
|
||||
|
||||
public var socket(default, null):Socket;
|
||||
private var sendQueue:Array<O>;
|
||||
private var timer:Timer;
|
||||
|
||||
private var sendQueue:Array<O>;
|
||||
private var timer:Timer;
|
||||
public function new(socket:Socket, i:Class<I>) {
|
||||
super(i);
|
||||
this.socket = socket;
|
||||
socket.setFastSend(true);
|
||||
socket.output.bigEndian = false;
|
||||
socket.input.bigEndian = false;
|
||||
sendHandler.connect(pushPacket);
|
||||
sendQueue = [];
|
||||
timer = new Timer(1);
|
||||
timer.run = sendRun;
|
||||
}
|
||||
|
||||
public function new(socket:Socket, i:Class<I>) {
|
||||
super(i);
|
||||
this.socket = socket;
|
||||
socket.setFastSend(true);
|
||||
socket.output.bigEndian = false;
|
||||
socket.input.bigEndian = false;
|
||||
sendHandler.connect(pushPacket);
|
||||
sendQueue = [];
|
||||
timer = new Timer(1);
|
||||
timer.run = sendRun;
|
||||
private function sendPacket(packet:O):Void {
|
||||
try {
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.output.writeUInt16(bytes.length);
|
||||
socket.output.write(bytes);
|
||||
socket.output.flush();
|
||||
} catch (error:Dynamic) {
|
||||
L.e('Proto', 'Error send packet: ${packet}', error);
|
||||
}
|
||||
}
|
||||
|
||||
private function sendPacket(packet:O):Void {
|
||||
try {
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
socket.output.writeUInt16(bytes.length);
|
||||
socket.output.write(bytes);
|
||||
socket.output.flush();
|
||||
} catch (error:Dynamic) {
|
||||
L.e('Proto', 'Error send packet: ${packet}', error);
|
||||
}
|
||||
private function sendRun():Void {
|
||||
if (sendQueue.length > 0) {
|
||||
for (packet in sendQueue) {
|
||||
sendPacket(packet);
|
||||
}
|
||||
sendQueue = [];
|
||||
}
|
||||
}
|
||||
|
||||
private function sendRun():Void {
|
||||
if (sendQueue.length > 0) {
|
||||
for (packet in sendQueue) {
|
||||
sendPacket(packet);
|
||||
}
|
||||
sendQueue = [];
|
||||
}
|
||||
}
|
||||
private function pushPacket(packet:O):Void {
|
||||
sendQueue.push(packet);
|
||||
}
|
||||
|
||||
private function pushPacket(packet:O):Void {
|
||||
sendQueue.push(packet);
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
}
|
||||
super.disconnect();
|
||||
override public function disconnect():Void {
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
}
|
||||
super.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,171 +8,166 @@ import protohx.Message;
|
||||
import sys.net.Socket;
|
||||
|
||||
class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
private var opened:Bool;
|
||||
|
||||
private var opened:Bool;
|
||||
public function new(socket:Socket, i:Class<I>) {
|
||||
super(socket, i);
|
||||
opened = false;
|
||||
}
|
||||
|
||||
public function new(socket:Socket, i:Class<I>) {
|
||||
super(socket, i);
|
||||
opened = false;
|
||||
override private function sendPacket(packet:O):Void {
|
||||
var data = PacketUtil.toBytes(packet);
|
||||
writeData(data, socket);
|
||||
}
|
||||
|
||||
override public function pushData(bytes:Bytes):Void {
|
||||
if (!opened) {
|
||||
var str:String = bytes.getString(0, bytes.length);
|
||||
if (StringTools.startsWith(str, "GET")) {
|
||||
var r = ~/Sec-WebSocket-Key:\s*([A-z0-9=+\/]+)/;
|
||||
r.match(str);
|
||||
opened = true;
|
||||
sendServerHandShake(socket, r.matched(1));
|
||||
}
|
||||
} else {
|
||||
var data = parseData(bytes);
|
||||
if (data != null) {
|
||||
var packet:I = PacketUtil.fromBytes(data, queue.packetClass);
|
||||
receive(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function encodeBase64(content:String):String {
|
||||
var suffix = switch (content.length % 3) {
|
||||
case 2: "=";
|
||||
case 1: "==";
|
||||
default: "";
|
||||
};
|
||||
return BaseCode.encode(content, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") + suffix;
|
||||
}
|
||||
|
||||
private function hex2data(hex:String):String {
|
||||
var data = "";
|
||||
for (i in 0...Std.int(hex.length / 2)) {
|
||||
data += String.fromCharCode(Std.parseInt("0x" + hex.substr(i * 2, 2)));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private function sendServerHandShake(socket:sys.net.Socket, inpKey:String) {
|
||||
var outKey = encodeBase64(hex2data(Sha1.encode(StringTools.trim(inpKey) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")));
|
||||
|
||||
var s = "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n"
|
||||
+ "Sec-WebSocket-Accept: " + outKey + "\r\n" + "\r\n";
|
||||
|
||||
socket.output.writeString(s);
|
||||
}
|
||||
|
||||
private function writeData(data:Bytes, socket:sys.net.Socket, isServer = true):Void {
|
||||
socket.output.writeByte(130);
|
||||
|
||||
var len = 0;
|
||||
if (data.length < 126)
|
||||
len = data.length;
|
||||
else if (data.length < 65536)
|
||||
len = 126;
|
||||
else
|
||||
len = 127;
|
||||
|
||||
socket.output.writeByte(len | (!isServer ? 0x80 : 0x00));
|
||||
|
||||
if (data.length >= 126) {
|
||||
if (data.length < 65536) {
|
||||
socket.output.writeByte((data.length >> 8) & 0xFF);
|
||||
socket.output.writeByte(data.length & 0xFF);
|
||||
} else {
|
||||
socket.output.writeByte((data.length >> 24) & 0xFF);
|
||||
socket.output.writeByte((data.length >> 16) & 0xFF);
|
||||
socket.output.writeByte((data.length >> 8) & 0xFF);
|
||||
socket.output.writeByte(data.length & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
override private function sendPacket(packet:O):Void {
|
||||
var data = PacketUtil.toBytes(packet);
|
||||
writeData(data, socket);
|
||||
if (isServer) {
|
||||
socket.output.writeBytes(data, 0, data.length);
|
||||
} 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 BytesBuffer();
|
||||
for (i in 0...data.length) {
|
||||
maskedData.addByte(data.get(i) ^ mask[i % 4]);
|
||||
}
|
||||
socket.output.writeBytes(maskedData.getBytes(), 0, maskedData.length);
|
||||
}
|
||||
}
|
||||
|
||||
private function parseData(bytes:Bytes):Bytes {
|
||||
var p = 0;
|
||||
var opcode = bytes.get(p++);
|
||||
|
||||
if (opcode == 0x00) {
|
||||
var data = new BytesBuffer();
|
||||
var b:Int;
|
||||
while ((b = bytes.get(p++)) != 0xFF) {
|
||||
data.addByte(b);
|
||||
}
|
||||
return data.getBytes();
|
||||
}
|
||||
|
||||
override public function pushData(bytes:Bytes):Void {
|
||||
if (!opened) {
|
||||
var str:String = bytes.getString(0, bytes.length);
|
||||
if (StringTools.startsWith(str, "GET")) {
|
||||
var r = ~/Sec-WebSocket-Key:\s*([A-z0-9=+\/]+)/;
|
||||
r.match(str);
|
||||
opened = true;
|
||||
sendServerHandShake(socket, r.matched(1));
|
||||
}
|
||||
} else {
|
||||
var data = parseData(bytes);
|
||||
if (data != null) {
|
||||
var packet:I = PacketUtil.fromBytes(data, queue.packetClass);
|
||||
receive(packet);
|
||||
}
|
||||
// 130 = binary data
|
||||
if (opcode == 130) {
|
||||
var len = bytes.get(p++);
|
||||
|
||||
// mask
|
||||
if (len & 0x80 != 0) {
|
||||
len &= 0x7F;
|
||||
|
||||
if (len == 126) {
|
||||
var b2 = bytes.get(p++);
|
||||
var b3 = bytes.get(p++);
|
||||
len = (b2 << 8) + b3;
|
||||
} else if (len == 127) {
|
||||
var b2 = bytes.get(p++);
|
||||
var b3 = bytes.get(p++);
|
||||
var b4 = bytes.get(p++);
|
||||
var b5 = bytes.get(p++);
|
||||
len = (b2 << 24) + (b3 << 16) + (b4 << 8) + b5;
|
||||
}
|
||||
|
||||
// Lib.println("len = " + len);
|
||||
|
||||
// direct array init not work corectly!
|
||||
var mask = [];
|
||||
mask.push(bytes.get(p++));
|
||||
mask.push(bytes.get(p++));
|
||||
mask.push(bytes.get(p++));
|
||||
mask.push(bytes.get(p++));
|
||||
|
||||
// Lib.println("mask = " + mask);
|
||||
|
||||
var data = new BytesBuffer();
|
||||
for (i in 0...len) {
|
||||
data.addByte(bytes.get(p++) ^ mask[i % 4]);
|
||||
}
|
||||
|
||||
// Lib.println("readed = " + data.toString());
|
||||
return data.getBytes();
|
||||
} else {
|
||||
throw "Expected masked data.";
|
||||
}
|
||||
}
|
||||
|
||||
private function encodeBase64(content:String):String {
|
||||
var suffix = switch (content.length % 3)
|
||||
{
|
||||
case 2: "=";
|
||||
case 1: "==";
|
||||
default: "";
|
||||
};
|
||||
return BaseCode.encode(content, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") + suffix;
|
||||
}
|
||||
|
||||
private function hex2data(hex:String):String {
|
||||
var data = "";
|
||||
for (i in 0...Std.int(hex.length / 2)) {
|
||||
data += String.fromCharCode(Std.parseInt("0x" + hex.substr(i * 2, 2)));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private function sendServerHandShake(socket:sys.net.Socket, inpKey:String) {
|
||||
var outKey = encodeBase64(hex2data(Sha1.encode(StringTools.trim(inpKey) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")));
|
||||
|
||||
var s = "HTTP/1.1 101 Switching Protocols\r\n"
|
||||
+ "Upgrade: websocket\r\n"
|
||||
+ "Connection: Upgrade\r\n"
|
||||
+ "Sec-WebSocket-Accept: " + outKey + "\r\n"
|
||||
+ "\r\n";
|
||||
|
||||
socket.output.writeString(s);
|
||||
}
|
||||
|
||||
private function writeData(data:Bytes, socket:sys.net.Socket, isServer = true):Void {
|
||||
socket.output.writeByte(130);
|
||||
|
||||
var len = 0;
|
||||
if (data.length < 126) len = data.length;
|
||||
else if (data.length < 65536) len = 126;
|
||||
else len = 127;
|
||||
|
||||
socket.output.writeByte(len | (!isServer ? 0x80 : 0x00));
|
||||
|
||||
if (data.length >= 126) {
|
||||
if (data.length < 65536) {
|
||||
socket.output.writeByte((data.length >> 8) & 0xFF);
|
||||
socket.output.writeByte(data.length & 0xFF);
|
||||
}
|
||||
else {
|
||||
socket.output.writeByte((data.length >> 24) & 0xFF);
|
||||
socket.output.writeByte((data.length >> 16) & 0xFF);
|
||||
socket.output.writeByte((data.length >> 8) & 0xFF);
|
||||
socket.output.writeByte(data.length & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
if (isServer) {
|
||||
socket.output.writeBytes(data, 0, data.length);
|
||||
}
|
||||
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 BytesBuffer();
|
||||
for (i in 0...data.length) {
|
||||
maskedData.addByte(data.get(i) ^ mask[i % 4]);
|
||||
}
|
||||
socket.output.writeBytes(maskedData.getBytes(), 0, maskedData.length);
|
||||
}
|
||||
}
|
||||
|
||||
private function parseData(bytes:Bytes):Bytes {
|
||||
var p = 0;
|
||||
var opcode = bytes.get(p++);
|
||||
|
||||
if (opcode == 0x00) {
|
||||
var data = new BytesBuffer();
|
||||
var b:Int;
|
||||
while ((b = bytes.get(p++)) != 0xFF) {
|
||||
data.addByte(b);
|
||||
}
|
||||
return data.getBytes();
|
||||
}
|
||||
|
||||
// 130 = binary data
|
||||
if (opcode == 130) {
|
||||
var len = bytes.get(p++);
|
||||
|
||||
// mask
|
||||
if (len & 0x80 != 0) {
|
||||
len &= 0x7F;
|
||||
|
||||
if (len == 126) {
|
||||
var b2 = bytes.get(p++);
|
||||
var b3 = bytes.get(p++);
|
||||
len = (b2 << 8) + b3;
|
||||
}
|
||||
else if (len == 127) {
|
||||
var b2 = bytes.get(p++);
|
||||
var b3 = bytes.get(p++);
|
||||
var b4 = bytes.get(p++);
|
||||
var b5 = bytes.get(p++);
|
||||
len = (b2 << 24) + (b3 << 16) + (b4 << 8) + b5;
|
||||
}
|
||||
|
||||
//Lib.println("len = " + len);
|
||||
|
||||
// direct array init not work corectly!
|
||||
var mask = [];
|
||||
mask.push(bytes.get(p++));
|
||||
mask.push(bytes.get(p++));
|
||||
mask.push(bytes.get(p++));
|
||||
mask.push(bytes.get(p++));
|
||||
|
||||
//Lib.println("mask = " + mask);
|
||||
|
||||
var data = new BytesBuffer();
|
||||
for (i in 0...len) {
|
||||
data.addByte(bytes.get(p++) ^ mask[i % 4]);
|
||||
}
|
||||
|
||||
//Lib.println("readed = " + data.toString());
|
||||
return data.getBytes();
|
||||
} else {
|
||||
throw "Expected masked data.";
|
||||
}
|
||||
}
|
||||
|
||||
if (opcode == 136) {
|
||||
//socket.close();
|
||||
opened = false;
|
||||
return null;
|
||||
} else {
|
||||
throw "Unsupported websocket opcode: " + opcode;
|
||||
}
|
||||
return null;
|
||||
if (opcode == 136) {
|
||||
// socket.close();
|
||||
opened = false;
|
||||
return null;
|
||||
} else {
|
||||
throw "Unsupported websocket opcode: " + opcode;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user