Skip to content

Commit

Permalink
Move sockdrive implementation into protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Feb 2, 2024
1 parent b64975c commit 8fd5f53
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 4 deletions.
2 changes: 1 addition & 1 deletion native/dosbox-x
Submodule dosbox-x updated 1 files
+9 −5 src/gui/sdlmain.cpp
32 changes: 32 additions & 0 deletions native/jsdos/sockdrive-noop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <sockdrive.h>

size_t sockdrive_open(const char* url,
const char* owner, const char* name, const char* token) {
return 0;
}

uint8_t sockdrive_read(size_t handle, uint32_t sector, uint8_t * buffer) {
return 5;
}

uint8_t sockdrive_write(size_t handle, uint32_t sector, uint8_t* buffer) {
return 5;
}

uint32_t sockdrive_size(size_t handle) {
return 0;
}
uint32_t sockdrive_heads(size_t handle) {
return 0;
}
uint32_t sockdrive_sectors(size_t handle) {
return 0;
}
uint32_t sockdrive_cylinders(size_t handle) {
return 0;
}
uint32_t sockdrive_sector_size(size_t handle) {
return 0;
}
void sockdrive_close(size_t handle) {
}
2 changes: 1 addition & 1 deletion native/sockdrive
Submodule sockdrive updated 2 files
+0 −140 js/sockdrive.cpp
+22 −0 lz4/lz4.c
85 changes: 85 additions & 0 deletions src/dos/dosbox/cpp/worker-protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <cstdlib>
#include <string>

#include <sockdrive.h>

NetworkType connectNetwork = NETWORK_NA;
std::string connectToAddress("");

Expand Down Expand Up @@ -690,3 +692,86 @@ int main(int argc, char **argv) {
emscripten_exit_with_live_runtime();
return 0;
}


// sockdrive impl

const char *sockdriveJsImpl =
#include "../../../../native/sockdrive/js/dist/sockdriveNative.js"
;

EM_ASYNC_JS(size_t, em_sockdrive_open, (const char* url,
const char* owner, const char* name, const char* token,
const char* jsImpl), {
url = UTF8ToString(url);
owner = UTF8ToString(owner);
name = UTF8ToString(name);
token = UTF8ToString(token);

if (!Module.sockdrive) {
jsImpl = UTF8ToString(jsImpl);
eval(jsImpl);
Module.sockdrive.onOpen = (drive, read, write) => {
Module.log("sockdrive: " + drive + ", read=" + read + ", write=" + write);
};
Module.sockdrive.onError = (e) => {
Module.err(e.message ?? "unable to open sockdrive");
};
}

try {
return await Module.sockdrive.open(url, owner, name, token.length > 0 ? token : Module.token)
} catch (e) {
Module.err(e.message ?? "sockdrive not connected");
return 0;
}
});

EM_JS(uint8_t, em_sockdrive_read_sync, (size_t handle, uint32_t sector, uint8_t * buffer), {
return Module.sockdrive.read(handle, sector, buffer, true);
});

EM_ASYNC_JS(uint8_t, em_sockdrive_read_async, (size_t handle, uint32_t sector, uint8_t * buffer), {
return Module.sockdrive.read(handle, sector, buffer, false);
});

EM_JS(uint8_t, sockdrive_write, (size_t handle, uint32_t sector, uint8_t * buffer), {
return Module.sockdrive.write(handle, sector, buffer);
});

EM_JS(void, sockdrive_close, (size_t handle), {
Module.sockdrive.close(handle);
});

EM_JS(uint32_t, sockdrive_size, (size_t handle), {
return Module.sockdrive.size(handle);
});

EM_JS(uint32_t, sockdrive_heads, (size_t handle), {
return Module.sockdrive.heads(handle);
});

EM_JS(uint32_t, sockdrive_sectors, (size_t handle), {
return Module.sockdrive.sectors(handle);
});

EM_JS(uint32_t, sockdrive_sector_size, (size_t handle), {
return Module.sockdrive.sector_size(handle);
});

EM_JS(uint32_t, sockdrive_cylinders, (size_t handle), {
return Module.sockdrive.cylinders(handle);
});

size_t sockdrive_open(const char* url,
const char* owner, const char* name, const char* token) {
return em_sockdrive_open(url, owner, name, token, sockdriveJsImpl);
}

uint8_t sockdrive_read(size_t handle, uint32_t sector, uint8_t * buffer) {
auto status = em_sockdrive_read_sync(handle, sector, buffer);
if (status == 255) {
return em_sockdrive_read_async(handle, sector, buffer);
}
return status;
}
1 change: 0 additions & 1 deletion targets/dosbox-x-sdl2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,6 @@ set(SOURCES_X_JSDOS_MAIN
"${NATIVE_DIR}/sdl2net/SDLnet.c"
"${NATIVE_DIR}/sdl2net/SDLnetTCP.c"
"${NATIVE_DIR}/sdl2net/SDLnetselect.c"
"${NATIVE_DIR}/sockdrive/js/sockdrive.cpp"
)

add_library(libdosbox-x-sdl2 OBJECT ${SOURCES_X_SDL} ${SOURCES_X_CORE} ${SOURCES_X_JSDOS_CORE})
Expand Down
2 changes: 1 addition & 1 deletion targets/sokol.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ else ()
add_executable(dosbox-sokol ${SOURCES_SOKOL} ${SOURCES_SOKOL_CLIENT})
target_link_libraries(dosbox-sokol libdosbox libdosbox-core)

add_executable(dosbox-x-sokol ${SOURCES_SOKOL} ${SOURCES_SOKOL_CLIENT})
add_executable(dosbox-x-sokol ${SOURCES_SOKOL} ${SOURCES_SOKOL_CLIENT} "${NATIVE_DIR}/jsdos/sockdrive-noop.cpp")
target_link_libraries(dosbox-x-sokol libdosbox-x-jsdos)

if (APPLE)
Expand Down

0 comments on commit 8fd5f53

Please sign in to comment.