I am trying to write a wrapper around the Oat++ system to simplify usage across multiple microservices, but, I have run into an issue.
Aim
I want to be able to specify the service provider points (address, port and address type of IPV4/IPV6) via a class function.
Issue
If I run the minimum viable code below and try to connect to HTTP:://localhost:8888/test I can't connect to the port (I get a timeout). If I change the dynamic for loop for the hard-coded provider and try to connect I get the expected 404.
So change:
for (auto entry = m_serviceProviderEntry.begin ();
entry != m_serviceProviderEntry.end (); entry++)
{
auto addrType = (*entry).networkType == SERVICENETWORKTYPE_IPV4 ?
oatpp::network::Address::IP_4 : oatpp::network::Address::IP_6;
auto provider = oatpp::network::tcp::server::ConnectionProvider::createShared (
{ (*entry).address, v_uint16 ((*entry).networkPort), addrType });
m_connectionProviders.push_back (provider);
oatpp::network::Server server (provider, m_connectionHandler);
server.run (true);
}
for:
auto provider = oatpp::network::tcp::server::ConnectionProvider::createShared (
{ "localhost", 8888, oatpp::network::Address::IP_4 });
oatpp::network::Server server (provider, m_connectionHandler);
server.run (true);
}
Any suggestions or help would be gratefully appreciated as I don't understand why this is failing as it is, maybe use of std::shared_ptr in a std::list?
Minimal viable and repeatable code
#include <winsock2.h>
#include "oatpp/web/server/HttpConnectionHandler.hpp"
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
#include "oatpp/network/Server.hpp"
enum ServiceNetworkType
{
SERVICENETWORKTYPE_IPV4,
SERVICENETWORKTYPE_IPV6
};
struct ServiceProviderEntry
{
std::string address;
v_uint16 networkPort;
ServiceNetworkType networkType;
};
class Example
{
public:
Example () : m_shutdownRequested (false) { }
bool Initialise ()
{
int iResult;
WSADATA wsaData;
iResult = WSAStartup (MAKEWORD (2, 2), &wsaData);
if (iResult != 0) return false;
return true;
}
void Execute ()
{
m_router = oatpp::web::server::HttpRouter::createShared ();
m_connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared (m_router);
for (auto entry = m_serviceProviderEntry.begin ();
entry != m_serviceProviderEntry.end (); entry++)
{
auto addrType = (*entry).networkType == SERVICENETWORKTYPE_IPV4 ?
oatpp::network::Address::IP_4 : oatpp::network::Address::IP_6;
auto provider = oatpp::network::tcp::server::ConnectionProvider::createShared (
{ (*entry).address, v_uint16 ((*entry).networkPort), addrType });
m_connectionProviders.push_back (provider);
oatpp::network::Server server (provider, m_connectionHandler);
server.run (true);
}
while (!m_shutdownRequested) { }
}
void AddServiceProvider (std::string address,
int networkPort,
ServiceNetworkType networkType)
{
ServiceProviderEntry entry = { address,
v_uint16 (networkPort),
networkType };
m_serviceProviderEntry.push_back (entry);
}
private:
bool m_shutdownRequested;
std::shared_ptr<oatpp::web::server::HttpRouter> m_router;
std::shared_ptr<oatpp::web::server::HttpConnectionHandler> m_connectionHandler;
std::list <std::shared_ptr<oatpp::network::tcp::server::ConnectionProvider>> m_connectionProviders;
std::list<ServiceProviderEntry> m_serviceProviderEntry;
};
int main ()
{
auto app = new Example ();
app->Initialise ();
app->AddServiceProvider ("localhost", 8888, SERVICENETWORKTYPE_IPV4);
app->Execute ();
return EXIT_SUCCESS;
}
Here is the issue:
At the end of each iteration your server object is deleted.
ConnectionProviderobject is deleted too.Possible solution would look something like the following: