summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-06-19 16:38:12 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-06-19 16:38:12 +0800
commit35216c30aea2f85966bb59999ad7293726b5cacb (patch)
tree546b76d70a5e253c949b32b0c6e4f332532d4c06
parent3a8b5d69fcfa647146bbdf55657624915e1f9549 (diff)
internal: seperate client and clientmanager header
-rw-r--r--include/libsock/server/client.hpp42
-rw-r--r--include/libsock/server/clients.hpp51
-rw-r--r--src/server/client.cpp1
-rw-r--r--src/server/clients.cpp1
4 files changed, 59 insertions, 36 deletions
diff --git a/include/libsock/server/client.hpp b/include/libsock/server/client.hpp
index 9c7fc9f..1799f34 100644
--- a/include/libsock/server/client.hpp
+++ b/include/libsock/server/client.hpp
@@ -1,7 +1,6 @@
#pragma once
#include "../types.hpp"
-#include "server.hpp"
#include <any>
#include <format>
#include <functional>
@@ -9,12 +8,16 @@
#include <optional>
#include <string>
#include <sys/socket.h>
-#include <thread>
-#include <vector>
namespace LibSock {
namespace Server {
+class Server;
+class Clients;
+
+extern SP<Server> pServer;
+extern SP<Clients> pClients;
+
struct SRecvData {
std::string data;
const size_t size = 1024;
@@ -31,9 +34,6 @@ enum eEventType : std::uint8_t {
WRITE
};
-class Clients;
-inline SP<Clients> pClients;
-
class Client {
public:
~Client();
@@ -70,35 +70,5 @@ class Client {
friend class Clients;
};
-struct SData {
- std::string msg;
- std::optional<WP<Client>> sender = std::nullopt;
-};
-
-class Clients {
- public:
- Clients();
- ~Clients();
-
- WP<Client> newClient(std::optional<std::function<std::any(WP<Client>)>> cb = std::nullopt);
-
- void broadcast(const std::string &msg, std::optional<WP<Client>> self = std::nullopt); // second param only specified when we want to exclude the sender
- void kick(WP<Client> client, const bool kill = false, const std::string &reason = "", std::optional<std::function<std::any(const std::vector<std::pair<std::jthread, SP<Client>>>::iterator &)>> cb = std::nullopt);
- void addClient(const Client &client);
-
- void shutdownClients(std::optional<std::function<std::any(const std::vector<std::pair<std::jthread, SP<Client>>> &)>> cb = std::nullopt);
-
- SP<Client> getByIp(const std::string &ip) const;
- std::vector<SP<Client>> getClients() const;
- std::vector<SData> getDatas() const;
-
- private:
- void broadcast(const SData &msg);
-
- std::vector<std::pair<std::jthread, SP<Client>>> m_vClients;
- std::vector<SData> m_vDatas;
-
- friend class Client;
-};
} // namespace Server
} // namespace LibSock
diff --git a/include/libsock/server/clients.hpp b/include/libsock/server/clients.hpp
new file mode 100644
index 0000000..9b94127
--- /dev/null
+++ b/include/libsock/server/clients.hpp
@@ -0,0 +1,51 @@
+#pragma once
+
+#include "../types.hpp"
+#include <any>
+#include <functional>
+#include <optional>
+#include <string>
+#include <thread>
+#include <vector>
+
+namespace LibSock {
+namespace Server {
+
+class Client;
+
+struct SData {
+ std::string msg;
+ std::optional<WP<Client>> sender = std::nullopt;
+};
+class Clients {
+ public:
+ Clients();
+ ~Clients();
+
+ WP<Client> newClient(
+ std::optional<std::function<std::any(WP<Client>)>> cb = std::nullopt
+ );
+
+ void broadcast(const std::string &msg,
+ std::optional<WP<Client>> self = std::nullopt); // second param only specified when we
+ // want to exclude the sender
+ void kick(WP<Client> client, const bool kill = false, const std::string &reason = "", std::optional<std::function<std::any(const std::vector<std::pair<std::jthread, SP<Client>>>::iterator &)>> cb = std::nullopt);
+ void addClient(const Client &client);
+
+ void shutdownClients(std::optional<std::function<std::any(const std::vector<std::pair<std::jthread, SP<Client>>> &)>> cb = std::nullopt);
+
+ SP<Client> getByIp(const std::string &ip) const;
+ std::vector<SP<Client>> getClients() const;
+ std::vector<SData> getDatas() const;
+
+ private:
+ void broadcast(const SData &msg);
+
+ std::vector<std::pair<std::jthread, SP<Client>>> m_vClients;
+ std::vector<SData> m_vDatas;
+
+ friend class Client;
+};
+
+} // namespace Server
+} // namespace LibSock
diff --git a/src/server/client.cpp b/src/server/client.cpp
index 65a5e0c..cfc512a 100644
--- a/src/server/client.cpp
+++ b/src/server/client.cpp
@@ -1,4 +1,5 @@
#include "libsock/server/client.hpp"
+#include "libsock/server/clients.hpp"
#include "libsock/server/server.hpp"
#include "libsock/types.hpp"
#include "misc/FileDescriptor.hpp"
diff --git a/src/server/clients.cpp b/src/server/clients.cpp
index 38ce878..c957d92 100644
--- a/src/server/clients.cpp
+++ b/src/server/clients.cpp
@@ -1,3 +1,4 @@
+#include "libsock/server/clients.hpp"
#include "libsock/server/client.hpp"
#include <algorithm>
#include <any>