Commit 3547c9ab authored by nanahira's avatar nanahira

fix

parent c7e68ffa
...@@ -117,12 +117,9 @@ class YGOProMessagesHelper { ...@@ -117,12 +117,9 @@ class YGOProMessagesHelper {
} }
return parseInt(translatedProto); return parseInt(translatedProto);
} }
sendMessage(socket, protostr, info) { prepareMessage(protostr, info) {
const { direction, proto } = this.getDirectionAndProto(protostr); const { direction, proto } = this.getDirectionAndProto(protostr);
let buffer; let buffer;
if (!socket.remoteAddress) {
return;
}
//console.log(proto, this.proto_structs[direction][proto]); //console.log(proto, this.proto_structs[direction][proto]);
//const directionProtoList = this.constants[direction]; //const directionProtoList = this.constants[direction];
if (typeof info === 'undefined') { if (typeof info === 'undefined') {
...@@ -139,13 +136,27 @@ class YGOProMessagesHelper { ...@@ -139,13 +136,27 @@ class YGOProMessagesHelper {
} }
const translatedProto = this.translateProto(proto, direction); const translatedProto = this.translateProto(proto, direction);
let sendBuffer = Buffer.allocUnsafe(3 + (buffer ? buffer.length : 0)); let sendBuffer = Buffer.allocUnsafe(3 + (buffer ? buffer.length : 0));
if (buffer) {
sendBuffer.writeUInt16LE(buffer.length + 1, 0); sendBuffer.writeUInt16LE(buffer.length + 1, 0);
sendBuffer.writeUInt8(translatedProto, 2); sendBuffer.writeUInt8(translatedProto, 2);
if (buffer) {
buffer.copy(sendBuffer, 3); buffer.copy(sendBuffer, 3);
} }
else {
sendBuffer.writeUInt16LE(1, 0);
sendBuffer.writeUInt8(translatedProto, 2);
}
return buffer;
}
sendMessage(socket, protostr, info) {
const sendBuffer = this.prepareMessage(protostr, info);
socket.write(sendBuffer); socket.write(sendBuffer);
} }
sendMessageAsync(socket, protostr, info) {
const sendBuffer = this.prepareMessage(protostr, info);
return new Promise(done => {
socket.write(sendBuffer, done);
});
}
addHandler(protostr, handler, synchronous, priority) { addHandler(protostr, handler, synchronous, priority) {
if (priority < 0 || priority > 4) { if (priority < 0 || priority > 4) {
throw "Invalid priority: " + priority; throw "Invalid priority: " + priority;
......
...@@ -147,15 +147,12 @@ export class YGOProMessagesHelper { ...@@ -147,15 +147,12 @@ export class YGOProMessagesHelper {
return parseInt(translatedProto); return parseInt(translatedProto);
} }
sendMessage(socket: net.Socket, protostr: string, info: string | Buffer | any) { prepareMessage(protostr: string, info?: string | Buffer | any): Buffer {
const { const {
direction, direction,
proto proto
} = this.getDirectionAndProto(protostr); } = this.getDirectionAndProto(protostr);
let buffer: Buffer; let buffer: Buffer;
if (!socket.remoteAddress) {
return;
}
//console.log(proto, this.proto_structs[direction][proto]); //console.log(proto, this.proto_structs[direction][proto]);
//const directionProtoList = this.constants[direction]; //const directionProtoList = this.constants[direction];
if (typeof info === 'undefined') { if (typeof info === 'undefined') {
...@@ -170,14 +167,29 @@ export class YGOProMessagesHelper { ...@@ -170,14 +167,29 @@ export class YGOProMessagesHelper {
} }
const translatedProto = this.translateProto(proto, direction); const translatedProto = this.translateProto(proto, direction);
let sendBuffer = Buffer.allocUnsafe(3 + (buffer ? buffer.length : 0)); let sendBuffer = Buffer.allocUnsafe(3 + (buffer ? buffer.length : 0));
if (buffer) {
sendBuffer.writeUInt16LE(buffer.length + 1, 0); sendBuffer.writeUInt16LE(buffer.length + 1, 0);
sendBuffer.writeUInt8(translatedProto, 2); sendBuffer.writeUInt8(translatedProto, 2);
if (buffer) { buffer.copy(sendBuffer, 3);
buffer.copy(sendBuffer, 3) } else {
sendBuffer.writeUInt16LE(1, 0);
sendBuffer.writeUInt8(translatedProto, 2);
} }
return buffer;
}
sendMessage(socket: net.Socket, protostr: string, info?: string | Buffer | any) {
const sendBuffer = this.prepareMessage(protostr, info);
socket.write(sendBuffer); socket.write(sendBuffer);
} }
sendMessageAsync(socket: net.Socket, protostr: string, info?: string | Buffer | any): Promise<Error> {
const sendBuffer = this.prepareMessage(protostr, info);
return new Promise(done => {
socket.write(sendBuffer, done);
});
}
addHandler(protostr: string, handler: (buffer: Buffer, info: any, datas: Buffer[], params: any) => Promise<boolean>, synchronous: boolean, priority: number) { addHandler(protostr: string, handler: (buffer: Buffer, info: any, datas: Buffer[], params: any) => Promise<boolean>, synchronous: boolean, priority: number) {
if (priority < 0 || priority > 4) { if (priority < 0 || priority > 4) {
throw "Invalid priority: " + priority; throw "Invalid priority: " + priority;
...@@ -196,7 +208,7 @@ export class YGOProMessagesHelper { ...@@ -196,7 +208,7 @@ export class YGOProMessagesHelper {
handlerCollection.get(translatedProto).push(handlerObj); handlerCollection.get(translatedProto).push(handlerObj);
} }
async handleBuffer(messageBuffer: Buffer, direction: string, protoFilter: string[], params: any): Promise<HandleResult> { async handleBuffer(messageBuffer: Buffer, direction: string, protoFilter?: string[], params?: any): Promise<HandleResult> {
let feedback: Feedback = null; let feedback: Feedback = null;
let messageLength = 0; let messageLength = 0;
let bufferProto = 0; let bufferProto = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment