Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
SystemBalanceLoader.h
浏览该文件的文档.
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// 此处测试代码不便于模拟
98// GCOVR_EXCL_START
99
100template<class SERVICE>
101bool sese::service::SystemBalanceLoader::init(std::function<SERVICE *()> creator) noexcept {
102 if (address == nullptr) return false;
103
104 sese::net::ReusableSocket reusable_socket(address);
105 for (size_t i = 0; i < threads; ++i) {
106 auto sub_socket = reusable_socket.makeRawSocket();
107 if (sub_socket == -1) {
108 goto freeSocket;
109 }
110 if (0 != sese::net::Socket::setNonblocking(sub_socket)) {
111 goto freeSocket;
112 }
113 if (0 != sese::net::Socket::listen(sub_socket, 32)) {
114 goto freeSocket;
115 }
116 socketVector.emplace_back(sub_socket);
117 }
118
119 for (size_t i = 0; i < threads; ++i) {
120 auto event = creator();
121 if (event == nullptr) {
122 goto freeEvent;
123 }
124 event->setListenFd(static_cast<int>(socketVector[i]));
125 if (!event->init()) {
126 delete event;
127 goto freeEvent;
128 } else {
129 eventLoopVector.emplace_back(event);
130 }
131 }
132
133 return true;
134
135freeEvent:
136 for (decltype(auto) event_loop: eventLoopVector) {
137 delete event_loop;
138 }
139 eventLoopVector.clear();
140
141freeSocket:
142 for (decltype(auto) sub_socket: socketVector) {
143 sese::net::Socket::close(sub_socket);
144 }
145 socketVector.clear();
146 return false;
147}
148
149// GCOVR_EXCL_STOP