From 7a9ade7740de07f7091940c7d38e4ee3781562ec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= jaakko.keranen@iki.fi
Date: Sun, 25 Apr 2021 08:57:59 +0300
Subject: [PATCH 1/1] GmRequest: Use unique IDs to avoid confusion
It is conceivable that a newly created GmRequest gets the same memory location than the one just destroyed.
IssueID #148
src/gmrequest.c | 21 ++++++++++++++++-----
src/gmrequest.h | 1 +
src/ui/command.c | 9 +++++++++
src/ui/command.h | 21 +++++++++++----------
src/ui/documentwidget.c | 18 +++++++++++++-----
5 files changed, 50 insertions(+), 20 deletions(-)
diff --git a/src/gmrequest.c b/src/gmrequest.c
index c968990c..693bfe4a 100644
--- a/src/gmrequest.c
+++ b/src/gmrequest.c
@@ -116,6 +116,8 @@ void deserialize_GmResponse(iGmResponse *d, iStream *ins) {
/----------------------------------------------------------------------------------------------/
+static iAtomicInt idGen_;
enum iGmRequestState {
initialized_GmRequestState,
receivingHeader_GmRequestState,
@@ -126,6 +128,7 @@ enum iGmRequestState {
struct Impl_GmRequest {
iObject object;
iMutex * mtx;
iGmCerts * certs; /* not owned */
enum iGmRequestState state;
@@ -471,7 +474,8 @@ static void beginGopherConnection_GmRequest_(iGmRequest *d, const iString *host,
/----------------------------------------------------------------------------------------------/
void init_GmRequest(iGmRequest *d, iGmCerts *certs) {
d->resp = new_GmResponse();
d->isFilterEnabled = iTrue;
d->isRespLocked = iFalse;
@@ -713,11 +717,18 @@ void unlockResponse_GmRequest(iGmRequest *d) {
}
}
+uint32_t id_GmRequest(const iGmRequest *d) {
+}
iBool isFinished_GmRequest(const iGmRequest *d) {
done = (d->state == finished_GmRequestState || d->state == failure_GmRequestState));
iBool done;
iGuardMutex(d->mtx,
done = (d->state == finished_GmRequestState || d->state == failure_GmRequestState));
return done;
}
enum iGmStatusCode status_GmRequest(const iGmRequest *d) {
diff --git a/src/gmrequest.h b/src/gmrequest.h
index 9f20e0eb..2cf9e4ff 100644
--- a/src/gmrequest.h
+++ b/src/gmrequest.h
@@ -73,6 +73,7 @@ void cancel_GmRequest (iGmRequest *);
iGmResponse * lockResponse_GmRequest (iGmRequest *);
void unlockResponse_GmRequest (iGmRequest *);
+uint32_t id_GmRequest (const iGmRequest ); / unique ID */
iBool isFinished_GmRequest (const iGmRequest *);
enum iGmStatusCode status_GmRequest (const iGmRequest *);
const iString * meta_GmRequest (const iGmRequest *);
diff --git a/src/ui/command.c b/src/ui/command.c
index c5ca164e..3ae0f0c9 100644
--- a/src/ui/command.c
+++ b/src/ui/command.c
@@ -50,6 +50,15 @@ int arg_Command(const char *cmd) {
return argLabel_Command(cmd, "arg");
}
+uint32_t argU32Label_Command(const char *cmd, const char *label) {
return strtoul(ptr + size_String(tok), NULL, 10);
+}
float argfLabel_Command(const char *cmd, const char *label) {
const iString *tok = tokenString_(label);
const char *ptr = strstr(cmd, cstr_String(tok));
diff --git a/src/ui/command.h b/src/ui/command.h
index 43992b8e..10a29101 100644
--- a/src/ui/command.h
+++ b/src/ui/command.h
@@ -25,16 +25,17 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#include <the_Foundation/range.h>
#include <the_Foundation/vec2.h>
-iBool equal_Command (const char *commandWithArgs, const char *command);
-int arg_Command (const char ); / arg: */
-float argf_Command (const char ); / arg: */
-int argLabel_Command (const char *, const char *label);
-float argfLabel_Command (const char *, const char *label);
-void * pointer_Command (const char ); / ptr: */
-void * pointerLabel_Command (const char *, const char *label);
-iInt2 coord_Command (const char *);
-iInt2 dir_Command (const char *);
+iBool equal_Command (const char *commandWithArgs, const char *command);
+int arg_Command (const char ); / arg: */
+float argf_Command (const char ); / arg: */
+int argLabel_Command (const char *, const char *label);
+uint32_t argU32Label_Command (const char *, const char *label);
+float argfLabel_Command (const char *, const char *label);
+void * pointer_Command (const char ); / ptr: */
+void * pointerLabel_Command (const char *, const char *label);
+iInt2 coord_Command (const char *);
+iInt2 dir_Command (const char *);
const iString * string_Command (const char *, const char label); / space-delimited */
iRangecc range_Command (const char *, const char label); / space-delimited */
diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c
index f3c9ea82..13ef878f 100644
--- a/src/ui/documentwidget.c
+++ b/src/ui/documentwidget.c
@@ -394,13 +394,21 @@ static void requestUpdated_DocumentWidget_(iAnyObject *obj) {
iDocumentWidget *d = obj;
const int wasUpdated = exchange_Atomic(&d->isRequestUpdated, iTrue);
if (!wasUpdated) {
postCommand_Widget(obj, "document.request.updated doc:%p request:%p", d, d->request);
postCommand_Widget(obj,
"document.request.updated doc:%p reqid:%u request:%p",
d,
id_GmRequest(d->request),
d->request);
}
}
static void requestFinished_DocumentWidget_(iAnyObject *obj) {
iDocumentWidget *d = obj;
"document.request.finished doc:%p reqid:%u request:%p",
d,
id_GmRequest(d->request),
d->request);
}
static int documentWidth_DocumentWidget_(const iDocumentWidget *d) {
@@ -965,7 +973,7 @@ static void updateDocument_DocumentWidget_(iDocumentWidget *d, const iGmResponse
if (d->state == ready_RequestState) {
return;
}
/* TODO: Do document update in the background. However, that requires a text metrics calculator
that does not try to cache the glyph bitmaps. */
const enum iGmStatusCode statusCode = response->statusCode;
@@ -1892,7 +1900,7 @@ static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd)
return iTrue;
}
else if (equalWidget_Command(cmd, w, "document.request.updated") &&
d->request && pointerLabel_Command(cmd, "request") == d->request) {
id_GmRequest(d->request) == argU32Label_Command(cmd, "reqid")) {
set_Block(&d->sourceContent, &lockResponse_GmRequest(d->request)->body);
unlockResponse_GmRequest(d->request);
if (document_App() == d) {
@@ -1903,7 +1911,7 @@ static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd)
return iFalse;
}
else if (equalWidget_Command(cmd, w, "document.request.finished") &&
d->request && pointerLabel_Command(cmd, "request") == d->request) {
id_GmRequest(d->request) == argU32Label_Command(cmd, "reqid")) {
set_Block(&d->sourceContent, body_GmRequest(d->request));
if (!isSuccess_GmStatusCode(status_GmRequest(d->request))) {
format_String(&d->sourceHeader,
--
2.25.1
text/plain
This content has been proxied by September (ba2dc).