cool and cute pool
This commit is contained in:
parent
3ce74b2312
commit
8976efe29b
2 changed files with 46 additions and 3 deletions
|
@ -18,7 +18,7 @@
|
||||||
class User;
|
class User;
|
||||||
class Test : sosc::Pool<User> {
|
class Test : sosc::Pool<User> {
|
||||||
protected:
|
protected:
|
||||||
void ProcessClient(User* client) override;
|
bool ProcessClient(User* client) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
|
@ -42,6 +42,9 @@ public:
|
||||||
protected:
|
protected:
|
||||||
virtual bool ProcessClient(T* client) = 0;
|
virtual bool ProcessClient(T* client) = 0;
|
||||||
private:
|
private:
|
||||||
|
bool IsStackFull(int stackCount) const;
|
||||||
|
bool CanAddStack() const;
|
||||||
|
|
||||||
class Stack {
|
class Stack {
|
||||||
public:
|
public:
|
||||||
Stack(Pool<T>* pool);
|
Stack(Pool<T>* pool);
|
||||||
|
@ -94,10 +97,50 @@ void Pool<T>::Start() {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
bool Pool<T>::AddClient(T* client) {
|
bool Pool<T>::IsStackFull(int stackCount) const {
|
||||||
// TODO
|
poolinfo_t *info = &this->info;
|
||||||
|
return info->max_size != -1
|
||||||
|
&& stackCount <
|
||||||
|
info->initial_size
|
||||||
|
+ (info->size_growth
|
||||||
|
* (this->stacks.size() - info->initial_count))
|
||||||
|
&& stackCount < info->max_size;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
template<class T>
|
||||||
|
bool Pool<T>::CanAddStack() const {
|
||||||
|
return this->info.max_count == -1
|
||||||
|
|| this->stacks.size() < this->info.max_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
bool Pool<T>::AddClient(T* client) {
|
||||||
|
if(!this->is_running)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(this->info.max_total != -1)
|
||||||
|
if(this->ClientCount() >= this->info.max_total)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int lowestCount = -1;
|
||||||
|
Stack* lowestStack = nullptr;
|
||||||
|
for(auto i = this->stacks.begin(); i != this->stacks.end(); ++i) {
|
||||||
|
int thisCount;
|
||||||
|
if((thisCount = i->ClientCount()) > lowestCount) {
|
||||||
|
lowestCount = thisCount;
|
||||||
|
lowestStack = &(*i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(lowestStack != nullptr && !this->IsStackFull(lowestCount))
|
||||||
|
lowestStack->AddClient(client);
|
||||||
|
else if(this->CanAddStack()) {
|
||||||
|
this->stacks.push_back(Stack(this));
|
||||||
|
this->stacks.back().AddClient(client);
|
||||||
|
} else
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
|
|
Loading…
Reference in a new issue