Skip to content

Commit

Permalink
init draft
Browse files Browse the repository at this point in the history
  • Loading branch information
shihuaping authored and shihuaping committed Aug 14, 2017
1 parent 1e4e6c8 commit 0fc2a8c
Show file tree
Hide file tree
Showing 23 changed files with 567 additions and 0 deletions.
43 changes: 43 additions & 0 deletions gate_svr/client-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

const logger = require('./logger');
const connections = require('./connections');
const cmdDefine = require('../protocol/cmd-define');

function onConnection(fd,ws) {
logger.info("client connected,fd:%d,ip:%s", fd, ws.ip);
connections.addClientConn(fd,ws);
}

function onMessage(ws,msg) {

logger.info("client read data,fd:%d,ip:%s", ws.psudoID, ws.ip);
connections.updateClient(ws);

var jObj = JSON.parse(msg);
var mainCmd = jObj.head.mcmd;
var subCmd = jObj.head.scmd;

switch (mainCmd) {
case cmdDefine.HEART_BEAT:
ws.send(msg);
break;
case cmdDefine.LOGIN:

default:
logger.error("client fd:%d send unkown cmd:%d", ws.psudoID, mainCmd);
}
}

function onError(ws,err) {
logger.error("client error,fd:%d,ip:%s", fd, ws.ip);
logger.error(err);
const fd = ws.psudoID;
connections.removeClientConn(fd);
}

function onClose(ws) {
logger.info("client close,fd:%d,ip:%s", fd, ws.ip);

const fd = ws.psudoID;
connections.removeClientConn(fd);
}
25 changes: 25 additions & 0 deletions gate_svr/config/log4js.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"appenders": {
"gatefile": {
"type": "file",
"filename": "log/gate.log",
"maxLogSize": 10485760,
"numBackups": 3
},
"console": {
"type": "console",
"level": "debug"
}
},
"categories": {
"default": {
"appenders": [
"console",
"gatefile"
],
"level": "debug"
}
}
}


8 changes: 8 additions & 0 deletions gate_svr/config/sys-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

{
"svrHost":"127.0.0.1",
"svrPort":9000,
"redisHost":"127.0.0.1",
"redisPort":6379,
"redisPassword":""
}
142 changes: 142 additions & 0 deletions gate_svr/connections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@

var clientConns = {};
var svrConns = {};
var mapConns = {};

function addClientConn(fd,conn) {
clientConns[fd] = {conn:conn,updateTime:new Date().getTime()};
}

function updateClientConn(conn) {
conn.updateTime = new Date().getTime();
}

function addSvrConn(fd, conn) {
svrConns[fd] = {conn:conn,updateTime:new Date().getTime()};
}

function addMapConn(fdClient, fdSvr) {
mapConns[fdClient] = fdSvr;
mapConns[fdSvr] = fdClient;
}

function updateSvrConn(conn) {
conn.updateTime = new Date().getTime();
}

function getClientConn(fd) {
if (fd in clientConns) {
return clientConns[fd].conn;
}
return null;
}

function getSvrConn(fd) {
if (fd in svrConns) {
return svrConns[fd].conn;
}
return null;
}

function getMapConn(fd) {
if (fd in mapConns) {
return mapConns[fd];
}
return 0;
}

function removeSvrConn(fd) {
const clientFd = getMapConn(fd);
if (!!clientFd) {
delete mapConns[clientFd];
delete mapConns[fd];
}
if (fd in svrConns) {
delete svrConns[fd];
}
if (clientFd in clientConns) {
const conn = clientConns[clientFd];
if (!!conn) conn.close();
delete clientConns[clientFd];
}
}

function removeClientConn(fd) {
const svrFd = getMapConn(fd);
if (!!svrFd) {
delete mapConns[svrFd];
delete mapConns[fd];
}
if (fd in clientConns) {
delete clientConns[fd];
}
if (svrFd in svrConns) {
const conn = [svrFd];
if (!!conn) conn.destroy();
delete svrConns[svrFd];
}
}

function checkTimeout() {

var outTimeFd = [];
const now = new Date().getTime();
for (k in clientConns) {
if ((now - clientConns[k].updateTime) > 60*1000) {
outTimeFd.push(k);
}
}

for (var i=0; i<outTimeFd.length;i++ ) {
logger.error("client fd:%d timeout", outTimeFd[i]);
const fd = outTimeFd[i];
if (fd in clientConns) {
delete clientConns[fd];
const svrFd = mapConns[fd];
if (!!svrFd) {
if (svrFd in svrConns) {
svrConns[svrFd].conn.destroy();
delete svrConns[svrFd];
}
}
}
}

outTimeFd = []
for (k in svrConns) {
if((now - svrConns[k].updateTime) > 60*1000) {
outTimeFd.push(k);
}
}

for (var i=0; i<outTimeFd.length;i++ ) {
logger.error("server fd:%d timeout", outTimeFd[i]);
const fd = outTimeFd[i];
if (fd in clientConns) {
svrConns[fd].conn.destroy();
delete svrConns[fd];
const clientFd = mapConns[fd];
if (!!clientFd) {
if (clientFd in clientConns) {
clientConns[svrFd].conn.close();
delete clientConns[svrFd];
}
}
}
}


}

