-
-
Notifications
You must be signed in to change notification settings - Fork 146
Lua uvFifoListener ffi #1156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Lua uvFifoListener ffi #1156
Changes from 3 commits
b35017e
ed1b9f2
e0c4ca6
796698a
d0a6d42
ea79db0
22cf8ae
6d1eaa6
dcf1fc4
9eaa15b
47f54b6
4c7c11b
5bd5e0a
58dc601
ba060e6
19dbddd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ ffi.cdef [[ | |
| typedef struct { char opaque[?]; } LuaFile; | ||
| typedef struct { uint32_t size; uint8_t data[?]; } LuaBuffer; | ||
| typedef struct { char opaque[?]; } LuaSlice; | ||
| typedef struct { char opaque[?]; } LuaServer; | ||
|
|
||
| enum FileOps { | ||
| READ, | ||
|
|
@@ -49,6 +50,11 @@ LuaFile* subFile(LuaFile*, uint64_t start, int64_t size); | |
|
|
||
| LuaFile* uvFifo(const char* address, int port); | ||
|
|
||
| LuaServer* uvFifoListener(); | ||
|
|
||
| void stopListener(LuaServer* server); | ||
| void startListener(LuaServer* server, unsigned port, void (*cb)(LuaFile* fifo)); | ||
|
|
||
| void closeFile(LuaFile* wrapper); | ||
|
|
||
| uint64_t readFileRawPtr(LuaFile* wrapper, void* dst, uint64_t size); | ||
|
|
@@ -339,6 +345,40 @@ local function createFileWrapper(wrapper) | |
| return file | ||
| end | ||
|
|
||
| local function createUvListener(port, cb) | ||
| if type(port) ~= 'number' then error 'port argument must be an unsigned number' end | ||
| if (port) == nil then error 'must provide a port' end | ||
| if cb == nil then error 'must provide a callback function' end | ||
|
|
||
| _callback = function(fifo) end | ||
|
|
||
| if cb ~= nil then | ||
| if type(cb) ~= 'function' then error 'callback must be a function' end | ||
| _callback = function(fifo) | ||
| file = createFileWrapper(fifo) | ||
| cb(file) | ||
| end | ||
| end | ||
|
|
||
| _callback = ffi.cast('void (*)(LuaFile* fifo)', _callback) | ||
|
|
||
| local wrapper = C.uvFifoListener() | ||
|
|
||
| local listener = { | ||
| _type = "LuaServer", | ||
| _wrapper = wrapper, | ||
| _proxy = newproxy(), | ||
| _cb = _callback, | ||
| _port = port, | ||
| startListener = function(listener) C.startListener(listener._wrapper, listener._port, listener._cb) end, | ||
| stopListener = function(listener) C.stopListener(listener._wrapper) end, | ||
| } | ||
| -- Use a proxy instead of doing this on the wrapper directly using ffi.gc, because of a bug in LuaJIT, | ||
| -- where circular references on finalizers using ffi.gc won't actually collect anything. | ||
| debug.setmetatable(listener._proxy, { __gc = function() C.stopListener(listener._wrapper) end }) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should (1) call the deleter of the class instead, and (2) it should free the callback in Lua. |
||
| return listener | ||
| end | ||
|
|
||
| local function open(filename, t) | ||
| if (t == nil) then t = 'READ' end | ||
| if (t == 'DOWNLOAD_URL_AND_WAIT') then | ||
|
|
@@ -395,6 +435,10 @@ local function uvFifo(address, port) | |
| return createFileWrapper(C.uvFifo(address, port)) | ||
| end | ||
|
|
||
| local function uvFifoListener(port, callback) | ||
| return createUvListener(port, callback) | ||
| end | ||
|
|
||
| if (type(Support) ~= 'table') then Support = {} end | ||
|
|
||
| Support.NewLuaBuffer = function(size) | ||
|
|
@@ -408,6 +452,8 @@ Support.File = { | |
| buffer = buffer, | ||
| zReader = zReader, | ||
| uvFifo = uvFifo, | ||
| uvFifoListener = uvFifoListener, | ||
| _createUvListener = createUvListener, | ||
| _createFileWrapper = createFileWrapper, | ||
| _createSliceWrapper = createSliceWrapper, | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| namespace { | ||
|
|
||
| using LuaFile = PCSX::LuaFFI::LuaFile; | ||
| using LuaServer = PCSX::LuaFFI::LuaServer; | ||
|
|
||
| enum FileOps { | ||
| READ, | ||
|
|
@@ -73,6 +74,22 @@ LuaFile* subFile(LuaFile* wrapper, uint64_t start, int64_t size) { | |
| } | ||
| LuaFile* uvFifo(const char* address, int port) { return new LuaFile(new PCSX::UvFifo(address, port)); } | ||
|
|
||
| LuaServer* uvFifoListener() {return new LuaServer(new PCSX::UvFifoListener());} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also need a function to delete a LuaServer. It's not simple as it needs to schedule a stop if the server is running, and then delete in the async close callback. |
||
|
|
||
| void startListener(LuaServer* server, unsigned port, void (*cb)(LuaFile* fifo)) { server->m_listener->start(port, PCSX::g_system->getLoop(), &server->m_async, [cb, server](PCSX::UvFifo* fifo) { | ||
| if (fifo) { | ||
| cb(new LuaFile(fifo)); | ||
| } else { | ||
| server->m_async.data = server; | ||
| uv_close(reinterpret_cast<uv_handle_t*>(&server->m_async), [](uv_handle_t* handle) {}); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| void stopListener(LuaServer* server) { | ||
| server->m_listener->stop(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably need a status variable to avoid double stops. It needs to be "STARTED", "STOPPING", "STOPPED". |
||
| } | ||
|
|
||
| void closeFile(LuaFile* wrapper) { wrapper->file->close(); } | ||
|
|
||
| uint64_t readFileRawPtr(LuaFile* wrapper, void* dst, uint64_t size) { return wrapper->file->read(dst, size); } | ||
|
|
@@ -194,6 +211,9 @@ static void registerAllSymbols(PCSX::Lua L) { | |
| REGISTER(L, bufferFileEmpty); | ||
| REGISTER(L, subFile); | ||
| REGISTER(L, uvFifo); | ||
| REGISTER(L, uvFifoListener); | ||
| REGISTER(L, stopListener); | ||
| REGISTER(L, startListener); | ||
|
|
||
| REGISTER(L, closeFile); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we check for
cb == nilbefore, this check isn't really useful.Also, we're going to overwrite
_callbackafterward. And it should be declared aslocal.