Commit 894a1a31 authored by mercury233's avatar mercury233

test1

parent 086af16a
......@@ -4,8 +4,13 @@ config.json
/ygocore/
/ygocore
/node_modules/
node_modules/.bin/
node_modules/bunyan/
node_modules/request/
node_modules/underscore/
node_modules/underscore.string/
*.heapsnapshot
*.tmp
*.bak
*.log
......
function byteField(p, offset) {
this.length = 1;
this.get = function () {
return p.buf[offset];
}
this.set = function (val) {
p.buf[offset] = val;
}
}
function boolField(p, offset, length) {
this.length = length;
this.get = function () {
return (p.buf[offset] > 0 );
}
this.set = function (val) {
p.buf[offset] = val ? 1 : 0;
}
}
function intField(p, offset, length, le, signed) {
this.length = length;
function bec(cb) {
for (var i = 0; i < length; i++)
cb(i, length - i - 1);
}
function lec(cb) {
for (var i = 0; i < length; i++)
cb(i, i);
}
function getUVal(bor) {
var val = 0;
bor(function (i, o) {
val += Math.pow(256, o) * p.buf[offset + i];
})
return val;
}
function getSVal(bor) {
var val = getUVal(bor);
if ((p.buf[offset + ( le ? (length - 1) : 0)] & 0x80) == 0x80) {
val -= Math.pow(256, length);
}
return val;
}
function setVal(bor, val) {
bor(function (i, o) {
p.buf[offset + i] = Math.floor(val / Math.pow(256, o)) & 0xff;
});
}
this.get = function () {
var bor = le ? lec : bec;
return ( signed ? getSVal(bor) : getUVal(bor));
}
this.set = function (val) {
var bor = le ? lec : bec;
setVal(bor, val);
}
}
function charField(p, offset, length, encoding) {
this.length = length;
this.encoding = encoding;
this.get = function () {
var result = p.buf.toString(this.encoding, offset, offset + length);
var strlen = result.indexOf("\0");
if (strlen == -1) {
return result;
} else {
return result.slice(0, strlen);
}
}
this.set = function (val) {
val += "\0";
if (val.length > length)
val = val.substring(0, length);
p.buf.write(val, offset, this.encoding);
}
}
function structField(p, offset, struct) {
this.length = struct.length();
this.get = function () {
return struct;
}
this.set = function (val) {
struct.set(val);
}
this.allocate = function () {
struct._setBuff(p.buf.slice(offset, offset + struct.length()));
}
}
function arrayField(p, offset, len, type) {
var as = Struct();
var args = [].slice.call(arguments, 4);
args.unshift(0);
for (var i = 0; i < len; i++) {
if (type instanceof Struct) {
as.struct(i, type.clone());
} else if (type in as) {
args[0] = i;
as[type].apply(as, args);
}
}
this.length = as.length();
this.allocate = function () {
as._setBuff(p.buf.slice(offset, offset + as.length()));
}
this.get = function () {
return as;
}
this.set = function (val) {
as.set(val);
}
}
function Struct() {
if (!(this instanceof Struct))
return new Struct;
var priv = {
buf: {},
allocated: false,
len: 0,
fields: {},
closures: []
}, self = this;
function checkAllocated() {
if (priv.allocated)
throw new Error('Cant change struct after allocation');
}
this.word8 = function (key) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new byteField(p, p.len);
p.len++;
});
return this;
};
// Create handlers for various Bool Field Variants
[1, 2, 3, 4].forEach(function (n) {
self['bool' + (n == 1 ? '' : n)] = function (key) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new boolField(p, p.len, n);
p.len += n;
});
return this;
}
});
// Create handlers for various Integer Field Variants
[1, 2, 3, 4, 6, 8].forEach(function (n) {
[true, false].forEach(function (le) {
[true, false].forEach(function (signed) {
var name = 'word' + (n * 8) + ( signed ? 'S' : 'U') + ( le ? 'le' : 'be');
self[name] = function (key) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new intField(p, p.len, n, le, signed);
p.len += n;
});
return this;
};
});
});
});
this.chars = function (key, length, encoding) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new charField(p, p.len, length, encoding || 'ascii');
p.len += length;
});
return this;
}
this.struct = function (key, struct) {
checkAllocated();
priv.closures.push(function (p) {
p.fields[key] = new structField(p, p.len, struct.clone());
p.len += p.fields[key].length;
});
return this;
}
function construct(constructor, args) {
function F() {
return constructor.apply(this, args);
}
F.prototype = constructor.prototype;
return new F();
}
this.array = function (key, length, type) {
checkAllocated();
var args = [].slice.call(arguments, 1);
args.unshift(null);
args.unshift(null);
priv.closures.push(function (p) {
args[0] = p;
args[1] = p.len;
p.fields[key] = construct(arrayField, args);
p.len += p.fields[key].length;
});
return this;
}
var beenHere = false;
function applyClosures(p) {
if (beenHere)
return;
p.closures.forEach(function (el) {
el(p);
});
beenHere = true;
}
function allocateFields() {
for (var key in priv.fields) {
if ('allocate' in priv.fields[key])
priv.fields[key].allocate();
}
}
this._setBuff = function (buff) {
priv.buf = buff;
applyClosures(priv);
allocateFields();
priv.allocated = true;
}
this.allocate = function () {
applyClosures(priv);
priv.buf = new Buffer(priv.len);
allocateFields();
priv.allocated = true;
return this;
}
this._getPriv = function () {
return priv;
}
this.clone = function () {
var c = new Struct;
var p = c._getPriv();
p.closures = priv.closures;
return c;
}
this.length = function () {
applyClosures(priv);
return priv.len;
}
this.get = function (key) {
if (key in priv.fields) {
return priv.fields[key].get();
} else
throw new Error('Can not find field ' + key);
}
this.set = function (key, val) {
if (arguments.length == 2) {
if (key in priv.fields) {
priv.fields[key].set(val);
} else
throw new Error('Can not find field ' + key);
} else if (Buffer.isBuffer(key)) {
this._setBuff(key);
} else {
for (var k in key) {
this.set(k, key[k]);
}
}
}
this.buffer = function () {
return priv.buf;
}
function getFields() {
var fields = {};
Object.keys(priv.fields).forEach(function (key) {
Object.defineProperty(fields, key, {
get: function () {
var res = self.get(key);
if (res instanceof Struct) return res.fields;
else return res;
},
set: function (newVal) {
self.set(key, newVal);
},
enumerable: true
});
});
return fields;
};
var _fields;
Object.defineProperty(this, 'fields', {
get: function () {
if (_fields) return _fields;
return (_fields = getFields());
},
enumerable: true,
configurable: true
});
}
exports.Struct = Struct;
......@@ -93,10 +93,12 @@ class Room
player.server.connect @port, '127.0.0.1',=>
player.server.write buffer for buffer in player.pre_establish_buffers
player.established = true
player.pre_establish_buffers = null
delete: ->
#积分
return if @deleted
#log.info 'room-delete', this.name, Room.all.length
index = _.indexOf(Room.all, this)
#Room.all[index] = null unless index == -1
Room.all.splice(index, 1) unless index == -1
......@@ -109,6 +111,7 @@ class Room
client.server.connect @port, '127.0.0.1', ->
client.server.write buffer for buffer in client.pre_establish_buffers
client.established = true
client.pre_establish_buffers = null
disconnect: (client, error)->
if client.is_post_watcher
......
......@@ -118,7 +118,8 @@
buffer = ref[i];
player.server.write(buffer);
}
return player.established = true;
player.established = true;
return player.pre_establish_buffers = null;
});
});
};
......@@ -147,7 +148,8 @@
buffer = ref[i];
client.server.write(buffer);
}
return client.established = true;
client.established = true;
return client.pre_establish_buffers = null;
});
}
};
......
......@@ -16,7 +16,7 @@ request = require 'request'
bunyan = require 'bunyan'
#heapdump = require 'heapdump'
heapdump = require 'heapdump'
#配置文件
settings = require './config.json'
......@@ -35,17 +35,32 @@ if process.argv[2] == '--debug'
log = bunyan.createLogger name: "mycard-debug"
else
log = bunyan.createLogger name: "mycard"
###
#定时清理关闭的连接
Graveyard = []
send_to_graveyard = (socket) ->
unless _.indexOf(Graveyard, socket)
Graveyard.push(socket)
tribute = (socket) ->
setTimeout send_to_graveyard(socket), 30000
setInterval ()->
log.info Graveyard
, 30000
###
#网络连接
net.createServer (client) ->
server = new net.Socket()
client.server = server
client.setTimeout(300000) # 5分钟
client.setTimeout(300000) #5分钟
#释放处理
client.on 'close', (had_error) ->
#log.info "client closed", client.name, had_error
#tribute(client)
unless client.closed
client.closed = true
client.room.disconnect(client) if client.room
......@@ -53,6 +68,7 @@ net.createServer (client) ->
client.on 'error', (error)->
#log.info "client error", client.name, error
#tribute(client)
unless client.closed
client.closed = error
client.room.disconnect(client, error) if client.room
......@@ -63,6 +79,7 @@ net.createServer (client) ->
server.on 'close', (had_error) ->
#log.info "server closed", client.name, had_error
#tribute(server)
server.closed = true unless server.closed
unless client.closed
ygopro.stoc_send_chat(client, "服务器关闭了连接")
......@@ -70,6 +87,7 @@ net.createServer (client) ->
server.on 'error', (error)->
#log.info "server error", client.name, error
#tribute(server)
server.closed = error
unless client.closed
ygopro.stoc_send_chat(client, "服务器错误: #{error}")
......@@ -161,7 +179,7 @@ net.createServer (client) ->
stoc_proto = 0
else
break
return 0
.listen settings.port, ->
log.info "server started", settings.ip, settings.port
......
// Generated by CoffeeScript 1.9.3
(function() {
var Room, _, bunyan, debug, dialogues, execFile, fs, http, http_server, log, net, os, path, request, settings, tips, url, ygopro;
var Room, _, bunyan, debug, dialogues, execFile, fs, heapdump, http, http_server, log, net, os, path, request, settings, tips, url, ygopro;
net = require('net');
......@@ -26,6 +26,8 @@
bunyan = require('bunyan');
heapdump = require('heapdump');
settings = require('./config.json');
ygopro = require('./ygopro.js');
......@@ -50,6 +52,23 @@
});
}
/*
#定时清理关闭的连接
Graveyard = []
send_to_graveyard = (socket) ->
unless _.indexOf(Graveyard, socket)
Graveyard.push(socket)
tribute = (socket) ->
setTimeout send_to_graveyard(socket), 30000
setInterval ()->
log.info Graveyard
, 30000
*/
net.createServer(function(client) {
var ctos_buffer, ctos_message_length, ctos_proto, server, stoc_buffer, stoc_message_length, stoc_proto;
server = new net.Socket();
......@@ -146,7 +165,7 @@
stoc_buffer = new Buffer(0);
stoc_message_length = 0;
stoc_proto = 0;
return server.on('data', function(data) {
server.on('data', function(data) {
var b, results, stanzas, struct;
stoc_buffer = Buffer.concat([stoc_buffer, data], stoc_buffer.length + data.length);
client.write(data);
......@@ -186,6 +205,7 @@
}
return results;
});
return 0;
}).listen(settings.port, function() {
return log.info("server started", settings.ip, settings.port);
});
......
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