forked from shihuaping/gamex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
218ef52
commit 580b35d
Showing
12 changed files
with
207 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |