MXS-2196: Store listeners in a global list

The listeners are now stored in their own list which allows them to be a
component separate from the service. The next step is to remove the
listener iterator functionality and replace it with its STL counterpart.
This commit is contained in:
Markus Mäkelä 2018-11-29 12:56:01 +02:00
parent 5926ac2c3c
commit 6f9b9f5e95
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
2 changed files with 51 additions and 44 deletions

View File

@ -15,6 +15,7 @@
#include <maxscale/ccdefs.hh>
#include <string>
#include <memory>
#include <maxbase/jansson.h>
#include <maxscale/protocol.h>
@ -33,26 +34,28 @@ class SERV_LISTENER
{
public:
SERV_LISTENER(SERVICE* service, const std::string& name, const std::string& address, uint16_t port,
const std::string& protocol, const std::string& authenticator,
const std::string& auth_opts, void* auth_instance, SSL_LISTENER* ssl);
const std::string& protocol, const std::string& authenticator,
const std::string& auth_opts, void* auth_instance, SSL_LISTENER* ssl);
~SERV_LISTENER();
public:
std::string name; /**< Name of the listener */
std::string protocol; /**< Protocol module to load */
uint16_t port; /**< Port to listen on */
std::string address; /**< Address to listen with */
std::string authenticator;/**< Name of authenticator */
std::string auth_options; /**< Authenticator options */
void* auth_instance;/**< Authenticator instance */
SSL_LISTENER* ssl; /**< Structure of SSL data or NULL */
struct dcb* listener; /**< The DCB for the listener */
struct users* users; /**< The user data for this listener */
SERVICE* service; /**< The service which used by this listener */
int active; /**< True if the port has not been deleted */
SERV_LISTENER* next; /**< Next service protocol */
std::string name; /**< Name of the listener */
std::string protocol; /**< Protocol module to load */
uint16_t port; /**< Port to listen on */
std::string address; /**< Address to listen with */
std::string authenticator; /**< Name of authenticator */
std::string auth_options; /**< Authenticator options */
void* auth_instance; /**< Authenticator instance */
SSL_LISTENER* ssl; /**< Structure of SSL data or NULL */
struct dcb* listener; /**< The DCB for the listener */
struct users* users; /**< The user data for this listener */
SERVICE* service; /**< The service which used by this listener */
int active; /**< True if the port has not been deleted */
SERV_LISTENER* next; /**< Next service protocol */
};
using SListener = std::shared_ptr<SERV_LISTENER>;
typedef struct listener_iterator
{
SERV_LISTENER* current;
@ -94,13 +97,13 @@ json_t* listener_to_json(const SERV_LISTENER* listener);
* @return New listener or nullptr on error
*/
SERV_LISTENER* listener_alloc(SERVICE* service,
const char* name,
const char* protocol,
const char* address,
unsigned short port,
const char* authenticator,
const char* auth_options,
SSL_LISTENER* ssl);
const char* name,
const char* protocol,
const char* address,
unsigned short port,
const char* authenticator,
const char* auth_options,
SSL_LISTENER* ssl);
/**
* @brief Free a listener

View File

@ -11,29 +11,20 @@
* Public License.
*/
/**
* @file listener.c - Listener generic functions
*
* Listeners wait for new client connections and, if the connection is successful
* a new session is created. A listener typically knows about a port or a socket,
* and a few other things. It may know about SSL if it is expecting an SSL
* connection.
*
* @verbatim
* Revision History
*
* Date Who Description
* 26/01/16 Martin Brampton Initial implementation
*
* @endverbatim
*/
#include <maxscale/listener.hh>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <string>
#include <maxscale/listener.hh>
#include <algorithm>
#include <list>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include <maxscale/paths.h>
#include <maxscale/ssl.h>
#include <maxscale/protocol.h>
@ -43,9 +34,11 @@
#include <maxscale/service.hh>
#include <maxscale/poll.h>
static std::list<SListener> all_listeners;
static std::mutex listener_lock;
static RSA* rsa_512 = NULL;
static RSA* rsa_1024 = NULL;
static RSA* tmp_rsa_callback(SSL* s, int is_export, int keylength);
SERV_LISTENER::SERV_LISTENER(SERVICE* service, const std::string& name, const std::string& address,
@ -110,13 +103,24 @@ SERV_LISTENER* listener_alloc(SERVICE* service,
return nullptr;
}
return new(std::nothrow) SERV_LISTENER(service, name, address, port, protocol, authenticator,
auth_options, auth_instance, ssl);
auto listener = new(std::nothrow) SERV_LISTENER(service, name, address, port, protocol, authenticator,
auth_options, auth_instance, ssl);
if (listener)
{
std::lock_guard<std::mutex> guard(listener_lock);
all_listeners.emplace_back(listener);
}
return listener;
}
void listener_free(SERV_LISTENER* listener)
{
delete listener;
std::lock_guard<std::mutex> guard(listener_lock);
all_listeners.remove_if([&](const SListener& l) {
return l.get() == listener;
});
}
void listener_destroy(SERV_LISTENER* listener)