Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
sese::service::UserBalanceLoader Class Reference

User load balancer, applicable to all platforms. More...

#include <UserBalanceLoader.h>

Classes

struct  SocketStatus
 Socket status. More...
 

Public Member Functions

 ~UserBalanceLoader () noexcept
 
void setThreads (size_t th) noexcept
 
void setAddress (const net::IPAddress::Ptr &addr) noexcept
 
void setAcceptTimeout (uint32_t to) noexcept
 
void setDispatchTimeout (uint32_t to) noexcept
 
bool isStarted () const
 
template<class SERVICE >
bool init () noexcept
 
template<class SERVICE >
bool init (std::function< SERVICE *()> creator) noexcept
 
void start () noexcept
 Start the current loader and service.
 
void stop () noexcept
 Stop the current loader and unload services.
 
void dispatchSocket (socket_t sock, void *data)
 
void setOnDispatchedCallbackFunction (const std::function< void(int, sese::event::EventLoop *, void *)> &cb)
 

Protected Member Functions

void master () noexcept
 
void slave (sese::event::EventLoop *event_loop, std::queue< SocketStatus > *master_queue, std::queue< SocketStatus > *slave_queue, std::mutex *mutex) noexcept
 

Protected Attributes

std::atomic_bool _isStart {false}
 
std::atomic_bool _isStop {false}
 
uint32_t acceptTimeout = 100
 
uint32_t dispatchTimeout = 100
 
size_t threads {2}
 
std::vector< sese::event::EventLoop * > eventLoopVector
 
std::vector< sese::Thread::PtrthreadVector
 
sese::net::IPAddress::Ptr address = sese::net::IPv4Address::localhost(8080)
 
sese::net::Socketsocket {nullptr}
 
sese::service::MasterEventLoopmasterEventLoop {nullptr}
 
sese::Thread::Ptr masterThread {nullptr}
 
std::queue< SocketStatus > * masterSocketQueueArray {nullptr}
 socket_t exchange queues
 
std::queue< SocketStatus > * slaveSocketQueueArray {nullptr}
 
std::mutex * mutexArray {nullptr}
 
std::function< void(int, sese::event::EventLoop *event_loop, void *)> onDispatchedCallbackFunction
 

Detailed Description

User load balancer, applicable to all platforms.

Constructor & Destructor Documentation

◆ ~UserBalanceLoader()

sese::service::UserBalanceLoader::~UserBalanceLoader ( )
noexcept

Member Function Documentation

◆ dispatchSocket()

void sese::service::UserBalanceLoader::dispatchSocket ( sese::socket_t sock,
void * data )

◆ init() [1/2]

template<class SERVICE >
bool sese::service::UserBalanceLoader::init ( )
noexcept

Initialize loader resources

Template Parameters
SERVICEService to be started
Returns
Whether initialization is successful

References init().

Referenced by sese::iocp::v1::IOCPServer::init(), and init().

◆ init() [2/2]

template<class SERVICE >
bool sese::service::UserBalanceLoader::init ( std::function< SERVICE *()> creator)
noexcept

Initialize balancer resources

Template Parameters
SERVICEService to be started
Parameters
creatorService creation function, returns an instance pointer on success, otherwise should return nullptr
Returns
Whether initialization is successful

References sese::net::Socket::IPv4, sese::net::Socket::IPv6, and sese::net::Socket::TCP.

◆ isStarted()

bool sese::service::UserBalanceLoader::isStarted ( ) const
inlinenodiscard

Get the current loader status

Returns
Loader status

References _isStart.

◆ master()

void sese::service::UserBalanceLoader::master ( )
protectednoexcept

Referenced by start().

◆ setAcceptTimeout()

void sese::service::UserBalanceLoader::setAcceptTimeout ( uint32_t to)
inlinenoexcept

Set the timeout for the main listening thread

Parameters
toTimeout in milliseconds

References acceptTimeout.

Referenced by sese::iocp::v1::IOCPServer::setAcceptTimeout().

◆ setAddress()

void sese::service::UserBalanceLoader::setAddress ( const net::IPAddress::Ptr & addr)
inlinenoexcept

