Adding save fetch capabilities.

This commit is contained in:
Sergiotarxz 2023-04-02 17:27:49 +02:00
parent 97db819b1e
commit 4dfdd41c08
7 changed files with 30 additions and 19 deletions

View File

@ -31,6 +31,8 @@ struct msClientConnectionData {
pthread_mutex_t *mutexSaveRequests;
//! The number of save requests.
size_t saveRequestsLen;
//! The number of frame.
size_t numberFrame;
};
/**

View File

@ -15,6 +15,7 @@ msClientConnectionDataNew(size_t numberOfThread, int clientFd) {
data->mutexSaveRequests = malloc(sizeof *data->mutexSaveRequests);
data->saveRequests = NULL;
data->saveRequestsLen = 0;
data->numberFrame = 0;
data->lastFrameDate = calloc(sizeof *data->lastFrameDate, 1);
pthread_mutex_init(data->mutexSendPacket, NULL);
pthread_mutex_init(data->mutexPressKey, NULL);

View File

@ -60,6 +60,10 @@ msCoreControllerLoadGame (const unsigned char *rom, size_t rom_len,
mCoreConfigSetIntValue(&core->config, "videoSync", 1);
core->desiredVideoDimensions(core, &width, &height);
if (core->platform(core) == mPLATFORM_GB) {
width = 160;
height = 144;
}
color_t *outputBuffer = malloc((sizeof *outputBuffer) * width * height);
printf("controller->outputBuffer width: %u\n", width);
core->setVideoBuffer(core, outputBuffer, width);
@ -87,7 +91,7 @@ mCoreThreadAfterStart(struct mCoreThread *threadContext) {
struct msClientConnectionData *data = (struct msClientConnectionData *)threadContext->userData;
struct msCoreController *coreController = data->coreController;
struct mCore *core = coreController->threadContext.core;
if (!mCoreLoadStateNamed (core, coreController->saveState, SAVESTATE_SAVEDATA & SAVESTATE_RTC)) {
if (!mCoreLoadStateNamed (core, coreController->saveState, SAVESTATE_SAVEDATA | SAVESTATE_RTC | SAVESTATE_SCREENSHOT)) {
fprintf(stderr, "Unable to load save state\n");
}
}

View File

@ -40,7 +40,6 @@ msThreadCallbackSetFrame(struct mCoreThread *threadContext) {
#define SEC_TO_NANO(sec) \
(((uint64_t)sec) * 1000 * 1000 * 1000)
struct msClientConnectionData *data = (struct msClientConnectionData *)threadContext->userData;
unsigned int stride;
color_t *outputBuffer;
unsigned width, height;
unsigned char *rawData = NULL;
@ -55,7 +54,6 @@ msThreadCallbackSetFrame(struct mCoreThread *threadContext) {
if (!data || !data->coreController) {
goto return_ms_thread_callback_set_frame;
}
stride = data->coreController->stride;
outputBuffer = data->coreController->outputBuffer;
while (nanoCurrent < nanoLast && nanoLast != 0 && nanoLast - nanoCurrent < SEC_TO_NANO(10)) {
@ -66,8 +64,16 @@ msThreadCallbackSetFrame(struct mCoreThread *threadContext) {
*data->lastFrameDate = currentDate;
data->coreController->threadContext.core->desiredVideoDimensions(data->coreController->threadContext.core, &width, &height);
if (data->coreController->threadContext.core->platform(data->coreController->threadContext.core) == mPLATFORM_GB) {
width = 160;
height = 144;
}
sendFrame = msPacketSendFrameNew(stride, width * height, outputBuffer);
if (data->numberFrame++ % 10 != 0) {
return;
}
sendFrame = msPacketSendFrameNew(width, width * height, outputBuffer);
rawData = msPacketSendFrameSerialize(sendFrame, &rawDataLen);
if (!rawData) {
goto return_ms_thread_callback_set_frame;

View File

@ -73,7 +73,7 @@ shrinkOrNullSaveRequestsDataArray(struct msClientConnectionData *data) {
}
static unsigned char *
VFileToBytes(struct VFile *file, size_t *len) {
vFileToBytes(struct VFile *file, size_t *len) {
unsigned char *result = NULL;
const ssize_t bufferLen = 120;
ssize_t readLen = 0;
@ -85,15 +85,10 @@ VFileToBytes(struct VFile *file, size_t *len) {
file->seek(file, 0, SEEK_SET);
while ((readLen = file->read(file, buffer, bufferLen)) && readLen) {
*len += readLen;
unsigned char *resultTmp= realloc(result, sizeof *result * *len);
if (!resultTmp) {
free(result);
result = NULL;
goto return_vfile_to_bytes;
}
result = resultTmp;
size_t size = sizeof *result * *len;
result = realloc(result, size);
// memcpy_s not available in linux.
memcpy(&result[*len-1-readLen], buffer, readLen); // NOLINT
memcpy(&result[*len - readLen], buffer, readLen); // NOLINT
}
return_vfile_to_bytes:
return result;
@ -128,8 +123,8 @@ unlock_ms_packet_save_request_send_response:
return;
}
saveState = VFileMemChunk (NULL, 0);
mCoreSaveStateNamed(core, saveState, SAVESTATE_SAVEDATA & SAVESTATE_RTC);
save = VFileToBytes(saveState, &saveLen);
mCoreSaveStateNamed(core, saveState, SAVESTATE_ALL);
save = vFileToBytes(saveState, &saveLen);
response = msPacketSaveResponseNew(request->identifier, saveLen, save);
rawData = msPacketSaveResponseSerialize(response, &rawDataLen);
if (!rawData) {
@ -172,6 +167,7 @@ msPacketSaveRequestHandle(const struct msPacket *packet, struct msPacketSaveRequ
pthread_mutex_unlock(data->mutexSaveRequests);
mCoreThreadRunFunction(&coreController->threadContext, &msPacketSaveRequestSendResponse);
result = true;
return_ms_packet_save_request_handle:
msPacketSaveRequestDestroy(&self);
return result;

View File

@ -1,3 +1,4 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@ -33,14 +34,15 @@ msPacketSaveResponseSerialize(struct msPacketSaveResponse *const self, size_t *l
*len = sizeof identifier + sizeof saveLen + sizeof *result * saveLen;
result = malloc(*len);
htobe64(identifier);
htobe64(saveLen);
identifier = htobe64(identifier);
saveLen = htobe64(saveLen);
memcpy(&result[currentCursor], &identifier, sizeof identifier); // NOLINT
currentCursor += sizeof identifier;
memcpy(&result[currentCursor], &saveLen, sizeof saveLen); // NOLINT
currentCursor += sizeof saveLen;
saveLen = be64toh(saveLen);
memcpy(&result[currentCursor], self->save, sizeof *result * saveLen); // NOLINT

View File

@ -30,7 +30,7 @@ msPacketSendFrameSerialize(struct msPacketSendFrame *const self, size_t *len) {
unsigned int stride = self->stride;
size_t outputBufferSizeBytes = self->outputBufferSize * sizeof (uint32_t);
stride = htobe32(stride);
*len = (sizeof self->stride) + (sizeof self->outputBufferSize) + (outputBufferSizeBytes);
*len = (sizeof stride) + (sizeof outputBufferSizeBytes) + (outputBufferSizeBytes);
outputBufferSizeBytes = htobe64(outputBufferSizeBytes);
bytes = malloc(*len);
#define GOTO_END_SERIALIZE_FAIL() \
@ -38,7 +38,7 @@ msPacketSendFrameSerialize(struct msPacketSendFrame *const self, size_t *len) {
bytes = NULL; \
goto return_ms_packet_send_frame_serialize;
FILE *fp = fmemopen(bytes, sizeof *bytes * *len, "w");
if (fwrite(&self->stride, sizeof self->stride, 1, fp) == -1) {
if (fwrite(&stride, sizeof stride, 1, fp) == -1) {
GOTO_END_SERIALIZE_FAIL();
}
if (fwrite(&outputBufferSizeBytes, sizeof outputBufferSizeBytes, 1, fp) == -1) {