StevEngine
StevEngine is a customizable C++ game engine.
Loading...
Searching...
No Matches
client.hpp
1#pragma once
2#ifdef StevEngine_NETWORKING
3#include "networking.hpp"
4#include "utilities/ID.hpp"
5
6#include <string>
7
12
13namespace StevEngine::Networking::Client {
14
19 using MessageFunction = std::function<void(MessageData message)>;
20
28 class Manager {
29 private:
36 class MessageHandler {
37 public:
42 MessageHandler(const MessageFunction& function);
43
48 MessageHandler(const MessageHandler& copy);
49
54 void operator= (const MessageHandler& copy);
55
60 void operator() (MessageData message) const;
61
67 bool operator== (const MessageHandler& other) const;
68
73 Utilities::ID getId() const { return id; }
74
75 private:
76 MessageFunction function;
77 Utilities::ID id;
78 };
79 public:
90 Manager(std::string ip, int port);
91
97 ~Manager();
98
103 void send(const Message& message, bool reliable = true) const;
104
110 void send(const MessageID& id, MessageData data = MessageData(), bool reliable = true) const;
111
118 Utilities::ID listen(MessageID id, MessageFunction function);
119
125 void unlisten(MessageID id, const Utilities::ID handler);
126
130 bool getConnected() const { return isConnected; };
131
132 private:
133 sockaddr_in serverAddress;
134
136 std::unordered_map<MessageID, std::vector<MessageHandler>> subscribers;
137
138 fd_set readfds;
143 bool connect();
144
145 bool isConnected = false;
146
152 void disconnect();
153
158 void recieve(const Message& message);
159
160 Socket tcp, udp;
161 Utilities::ID id = Utilities::ID::empty;
162
163 // Ping/timeout handling
164 float sinceLastPing = 0;
165 };
166}
167
168#endif