Skip to content

Commit

Permalink
提交其它修改
Browse files Browse the repository at this point in the history
  • Loading branch information
shihuaping committed Aug 14, 2017
1 parent 218ef52 commit 580b35d
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 15 deletions.
4 changes: 3 additions & 1 deletion gate_svr/config/sys-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
"svrPort":9000,
"redisHost":"127.0.0.1",
"redisPort":6379,
"redisPassword":""
"redisPassword":"",
"centerSvrHost":"127.0.0.1",
"centerSvrPort":9200
}
2 changes: 1 addition & 1 deletion gate_svr/connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ setInterval(checkTimeout, 10*1000);
exports.addClientConn = addClientConn;
exports.updateClientConn = updateClientConn;
exports.addSvrConn = addSvrConn;
exports.updateSvrConn = updateSvrConn();
exports.updateSvrConn = updateSvrConn;
exports.getClientConn = getClientConn;
exports.getSvrConn = getSvrConn;
exports.addMapConn = addMapConn;
Expand Down
5 changes: 5 additions & 0 deletions gate_svr/gate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
const wsServer = require('./ws-server');
const sysConfig = require('./config/sys-config.json');
const logger = require('./logger');
const registerCenter = require('./register-center');


logger.debug(sysConfig);

registerCenter.registerSelf();
setInterval(registerCenter.registerSelf, 10*1000);

wsServer.startServer(sysConfig);
74 changes: 74 additions & 0 deletions gate_svr/register-center.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

const net = require('net');
const sysConfig = require('./config/sys-config.json');
const packet = require('../protocol/packet');
const cmdDefine = require('../protocol/cmd-define');
const constDefine = require('../lib/const-define');
const shortID = require('./short-ID');
const logger = require('./logger');



let conn = null;

function getNewConnection() {

let port = sysConfig.centerSvrPort;
let host = sysConfig.centerSvrHost;

console.log("connec to host:%s,port:%d", host, port);
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);
});

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

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

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

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

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

}

getNewConnection();

function registerSelf() {

console.log("....");
if (conn.destroyed) {
getNewConnection();
return;
}
const port = sysConfig.centerPort;
const host = sysConfig.centerHost;

let jObj = packet.getPacket(cmdDefine.CENTER, cmdDefine.SUB_CENTER_UPDATE);

let serverInfo = {};
serverInfo.type = constDefine.SERVER_TYPE_GATE;
serverInfo.port = sysConfig.svrPort;

jObj.body = serverInfo;

let json = JSON.stringify(jObj);
console.log(json);
conn.write(json);

conn.destroy();
}



exports.registerSelf = registerSelf;
6 changes: 6 additions & 0 deletions lib/const-define.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

module.exports = {
SERVER_TYPE_LOGIN : 2,
SERVER_TYPE_CENTER : 1,
SERVER_TYPE_GATE : 3
}
7 changes: 7 additions & 0 deletions lib/rds-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

module.exports = {

KEY_SERVER_TYPE : "server_type",
SERVER_TYPE_LIST : [1,2,3],

}
6 changes: 5 additions & 1 deletion login_svr/config/sys-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
"svrPort":9100,
"redisHost":"127.0.0.1",
"redisPort":6379,
"redisPassword":""
"redisPassword":"",
"mysqlHost":"127.0.0.1",
"mysqlPort":3306,
"mysqlUsername":"root",
"mysqlPassword":"123456"
}
51 changes: 44 additions & 7 deletions login_svr/db-oper.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@

const mysql = require('mysql');
const sysConfig = require('./config/sys-config.json');
const logger = require('./logger');


var pool = null;

function getConnection() {
mysql.createPool()
function getPool() {
pool = mysql.createPool(
{
connectionLimit: 50,
host: sysConfig.mysqlHost,
port: sysConfig.mysqlPort,
user: sysConfig.mysqlUsername,
password: sysConfig.mysqlPassword,
database: 'gamedb'
}
);
}

getPool();

function login(userInfo) {
return new Promise(function (resolve, reject) {
try {
//avoid sql injection
let sql = 'select * from user_base where account=' + mysql.escape(userInfo.account) +
' and password=' + mysql.escape(userInfo.password);
console.log(sql);

pool.getConnection(function (err, conn) {
conn.query(sql, function (err, results, fields) {

conn.release();
if (err) {
throw err;
}

resolve(results);
});
});

} catch (ex) {
logger.error(ex);
reject(ex);
}
});

}

var sql = `select * from user_base where account='${userInfo.account}' and password='${userInfo.password}';`;
const sqlConn = getConnection();
exports.login = login;

sqlConn.query
}
15 changes: 13 additions & 2 deletions login_svr/login-handler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@

const cmdDefine = require('../protocol/cmd-define');
const dbOper = require('./db-oper');
const packet = require('../protocol/packet');

function process(socket, jObj) {
async function process(socket, jObj) {

const mainCmd = jObj.head.mcmd;
const subCmd = jObj.head.scmd;

switch (subCmd) {
case cmdDefine.SUB_LOGIN_ACCOUNT:
let data = await dbOper.login(userInfo);

if (data.length > 0) {
data.token = "just for test";

var retObj = packet.getPacket(mainCmd,subCmd+100);
retObj.body = data;
socket.write(JSON.stringify(retObj));
}

break;
case cmdDefine.SUB_LOGIN_PHONE:
break;
Expand Down
9 changes: 6 additions & 3 deletions login_svr/sql/login.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ create table if not exists user_base (
account varchar(64) not null default '' comment '用户帐号',
name varchar(64) not null default '' comment '用户名',
nickName varchar(64) not null default '' comment '昵称',
password varchar(64) not null default '' comment '用户密码',
password char(32) not null default '' comment '用户密码',
token char(32) not null default '' comment '用户动态token',
sex int not null default 0 comment '性别',
city int not null default 0 comment '城市',
province int not null default 0 comment '省份',
faceURL char(256) not null default '' comment '用户头像',
)engine=innodb default charset 'utf8';
faceURL char(255) not null default '' comment '用户头像',
createTime timestamp not null default 0 comment '创建时间',
index(account,password)
) engine=innodb default charset 'utf8';
27 changes: 27 additions & 0 deletions protocol/cmd-define.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@

/*
1-1000 // framework
1001-1200 // login server
1201-1300 // center server
3000-10000 // game server
{
"head":{
"mcmd":1,
"scmd:":1,
"remoteAddress":"127.0.0.1",
"seqNo":0
},
"body": {
//as your wish
}
}
*/

module.exports = {
HEART_BEAT:1,

LOGIN:1001,
SUB_LOGIN_ACCOUNT:1,
SUB_LOGIN_PHONE:2,
SUB_LOGIN_VISITOR:3,

CENTER:1201,
SUB_CENTER_UPDATE:1,
SUB_CENTER_GET:2
};
16 changes: 16 additions & 0 deletions protocol/packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

function Packet(mainCmd,subCmd) {

this.head = {};
this.head.mcmd = mainCmd;
this.head.scmd = subCmd;
this.body = {};
}

function getPacket(mainCmd,subCmd) {
return new Packet(mainCmd,subCmd);
}

exports.getPacket = getPacket;

console.log(getPacket(0,0))

0 comments on commit 580b35d

Please sign in to comment.