Commit e8ef0195 authored by nanahira's avatar nanahira

add custom ctos and stoc

parent 384c2f69
...@@ -1615,16 +1615,25 @@ net.createServer (client) -> ...@@ -1615,16 +1615,25 @@ net.createServer (client) ->
cancel = false cancel = false
if settings.modules.reconnect.enabled and client.pre_reconnecting and ygopro.constants.CTOS[ctos_proto] != 'UPDATE_DECK' if settings.modules.reconnect.enabled and client.pre_reconnecting and ygopro.constants.CTOS[ctos_proto] != 'UPDATE_DECK'
cancel = true cancel = true
if ygopro.ctos_follows[ctos_proto] and !cancel
b = ctos_buffer.slice(3, ctos_message_length - 1 + 3) b = ctos_buffer.slice(3, ctos_message_length - 1 + 3)
info = null info = null
if struct = ygopro.structs[ygopro.proto_structs.CTOS[ygopro.constants.CTOS[ctos_proto]]] if struct = ygopro.structs[ygopro.proto_structs.CTOS[ygopro.constants.CTOS[ctos_proto]]]
struct._setBuff(b) struct._setBuff(b)
info = _.clone(struct.fields) info = _.clone(struct.fields)
if ygopro.ctos_follows[ctos_proto].synchronous if ygopro.ctos_follows_before[ctos_proto] and !cancel
cancel = ygopro.ctos_follows[ctos_proto].callback b, info, client, client.server, datas for ctos_event in ygopro.ctos_follows_before[ctos_proto]
else result = ctos_event.callback b, info, client, client.server, datas
ygopro.ctos_follows[ctos_proto].callback b, info, client, client.server, datas if result and ctos_event.synchronous
cancel = true
if ygopro.ctos_follows[ctos_proto] and !cancel
result = ygopro.ctos_follows[ctos_proto].callback b, info, client, client.server, datas
if result and ygopro.ctos_follows[ctos_proto].synchronous
cancel = true
if ygopro.ctos_follows_after[ctos_proto] and !cancel
for ctos_event in ygopro.ctos_follows_after[ctos_proto]
result = ctos_event.callback b, info, client, client.server, datas
if result and ctos_event.synchronous
cancel = true
datas.push ctos_buffer.slice(0, 2 + ctos_message_length) unless cancel datas.push ctos_buffer.slice(0, 2 + ctos_message_length) unless cancel
ctos_buffer = ctos_buffer.slice(2 + ctos_message_length) ctos_buffer = ctos_buffer.slice(2 + ctos_message_length)
ctos_message_length = 0 ctos_message_length = 0
...@@ -1683,17 +1692,25 @@ net.createServer (client) -> ...@@ -1683,17 +1692,25 @@ net.createServer (client) ->
if stoc_buffer.length >= 2 + stoc_message_length if stoc_buffer.length >= 2 + stoc_message_length
#console.log "STOC", ygopro.constants.STOC[stoc_proto] #console.log "STOC", ygopro.constants.STOC[stoc_proto]
cancel = false cancel = false
stanzas = stoc_proto
if ygopro.stoc_follows[stoc_proto]
b = stoc_buffer.slice(3, stoc_message_length - 1 + 3) b = stoc_buffer.slice(3, stoc_message_length - 1 + 3)
info = null info = null
if struct = ygopro.structs[ygopro.proto_structs.STOC[ygopro.constants.STOC[stoc_proto]]] if struct = ygopro.structs[ygopro.proto_structs.STOC[ygopro.constants.STOC[stoc_proto]]]
struct._setBuff(b) struct._setBuff(b)
info = _.clone(struct.fields) info = _.clone(struct.fields)
if ygopro.stoc_follows[stoc_proto].synchronous if ygopro.stoc_follows_before[stoc_proto] and !cancel
cancel = ygopro.stoc_follows[stoc_proto].callback b, info, server.client, server, datas for stoc_event in ygopro.stoc_follows_before[stoc_proto]
else result = stoc_event.callback b, info, server.client, server, datas
ygopro.stoc_follows[stoc_proto].callback b, info, server.client, server, datas if result and stoc_event.synchronous
cancel = true
if ygopro.stoc_follows[stoc_proto] and !cancel
result = ygopro.stoc_follows[stoc_proto].callback b, info, server.client, server, datas
if result and ygopro.stoc_follows[stoc_proto].synchronous
cancel = true
if ygopro.stoc_follows_after[stoc_proto] and !cancel
for stoc_event in ygopro.stoc_follows_after[stoc_proto]
result = stoc_event.callback b, info, server.client, server, datas
if result and stoc_event.synchronous
cancel = true
datas.push stoc_buffer.slice(0, 2 + stoc_message_length) unless cancel datas.push stoc_buffer.slice(0, 2 + stoc_message_length) unless cancel
stoc_buffer = stoc_buffer.slice(2 + stoc_message_length) stoc_buffer = stoc_buffer.slice(2 + stoc_message_length)
stoc_message_length = 0 stoc_message_length = 0
......
...@@ -2037,7 +2037,7 @@ ...@@ -2037,7 +2037,7 @@
} }
client.pre_establish_buffers = new Array(); client.pre_establish_buffers = new Array();
client.on('data', function(ctos_buffer) { client.on('data', function(ctos_buffer) {
var b, bad_ip_count, buffer, cancel, ctos_message_length, ctos_proto, datas, info, len2, len3, looplimit, m, n, room, struct; var b, bad_ip_count, buffer, cancel, ctos_event, ctos_message_length, ctos_proto, datas, info, len2, len3, len4, len5, looplimit, m, n, o, p, ref2, ref3, result, room, struct;
if (client.is_post_watcher) { if (client.is_post_watcher) {
room = ROOM_all[client.rid]; room = ROOM_all[client.rid];
if (room && !CLIENT_is_banned_by_mc(client)) { if (room && !CLIENT_is_banned_by_mc(client)) {
...@@ -2071,17 +2071,36 @@ ...@@ -2071,17 +2071,36 @@
if (settings.modules.reconnect.enabled && client.pre_reconnecting && ygopro.constants.CTOS[ctos_proto] !== 'UPDATE_DECK') { if (settings.modules.reconnect.enabled && client.pre_reconnecting && ygopro.constants.CTOS[ctos_proto] !== 'UPDATE_DECK') {
cancel = true; cancel = true;
} }
if (ygopro.ctos_follows[ctos_proto] && !cancel) {
b = ctos_buffer.slice(3, ctos_message_length - 1 + 3); b = ctos_buffer.slice(3, ctos_message_length - 1 + 3);
info = null; info = null;
if (struct = ygopro.structs[ygopro.proto_structs.CTOS[ygopro.constants.CTOS[ctos_proto]]]) { if (struct = ygopro.structs[ygopro.proto_structs.CTOS[ygopro.constants.CTOS[ctos_proto]]]) {
struct._setBuff(b); struct._setBuff(b);
info = _.clone(struct.fields); info = _.clone(struct.fields);
} }
if (ygopro.ctos_follows[ctos_proto].synchronous) { if (ygopro.ctos_follows_before[ctos_proto] && !cancel) {
cancel = ygopro.ctos_follows[ctos_proto].callback(b, info, client, client.server, datas); ref2 = ygopro.ctos_follows_before[ctos_proto];
} else { for (m = 0, len2 = ref2.length; m < len2; m++) {
ygopro.ctos_follows[ctos_proto].callback(b, info, client, client.server, datas); ctos_event = ref2[m];
result = ctos_event.callback(b, info, client, client.server, datas);
if (result && ctos_event.synchronous) {
cancel = true;
}
}
}
if (ygopro.ctos_follows[ctos_proto] && !cancel) {
result = ygopro.ctos_follows[ctos_proto].callback(b, info, client, client.server, datas);
if (result && ygopro.ctos_follows[ctos_proto].synchronous) {
cancel = true;
}
}
if (ygopro.ctos_follows_after[ctos_proto] && !cancel) {
ref3 = ygopro.ctos_follows_after[ctos_proto];
for (n = 0, len3 = ref3.length; n < len3; n++) {
ctos_event = ref3[n];
result = ctos_event.callback(b, info, client, client.server, datas);
if (result && ctos_event.synchronous) {
cancel = true;
}
} }
} }
if (!cancel) { if (!cancel) {
...@@ -2114,20 +2133,20 @@ ...@@ -2114,20 +2133,20 @@
return; return;
} }
if (client.established) { if (client.established) {
for (m = 0, len2 = datas.length; m < len2; m++) { for (o = 0, len4 = datas.length; o < len4; o++) {
buffer = datas[m]; buffer = datas[o];
client.server.write(buffer); client.server.write(buffer);
} }
} else { } else {
for (n = 0, len3 = datas.length; n < len3; n++) { for (p = 0, len5 = datas.length; p < len5; p++) {
buffer = datas[n]; buffer = datas[p];
client.pre_establish_buffers.push(buffer); client.pre_establish_buffers.push(buffer);
} }
} }
} }
}); });
server.on('data', function(stoc_buffer) { server.on('data', function(stoc_buffer) {
var b, buffer, cancel, datas, info, len2, looplimit, m, stanzas, stoc_message_length, stoc_proto, struct; var b, buffer, cancel, datas, info, len2, len3, len4, looplimit, m, n, o, ref2, ref3, result, stoc_event, stoc_message_length, stoc_proto, struct;
stoc_message_length = 0; stoc_message_length = 0;
stoc_proto = 0; stoc_proto = 0;
datas = []; datas = [];
...@@ -2152,18 +2171,36 @@ ...@@ -2152,18 +2171,36 @@
} else { } else {
if (stoc_buffer.length >= 2 + stoc_message_length) { if (stoc_buffer.length >= 2 + stoc_message_length) {
cancel = false; cancel = false;
stanzas = stoc_proto;
if (ygopro.stoc_follows[stoc_proto]) {
b = stoc_buffer.slice(3, stoc_message_length - 1 + 3); b = stoc_buffer.slice(3, stoc_message_length - 1 + 3);
info = null; info = null;
if (struct = ygopro.structs[ygopro.proto_structs.STOC[ygopro.constants.STOC[stoc_proto]]]) { if (struct = ygopro.structs[ygopro.proto_structs.STOC[ygopro.constants.STOC[stoc_proto]]]) {
struct._setBuff(b); struct._setBuff(b);
info = _.clone(struct.fields); info = _.clone(struct.fields);
} }
if (ygopro.stoc_follows[stoc_proto].synchronous) { if (ygopro.stoc_follows_before[stoc_proto] && !cancel) {
cancel = ygopro.stoc_follows[stoc_proto].callback(b, info, server.client, server, datas); ref2 = ygopro.stoc_follows_before[stoc_proto];
} else { for (m = 0, len2 = ref2.length; m < len2; m++) {
ygopro.stoc_follows[stoc_proto].callback(b, info, server.client, server, datas); stoc_event = ref2[m];
result = stoc_event.callback(b, info, server.client, server, datas);
if (result && stoc_event.synchronous) {
cancel = true;
}
}
}
if (ygopro.stoc_follows[stoc_proto] && !cancel) {
result = ygopro.stoc_follows[stoc_proto].callback(b, info, server.client, server, datas);
if (result && ygopro.stoc_follows[stoc_proto].synchronous) {
cancel = true;
}
}
if (ygopro.stoc_follows_after[stoc_proto] && !cancel) {
ref3 = ygopro.stoc_follows_after[stoc_proto];
for (n = 0, len3 = ref3.length; n < len3; n++) {
stoc_event = ref3[n];
result = stoc_event.callback(b, info, server.client, server, datas);
if (result && stoc_event.synchronous) {
cancel = true;
}
} }
} }
if (!cancel) { if (!cancel) {
...@@ -2185,8 +2222,8 @@ ...@@ -2185,8 +2222,8 @@
} }
} }
if (server.client && !server.client.closed) { if (server.client && !server.client.closed) {
for (m = 0, len2 = datas.length; m < len2; m++) { for (o = 0, len4 = datas.length; o < len4; o++) {
buffer = datas[m]; buffer = datas[o];
server.client.write(buffer); server.client.write(buffer);
} }
} }
......
...@@ -37,26 +37,55 @@ for name, declaration of structs_declaration ...@@ -37,26 +37,55 @@ for name, declaration of structs_declaration
#消息跟踪函数 需要重构, 另暂时只支持异步, 同步没做. #消息跟踪函数 需要重构, 另暂时只支持异步, 同步没做.
@stoc_follows = {} @stoc_follows = {}
@stoc_follows_before = {}
@stoc_follows_after = {}
@ctos_follows = {} @ctos_follows = {}
@stoc_follow = (proto, synchronous, callback)-> @ctos_follows_before = {}
if typeof proto == 'string' @ctos_follows_after = {}
for key, value of @constants.STOC
@replace_proto = (proto, tp) ->
if typeof(proto) != "string"
return proto
changed_proto = proto
for key, value of @constants[tp]
if value == proto if value == proto
proto = key changed_proto = key
break break
throw "unknown proto" if !@constants.STOC[proto] throw "unknown proto" if !@constants[tp][changed_proto]
@stoc_follows[proto] = {callback: callback, synchronous: synchronous} return changed_proto
@stoc_follow = (proto, synchronous, callback)->
changed_proto = @replace_proto(proto, "STOC")
@stoc_follows[changed_proto] = {callback: callback, synchronous: synchronous}
return
@stoc_follow_before = (proto, synchronous, callback)->
changed_proto = @replace_proto(proto, "STOC")
if !@stoc_follows_before[changed_proto]
@stoc_follows_before[changed_proto] = []
@stoc_follows_before[changed_proto].push({callback: callback, synchronous: synchronous})
return
@stoc_follow_after = (proto, synchronous, callback)->
changed_proto = @replace_proto(proto, "STOC")
if !@stoc_follows_after[changed_proto]
@stoc_follows_after[changed_proto] = []
@stoc_follows_after[changed_proto].push({callback: callback, synchronous: synchronous})
return return
@ctos_follow = (proto, synchronous, callback)-> @ctos_follow = (proto, synchronous, callback)->
if typeof proto == 'string' changed_proto = @replace_proto(proto, "CTOS")
for key, value of @constants.CTOS @ctos_follows[changed_proto] = {callback: callback, synchronous: synchronous}
if value == proto return
proto = key @ctos_follow_before = (proto, synchronous, callback)->
break changed_proto = @replace_proto(proto, "CTOS")
throw "unknown proto" if !@constants.CTOS[proto] if !@ctos_follows_before[changed_proto]
@ctos_follows[proto] = {callback: callback, synchronous: synchronous} @ctos_follows_before[changed_proto] = []
@ctos_follows_before[changed_proto].push({callback: callback, synchronous: synchronous})
return
@ctos_follow_after = (proto, synchronous, callback)->
changed_proto = @replace_proto(proto, "CTOS")
if !@ctos_follows_after[changed_proto]
@ctos_follows_after[changed_proto] = []
@ctos_follows_after[changed_proto].push({callback: callback, synchronous: synchronous})
return return
#消息发送函数,至少要把俩合起来.... #消息发送函数,至少要把俩合起来....
@stoc_send = (socket, proto, info)-> @stoc_send = (socket, proto, info)->
......
...@@ -58,48 +58,100 @@ ...@@ -58,48 +58,100 @@
this.stoc_follows = {}; this.stoc_follows = {};
this.stoc_follows_before = {};
this.stoc_follows_after = {};
this.ctos_follows = {}; this.ctos_follows = {};
this.stoc_follow = function(proto, synchronous, callback) { this.ctos_follows_before = {};
var key, ref, value;
if (typeof proto === 'string') { this.ctos_follows_after = {};
ref = this.constants.STOC;
this.replace_proto = function(proto, tp) {
var changed_proto, key, ref, value;
if (typeof proto !== "string") {
return proto;
}
changed_proto = proto;
ref = this.constants[tp];
for (key in ref) { for (key in ref) {
value = ref[key]; value = ref[key];
if (value === proto) { if (value === proto) {
proto = key; changed_proto = key;
break; break;
} }
} }
if (!this.constants.STOC[proto]) { if (!this.constants[tp][changed_proto]) {
throw "unknown proto"; throw "unknown proto";
} }
} return changed_proto;
this.stoc_follows[proto] = { };
this.stoc_follow = function(proto, synchronous, callback) {
var changed_proto;
changed_proto = this.replace_proto(proto, "STOC");
this.stoc_follows[changed_proto] = {
callback: callback, callback: callback,
synchronous: synchronous synchronous: synchronous
}; };
}; };
this.ctos_follow = function(proto, synchronous, callback) { this.stoc_follow_before = function(proto, synchronous, callback) {
var key, ref, value; var changed_proto;
if (typeof proto === 'string') { changed_proto = this.replace_proto(proto, "STOC");
ref = this.constants.CTOS; if (!this.stoc_follows_before[changed_proto]) {
for (key in ref) { this.stoc_follows_before[changed_proto] = [];
value = ref[key];
if (value === proto) {
proto = key;
break;
}
} }
if (!this.constants.CTOS[proto]) { this.stoc_follows_before[changed_proto].push({
throw "unknown proto"; callback: callback,
synchronous: synchronous
});
};
this.stoc_follow_after = function(proto, synchronous, callback) {
var changed_proto;
changed_proto = this.replace_proto(proto, "STOC");
if (!this.stoc_follows_after[changed_proto]) {
this.stoc_follows_after[changed_proto] = [];
} }
this.stoc_follows_after[changed_proto].push({
callback: callback,
synchronous: synchronous
});
};
this.ctos_follow = function(proto, synchronous, callback) {
var changed_proto;
changed_proto = this.replace_proto(proto, "CTOS");
this.ctos_follows[changed_proto] = {
callback: callback,
synchronous: synchronous
};
};
this.ctos_follow_before = function(proto, synchronous, callback) {
var changed_proto;
changed_proto = this.replace_proto(proto, "CTOS");
if (!this.ctos_follows_before[changed_proto]) {
this.ctos_follows_before[changed_proto] = [];
} }
this.ctos_follows[proto] = { this.ctos_follows_before[changed_proto].push({
callback: callback, callback: callback,
synchronous: synchronous synchronous: synchronous
});
}; };
this.ctos_follow_after = function(proto, synchronous, callback) {
var changed_proto;
changed_proto = this.replace_proto(proto, "CTOS");
if (!this.ctos_follows_after[changed_proto]) {
this.ctos_follows_after[changed_proto] = [];
}
this.ctos_follows_after[changed_proto].push({
callback: callback,
synchronous: synchronous
});
}; };
this.stoc_send = function(socket, proto, info) { this.stoc_send = function(socket, proto, info) {
......
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