Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
SystemBalanceLoader.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
20
21#pragma once
22
23#include "sese/event/Event.h"
25#include "sese/thread/Thread.h"
26
27#include <atomic>
28#include <functional>
29#include <mutex>
30
31namespace sese::service {
32
37public:
38 ~SystemBalanceLoader() noexcept;
39
42 void setThreads(size_t th) noexcept;
43
46 void setAddress(const net::IPAddress::Ptr &addr) noexcept { SystemBalanceLoader::address = addr; }
47
50 void setAcceptTimeout(uint32_t to) noexcept { SystemBalanceLoader::timeout = to; }
51
54 void setDispatchTimeout(uint32_t to) noexcept { SystemBalanceLoader::timeout = to; }
55
58 [[nodiscard]] bool isStarted() const { return _isStart; }
59
63 template<class SERVICE>
64 bool init() noexcept;
65
70 template<class SERVICE>
71 bool init(std::function<SERVICE *()> creator) noexcept;
72
74 void start() noexcept;
75
77 void stop() noexcept;
78
79protected:
80 std::atomic_bool _isStart{false};
81 std::atomic_bool _isStop{false};
82
83 uint32_t timeout = 100;
84 size_t threads{2};
85 std::vector<socket_t> socketVector;
86 std::vector<event::EventLoop *> eventLoopVector;
87 std::vector<sese::Thread::Ptr> threadVector;
89};
90} // namespace sese::service
91
92template<class SERVICE>
94 return sese::service::SystemBalanceLoader::init<SERVICE>([]() -> SERVICE * { return new SERVICE; });
95}
96
97// GCOVR_EXCL_START
98
99template<class SERVICE>
100bool sese::service::SystemBalanceLoader::init(std::function<SERVICE *()> creator) noexcept {
101 if (address == nullptr) return false;
102
103 sese::net::ReusableSocket reusable_socket(address);
104 for (size_t i = 0; i < threads; ++i) {
105 auto sub_socket = reusable_socket.makeRawSocket();
106 if (sub_socket == -1) {
107 goto freeSocket;
108 }
109 if (0 != sese::net::Socket::setNonblocking(sub_socket)) {
110 goto freeSocket;
111 }
112 if (0 != sese::net::Socket::listen(sub_socket, 32)) {
113 goto freeSocket;
114 }
115 socketVector.emplace_back(sub_socket);
116 }
117
118 for (size_t i = 0; i < threads; ++i) {
119 auto event = creator();
120 if (event == nullptr) {
121 goto freeEvent;
122 }
123 event->setListenFd(static_cast<int>(socketVector[i]));
124 if (!event->init()) {
125 delete event;
126 goto freeEvent;
127 } else {
128 eventLoopVector.emplace_back(event);
129 }
130 }
131
132 return true;
133
134freeEvent:
135 for (decltype(auto) event_loop: eventLoopVector) {
136 delete event_loop;
137 }
138 eventLoopVector.clear();
139
140freeSocket:
141 for (decltype(auto) sub_socket: socketVector) {
142 sese::net::Socket::close(sub_socket);
143 }
144 socketVector.clear();
145 return false;
146}
147
148// GCOVR_EXCL_STOP