/** @file * @author Edouard DUPIN * @copyright 2016, Edouard DUPIN, all right reserved * @license APACHE v2.0 (see license file) */ #include #include #include #include #include namespace appl { class TcpServerInput { private: enet::TcpServer m_interface; std::thread* m_thread; bool m_threadRunning; appl::GateWay* m_gateway; public: TcpServerInput(appl::GateWay* _gateway) : m_thread(nullptr), m_threadRunning(false), m_gateway(_gateway) { } virtual ~TcpServerInput() {} void start(const std::string& _host, uint16_t _port) { m_interface.setHostNane(_host); m_interface.setPort(_port); m_interface.link(); m_threadRunning = true; m_thread = new std::thread([&](void *){ this->threadCallback();}, nullptr); if (m_thread == nullptr) { m_threadRunning = false; APPL_ERROR("creating callback thread!"); return; } } void stop() { if (m_thread != nullptr) { m_threadRunning = false; } m_interface.unlink(); if (m_thread != nullptr) { m_thread->join(); delete m_thread; m_thread = nullptr; } } void threadCallback() { // get datas: while (m_threadRunning == true) { // READ section data: enet::Tcp data = std::move(m_interface.waitNext()); APPL_VERBOSE("New connection"); m_gateway->newService(std::move(data)); } } }; } void appl::GateWay::newService(enet::Tcp _connection) { APPL_WARNING("New TCP connection (service)"); ememory::SharedPtr tmp = ememory::makeShared(std::move(_connection)); tmp->start(this, m_idIncrement++); m_listIODirect.push_back(tmp); } appl::GateWay::GateWay() : m_idIncrement(10), propertyUserName(this, "user", "no-name", "User name of the interface"), // must be set befor start ... propertyRouterIp(this, "router-ip", "127.0.0.1", "Ip to listen client", &appl::GateWay::onPropertyChangeClientIp), propertyRouterPort(this, "router-port", 1984, "Port to listen client", &appl::GateWay::onPropertyChangeClientPort), propertyServiceIp(this, "service-ip", "127.0.0.1", "Ip to listen client", &appl::GateWay::onPropertyChangeServiceIp), propertyServicePort(this, "service-port", 1982, "Port to listen client", &appl::GateWay::onPropertyChangeServicePort), propertyServiceMax(this, "service-max", 0x7FFF, "Maximum of client at the same time", &appl::GateWay::onPropertyChangeServiceMax) { m_interfaceNewService = ememory::makeShared(this); } appl::GateWay::~GateWay() { } void appl::GateWay::start() { m_routerClient = ememory::makeShared(*propertyRouterIp, *propertyRouterPort, *propertyUserName, this); m_interfaceNewService->start(*propertyServiceIp, *propertyServicePort); } void appl::GateWay::stop() { m_routerClient.reset(); } /* ememory::SharedPtr appl::GateWay::get(const std::string& _serviceName) { for (auto &it : m_serviceList) { if (it == nullptr) { continue; } if (it->getName() != _serviceName) { continue; } return it; } return nullptr; } */ std::vector appl::GateWay::getAllServiceName() { std::vector out; // TODO : Change this it is old and deprecated ... /* for (auto &it : m_serviceList) { if (it == nullptr) { continue; } out.push_back(it->getName()); } */ return out; } void appl::GateWay::send(ememory::SharedPtr _data) { APPL_TODO("Implement Send to a specific IO ..."); /* if (m_routerClient != nullptr) { m_routerClient->answer(_userSessionId, _data); } */ } void appl::GateWay::cleanIO() { APPL_VERBOSE("Check if something need to be clean ..."); /* auto it = m_listIODirect.begin(); while (it != m_listIODirect.end()) { if (*it != nullptr) { if ((*it)->isAlive() == false) { it = m_listIODirect.erase(it); continue; } } else { it = m_listIODirect.erase(it); continue; } ++it; } */ if (m_routerClient != nullptr) { m_routerClient->clean(); } } void appl::GateWay::onClientConnect(const bool& _value) { APPL_TODO("Client connection: " << _value); } void appl::GateWay::onServiceConnect(const bool& _value) { APPL_TODO("Service connection: " << _value); } void appl::GateWay::onPropertyChangeClientIp() { } void appl::GateWay::onPropertyChangeClientPort() { } void appl::GateWay::onPropertyChangeClientMax() { } void appl::GateWay::onPropertyChangeServiceIp() { } void appl::GateWay::onPropertyChangeServicePort() { } void appl::GateWay::onPropertyChangeServiceMax() { }