2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-09-01 16:31:14 +08:00
|
|
|
#if (AX_TARGET_PLATFORM != AX_PLATFORM_EMSCRIPTEN)
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/PlatformConfig.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "CurlTest.h"
|
|
|
|
#include "stdio.h"
|
|
|
|
#include "stdlib.h"
|
|
|
|
#include "curl/curl.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
CurlTests::CurlTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(CurlTest);
|
|
|
|
}
|
|
|
|
|
|
|
|
CurlTest::CurlTest()
|
|
|
|
{
|
|
|
|
auto label = Label::createWithTTF("Curl Test", "fonts/arial.ttf", 28);
|
|
|
|
addChild(label, 0);
|
2021-12-31 12:12:40 +08:00
|
|
|
label->setPosition(VisibleRect::center().x, VisibleRect::top().y - 50);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(CurlTest::onTouchesEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
// create a label to display the tip string
|
|
|
|
_label = Label::createWithTTF("Touch the screen to connect", "fonts/arial.ttf", 22);
|
|
|
|
_label->setPosition(VisibleRect::center());
|
|
|
|
addChild(_label, 0);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_label->retain();
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
struct MemoryStruct
|
|
|
|
{
|
|
|
|
char* memory;
|
|
|
|
size_t size;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb, void* userp)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
size_t realsize = size * nmemb;
|
|
|
|
struct MemoryStruct* mem = (struct MemoryStruct*)userp;
|
|
|
|
|
|
|
|
mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1);
|
|
|
|
if (mem->memory == NULL)
|
|
|
|
{
|
|
|
|
/* out of memory! */
|
|
|
|
printf("not enough memory (realloc returned NULL)\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&(mem->memory[mem->size]), contents, realsize);
|
|
|
|
mem->size += realsize;
|
|
|
|
mem->memory[mem->size] = 0;
|
|
|
|
|
|
|
|
return realsize;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// the test code is
|
|
|
|
// http://curl.haxx.se/mail/lib-2009-12/0071.html
|
2021-12-31 12:12:40 +08:00
|
|
|
void CurlTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
CURL* curl;
|
2019-11-23 20:27:39 +08:00
|
|
|
CURLcode res;
|
|
|
|
char buffer[10];
|
|
|
|
|
|
|
|
struct MemoryStruct chunk;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
chunk.memory = (char*)malloc(1); /* will be grown as needed by the realloc above */
|
|
|
|
chunk.size = 0; /* no data at this point */
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
curl = curl_easy_init();
|
2021-12-31 12:12:40 +08:00
|
|
|
if (curl)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, "http://webtest.cocos2d-x.org/curltest");
|
2021-12-31 12:12:40 +08:00
|
|
|
// code from http://curl.haxx.se/libcurl/c/getinmemory.html
|
2019-11-23 20:27:39 +08:00
|
|
|
/* we pass our 'chunk' struct to the callback function */
|
2021-12-31 12:12:40 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk);
|
|
|
|
// If we don't provide a write function for curl, it will receive error code 23 on windows.
|
2019-11-23 20:27:39 +08:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
|
|
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
/* always cleanup */
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
if (res == 0)
|
|
|
|
{
|
|
|
|
_label->setString(StringUtils::format("Connect successfully!\n%s", chunk.memory));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
sprintf(buffer, "code: %i", res);
|
2019-11-23 20:27:39 +08:00
|
|
|
_label->setString(buffer);
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
else
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_label->setString("no curl");
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CurlTest::~CurlTest()
|
|
|
|
{
|
|
|
|
_label->release();
|
|
|
|
}
|
2023-09-01 16:31:14 +08:00
|
|
|
|
|
|
|
#endif
|