Skip to content

Commit

Permalink
草稿
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanfengyun committed Dec 24, 2016
1 parent de312bf commit 950973d
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./skynet/3rd/lua/lua test/client.lua
16 changes: 16 additions & 0 deletions config
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root="."
thread = 8
logger = nil
logpath = "."
harbor = 1
address = "127.0.0.1:2526"
master = "127.0.0.1:2013"
start = "main" -- main script
bootstrap = "snlua bootstrap" -- The service for bootstrap
standalone = "0.0.0.0:2013"
luaservice = "./service/?.lua;./skynet/service/?.lua"
lualoader = "./skynet/lualib/loader.lua"
lua_path = "./skynet/lualib/?.lua;./skynet/lualib/?/init.lua"
lua_cpath = "./luaclib/?.so;./skynet/luaclib/?.so"
cpath = "./skynet/cservice/?.so"
-- daemon = "./skynet.pid"
3 changes: 3 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

./skynet/skynet config
47 changes: 47 additions & 0 deletions service/agent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local skynet = require "skynet"
local netpack = require "netpack"
local socket = require "socket"
local sproto = require "sproto"
local sprotoloader = require "sprotoloader"

local WATCHDOG
local send_request

local CMD = {}
local REQUEST = {}
local client_fd

local function send_package(pack)
local package = string.pack(">s2", pack)
socket.write(client_fd, package)
end

skynet.register_protocol {
name = "client",
id = skynet.PTYPE_CLIENT,
unpack = function (msg, sz)
return "data", netpack.tostring(msg, sz)
end,
dispatch = function (_, _, type, str)
print(type, str)
end
}

function CMD.start(conf)
local fd = conf.client
local gate = conf.gate
WATCHDOG = conf.watchdog
client_fd = fd
skynet.call(gate, "lua", "forward", fd)
end

function CMD.disconnect()
skynet.exit()
end

skynet.start(function()
skynet.dispatch("lua", function(_,_, command, ...)
local f = CMD[command]
skynet.ret(skynet.pack(f(...)))
end)
end)
19 changes: 19 additions & 0 deletions service/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local skynet = require "skynet"

local max_client = 64

skynet.start(function()
skynet.error("Server start")
if not skynet.getenv "daemon" then
local console = skynet.newservice("console")
end
skynet.newservice("debug_console",8000)
local watchdog = skynet.newservice("watchdog")
skynet.call(watchdog, "lua", "start", {
port = 8888,
maxclient = max_client,
nodelay = true,
})
skynet.error("Watchdog listen on", 8888)
skynet.exit()
end)
64 changes: 64 additions & 0 deletions service/watchdog.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local skynet = require "skynet"
local netpack = require "netpack"

local CMD = {}
local SOCKET = {}
local gate
local agent = {}

function SOCKET.open(fd, addr)
skynet.error("New client from : " .. addr)
agent[fd] = skynet.newservice("agent")
skynet.call(agent[fd], "lua", "start", { gate = gate, client = fd, watchdog = skynet.self() })
end

local function close_agent(fd)
local a = agent[fd]
agent[fd] = nil
if a then
skynet.call(gate, "lua", "kick", fd)
-- disconnect never return
skynet.send(a, "lua", "disconnect")
end
end

function SOCKET.close(fd)
print("socket close",fd)
close_agent(fd)
end

function SOCKET.error(fd, msg)
print("socket error",fd, msg)
close_agent(fd)
end

function SOCKET.warning(fd, size)
-- size K bytes havn't send out in fd
print("socket warning", fd, size)
end

function SOCKET.data(fd, msg)
end

function CMD.start(conf)
skynet.call(gate, "lua", "open" , conf)
end

function CMD.close(fd)
close_agent(fd)
end

skynet.start(function()
skynet.dispatch("lua", function(session, source, cmd, subcmd, ...)
if cmd == "socket" then
local f = SOCKET[subcmd]
f(...)
-- socket api don't need return
else
local f = assert(CMD[cmd])
skynet.ret(skynet.pack(f(subcmd, ...)))
end
end)

gate = skynet.newservice("gate")
end)
15 changes: 15 additions & 0 deletions test/client.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package.cpath = "./skynet/luaclib/?.so"
package.path = "./skynet/lualib/?.lua"


local socket = require "clientsocket"

local fd = assert(socket.connect("127.0.0.1", 8888))

local function send(data)
local package = string.pack(">s2",data)
socket.send(fd, package)
end

send("hello")
socket.close(fd)

0 comments on commit 950973d

Please sign in to comment.