├── .gitmodules ├── 3rd └── msgpack │ ├── msgpack.c │ ├── msgpack.so │ ├── run.sh │ └── test.lua ├── README.md ├── common └── message.lua ├── config ├── gameserver.cfg └── loginserver.cfg ├── gameserver ├── agent.lua ├── dbservice.lua ├── gameservice.lua ├── main.lua ├── roleservice.lua ├── tags └── watchdog.lua ├── loginserver ├── agent.lua ├── dbservice.lua ├── loginservice.lua ├── main.lua ├── roleservice.lua ├── tags └── watchdog.lua └── proto ├── README ├── login_message.pb ├── login_message.proto ├── role_message.pb └── role_message.proto /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "3rd/pbc"] 2 | path = 3rd/pbc 3 | url = https://github.com/cloudwu/pbc.git 4 | [submodule "skynet"] 5 | path = skynet 6 | url = https://github.com/cloudwu/skynet.git 7 | -------------------------------------------------------------------------------- /3rd/msgpack/msgpack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | static int _unpack(lua_State *L) 11 | { 12 | uint32_t msgno; 13 | const char * data; 14 | const char * msg; 15 | size_t size; 16 | 17 | uint8_t * buffer= (uint8_t *)malloc(4); 18 | 19 | data = luaL_checklstring(L, 1, &size); 20 | 21 | memcpy(buffer, data, 4); 22 | msg = data+4; 23 | 24 | msgno = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]; 25 | 26 | lua_newtable(L); 27 | 28 | lua_pushstring(L, "msgno"); 29 | lua_pushinteger(L, msgno); 30 | lua_settable(L, -3); 31 | 32 | lua_pushstring(L, "msg"); 33 | lua_pushstring(L, msg); 34 | lua_settable(L, -3); 35 | return 1; 36 | } 37 | 38 | static int _pack(lua_State *L) 39 | { 40 | uint32_t msgno; 41 | const char * msg; 42 | size_t size; 43 | uint8_t * buffer; 44 | 45 | msgno = luaL_checkinteger(L, 1); 46 | msg = luaL_checklstring(L, 2, &size); 47 | if (size > 0x10000) {//2^16bit=2byte 48 | return luaL_error(L, "Invalid size (too long) of data : %d", (int)size); 49 | } 50 | 51 | buffer = (uint8_t*)malloc(size+4); 52 | 53 | buffer[0] = (msgno >> 24) & 0xff; 54 | buffer[1] = (msgno >> 16) & 0xff; 55 | buffer[2] = (msgno >> 8) & 0xff; 56 | buffer[3] = msgno & 0xff; 57 | 58 | memcpy(buffer+4, msg, size); 59 | lua_pushlstring(L, (const char *)buffer, size+4); 60 | return 1; 61 | } 62 | 63 | static const struct luaL_Reg lib[] = 64 | { 65 | {"pack", _pack}, 66 | {"unpack", _unpack}, 67 | {NULL, NULL} 68 | }; 69 | 70 | int luaopen_msgpack_core(lua_State *L) 71 | { 72 | luaL_newlib(L, lib); 73 | return 1; 74 | } 75 | -------------------------------------------------------------------------------- /3rd/msgpack/msgpack.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heluocs/skynet-unity-server/81a45c9277812c9cbdfc4690de46177faf396cd1/3rd/msgpack/msgpack.so -------------------------------------------------------------------------------- /3rd/msgpack/run.sh: -------------------------------------------------------------------------------- 1 | gcc -g -O2 -Wall -I../../skynet/3rd/lua -fPIC --shared ./msgpack.c -o ./msgpack.so 2 | -------------------------------------------------------------------------------- /3rd/msgpack/test.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env lua 2 | msgpack = require("msgpack.core") 3 | 4 | local msgno = 1101 5 | local msg = "hello skynet" 6 | x = msgpack.unpack(msgpack.pack(msgno, msg)) 7 | print(x.msgno, x.msg) 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # skynet-unity-server 2 | An example of skynet server with unity client 3 | -------------------------------------------------------------------------------- /common/message.lua: -------------------------------------------------------------------------------- 1 | local message = {} 2 | 3 | message.MSG_LOGIN_MODULE = 0x00010000 4 | message.MSG_ACCOUNT_LOGIN_REQUEST_C2S = 0x00010001 5 | message.MSG_ACCOUNT_LOGIN_RESPONSE_S2C = 0x00010002 6 | message.MSG_ACCOUNT_REGIST_REQUEST_C2S = 0x00010003 7 | message.MSG_ACCOUNT_REGIST_RESPONSE_S2C = 0x00010004 8 | 9 | message.MSG_ROLE_MODULE = 0x00020000 10 | message.MSG_ROLE_LIST_REQUEST_C2S = 0x00020001 11 | message.MSG_ROLE_LIST_RESPONSE_S2C = 0x00020002 12 | message.MSG_ROLE_CREATE_REQUEST_C2S = 0x00020003 13 | message.MSG_ROLE_CREATE_RESPONSE_S2C = 0x00020004 14 | 15 | return message 16 | -------------------------------------------------------------------------------- /config/gameserver.cfg: -------------------------------------------------------------------------------- 1 | root = "./" 2 | thread = 8 3 | logger = nil 4 | logpath = "." 5 | harbor = 1 6 | address = "127.0.0.1:2526" 7 | master = "127.0.0.1:2013" 8 | start = "main" -- main script 9 | bootstrap = "snlua bootstrap" -- The service for bootstrap 10 | standalone = "0.0.0.0:2013" 11 | luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."../gameserver/?.lua" 12 | lualoader = "lualib/loader.lua" 13 | -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run 14 | snax = root.."../gameserver/?.lua;"..root.."test/?.lua" 15 | -- snax_interface_g = "snax_g" 16 | cpath = root.."cservice/?.so" 17 | -- daemon = "./skynet.pid" 18 | lua_path = root.."lualib/?.lua;"..root.."../common/?.lua" 19 | --lua_cpath = root.."luaclib/?.so" 20 | -------------------------------------------------------------------------------- /config/loginserver.cfg: -------------------------------------------------------------------------------- 1 | root = "./" 2 | thread = 8 3 | logger = nil 4 | logpath = "." 5 | harbor = 1 6 | address = "127.0.0.1:2526" 7 | master = "127.0.0.1:2013" 8 | start = "main" -- main script 9 | bootstrap = "snlua bootstrap" -- The service for bootstrap 10 | standalone = "0.0.0.0:2013" 11 | luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."../loginserver/?.lua" 12 | lualoader = "lualib/loader.lua" 13 | -- preload = "./examples/preload.lua" -- run preload.lua before every lua service run 14 | snax = root.."../loginserver/?.lua;"..root.."test/?.lua" 15 | -- snax_interface_g = "snax_g" 16 | cpath = root.."cservice/?.so" 17 | -- daemon = "./skynet.pid" 18 | lua_path = root.."lualib/?.lua;"..root.."../common/?.lua" 19 | --lua_cpath = root.."luaclib/?.so" 20 | -------------------------------------------------------------------------------- /gameserver/agent.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | require "skynet.manager" 3 | local netpack = require "netpack" 4 | local socket = require "socket" 5 | local message = require "message" 6 | msgpack = require "msgpack.core" 7 | 8 | local host 9 | 10 | local CMD = {} 11 | local client_fd 12 | 13 | local function send_response(package) 14 | data = msgpack.unpack(package) 15 | print(" resp ok : ", data.msgno) 16 | socket.write(client_fd, netpack.pack(package)) 17 | end 18 | 19 | skynet.register_protocol { 20 | name = "client", 21 | id = skynet.PTYPE_CLIENT, 22 | unpack = function(msg, sz) 23 | return skynet.tostring(msg, sz) 24 | end, 25 | dispatch = function(_, _, msg) 26 | print("------------client dispatch------------") 27 | data = msgpack.unpack(msg) 28 | module = data.msgno >> 16 29 | opcode = data.msgno & 0xffff 30 | 31 | local ok, result 32 | if module == message.MSG_LOGIN_MODULE >> 16 then 33 | ok, result = pcall(skynet.call, "loginserver", "lua", "dispatch", opcode, data.msg) 34 | if ok then 35 | send_response(result) 36 | else 37 | print("login error") 38 | end 39 | else 40 | print("server receive error msg") 41 | end 42 | end 43 | } 44 | 45 | function CMD.start(gate, fd, proto) 46 | client_fd = fd 47 | skynet.call(gate, "lua", "forward", fd) 48 | end 49 | 50 | function CMD.disconnect() 51 | print("---a client disconnect") 52 | skynet.exit() 53 | end 54 | 55 | skynet.start(function() 56 | print("---start agent---") 57 | skynet.dispatch("lua", function(session, source, cmd, ...) 58 | print("---agent cmd ", cmd) 59 | local f = CMD[cmd] 60 | skynet.ret(skynet.pack(f(...))) 61 | end) 62 | end) 63 | -------------------------------------------------------------------------------- /gameserver/dbservice.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | require "skynet.manager" 3 | local mysql = require "mysql" 4 | 5 | local db 6 | local CMD = {} 7 | 8 | function CMD.query(sql) 9 | print("--- dbserver query:" .. sql) 10 | return db:query(sql) 11 | end 12 | 13 | skynet.start(function() 14 | skynet.dispatch("lua", function(session, address, cmd, ...) 15 | local f = CMD[cmd] 16 | skynet.ret(skynet.pack(f(...))) 17 | end) 18 | 19 | db = mysql.connect { 20 | host = "114.215.210.189", 21 | port = 3306, 22 | database = "simplerpg", 23 | user = "root", 24 | password = "root", 25 | max_packet_size = 1024 * 1024 26 | } 27 | 28 | if not db then 29 | print("failedi to connect mysql") 30 | end 31 | 32 | skynet.register "dbservice" 33 | end) 34 | 35 | -------------------------------------------------------------------------------- /gameserver/gameservice.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | require "skynet.manager" 3 | local netpack = require "netpack" 4 | local message = require "message" 5 | 6 | msgpack = require("msgpack.core") 7 | 8 | local CMD = {} 9 | local protobuf = {} 10 | 11 | local function processAccountLoginRequest(msg) 12 | print("---process login---") 13 | local data = protobuf.decode("CMsgAccountLoginRequest", msg) 14 | local account = data.account 15 | print("----account:" , account) 16 | 17 | local sql = "select * from tb_account where account = '" .. account .. "'" 18 | local ok, result = pcall(skynet.call, "dbservice", "lua", "query", sql) 19 | 20 | local tb = {} 21 | if ok then 22 | print(#result) 23 | if #result > 0 then 24 | for key,value in pairs(result) do 25 | tb.accountid = value["id"] 26 | end 27 | end 28 | --[[ 29 | for k, v in pairs(value) do 30 | print(k, v) 31 | end 32 | ]]-- 33 | end 34 | 35 | local msgbody = protobuf.encode("CMsgAccountLoginResponse", tb) 36 | return msgpack.pack(message.MSG_ACCOUNT_LOGIN_RESPONSE_S2C, msgbody) 37 | end 38 | 39 | local function processAccountRegistRequest(msg) 40 | print("---process regist---") 41 | local data = protobuf.decode("CMsgAccountRegistRequest", msg) 42 | local account = data.account 43 | print("---account:", account) 44 | 45 | local tb = {} 46 | local id = os.time() 47 | local sql = "insert into tb_account(id, account) values(".. id ..",'".. account .."')" 48 | local ok, result = pcall(skynet.call, "dbservice", "lua", "query", sql) 49 | if ok then 50 | tb.accountid = id 51 | end 52 | 53 | local msgbody = protobuf.encode("CMsgAccountRegistResponse", tb) 54 | return msgpack.pack(message.MSG_ACCOUNT_REGIST_RESPONSE_S2C, msgbody) 55 | end 56 | 57 | function CMD.dispatch(opcode, msg) 58 | print("login dispatch msgno " .. opcode) 59 | if opcode == message.MSG_ACCOUNT_LOGIN_REQUEST_C2S & 0x0000FFFF then 60 | return processAccountLoginRequest(msg) 61 | elseif opcode == message.MSG_ACCOUNT_REGIST_REQUEST_C2S & 0x0000FFFF then 62 | return processAccountRegistRequest(msg) 63 | end 64 | end 65 | 66 | skynet.start(function() 67 | print("---start game server---") 68 | skynet.dispatch("lua", function(session, source, cmd, ...) 69 | local f = CMD[cmd] 70 | skynet.ret(skynet.pack(f(...))) 71 | end) 72 | 73 | protobuf = require "protobuf" 74 | local login_data = io.open("../proto/login_message.pb", "rb") 75 | local buffer = login_data:read "*a" 76 | login_data:close() 77 | protobuf.register(buffer) 78 | 79 | skynet.register "gameservice" 80 | end) 81 | -------------------------------------------------------------------------------- /gameserver/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | 3 | local max_client = 64 4 | 5 | skynet.start(function() 6 | print("---server start---") 7 | 8 | skynet.newservice("dbservice") 9 | skynet.newservice("gameservice") 10 | 11 | local watchdog = skynet.newservice("watchdog") 12 | skynet.call(watchdog, "lua", "start", { 13 | port = 8888, 14 | maxclient = max_client, 15 | nodelay = true, 16 | }) 17 | print("Watchdog listen on ", 8800) 18 | 19 | skynet.exit() 20 | end) 21 | -------------------------------------------------------------------------------- /gameserver/roleservice.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | require "skynet.manager" 3 | 4 | -------------------------------------------------------------------------------- /gameserver/tags: -------------------------------------------------------------------------------- 1 | !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ 2 | !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ 3 | !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ 4 | !_TAG_PROGRAM_NAME Exuberant Ctags // 5 | !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ 6 | !_TAG_PROGRAM_VERSION 5.9~svn20110310 // 7 | CMD.close watchdog.lua /^function CMD.close(fd)$/;" f 8 | CMD.dispatch loginserver.lua /^function CMD.dispatch(opcode, msg)$/;" f 9 | CMD.query dbserver.lua /^function CMD.query(sql)$/;" f 10 | CMD.start agent.lua /^function CMD.start(gate, fd, proto)$/;" f 11 | CMD.start watchdog.lua /^function CMD.start(conf)$/;" f 12 | SOCKET.close watchdog.lua /^function SOCKET.close(fd)$/;" f 13 | SOCKET.data watchdog.lua /^function SOCKET.data(fd, msg)$/;" f 14 | SOCKET.error watchdog.lua /^function SOCKET.error(fd, msg)$/;" f 15 | SOCKET.open watchdog.lua /^function SOCKET.open(fd, addr)$/;" f 16 | close_agnet watchdog.lua /^local function close_agnet(fd)$/;" f 17 | dispatch agent.lua /^ dispatch = function(_, _, msg)$/;" f 18 | processAccountLoginRequest loginserver.lua /^local function processAccountLoginRequest(msg)$/;" f 19 | processAccountRegistRequest loginserver.lua /^local function processAccountRegistRequest(msg)$/;" f 20 | send_response agent.lua /^local function send_response(package)$/;" f 21 | unpack agent.lua /^ unpack = function(msg, sz)$/;" f 22 | -------------------------------------------------------------------------------- /gameserver/watchdog.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | require "skynet.manager" 3 | local netpack = require "netpack" 4 | 5 | local CMD = {} 6 | local SOCKET = {} 7 | local agent = {} 8 | local gate 9 | 10 | function SOCKET.open(fd, addr) 11 | skynet.error("New client from : " .. addr) 12 | agent[fd] = skynet.newservice("agent") 13 | skynet.call(agent[fd], "lua", "start", gate, fd, proto) 14 | end 15 | 16 | local function close_agnet(fd) 17 | local a = agent[fd] 18 | agent[fd] = nil 19 | if a then 20 | skynet.call(gate, "lua", "kick", fd) 21 | skynet.send(a, "lua", "disconnect") 22 | end 23 | end 24 | 25 | function SOCKET.close(fd) 26 | print("socket close", fd) 27 | close_agnet(fd) 28 | end 29 | 30 | function SOCKET.error(fd, msg) 31 | print("socket error", fd, msg) 32 | close_agnet(fd) 33 | end 34 | 35 | function SOCKET.data(fd, msg) 36 | end 37 | 38 | function CMD.start(conf) 39 | skynet.call(gate, "lua", "open", conf) 40 | end 41 | 42 | function CMD.close(fd) 43 | close_agnet(fd) 44 | end 45 | 46 | skynet.start(function() 47 | print("---start watchdog---") 48 | skynet.dispatch("lua", function(session, source, cmd, subcmd, ...) 49 | print("---watchdog cmd ", cmd) 50 | if cmd == "socket" then 51 | print("---watchdog subcmd ", subcmd) 52 | local f = SOCKET[subcmd] 53 | f(...) 54 | --socket api don't need return 55 | else 56 | local f = assert(CMD[cmd]) 57 | skynet.ret(skynet.pack(f(subcmd, ...))) 58 | end 59 | end) 60 | 61 | gate = skynet.newservice("gate") 62 | end) 63 | -------------------------------------------------------------------------------- /loginserver/agent.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | require "skynet.manager" 3 | local netpack = require "netpack" 4 | local socket = require "socket" 5 | local message = require "message" 6 | msgpack = require "msgpack.core" 7 | 8 | local host 9 | 10 | local CMD = {} 11 | local client_fd 12 | 13 | local function send_response(package) 14 | data = msgpack.unpack(package) 15 | print(" resp ok : ", data.msgno) 16 | socket.write(client_fd, netpack.pack(package)) 17 | end 18 | 19 | skynet.register_protocol { 20 | name = "client", 21 | id = skynet.PTYPE_CLIENT, 22 | unpack = function(msg, sz) 23 | return skynet.tostring(msg, sz) 24 | end, 25 | dispatch = function(_, _, msg) 26 | print("------------client dispatch------------") 27 | data = msgpack.unpack(msg) 28 | module = data.msgno >> 16 29 | opcode = data.msgno & 0xffff 30 | 31 | local ok, result 32 | if module == message.MSG_LOGIN_MODULE >> 16 then 33 | ok, result = pcall(skynet.call, "loginservice", "lua", "dispatch", opcode, data.msg) 34 | if ok then 35 | send_response(result) 36 | else 37 | print("login error") 38 | end 39 | elseif module == message.MSG_ROLE_MODULE >> 16 then 40 | ok, result = pcall(skynet.call, "roleservice", "lua", "dispatch", opcode, data.msg) 41 | if ok then 42 | send_response(result) 43 | else 44 | print("role error") 45 | end 46 | else 47 | print("server receive error msg") 48 | end 49 | end 50 | } 51 | 52 | function CMD.start(gate, fd, proto) 53 | client_fd = fd 54 | skynet.call(gate, "lua", "forward", fd) 55 | end 56 | 57 | function CMD.disconnect() 58 | print("---a client disconnect") 59 | skynet.exit() 60 | end 61 | 62 | skynet.start(function() 63 | print("---start agent---") 64 | skynet.dispatch("lua", function(session, source, cmd, ...) 65 | print("---agent cmd ", cmd) 66 | local f = CMD[cmd] 67 | skynet.ret(skynet.pack(f(...))) 68 | end) 69 | end) 70 | -------------------------------------------------------------------------------- /loginserver/dbservice.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | require "skynet.manager" 3 | local mysql = require "mysql" 4 | 5 | local db 6 | local CMD = {} 7 | 8 | function CMD.query(sql) 9 | print("--- dbserver query:" .. sql) 10 | return db:query(sql) 11 | end 12 | 13 | skynet.start(function() 14 | skynet.dispatch("lua", function(session, address, cmd, ...) 15 | local f = CMD[cmd] 16 | skynet.ret(skynet.pack(f(...))) 17 | end) 18 | 19 | db = mysql.connect { 20 | host = "114.215.210.189", 21 | port = 3306, 22 | database = "simplerpg", 23 | user = "root", 24 | password = "root", 25 | max_packet_size = 1024 * 1024 26 | } 27 | 28 | if not db then 29 | print("failedi to connect mysql") 30 | end 31 | 32 | skynet.register "dbservice" 33 | end) 34 | 35 | -------------------------------------------------------------------------------- /loginserver/loginservice.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | require "skynet.manager" 3 | local netpack = require "netpack" 4 | local message = require "message" 5 | 6 | msgpack = require("msgpack.core") 7 | 8 | local CMD = {} 9 | local protobuf = {} 10 | 11 | local function processAccountLoginRequest(msg) 12 | print("---process login---") 13 | local data = protobuf.decode("CMsgAccountLoginRequest", msg) 14 | local account = data.account 15 | print("----account:" , account) 16 | 17 | local sql = "select * from tb_account where account = '" .. account .. "'" 18 | local ok, result = pcall(skynet.call, "dbservice", "lua", "query", sql) 19 | 20 | local tb = {} 21 | if ok then 22 | print(#result) 23 | if #result > 0 then 24 | for key,value in pairs(result) do 25 | tb.accountid = value["id"] 26 | end 27 | end 28 | --[[ 29 | for k, v in pairs(value) do 30 | print(k, v) 31 | end 32 | ]]-- 33 | end 34 | 35 | local msgbody = protobuf.encode("CMsgAccountLoginResponse", tb) 36 | return msgpack.pack(message.MSG_ACCOUNT_LOGIN_RESPONSE_S2C, msgbody) 37 | end 38 | 39 | local function processAccountRegistRequest(msg) 40 | print("---process regist---") 41 | local data = protobuf.decode("CMsgAccountRegistRequest", msg) 42 | local account = data.account 43 | print("---account:", account) 44 | 45 | local tb = {} 46 | local id = os.time() 47 | local sql = "insert into tb_account(id, account) values(".. id ..",'".. account .."')" 48 | print(sql) 49 | local ok, result = pcall(skynet.call, "dbservice", "lua", "query", sql) 50 | if ok then 51 | tb.accountid = id 52 | print("regist user success!") 53 | end 54 | 55 | local msgbody = protobuf.encode("CMsgAccountRegistResponse", tb) 56 | return msgpack.pack(message.MSG_ACCOUNT_REGIST_RESPONSE_S2C, msgbody) 57 | end 58 | 59 | function CMD.dispatch(opcode, msg) 60 | print("login dispatch msgno " .. opcode) 61 | if opcode == message.MSG_ACCOUNT_LOGIN_REQUEST_C2S & 0x0000FFFF then 62 | return processAccountLoginRequest(msg) 63 | elseif opcode == message.MSG_ACCOUNT_REGIST_REQUEST_C2S & 0x0000FFFF then 64 | return processAccountRegistRequest(msg) 65 | end 66 | end 67 | 68 | skynet.start(function() 69 | print("---start login server---") 70 | skynet.dispatch("lua", function(session, source, cmd, ...) 71 | local f = CMD[cmd] 72 | skynet.ret(skynet.pack(f(...))) 73 | end) 74 | 75 | protobuf = require "protobuf" 76 | local login_data = io.open("../proto/login_message.pb", "rb") 77 | local buffer = login_data:read "*a" 78 | login_data:close() 79 | protobuf.register(buffer) 80 | 81 | skynet.register "loginservice" 82 | end) 83 | -------------------------------------------------------------------------------- /loginserver/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | 3 | local max_client = 64 4 | 5 | skynet.start(function() 6 | print("---server start---") 7 | 8 | skynet.newservice("dbservice") 9 | skynet.newservice("loginservice") 10 | skynet.newservice("roleservice") 11 | 12 | local watchdog = skynet.newservice("watchdog") 13 | skynet.call(watchdog, "lua", "start", { 14 | port = 8888, 15 | maxclient = max_client, 16 | nodelay = true, 17 | }) 18 | print("Watchdog listen on ", 8888) 19 | 20 | skynet.exit() 21 | end) 22 | -------------------------------------------------------------------------------- /loginserver/roleservice.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | require "skynet.manager" 3 | local netpack = require "netpack" 4 | local message = require "message" 5 | 6 | msgpack = require("msgpack.core") 7 | 8 | local CMD = {} 9 | local protobuf = {} 10 | 11 | local function processRoleListRequest(msg) 12 | print("---process role list---") 13 | local data = protobuf.decode("CMsgRoleListRequest", msg) 14 | local accountid = data.accountid 15 | print("---account:", accountid) 16 | 17 | local roles = {} 18 | local sql = "select * from tb_role where accountid = '" .. accountid .. "'" 19 | local ok, result = pcall(skynet.call, "dbservice", "lua", "query", sql) 20 | if ok then 21 | for key,value in pairs(result) do 22 | local role = {} 23 | role.id = value["id"] 24 | role.nickname = value["nickname"] 25 | role.level = value["level"] 26 | role.roletype = value["roletype"] 27 | table.insert(roles, role) 28 | end 29 | else 30 | print("---query error---") 31 | end 32 | 33 | local tb = {} 34 | tb.roles = roles 35 | local msgbody = protobuf.encode("CMsgRoleListResponse", tb) 36 | return msgpack.pack(message.MSG_ROLE_LIST_RESPONSE_S2C, msgbody) 37 | end 38 | 39 | local function processRoleCreateRequest(msg) 40 | print("---process role create---") 41 | local data = protobuf.decode("CMsgRoleCreateRequest", msg) 42 | local accountid = data.accountid 43 | local nickname = data.nickname 44 | local roletype = data.roletype 45 | print("---nickname:", nickname) 46 | 47 | local tb = {} 48 | local id = os.time() 49 | local role = {} 50 | local sql = "insert into tb_role(id, accountid, nickname, roletype) values ('"..id.."','"..accountid.."','"..nickname.."','"..roletype.."')" 51 | local ok, result = pcall(skynet.call, "dbservice", "lua", "query", sql) 52 | if ok then 53 | role.id = id 54 | role.nickname = nickname 55 | role.roletype = roletype 56 | role.level = 1 57 | else 58 | print("---query error---") 59 | end 60 | 61 | tb.role = role 62 | local msgbody = protobuf.encode("CMsgRoleCreateResponse", tb) 63 | return msgpack.pack(message.MSG_ROLE_CREATE_RESPONSE_S2C, msgbody) 64 | end 65 | 66 | function CMD.dispatch(opcode, msg) 67 | print("role dispatch msgno " .. opcode) 68 | if opcode == message.MSG_ROLE_LIST_REQUEST_C2S & 0x0000FFFF then 69 | return processRoleListRequest(msg) 70 | elseif opcode == message.MSG_ROLE_CREATE_REQUEST_C2S & 0x0000FFFF then 71 | return processRoleCreateRequest(msg) 72 | end 73 | end 74 | 75 | skynet.start(function() 76 | print("---start login server---") 77 | skynet.dispatch("lua", function(session, source, cmd, ...) 78 | local f = CMD[cmd] 79 | skynet.ret(skynet.pack(f(...))) 80 | end) 81 | 82 | protobuf = require "protobuf" 83 | local login_data = io.open("../proto/role_message.pb", "rb") 84 | local buffer = login_data:read "*a" 85 | login_data:close() 86 | protobuf.register(buffer) 87 | 88 | skynet.register "roleservice" 89 | end) 90 | -------------------------------------------------------------------------------- /loginserver/tags: -------------------------------------------------------------------------------- 1 | !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ 2 | !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ 3 | !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ 4 | !_TAG_PROGRAM_NAME Exuberant Ctags // 5 | !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ 6 | !_TAG_PROGRAM_VERSION 5.9~svn20110310 // 7 | CMD.close watchdog.lua /^function CMD.close(fd)$/;" f 8 | CMD.dispatch loginserver.lua /^function CMD.dispatch(opcode, msg)$/;" f 9 | CMD.query dbserver.lua /^function CMD.query(sql)$/;" f 10 | CMD.start agent.lua /^function CMD.start(gate, fd, proto)$/;" f 11 | CMD.start watchdog.lua /^function CMD.start(conf)$/;" f 12 | SOCKET.close watchdog.lua /^function SOCKET.close(fd)$/;" f 13 | SOCKET.data watchdog.lua /^function SOCKET.data(fd, msg)$/;" f 14 | SOCKET.error watchdog.lua /^function SOCKET.error(fd, msg)$/;" f 15 | SOCKET.open watchdog.lua /^function SOCKET.open(fd, addr)$/;" f 16 | close_agnet watchdog.lua /^local function close_agnet(fd)$/;" f 17 | dispatch agent.lua /^ dispatch = function(_, _, msg)$/;" f 18 | processAccountLoginRequest loginserver.lua /^local function processAccountLoginRequest(msg)$/;" f 19 | processAccountRegistRequest loginserver.lua /^local function processAccountRegistRequest(msg)$/;" f 20 | send_response agent.lua /^local function send_response(package)$/;" f 21 | unpack agent.lua /^ unpack = function(msg, sz)$/;" f 22 | -------------------------------------------------------------------------------- /loginserver/watchdog.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | require "skynet.manager" 3 | local netpack = require "netpack" 4 | 5 | local CMD = {} 6 | local SOCKET = {} 7 | local agent = {} 8 | local gate 9 | 10 | function SOCKET.open(fd, addr) 11 | skynet.error("New client from : " .. addr) 12 | agent[fd] = skynet.newservice("agent") 13 | skynet.call(agent[fd], "lua", "start", gate, fd, proto) 14 | end 15 | 16 | local function close_agnet(fd) 17 | local a = agent[fd] 18 | agent[fd] = nil 19 | if a then 20 | skynet.call(gate, "lua", "kick", fd) 21 | skynet.send(a, "lua", "disconnect") 22 | end 23 | end 24 | 25 | function SOCKET.close(fd) 26 | print("socket close", fd) 27 | close_agnet(fd) 28 | end 29 | 30 | function SOCKET.error(fd, msg) 31 | print("socket error", fd, msg) 32 | close_agnet(fd) 33 | end 34 | 35 | function SOCKET.data(fd, msg) 36 | end 37 | 38 | function CMD.start(conf) 39 | skynet.call(gate, "lua", "open", conf) 40 | end 41 | 42 | function CMD.close(fd) 43 | close_agnet(fd) 44 | end 45 | 46 | skynet.start(function() 47 | print("---start watchdog---") 48 | skynet.dispatch("lua", function(session, source, cmd, subcmd, ...) 49 | print("---watchdog cmd ", cmd) 50 | if cmd == "socket" then 51 | print("---watchdog subcmd ", subcmd) 52 | local f = SOCKET[subcmd] 53 | f(...) 54 | --socket api don't need return 55 | else 56 | local f = assert(CMD[cmd]) 57 | skynet.ret(skynet.pack(f(subcmd, ...))) 58 | end 59 | end) 60 | 61 | gate = skynet.newservice("gate") 62 | end) 63 | -------------------------------------------------------------------------------- /proto/README: -------------------------------------------------------------------------------- 1 | protoc -o ./msg.pb ./msg.proto 2 | -------------------------------------------------------------------------------- /proto/login_message.pb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heluocs/skynet-unity-server/81a45c9277812c9cbdfc4690de46177faf396cd1/proto/login_message.pb -------------------------------------------------------------------------------- /proto/login_message.proto: -------------------------------------------------------------------------------- 1 | //MSG_ACCOUNT_LOGIN_REQUEST_C2S 2 | message CMsgAccountLoginRequest 3 | { 4 | required string account = 1; 5 | } 6 | 7 | 8 | //MSG_ACCOUNT_LOGIN_RESPONSE_S2C 9 | message CMsgAccountLoginResponse 10 | { 11 | required int64 accountid = 1; 12 | } 13 | 14 | //MSG_ACCOUNT_REGIST_REQUEST_C2S 15 | message CMsgAccountRegistRequest 16 | { 17 | required string account = 1; 18 | } 19 | 20 | //MSG_ACCOUNT_REGIST_RESPONSE_S2C 21 | message CMsgAccountRegistResponse 22 | { 23 | required int64 accountid = 1; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /proto/role_message.pb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heluocs/skynet-unity-server/81a45c9277812c9cbdfc4690de46177faf396cd1/proto/role_message.pb -------------------------------------------------------------------------------- /proto/role_message.proto: -------------------------------------------------------------------------------- 1 | //MSG_ROLE_LIST_REQUEST_C2S 2 | message CMsgRoleListRequest 3 | { 4 | required string accountid = 1; 5 | } 6 | 7 | message Role 8 | { 9 | required int64 id = 1; 10 | required string nickname = 2; 11 | required int32 level = 3; 12 | required int32 roletype = 4; 13 | } 14 | 15 | //MSG_ROLE_LIST_RESPONSE_S2C 16 | message CMsgRoleListResponse 17 | { 18 | repeated Role roles = 1; 19 | } 20 | 21 | //MSG_ROLE_CREATE_REQUEST_C2S 22 | message CMsgRoleCreateRequest 23 | { 24 | required string accountid = 1; 25 | required string nickname = 2; 26 | required int32 roletype = 3; 27 | } 28 | 29 | //MSG_ROLE_CREATE_RESPONSE_S2C 30 | message CMsgRoleCreateResponse 31 | { 32 | required Role role = 1; 33 | } 34 | --------------------------------------------------------------------------------