Set the service startup address

Parameters
addrIP Address

References address.

Referenced by sese::iocp::v1::IOCPServer::setAddress().

◆ setDispatchTimeout()

void sese::service::UserBalanceLoader::setDispatchTimeout ( uint32_t to)
inlinenoexcept

Set the dispatch timeout for worker threads

Parameters
toTimeout in milliseconds

References dispatchTimeout.

Referenced by sese::iocp::v1::IOCPServer::setDispatchTimeout().

◆ setOnDispatchedCallbackFunction()

void sese::service::UserBalanceLoader::setOnDispatchedCallbackFunction ( const std::function< void(int, sese::event::EventLoop *, void *)> & cb)
inline

◆ setThreads()

void sese::service::UserBalanceLoader::setThreads ( size_t th)
noexcept

Set the number of threads used by the loader

Parameters
thNumber of threads

Referenced by sese::iocp::v1::IOCPServer::setThreads().

◆ slave()

void sese::service::UserBalanceLoader::slave ( sese::event::EventLoop * event_loop,
std::queue< SocketStatus > * master_queue,
std::queue< SocketStatus > * slave_queue,
std::mutex * mutex )
protectednoexcept

Referenced by start().

◆ start()

void sese::service::UserBalanceLoader::start ( )
noexcept

Start the current loader and service.

References master(), and slave().

Referenced by sese::iocp::v1::IOCPServer::init().

◆ stop()

void sese::service::UserBalanceLoader::stop ( )
noexcept

Stop the current loader and unload services.

References sese::net::Socket::close().

Referenced by sese::iocp::v1::IOCPServer::shutdown(), and ~UserBalanceLoader().

Member Data Documentation

◆ _isStart

std::atomic_bool sese::service::UserBalanceLoader::_isStart {false}
protected

Referenced by isStarted(), and ~UserBalanceLoader().

◆ _isStop

std::atomic_bool sese::service::UserBalanceLoader::_isStop {false}
protected

Referenced by ~UserBalanceLoader().

◆ acceptTimeout

uint32_t sese::service::UserBalanceLoader::acceptTimeout = 100
protected

Referenced by setAcceptTimeout().

◆ address

sese::net::IPAddress::Ptr sese::service::UserBalanceLoader::address = sese::net::IPv4Address::localhost(8080)
protected

Referenced by setAddress().

◆ dispatchTimeout

uint32_t sese::service::UserBalanceLoader::dispatchTimeout = 100
protected

Referenced by setDispatchTimeout().

◆ eventLoopVector

std::vector<sese::event::EventLoop *> sese::service::UserBalanceLoader::eventLoopVector
protected

Referenced by ~UserBalanceLoader().

◆ masterEventLoop

sese::service::MasterEventLoop* sese::service::UserBalanceLoader::masterEventLoop {nullptr}
protected

Referenced by ~UserBalanceLoader().

◆ masterSocketQueueArray

std::queue<SocketStatus>* sese::service::UserBalanceLoader::masterSocketQueueArray {nullptr}
protected

socket_t exchange queues

Referenced by ~UserBalanceLoader().

◆ masterThread

sese::Thread::Ptr sese::service::UserBalanceLoader::masterThread {nullptr}
protected

◆ mutexArray

std::mutex* sese::service::UserBalanceLoader::mutexArray {nullptr}
protected

◆ onDispatchedCallbackFunction

std::function<void(int, sese::event::EventLoop *event_loop, void *)> sese::service::UserBalanceLoader::onDispatchedCallbackFunction
protected

◆ slaveSocketQueueArray

std::queue<SocketStatus>* sese::service::UserBalanceLoader::slaveSocketQueueArray {nullptr}
protected

Referenced by ~UserBalanceLoader().

◆ socket

sese::net::Socket* sese::service::UserBalanceLoader::socket {nullptr}
protected

Referenced by ~UserBalanceLoader().

◆ threads

size_t sese::service::UserBalanceLoader::threads {2}
protected

◆ threadVector

std::vector<sese::Thread::Ptr> sese::service::UserBalanceLoader::threadVector
protected

The documentation for this class was generated from the following files: