2 #include <samchon/API.hpp> 4 #include <samchon/protocol/WebServer.hpp> 5 #include <samchon/protocol/IProtocol.hpp> 7 #include <samchon/templates/service/User.hpp> 10 #include <samchon/HashMap.hpp> 11 #include <samchon/library/RWMutex.hpp> 101 auto has(
const std::string &accountID)
const ->
bool 103 return account_map.
has(accountID);
112 auto get(
const std::string &accountID)
const -> std::shared_ptr<User>
114 return account_map.
get(accountID);
142 virtual void sendData(std::shared_ptr<protocol::Invoke> invoke)
override 144 std::vector<std::thread> threadArray;
147 threadArray.reserve(session_map.size());
148 for (
auto it = session_map.begin(); it != session_map.end(); it++)
149 threadArray.emplace_back(&
User::sendData, it->second.get(), invoke);
152 for (
auto it = threadArray.begin(); it != threadArray.end(); it++)
165 virtual void replyData(std::shared_ptr<protocol::Invoke>) = 0;
190 virtual void addClient(std::shared_ptr<protocol::ClientDriver> driver)
final 195 const std::string &session_id = web_driver->
getSessionID();
196 const std::string &path = web_driver->getPath();
201 std::shared_ptr<User> user;
205 it = session_map.find(session_id);
208 if (it == session_map.end())
212 user->my_weak_ptr = user;
216 session_map.insert({ session_id, user });
221 user->account_map = &account_map;
222 user->account_map_mtx = &account_map_mtx;
223 user->erase_user_function = std::bind(&Server::erase_user,
this, user.get());
228 std::shared_ptr<Client> client(user->createClient());
229 client->user_weak_ptr = user;
230 client->my_weak_ptr = client;
236 client->no = ++user->sequence;
237 user->insert({ client->no, client });
243 Service *service = client->createService(path);
244 service->client = client.get();
245 service->path = path;
247 service->user_weak_ptr = user;
248 service->client_weak_ptr = client;
251 client->service.reset(service);
256 client->driver->listen(client.get());
260 user->erase(client->no);
264 void erase_user(
User *user)
268 std::this_thread::sleep_for(std::chrono::seconds(30));
270 if (user->empty() ==
false)
273 if (user->account.empty() ==
false)
276 account_map.erase(user->account);
282 session_map.erase(user->session_id);
virtual void sendData(std::shared_ptr< protocol::Invoke > invoke) override
virtual void replyData(std::shared_ptr< protocol::Invoke >)=0
auto has(const Key &key) const -> bool
Whether have the item or not.
auto getSessionID() const -> std::string
auto has(const std::string &accountID) const -> bool
virtual void addClient(std::shared_ptr< protocol::ClientDriver > driver) final
auto get(const Key &key) -> T &
Get element.
void unlock() const
Unlock of read.
virtual ~Server()=default
virtual auto createUser() -> User *=0
Customized std::unordered_map.
virtual void sendData(std::shared_ptr< protocol::Invoke > invoke) override