mirror of
https://gitee.com/zyjblog/oatpp.git
synced 2024-12-22 22:16:37 +08:00
Merge branch 'master' into v_1.2.0
This commit is contained in:
commit
26154e039c
@ -69,13 +69,18 @@ oatpp::data::stream::Context& SimpleTCPConnectionProvider::ExtendedConnection::g
|
||||
// SimpleTCPConnectionProvider
|
||||
|
||||
SimpleTCPConnectionProvider::SimpleTCPConnectionProvider(v_uint16 port, bool useExtendedConnections)
|
||||
: m_port(port)
|
||||
, m_closed(false)
|
||||
, m_useExtendedConnections(useExtendedConnections)
|
||||
:SimpleTCPConnectionProvider("localhost", port, useExtendedConnections)
|
||||
{
|
||||
m_serverHandle = instantiateServer();
|
||||
setProperty(PROPERTY_HOST, "localhost");
|
||||
}
|
||||
|
||||
SimpleTCPConnectionProvider::SimpleTCPConnectionProvider(const oatpp::String& host, v_uint16 port, bool useExtendedConnections)
|
||||
: m_port(port)
|
||||
, m_closed(false)
|
||||
, m_useExtendedConnections(useExtendedConnections)
|
||||
{
|
||||
setProperty(PROPERTY_HOST, host);
|
||||
setProperty(PROPERTY_PORT, oatpp::utils::conversion::int32ToStr(port));
|
||||
m_serverHandle = instantiateServer();
|
||||
}
|
||||
|
||||
SimpleTCPConnectionProvider::~SimpleTCPConnectionProvider() {
|
||||
@ -164,42 +169,51 @@ oatpp::v_io_handle SimpleTCPConnectionProvider::instantiateServer(){
|
||||
struct addrinfo hints;
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET6;
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = 0;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
auto portStr = oatpp::utils::conversion::int32ToStr(m_port);
|
||||
|
||||
ret = getaddrinfo(NULL, (const char *) portStr->getData(), &hints, &result);
|
||||
auto portStr = oatpp::utils::conversion::int32ToStr(m_port);
|
||||
auto hostStr = getProperty(PROPERTY_HOST);
|
||||
|
||||
ret = getaddrinfo((const char *)hostStr.getData(), (const char *) portStr->getData(), &hints, &result);
|
||||
if (ret != 0) {
|
||||
OATPP_LOGE("[oatpp::network::server::SimpleTCPConnectionProvider::instantiateServer()]", "Error. Call to getaddrinfo() failed with result=%d: %s", ret, strerror(errno));
|
||||
throw std::runtime_error("[oatpp::network::server::SimpleTCPConnectionProvider::instantiateServer()]: Error. Call to getaddrinfo() failed.");
|
||||
}
|
||||
|
||||
serverHandle = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
||||
if (serverHandle < 0) {
|
||||
OATPP_LOGE("[oatpp::network::server::SimpleTCPConnectionProvider::instantiateServer()]", "Error. Couldn't open a socket: socket(%d, %d, %d) %s",
|
||||
result->ai_family, result->ai_socktype, result->ai_protocol, strerror(errno));
|
||||
throw std::runtime_error("[oatpp::network::server::SimpleTCPConnectionProvider::instantiateServer()]: Error. Couldn't open a socket");
|
||||
while(result != nullptr) {
|
||||
|
||||
serverHandle = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
||||
|
||||
if (serverHandle >= 0) {
|
||||
|
||||
if (setsockopt(serverHandle, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != 0) {
|
||||
OATPP_LOGW("[oatpp::network::server::SimpleTCPConnectionProvider::instantiateServer()]",
|
||||
"Warning. Failed to set %s for accepting socket: %s", "SO_REUSEADDR", strerror(errno));
|
||||
}
|
||||
|
||||
if (bind(serverHandle, result->ai_addr, (int) result->ai_addrlen) == 0 &&
|
||||
listen(serverHandle, 10000) == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
::close(serverHandle);
|
||||
|
||||
}
|
||||
|
||||
result = result->ai_next;
|
||||
|
||||
}
|
||||
|
||||
ret = setsockopt(serverHandle, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
|
||||
if(ret < 0) {
|
||||
OATPP_LOGE("[oatpp::network::server::SimpleTCPConnectionProvider::instantiateServer()]", "Warning. Failed to set %s for accepting socket: %s", "SO_REUSEADDR", strerror(errno));
|
||||
}
|
||||
|
||||
ret = bind(serverHandle, result->ai_addr, (int) result->ai_addrlen);
|
||||
|
||||
if(ret != 0) {
|
||||
::close(serverHandle);
|
||||
OATPP_LOGE("[oatpp::network::server::SimpleTCPConnectionProvider::instantiateServer()]", "Error. Failed to bind port %d: %s", m_port, strerror(errno));
|
||||
throw std::runtime_error("[oatpp::network::server::SimpleTCPConnectionProvider::instantiateServer()]: Error. Can't bind to address: %s");
|
||||
}
|
||||
|
||||
ret = listen(serverHandle, 10000);
|
||||
if(ret < 0) {
|
||||
::close(serverHandle);
|
||||
return -1 ;
|
||||
if (result == nullptr) {
|
||||
std::string err = strerror(errno);
|
||||
OATPP_LOGE("[oatpp::network::server::SimpleTCPConnectionProvider::instantiateServer()]",
|
||||
"Error. Couldn't bind. %s", err.c_str());
|
||||
throw std::runtime_error("[oatpp::network::server::SimpleTCPConnectionProvider::instantiateServer()]: "
|
||||
"Error. Couldn't bind " + err);
|
||||
}
|
||||
|
||||
fcntl(serverHandle, F_SETFL, O_NONBLOCK);
|
||||
|
@ -93,6 +93,15 @@ public:
|
||||
* `false` to use &id:oatpp::network::Connection;.
|
||||
*/
|
||||
SimpleTCPConnectionProvider(v_uint16 port, bool useExtendedConnections = false);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param host - host name without schema and port. Ex.: "oatpp.io", "127.0.0.1", "localhost".
|
||||
* @param port - port to listen for incoming connections.
|
||||
* @param useExtendedConnections - set `true` to use &l:SimpleTCPConnectionProvider::ExtendedConnection;.
|
||||
* `false` to use &id:oatpp::network::Connection;.
|
||||
*/
|
||||
SimpleTCPConnectionProvider(const oatpp::String& host, v_uint16 port, bool useExtendedConnections = false);
|
||||
public:
|
||||
|
||||
/**
|
||||
@ -105,6 +114,17 @@ public:
|
||||
return std::make_shared<SimpleTCPConnectionProvider>(port, useExtendedConnections);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create shared SimpleTCPConnectionProvider.
|
||||
* @param host - host name without schema and port. Ex.: "oatpp.io", "127.0.0.1", "localhost".
|
||||
* @param port - port to listen for incoming connections.
|
||||
* @param port
|
||||
* @return - `std::shared_ptr` to SimpleTCPConnectionProvider.
|
||||
*/
|
||||
static std::shared_ptr<SimpleTCPConnectionProvider> createShared(const oatpp::String& host, v_uint16 port, bool useExtendedConnections = false){
|
||||
return std::make_shared<SimpleTCPConnectionProvider>(host, port, useExtendedConnections);
|
||||
}
|
||||
|
||||
/**
|
||||
* Virtual destructor.
|
||||
*/
|
||||
|
@ -161,7 +161,7 @@ void runTests() {
|
||||
oatpp::test::web::FullTest test_virtual(0, 1000);
|
||||
test_virtual.run();
|
||||
|
||||
oatpp::test::web::FullTest test_port(8000, 10);
|
||||
oatpp::test::web::FullTest test_port(8000, 5);
|
||||
test_port.run();
|
||||
|
||||
}
|
||||
@ -171,7 +171,7 @@ void runTests() {
|
||||
oatpp::test::web::FullAsyncTest test_virtual(0, 1000);
|
||||
test_virtual.run();
|
||||
|
||||
oatpp::test::web::FullAsyncTest test_port(8000, 10);
|
||||
oatpp::test::web::FullAsyncTest test_port(8000, 5);
|
||||
test_port.run();
|
||||
|
||||
}
|
||||
@ -181,7 +181,7 @@ void runTests() {
|
||||
oatpp::test::web::FullAsyncClientTest test_virtual(0, 1000);
|
||||
test_virtual.run(20);
|
||||
|
||||
oatpp::test::web::FullAsyncClientTest test_port(8000, 10);
|
||||
oatpp::test::web::FullAsyncClientTest test_port(8000, 5);
|
||||
test_port.run(1);
|
||||
|
||||
}
|
||||
|
@ -116,7 +116,7 @@ public:
|
||||
}
|
||||
|
||||
return std::static_pointer_cast<oatpp::network::ClientConnectionProvider>(
|
||||
oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", m_port)
|
||||
oatpp::network::client::SimpleTCPConnectionProvider::createShared("localhost", m_port)
|
||||
);
|
||||
|
||||
}());
|
||||
@ -207,7 +207,7 @@ void ClientRetryTest::onRun() {
|
||||
for(v_int32 i = 0; i < 100; i ++) {
|
||||
threads.push_back(std::thread([client]{
|
||||
auto response = client->getRoot();
|
||||
OATPP_ASSERT(response);
|
||||
OATPP_ASSERT(response && "Test: server pops up");
|
||||
OATPP_ASSERT(response->getStatusCode() == 200);
|
||||
auto data = response->readBodyToString();
|
||||
OATPP_ASSERT(data == "Hello World!!!");
|
||||
@ -217,7 +217,7 @@ void ClientRetryTest::onRun() {
|
||||
OATPP_LOGD(TAG, "Waiting for server to start...");
|
||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||
|
||||
runServer(m_port, 1, 1, true, controller);
|
||||
runServer(m_port, 2, 2, true, controller);
|
||||
|
||||
for(std::thread& thread : threads) {
|
||||
thread.join();
|
||||
@ -248,7 +248,7 @@ void ClientRetryTest::onRun() {
|
||||
while(oatpp::base::Environment::getMicroTickCount() - tick0 < 10 * 1000 * 1000) {
|
||||
|
||||
auto response = client->getAvailability();
|
||||
OATPP_ASSERT(response);
|
||||
OATPP_ASSERT(response && "Test: unstable server!");
|
||||
OATPP_ASSERT(response->getStatusCode() == 200);
|
||||
auto data = response->readBodyToString();
|
||||
OATPP_ASSERT(data == "Hello World!!!");
|
||||
|
@ -108,7 +108,7 @@ public:
|
||||
}
|
||||
|
||||
return std::static_pointer_cast<oatpp::network::ClientConnectionProvider>(
|
||||
oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", m_port)
|
||||
oatpp::network::client::SimpleTCPConnectionProvider::createShared("localhost", m_port)
|
||||
);
|
||||
|
||||
}());
|
||||
|
@ -110,7 +110,7 @@ public:
|
||||
}
|
||||
|
||||
return std::static_pointer_cast<oatpp::network::ClientConnectionProvider>(
|
||||
oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", m_port)
|
||||
oatpp::network::client::SimpleTCPConnectionProvider::createShared("localhost", m_port)
|
||||
);
|
||||
|
||||
}());
|
||||
|
@ -107,7 +107,7 @@ public:
|
||||
}
|
||||
|
||||
return std::static_pointer_cast<oatpp::network::ClientConnectionProvider>(
|
||||
oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", m_port)
|
||||
oatpp::network::client::SimpleTCPConnectionProvider::createShared("localhost", m_port)
|
||||
);
|
||||
|
||||
}());
|
||||
|
@ -103,7 +103,7 @@ public:
|
||||
}
|
||||
|
||||
return std::static_pointer_cast<oatpp::network::ClientConnectionProvider>(
|
||||
oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", m_port)
|
||||
oatpp::network::client::SimpleTCPConnectionProvider::createShared("localhost", m_port)
|
||||
);
|
||||
|
||||
}());
|
||||
|
@ -98,7 +98,7 @@ public:
|
||||
}
|
||||
|
||||
return std::static_pointer_cast<oatpp::network::ClientConnectionProvider>(
|
||||
oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", m_port)
|
||||
oatpp::network::client::SimpleTCPConnectionProvider::createShared("localhost", m_port)
|
||||
);
|
||||
|
||||
}());
|
||||
|
Loading…
Reference in New Issue
Block a user