From 311dede079c1d6e09167fd4cbb22a4d8d63eb021 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 8 Jul 2018 19:36:41 -0400 Subject: [PATCH] Can't resolve --- lib/src/http_listener.cc | 38 ++++++++++++++++++++++++++++++++++---- lib/src/worker_thread.cc | 28 ++++++++++++++++------------ 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/lib/src/http_listener.cc b/lib/src/http_listener.cc index c5a74d4c..2c557d4f 100644 --- a/lib/src/http_listener.cc +++ b/lib/src/http_listener.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include "wings.h" #include "wings_thread.h" @@ -29,6 +30,10 @@ int64_t get_int(Dart_CObject *obj) void handleMessage(Dart_Port destPortId, Dart_CObject *message) { + if (message->type != Dart_CObject_kArray) { + return; + } + // We always expect an array to be sent. Dart_CObject_Type firstType = message->value.as_array.values[0]->type; @@ -46,19 +51,44 @@ void handleMessage(Dart_Port destPortId, Dart_CObject *message) { // The Dart world is trying to close this port. Dart_Port port = message->value.as_array.values[1]->value.as_send_port.id; - Dart_CloseNativePort(port); + //Dart_CloseNativePort(port); } else { // This is either a send or close message. + std::cout << "NVALUES: " << message->value.as_array.length << std::endl; int sockfd = (int)get_int(message->value.as_array.values[0]); printf("FD: %d\n", sockfd); - if (message->value.as_array.length == 2) + if (message->value.as_array.length >= 2) { auto *msg = message->value.as_array.values[1]; - printf("Length: %ld\n", msg->value.as_typed_data.length); - write(sockfd, msg->value.as_typed_data.values, (size_t)msg->value.as_typed_data.length); + + if (msg != nullptr) + { + + if (msg->type == Dart_CObject_kExternalTypedData) + { + std::cout << "ext typed data" << std::endl; + printf("Length: %ld\n", msg->value.as_external_typed_data.length); + std::cout << "ptr: " << msg->value.as_typed_data.values << std::endl; + write(sockfd, msg->value.as_external_typed_data.data, (size_t)msg->value.as_external_typed_data.length); + } + else if (msg->type == Dart_CObject_kTypedData) + { + std::cout << "regular typed data" << std::endl; + printf("Length: %ld\n", msg->value.as_typed_data.length); + write(sockfd, msg->value.as_typed_data.values, (size_t)msg->value.as_typed_data.length); + } + else + { + std::cout << "unknown type: " << ((Dart_CObject_Type) msg->type) << std::endl; + } + } + else + { + std::cout << "null msg!!!" << std::endl; + } } else { diff --git a/lib/src/worker_thread.cc b/lib/src/worker_thread.cc index d98790ce..b8ea5b22 100644 --- a/lib/src/worker_thread.cc +++ b/lib/src/worker_thread.cc @@ -1,4 +1,5 @@ //#include +#include #include #include #include "wings_thread.h" @@ -64,9 +65,8 @@ int send_string(http_parser *parser, char *str, size_t length, int code, bool as { //if (parser == nullptr) return 0; auto *rq = (requestInfo *)parser->data; + char *s = nullptr; //if (rq == nullptr) return 0; - auto *s = new char[length + 1]; - memset(s, 0, length + 1); Dart_CObject first{}; Dart_CObject second{}; @@ -77,7 +77,10 @@ int send_string(http_parser *parser, char *str, size_t length, int code, bool as if (!as_typed_data) { + s = new char[length + 1]; + memset(s, 0, length + 1); third.type = Dart_CObject_kString; + //strcpy(s, str); memcpy(s, str, length); third.value.as_string = s; } @@ -97,7 +100,8 @@ int send_string(http_parser *parser, char *str, size_t length, int code, bool as obj.value.as_array.length = 3; obj.value.as_array.values = list; Dart_PostCObject(rq->port, &obj); - delete[] s; + if (s != nullptr) + delete s; return 0; } @@ -145,7 +149,7 @@ int send_oncomplete(http_parser *parser, int code) } //void handleRequest(const std::shared_ptr &rq) -void handleRequest(requestInfo* rq) +void handleRequest(requestInfo *rq) { size_t len = 80 * 1024, nparsed; char buf[len]; @@ -153,21 +157,21 @@ void handleRequest(requestInfo* rq) memset(buf, 0, len); http_parser parser{}; - http_parser_init(&parser, HTTP_REQUEST); parser.data = rq; //rq.get(); + http_parser_init(&parser, HTTP_REQUEST); http_parser_settings settings{}; settings.on_message_begin = [](http_parser *parser) { - // std::cout << "mb" << std::endl; + std::cout << "mb" << std::endl; return send_notification(parser, 0); }; settings.on_message_complete = [](http_parser *parser) { - //std::cout << "mc" << std::endl; + std::cout << "mc" << std::endl; send_oncomplete(parser, 1); delete (requestInfo *)parser->data; - //std::cout << "deleted rq!" << std::endl; + std::cout << "deleted rq!" << std::endl; return 0; }; @@ -177,23 +181,23 @@ void handleRequest(requestInfo* rq) }; settings.on_header_field = [](http_parser *parser, const char *at, size_t length) { - // std::cout << "hf" << std::endl; + std::cout << "hf" << std::endl; return send_string(parser, (char *)at, length, 3); }; settings.on_header_value = [](http_parser *parser, const char *at, size_t length) { - // std::cout << "hv" << std::endl; + std::cout << "hv" << std::endl; return send_string(parser, (char *)at, length, 4); }; settings.on_body = [](http_parser *parser, const char *at, size_t length) { - // std::cout << "body" << std::endl; + std::cout << "body" << std::endl; return send_string(parser, (char *)at, length, 5, true); }; unsigned int isUpgrade = 0; - // std::cout << "start" << std::endl; + std::cout << "start" << std::endl; while ((recved = recv(rq->sock, buf, len, 0)) > 0) { if (isUpgrade)