Permalink
Browse files

utf8 module, some optimization, fixed some bugs in mumble module

  • Loading branch information...
nonchip committed Jun 21, 2014
1 parent cb69cab commit b4011706e7a17771f7625d64cd2641a13008fc17
@@ -6,3 +6,12 @@ function string.split(s, delimiter)
end
return result
end
function string.hexdump(buf)
for i=1,math.ceil(#buf/16) * 16 do
if (i-1) % 16 == 0 then io.write(string.format('%08X ', i-1)) end
io.write( i > #buf and ' ' or string.format('%02X ', buf:byte(i)) )
if i % 8 == 0 then io.write(' ') end
if i % 16 == 0 then io.write( buf:sub(i-16+1, i):gsub('%c','.'), '\n' ) end
end
end
@@ -10,13 +10,20 @@ table.insert(Y._init,{
local lanes=Y._bootstrap.env.rockcheck'lanes'
if not lanes.YlibError then
if lanes.configure then
return lanes.configure{ with_timers=true, track_lanes=true, protect_allocator=true, on_state_create=function()
lanes=lanes.configure{ with_timers=true, track_lanes=true, protect_allocator=true, on_state_create=function()
Y={_InLanes=true,_bootstrap_log=function(...) table.insert(Y._bootstrap_log_list,{...}) end,_bootstrap_log_list={}}
require 'YEng'
end}
else
return lanes
end
local oldgen=lanes.gen
lanes.gen=function(a,b)
if b then
return oldgen(a,b)
else
return oldgen('*',a)
end
end
return lanes
end
end
})
@@ -32,7 +32,6 @@ local function moduleLoader(modules, init, logger, path, lpath, blacklist)
end
local module_backup = _G['module']
local d
for d in lfs.sorteddir(path) do
local doIt = d ~= '.' and d ~= '..'
@@ -55,20 +54,19 @@ local function moduleLoader(modules, init, logger, path, lpath, blacklist)
return module_backup(modulename, ...)
end
end
local mod = require(lpath .. dname)
local mod=require(lpath .. dname)
if mod then
modules.loadlist[modtmp.modulename or dname] = { modtmp.options or {}, mod }
end
end
end
_G['module'] = module_backup
logger('YEng finding module load order...')
local continue = true
while continue do
continue = false
local k, v
logger('YEng finding module load order...')
for k, v in pairs(modules.loadlist) do
logger(' trying:', k)
local allow = mod_options_allowload(v[1])
@@ -11,7 +11,8 @@ local Y=Y or {}
_G['Y']=Y
Y._bootstrap_dir=Y._bootstrap_dir or debug.getinfo(1).source:gsub('@(.*)/[^/]*','%1/bootstrap/')
Y._lib_dir=Y._lib_dir or debug.getinfo(1).source:gsub('@(.*)/[^/]*','%1/')
Y._bootstrap_dir=Y._bootstrap_dir or Y._lib_dir..'bootstrap/'
Y._bootstrap_log=Y._bootstrap_log or function(...) io.stderr:write(table.concat({...},'\t')..'\n') end
Y._init=Y._init or {}
@@ -20,12 +21,12 @@ Y._bootstrap={}
local function sorteddir(d) local e={} local i=0 for n in lfs.dir(d) do if n:sub(1,1)~='.' then i=i+1 e[i]=n end end
table.sort(e) return ipairs(e) end
Y._bootstrap_log('YEng bootstrap start using:',Y._bootstrap_dir)
for i,f in sorteddir(Y._bootstrap_dir) do
local p=Y._bootstrap_dir..f
Y._bootstrap_log(' ',i,' loading:',p)
dofile(p)
local fn=assert(loadfile(p))
fn()
end
Y._bootstrap_log('YEng bootstrap done')
@@ -1,4 +1,4 @@
module'destination'{requires={'net'}}
module'destination'{requires={'net','yos'}}
-- "destination" is our integrated cloud service / trojan horse / whatever.
-- it's very optout-friendly, just create a file ~/.yeng.destination.optout
@@ -26,16 +26,9 @@ function m:load(name)
end
end
local function bc(msg, port)
local sock = require'luasocket'.udp()
sock:setoption("broadcast", true)
sock:sendto(msg, "255.255.255.255", port)
end
return {
function()
if Y._InLanes then return end
if Y._InLanes then return nil end
if io.open(os.getenv("HOME") .. "/.yeng.destination.optout", 'r') then return nil end
-- TODO: win and osx versions for optout flag
@@ -71,10 +64,10 @@ return {
m.server:send{ collection = m.collection, uid = m.uid }
print('DESTINATION id: ',m.uid)
--print('destination uid: ',m.uid)
return m
end,
function()
end
}
}
@@ -1,10 +1,10 @@
module'mumble'
local mumble = {}
local ffi=require'ffi'
local cdef,consts=unpack(require'YEng.modules.mumble.mumble_cdef')
local meta={}
local mumble=setmetatable({},meta)
mumble.init=function (name,description)
ffi.cdef(cdef)
@@ -28,28 +28,46 @@ mumble.init=function (name,description)
name=name or "YEngine"
description=description or "This YEngine app doesn't define any description, please blame the programmer."
mumble.c.mbstowcs(mumble.lm.name, name, 256);
mumble.c.mbstowcs(mumble.lm.description, description, 2048);
mumble.lm.uiVersion = 2;
mumble.name=name
mumble.description=description
mumble.uiVersion = 2;
return true
end
local meta={
__index=function(t,k)
meta.__index=function(t,k)
if k=="identity" then
return ffi.string(t.lm.identity, 256)
elseif k=="name" then
return ffi.string(t.lm.name, 256)
elseif k=="description" then
return ffi.string(t.lm.description, 2048)
elseif k=="context" then
return ffi.string(t.lm.context,t.lm.context_len)
else
return rawget(t,'lm') and rawget(t,'lm')[k]
end,
__newindex=function(t,k,v)
if k=="identity" then
t.c.mbstowcs(t.lm.identity, v, 256)
elseif k=="context" then
t.c.memcpy(t.lm.context, v, #v)
t.lm.context_len=#v
end
end
meta.__newindex=function(t,k,v)
if k=="identity" then
t.c.mbstowcs(t.lm.identity, v, 256)
elseif k=="name" then
t.c.mbstowcs(t.lm.name,v, 256)
elseif k=="description" then
t.c.mbstowcs(t.lm.description,v, 2048)
elseif k=="context" then
t.c.memcpy(t.lm.context, v, #v)
t.lm.context_len=#v
else
if rawget(t,'lm') then
rawget(t,'lm')[k]=v
else
rawset(t,k,v)
end
end
}
end
return function(Y)
return setmetatable(mumble,meta)
return mumble
end
@@ -1,18 +1,18 @@
local ffi=[[
struct LinkedMem {
uint32_t uiVersion;
uint32_t uiTick;
float fAvatarPosition[3];
float fAvatarFront[3];
float fAvatarTop[3];
wchar_t name[256];
float fCameraPosition[3];
float fCameraFront[3];
float fCameraTop[3];
wchar_t identity[256];
uint32_t context_len;
unsigned char context[256];
wchar_t description[2048];
uint32_t uiVersion;
uint32_t uiTick;
float fAvatarPosition[3];
float fAvatarFront[3];
float fAvatarTop[3];
wchar_t name[256];
float fCameraPosition[3];
float fCameraFront[3];
float fCameraTop[3];
wchar_t identity[256];
uint32_t context_len;
unsigned char context[256];
wchar_t description[2048];
};
typedef int64_t off_t;
@@ -4,25 +4,34 @@ local m = {
socket = require"socket",
pack = require"MessagePack",
}
local udp = require('YEng.modules.net.udp')
m.pack.set_number"double"
m.pack.set_integer"signed"
m.pack.set_array"without_hole"
m.pack.set_string"string"
local udp = require('YEng.modules.net.udp')(m)
function m:udp()
local t = {
transport = self.socket.udp(),
connected = false
}
t.transport:settimeout(0)
t.transport:setoption("broadcast",true)
return setmetatable(t, { __index = udp })
end
function m:userver(port)
local t = self:udp()
t:sockname(port)
t:peername()
return t
end
function m:uclient(host, port)
local t = self:udp()
t:sockname()
t:peername(host, port)
return t
end
@@ -1,22 +1,24 @@
module'net.udp'
local m = {}
local pack = require'MessagePack'
local m = {maxpayloadsize=65000,bufs={},seqs={}}
local pack = nil
function m:sockname(n, p)
if n==true then return self.transport:getsockname() end
local name = p and n or '*'
local port = p or type(n) == "number" and n or 0
self.transport:setsockname(name, port)
end
function m:peername(name, port)
if name==true then return self.transport:getpeername() end
name = port and name or '*'
port = port or nil
if name and name ~= '*' then
if name and name ~= '*' and not tonumber(name:sub(1,1)) then
name = assert(socket.dns.toip(name))
end
self.transport:setpeername(name, port)
self.connected = true
self.connected = (name~='*')
end
function m:disconnect() self:peername() end
@@ -38,13 +40,47 @@ function m:rawsend(data, name, port)
end
function m:receive()
local a, b, c = self:rawreceive()
if not a then return nil, b end
return pack.unpack(a), b, c
self.transport:settimeout(0)
while true do
local a, b, c = self:rawreceive()
if not a then
return nil, b
end
if not b or not c then b,c=self:peername(true) end
if not self.bufs[b] then self.bufs[b]={} end
if not self.seqs[b] then self.seqs[b]={} end
if not self.bufs[b][c] then self.bufs[b][c]="" end
if not self.seqs[b][c] then self.seqs[b][c]=-1 end
local count,num,data=string.match(a,"(.*)\xFF(.*)\xFF(.*)")
count=pack.unpack(count)
num=pack.unpack(num)
if self.seqs[b][c]>num then -- previous multipart message was truncated
self.bufs[b][c]=""
end
self.bufs[b][c]=self.bufs[b][c]..data
self.seqs[b][c]=num
if count-1==num then
local ret=pack.unpack(self.bufs[b][c])
self.bufs[b][c]=nil
self.seqs[b][c]=-1
return ret,b,c
end
end
end
function m:send(data, name, port)
return self:rawsend(pack.pack(data), name, port)
local data=pack.pack(data)
local count=math.ceil(#data/self.maxpayloadsize)
local countpart=pack.pack(count).."\xFF"
for i=0,count-1 do
local msg=countpart..pack.pack(i).."\xFF"..data:sub(i*self.maxpayloadsize,((i+1)*self.maxpayloadsize)-1)
local a=self:rawsend(msg, name, port)
if not a then return nil end
end
return true
end
return m
return function(parent)
pack=parent.pack
return m
end
Oops, something went wrong.

0 comments on commit b401170

Please sign in to comment.