setInterval(checkTimeout, 10*1000);

exports.addClientConn = addClientConn;
exports.updateClientConn = updateClientConn;
exports.addSvrConn = addSvrConn;
exports.updateSvrConn = updateSvrConn();
exports.getClientConn = getClientConn;
exports.getSvrConn = getSvrConn;
exports.addMapConn = addMapConn;
exports.getMapConn = getMapConn;
exports.removeClientConn = removeClientConn;
exports.removeSvrConn = removeSvrConn;
8 changes: 8 additions & 0 deletions gate_svr/gate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

const wsServer = require('./ws-server');
const sysConfig = require('./config/sys-config.json');
const logger = require('./logger');

logger.debug(sysConfig);

wsServer.startServer(sysConfig);
2 changes: 2 additions & 0 deletions gate_svr/log/gate.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[2017-08-13 10:28:40.647] [DEBUG] gate - { svrHost: '127.0.0.1', svrPort: 9000 }
[2017-08-13 10:29:28.450] [DEBUG] gate - { svrHost: '127.0.0.1', svrPort: 9000 }
7 changes: 7 additions & 0 deletions gate_svr/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

const config = require('./config/log4js.json');

const log4js = require('log4js');
log4js.configure(config);

module.exports = log4js.getLogger();
20 changes: 20 additions & 0 deletions gate_svr/redis-oper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

const sysConfig = require('./config/sys-config.json');
const redis = require('redis');
//const redisPool = require('redis-pool-connection');

var host = sysConfig.redisHost;
var port = sysConfig.redisPort;
var password = sysConfig.redisPassword;

function readCachedServers() {
var rds = null;
if (!!password && password.length > 0) {
rds = redis.createClient({host:host,port:port,password:password});
} else {
rds = redis.createClient({host:host,port:port});
}

conn.hget
}

29 changes: 29 additions & 0 deletions gate_svr/server-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

const logger = require('./logger');
const connections = require('./connections');

function onConnection(fd,tcpConn) {
logger.info("client connected,fd:%d,ip:%s", fd, tcpConn.ip);
connections.addSvrConn(fd,tcpConn);
}

function onMessage(tcpConn,msg) {
logger.info("client read data,fd:%d,ip:%s", fd, tcpConn.ip);
connections.updateSvrConn(tcpConn);

var jObj = JSON.parse(msg);
}

function onError(tcpConn,err) {
logger.error("client error,fd:%d,ip:%s", fd, tcpConn.ip);
logger.error(err);
const fd = ws.psudoID;
connections.removeSvrConn(fd);
}

function onClose(tcpConn) {
logger.info("client close,fd:%d,ip:%s", fd, tcpConn.ip);

const fd = tcpConn.psudoID;
connections.removeSvrConn(fd);
}
13 changes: 13 additions & 0 deletions gate_svr/short-ID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

var idx = 1024;

function getNextID() {
if(idx > 4200000000) {
idx = 1024;
}
idx++;
return idx;
}


exports.getNextID = getNextID;
11 changes: 11 additions & 0 deletions gate_svr/sys-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


var cached = {}

// read servers info from redis
function readCached() {

}


setInterval(readCached, 60*1000);
43 changes: 43 additions & 0 deletions gate_svr/tcp-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@


const net = require('net');
const sysConfig = require('./config/sys-config.json');
const serverHandler = require('./server-handler');
const logger = require('./logger');
const shortID = require('./short-ID');

function getNewConnection(port,host) {

if (!port) port = sysConfig.centerSvrPort;
if (!host) host = sysConfig.centerSvrHost;

var conn = net.createConnection({port:port,host:host}, function () {
const psudoID = shortID.getNextID();
conn.psudoID = psudoID;
conn.ip = host;

logger.info("connect to server,ip:%s,port:%d", host, port);
serverHandler.onConnect(psudoID, conn);
});

conn.on('end', function () {

logger.info("server connection closed,fd:%d,ip:%s", conn.psudoID, conn.ip);
serverHandler.onClose(conn);
});

conn.on('error', function (err) {

logger.info("server connection error,fd:%d,ip:%s", conn.psudoID, conn.ip);
logger.error(conn,err);
serverHandler.onError(conn);
});

conn.on('data', function (data) {
console.log("server fd:%d get data:%s", conn.psudoID, data);
serverHandler.onData(data);
});

};

exports.getNewConnection = getNewConnection;
Loading

0 comments on commit 0fc2a8c

Please sign in to comment.