summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-04-25 08:57:59 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-04-25 08:57:59 +0300
commit7887ec31b59b40dcfa7eca225fbc788e2c961727 (patch)
tree865f94b57666fd9adf6699310377ded0a86b42c6 /src
parentd57240fea98543eb5b0394a3e01268ee2e08b6ec (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/gmrequest.c21
-rw-r--r--src/gmrequest.h1
-rw-r--r--src/ui/command.c9
-rw-r--r--src/ui/command.h21
-rw-r--r--src/ui/documentwidget.c18
5 files changed, 50 insertions, 20 deletions
diff --git a/src/gmrequest.c b/src/gmrequest.c
index 8b87a638..8ab37c61 100644
--- a/src/gmrequest.c
+++ b/src/gmrequest.c
@@ -118,6 +118,8 @@ void deserialize_GmResponse(iGmResponse *d, iStream *ins) {
118 118
119/*----------------------------------------------------------------------------------------------*/ 119/*----------------------------------------------------------------------------------------------*/
120 120
121static iAtomicInt idGen_;
122
121enum iGmRequestState { 123enum iGmRequestState {
122 initialized_GmRequestState, 124 initialized_GmRequestState,
123 receivingHeader_GmRequestState, 125 receivingHeader_GmRequestState,
@@ -128,6 +130,7 @@ enum iGmRequestState {
128 130
129struct Impl_GmRequest { 131struct Impl_GmRequest {
130 iObject object; 132 iObject object;
133 uint32_t id;
131 iMutex * mtx; 134 iMutex * mtx;
132 iGmCerts * certs; /* not owned */ 135 iGmCerts * certs; /* not owned */
133 enum iGmRequestState state; 136 enum iGmRequestState state;
@@ -477,7 +480,8 @@ static void beginGopherConnection_GmRequest_(iGmRequest *d, const iString *host,
477/*----------------------------------------------------------------------------------------------*/ 480/*----------------------------------------------------------------------------------------------*/
478 481
479void init_GmRequest(iGmRequest *d, iGmCerts *certs) { 482void init_GmRequest(iGmRequest *d, iGmCerts *certs) {
480 d->mtx = new_Mutex(); 483 d->mtx = new_Mutex();
484 d->id = add_Atomic(&idGen_, 1) + 1;
481 d->resp = new_GmResponse(); 485 d->resp = new_GmResponse();
482 d->isFilterEnabled = iTrue; 486 d->isFilterEnabled = iTrue;
483 d->isRespLocked = iFalse; 487 d->isRespLocked = iFalse;
@@ -868,11 +872,18 @@ void unlockResponse_GmRequest(iGmRequest *d) {
868 } 872 }
869} 873}
870 874
875uint32_t id_GmRequest(const iGmRequest *d) {
876 return d ? d->id : 0;
877}
878
871iBool isFinished_GmRequest(const iGmRequest *d) { 879iBool isFinished_GmRequest(const iGmRequest *d) {
872 iBool done; 880 if (d) {
873 iGuardMutex(d->mtx, 881 iBool done;
874 done = (d->state == finished_GmRequestState || d->state == failure_GmRequestState)); 882 iGuardMutex(d->mtx,
875 return done; 883 done = (d->state == finished_GmRequestState || d->state == failure_GmRequestState));
884 return done;
885 }
886 return iTrue;
876} 887}
877 888
878enum iGmStatusCode status_GmRequest(const iGmRequest *d) { 889enum 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 *);
73iGmResponse * lockResponse_GmRequest (iGmRequest *); 73iGmResponse * lockResponse_GmRequest (iGmRequest *);
74void unlockResponse_GmRequest (iGmRequest *); 74void unlockResponse_GmRequest (iGmRequest *);
75 75
76uint32_t id_GmRequest (const iGmRequest *); /* unique ID */
76iBool isFinished_GmRequest (const iGmRequest *); 77iBool isFinished_GmRequest (const iGmRequest *);
77enum iGmStatusCode status_GmRequest (const iGmRequest *); 78enum iGmStatusCode status_GmRequest (const iGmRequest *);
78const iString * meta_GmRequest (const iGmRequest *); 79const 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) {
50 return argLabel_Command(cmd, "arg"); 50 return argLabel_Command(cmd, "arg");
51} 51}
52 52
53uint32_t argU32Label_Command(const char *cmd, const char *label) {
54 const iString *tok = tokenString_(label);
55 const char *ptr = strstr(cmd, cstr_String(tok));
56 if (ptr) {
57 return strtoul(ptr + size_String(tok), NULL, 10);
58 }
59 return 0;
60}
61
53float argfLabel_Command(const char *cmd, const char *label) { 62float argfLabel_Command(const char *cmd, const char *label) {
54 const iString *tok = tokenString_(label); 63 const iString *tok = tokenString_(label);
55 const char *ptr = strstr(cmd, cstr_String(tok)); 64 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. */
25#include <the_Foundation/range.h> 25#include <the_Foundation/range.h>
26#include <the_Foundation/vec2.h> 26#include <the_Foundation/vec2.h>
27 27
28iBool equal_Command (const char *commandWithArgs, const char *command); 28iBool equal_Command (const char *commandWithArgs, const char *command);
29 29
30int arg_Command (const char *); /* arg: */ 30int arg_Command (const char *); /* arg: */
31float argf_Command (const char *); /* arg: */ 31float argf_Command (const char *); /* arg: */
32int argLabel_Command (const char *, const char *label); 32int argLabel_Command (const char *, const char *label);
33float argfLabel_Command (const char *, const char *label); 33uint32_t argU32Label_Command (const char *, const char *label);
34void * pointer_Command (const char *); /* ptr: */ 34float argfLabel_Command (const char *, const char *label);
35void * pointerLabel_Command (const char *, const char *label); 35void * pointer_Command (const char *); /* ptr: */
36iInt2 coord_Command (const char *); 36void * pointerLabel_Command (const char *, const char *label);
37iInt2 dir_Command (const char *); 37iInt2 coord_Command (const char *);
38iInt2 dir_Command (const char *);
38 39
39const iString * string_Command (const char *, const char *label); /* space-delimited */ 40const iString * string_Command (const char *, const char *label); /* space-delimited */
40iRangecc range_Command (const char *, const char *label); /* space-delimited */ 41iRangecc range_Command (const char *, const char *label); /* space-delimited */
diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c
index 11baf9ee..cdf3faa5 100644
--- a/src/ui/documentwidget.c
+++ b/src/ui/documentwidget.c
@@ -442,13 +442,21 @@ static void requestUpdated_DocumentWidget_(iAnyObject *obj) {
442 iDocumentWidget *d = obj; 442 iDocumentWidget *d = obj;
443 const int wasUpdated = exchange_Atomic(&d->isRequestUpdated, iTrue); 443 const int wasUpdated = exchange_Atomic(&d->isRequestUpdated, iTrue);
444 if (!wasUpdated) { 444 if (!wasUpdated) {
445 postCommand_Widget(obj, "document.request.updated doc:%p request:%p", d, d->request); 445 postCommand_Widget(obj,
446 "document.request.updated doc:%p reqid:%u request:%p",
447 d,
448 id_GmRequest(d->request),
449 d->request);
446 } 450 }
447} 451}
448 452
449static void requestFinished_DocumentWidget_(iAnyObject *obj) { 453static void requestFinished_DocumentWidget_(iAnyObject *obj) {
450 iDocumentWidget *d = obj; 454 iDocumentWidget *d = obj;
451 postCommand_Widget(obj, "document.request.finished doc:%p request:%p", d, d->request); 455 postCommand_Widget(obj,
456 "document.request.finished doc:%p reqid:%u request:%p",
457 d,
458 id_GmRequest(d->request),
459 d->request);
452} 460}
453 461
454static int documentWidth_DocumentWidget_(const iDocumentWidget *d) { 462static int documentWidth_DocumentWidget_(const iDocumentWidget *d) {
@@ -1060,7 +1068,7 @@ static void updateDocument_DocumentWidget_(iDocumentWidget *d, const iGmResponse
1060 if (d->state == ready_RequestState) { 1068 if (d->state == ready_RequestState) {
1061 return; 1069 return;
1062 } 1070 }
1063 const iBool isRequestFinished = !d->request || isFinished_GmRequest(d->request); 1071 const iBool isRequestFinished = isFinished_GmRequest(d->request);
1064 /* TODO: Do document update in the background. However, that requires a text metrics calculator 1072 /* TODO: Do document update in the background. However, that requires a text metrics calculator
1065 that does not try to cache the glyph bitmaps. */ 1073 that does not try to cache the glyph bitmaps. */
1066 const enum iGmStatusCode statusCode = response->statusCode; 1074 const enum iGmStatusCode statusCode = response->statusCode;
@@ -2103,7 +2111,7 @@ static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd)
2103 return iTrue; 2111 return iTrue;
2104 } 2112 }
2105 else if (equalWidget_Command(cmd, w, "document.request.updated") && 2113 else if (equalWidget_Command(cmd, w, "document.request.updated") &&
2106 d->request && pointerLabel_Command(cmd, "request") == d->request) { 2114 id_GmRequest(d->request) == argU32Label_Command(cmd, "reqid")) {
2107 set_Block(&d->sourceContent, &lockResponse_GmRequest(d->request)->body); 2115 set_Block(&d->sourceContent, &lockResponse_GmRequest(d->request)->body);
2108 unlockResponse_GmRequest(d->request); 2116 unlockResponse_GmRequest(d->request);
2109 if (document_App() == d) { 2117 if (document_App() == d) {
@@ -2114,7 +2122,7 @@ static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd)
2114 return iFalse; 2122 return iFalse;
2115 } 2123 }
2116 else if (equalWidget_Command(cmd, w, "document.request.finished") && 2124 else if (equalWidget_Command(cmd, w, "document.request.finished") &&
2117 d->request && pointerLabel_Command(cmd, "request") == d->request) { 2125 id_GmRequest(d->request) == argU32Label_Command(cmd, "reqid")) {
2118 set_Block(&d->sourceContent, body_GmRequest(d->request)); 2126 set_Block(&d->sourceContent, body_GmRequest(d->request));
2119 if (!isSuccess_GmStatusCode(status_GmRequest(d->request))) { 2127 if (!isSuccess_GmStatusCode(status_GmRequest(d->request))) {
2120 format_String(&d->sourceHeader, 2128 format_String(&d->sourceHeader,