Optimize YAsioTest, see #150

This commit is contained in:
halx99 2020-08-10 00:20:29 +08:00
parent a72a75886d
commit adc97af519
1 changed files with 69 additions and 100 deletions

View File

@ -31,106 +31,88 @@
#include "yasio/obstream.hpp" #include "yasio/obstream.hpp"
#include "yasio/yasio.hpp" #include "yasio/yasio.hpp"
#include "yasio/platform/yasio_jni.cpp" #include "yasio/platform/yasio_jni.cpp"
#include "base/SimpleTimer.h"
USING_NS_CC; USING_NS_CC;
using namespace yasio; using namespace yasio;
using namespace yasio::inet; using namespace yasio::inet;
void yasioTest() class YNSM{
{ public:
yasio::inet::io_hostent endpoints[] = { {"github.com", 443} }; static YNSM* getInstance() {
static YNSM s_nsm;
return &s_nsm;
}
io_service service(endpoints, 1); YNSM() : _service(1) // 1 channel
{
resolv_fn_t resolv = [&](std::vector<ip::endpoint>& endpoints, const char* hostname, yasio::inet::io_hostent endpoints[] = { {"github.com", 443} };
unsigned short port) { _service.start([&](event_ptr&& event) {
return service.builtin_resolv(endpoints, hostname, port); switch (event->kind())
};
service.set_option(YOPT_S_RESOLV_FN, &resolv);
std::vector<transport_handle_t> transports;
deadline_timer udpconn_delay(service);
deadline_timer udp_heartbeat(service);
int total_bytes_transferred = 0;
int max_request_count = 3;
std::string respRawData;
std::string finalMessage;
service.start_service([&](event_ptr&& event) {
switch (event->kind())
{
case YEK_PACKET: {
auto packet = std::move(event->packet());
total_bytes_transferred += static_cast<int>(packet.size());
respRawData.insert(respRawData.end(), packet.begin(), packet.end());
break;
}
case YEK_CONNECT_RESPONSE:
if (event->status() == 0)
{ {
auto transport = event->transport(); case YEK_PACKET: {
if (event->cindex() == 0) auto packet = std::move(event->packet());
{ _respRawData.insert(_respRawData.end(), packet.begin(), packet.end());
obstream obs; break;
obs.write_bytes("GET / HTTP/1.1\r\n");
obs.write_bytes("Host: github.com\r\n");
obs.write_bytes("User-Agent: Mozilla/5.0 (Windows NT 10.0; "
"WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/78.0.3904.108 Safari/537.36\r\n");
obs.write_bytes("Accept: */*;q=0.8\r\n");
obs.write_bytes("Connection: Close\r\n\r\n");
service.write(transport, std::move(obs.buffer()));
} }
case YEK_CONNECT_RESPONSE:
if (event->status() == 0)
{
auto transport = event->transport();
if (event->cindex() == 0)
{
obstream obs;
obs.write_bytes("GET / HTTP/1.1\r\n");
transports.push_back(transport); obs.write_bytes("Host: github.com\r\n");
}
break;
case YEK_CONNECTION_LOST:
cocos2d::log("yasio-ssl recv data: %s\n---> %d bytes transferred", respRawData.c_str(), total_bytes_transferred);
respRawData.clear();
total_bytes_transferred = 0; obs.write_bytes("User-Agent: Mozilla/5.0 (Windows NT 10.0; "
if (--max_request_count > 0) "WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
{ "Chrome/78.0.3904.108 Safari/537.36\r\n");
udpconn_delay.expires_from_now(std::chrono::seconds(1)); obs.write_bytes("Accept: */*;q=0.8\r\n");
udpconn_delay.async_wait_once([&]() { service.open(0, YCK_SSL_CLIENT); }); obs.write_bytes("Connection: Close\r\n\r\n");
_service.write(transport, std::move(obs.buffer()));
}
}
break;
case YEK_CONNECTION_LOST:
cocos2d::log("yasio-ssl recv data: %s\n---> %d bytes transferred", _respRawData.c_str(), _respRawData.size());
_respRawData.clear();
break;
} }
else
service.stop_service();
break;
}
}); });
/* _timerId = stimer::loop(0x7fffffff, 0, [=](){
** If after 5 seconds no data interaction at application layer, _service.dispatch(128);
** send a heartbeat per 10 seconds when no response, try 2 times });
** if no response, then he connection will shutdown by driver.
** At windows will close with error: 10054
*/
service.set_option(YOPT_S_TCP_KEEPALIVE, 5, 10, 2);
service.open(0, YCK_SSL_CLIENT); // open http client
time_t duration = 0;
while (service.is_running())
{
service.dispatch();
if (duration >= 6000000)
{
for (auto transport : transports)
service.close(transport);
break;
}
duration += 50;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
} }
}
~YNSM(){
stimer::kill(_timerId); // don't like any network event
_service.stop(); // stop network service thread
}
// just test
void openHttpsConnection(){
/*
** If after 5 seconds no data interaction at application layer,
** send a heartbeat per 10 seconds when no response, try 2 times
** if no response, then he connection will shutdown by driver.
** At windows will close with error: 10054
*/
_service.set_option(YOPT_S_TCP_KEEPALIVE, 5, 10, 2);
_service.set_option(YOPT_C_REMOTE_ENDPOINT, 0, "github.com", 443);
_respRawData.clear();
_service.open(0, YCK_SSL_CLIENT); // open http client
}
io_service _service;
stimer::TIMER_ID _timerId;
std::string _respRawData;
};
YAsioTests::YAsioTests() YAsioTests::YAsioTests()
{ {
@ -155,23 +137,10 @@ YAsioTest::YAsioTest()
_label->retain(); _label->retain();
} }
// the test code is
// https://github.com/simdsoft/yasio/blob/master/tests/ssl/main.cpp
void YAsioTest::onTouchesEnded(const std::vector<Touch*>& touches, Event *event) void YAsioTest::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
{ {
std::thread t([=] { YNSM::getInstance()->openHttpsConnection();
Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] { _label->setString("Starting yasio https request test, see the output!");
_label->setString("Starting yasio https request test, see the output!");
});
yasioTest();
Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] {
_label->setString("See the output!");
});
});
t.detach();
} }
YAsioTest::~YAsioTest() YAsioTest::~YAsioTest()