Skip to content

Commit 70ea856

Browse files
committed
WIP
1 parent 8a2be72 commit 70ea856

20 files changed

+209
-95
lines changed

src/openvic-simulation/GameManager.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "GameManager.hpp"
22

33
#include <chrono>
4-
#include <string_view>
54
#include <cstddef>
65
#include <string_view>
76

src/openvic-simulation/GameManager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "openvic-simulation/multiplayer/ChatManager.hpp"
1414
#include "openvic-simulation/multiplayer/ClientManager.hpp"
1515
#include "openvic-simulation/multiplayer/HostManager.hpp"
16+
#include "openvic-simulation/player/PlayerManager.hpp"
1617
#include "openvic-simulation/utility/ForwardableSpan.hpp"
1718

1819
#include <function2/function2.hpp>

src/openvic-simulation/multiplayer/BaseMultiplayerManager.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#include "BaseMultiplayerManager.hpp"
32

43
#include "openvic-simulation/multiplayer/PacketType.hpp"

src/openvic-simulation/multiplayer/BaseMultiplayerManager.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
#include <cstdint>
44

5+
#include "openvic-simulation/multiplayer/Constants.hpp"
56
#include "openvic-simulation/multiplayer/HostSession.hpp"
67
#include "openvic-simulation/multiplayer/PacketType.hpp"
7-
#include "openvic-simulation/multiplayer/lowlevel/ReliableUdpClient.hpp"
8+
#include "openvic-simulation/multiplayer/lowlevel/Constants.hpp"
89
#include "openvic-simulation/types/RingBuffer.hpp"
910
#include "openvic-simulation/utility/Containers.hpp"
1011
#include "openvic-simulation/utility/Getters.hpp"
@@ -16,10 +17,10 @@ namespace OpenVic {
1617
BaseMultiplayerManager(GameManager* game_manager = nullptr);
1718
virtual ~BaseMultiplayerManager() = default;
1819

19-
using client_id_type = uint64_t;
20-
using sequence_type = ReliableUdpClient::sequence_type;
20+
using client_id_type = OpenVic::client_id_type;
21+
using sequence_type = reliable_udp_sequence_type;
2122

22-
static constexpr client_id_type HOST_ID = static_cast<client_id_type>(~0);
23+
static constexpr client_id_type HOST_ID = MP_HOST_ID;
2324

2425
virtual bool broadcast_packet(PacketType const& type, PacketType::argument_type argument);
2526
virtual bool send_packet(client_id_type client_id, PacketType const& type, PacketType::argument_type argument);

src/openvic-simulation/multiplayer/ChatManager.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#include "ChatManager.hpp"
32

43
#include <chrono>
@@ -11,15 +10,15 @@
1110

1211
using namespace OpenVic;
1312

14-
ChatGroup::ChatGroup(index_t index, memory::vector<BaseMultiplayerManager::client_id_type>&& clients)
13+
ChatGroup::ChatGroup(index_type index, memory::vector<BaseMultiplayerManager::client_id_type>&& clients)
1514
: index { index }, clients { std::move(clients) } {}
1615

1716
ChatManager::ChatManager(ClientManager* client_manager) : client_manager { client_manager } {}
1817

1918
bool ChatManager::send_private_message(BaseMultiplayerManager::client_id_type to, memory::string&& message) {
2019
MessageData data { MessageType::PRIVATE, std::move(message), to };
2120

22-
ChatMessageLog const& log = log_message(client_manager->get_client_id(), std::move(data));
21+
ChatMessageLog const& log = log_message(client_manager->get_player()->get_client_id(), std::move(data));
2322
bool was_sent = client_manager->broadcast_packet(PacketTypes::send_chat_message, &log);
2423
return was_sent;
2524
}
@@ -31,7 +30,7 @@ bool ChatManager::send_private_message(BaseMultiplayerManager::client_id_type to
3130
bool ChatManager::send_public_message(memory::string&& message) {
3231
MessageData data { MessageType::PUBLIC, std::move(message) };
3332

34-
ChatMessageLog const& log = log_message(client_manager->get_client_id(), std::move(data));
33+
ChatMessageLog const& log = log_message(client_manager->get_player()->get_client_id(), std::move(data));
3534
bool was_sent = client_manager->broadcast_packet(PacketTypes::send_chat_message, &log);
3635
return was_sent;
3736
}
@@ -43,12 +42,12 @@ bool ChatManager::send_public_message(std::string_view message) {
4342
bool ChatManager::send_group_message(ChatGroup const& group, memory::string&& message) {
4443
MessageData data { MessageType::GROUP, std::move(message), group.get_index() };
4544

46-
ChatMessageLog const& log = log_message(client_manager->get_client_id(), std::move(data));
45+
ChatMessageLog const& log = log_message(client_manager->get_player()->get_client_id(), std::move(data));
4746
bool was_sent = client_manager->broadcast_packet(PacketTypes::send_chat_message, &log);
4847
return was_sent;
4948
}
5049

51-
bool ChatManager::send_group_message(ChatGroup::index_t group_id, memory::string&& message) {
50+
bool ChatManager::send_group_message(ChatGroup::index_type group_id, memory::string&& message) {
5251
OV_ERR_FAIL_INDEX_V(group_id, groups.size(), false);
5352
return send_group_message(groups[group_id], std::move(message));
5453
}
@@ -57,7 +56,7 @@ bool ChatManager::send_group_message(ChatGroup const& group, std::string_view me
5756
return send_group_message(group, memory::string { message });
5857
}
5958

60-
bool ChatManager::send_group_message(ChatGroup::index_t group_id, std::string_view message) {
59+
bool ChatManager::send_group_message(ChatGroup::index_type group_id, std::string_view message) {
6160
return send_group_message(group_id, memory::string { message });
6261
}
6362

@@ -96,30 +95,30 @@ void ChatManager::_create_group(memory::vector<BaseMultiplayerManager::client_id
9695
group_created(last);
9796
}
9897

99-
void ChatManager::set_group(ChatGroup::index_t group_id, memory::vector<BaseMultiplayerManager::client_id_type>&& clients) {
98+
void ChatManager::set_group(ChatGroup::index_type group_id, memory::vector<BaseMultiplayerManager::client_id_type>&& clients) {
10099
OV_ERR_FAIL_INDEX(group_id, groups.size());
101100
client_manager->broadcast_packet(PacketTypes::modify_chat_group, PacketChatGroupModifyData { group_id, clients });
102101
}
103102

104-
void ChatManager::_set_group(ChatGroup::index_t group_id, memory::vector<BaseMultiplayerManager::client_id_type>&& clients) {
103+
void ChatManager::_set_group(ChatGroup::index_type group_id, memory::vector<BaseMultiplayerManager::client_id_type>&& clients) {
105104
ChatGroup& group = groups[group_id];
106105
std::swap(group.clients, clients);
107106
group_modified(group, clients);
108107
}
109108

110-
ChatGroup const& ChatManager::get_group(ChatGroup::index_t group_index) const {
109+
ChatGroup const& ChatManager::get_group(ChatGroup::index_type group_index) const {
111110
return groups.at(group_index);
112111
}
113112

114-
void ChatManager::delete_group(ChatGroup::index_t group_id) {
113+
void ChatManager::delete_group(ChatGroup::index_type group_id) {
115114
OV_ERR_FAIL_INDEX(group_id, groups.size());
116115
client_manager->broadcast_packet(
117116
PacketTypes::delete_chat_group,
118117
PacketType::argument_type { std::in_place_index<PacketTypes::delete_chat_group.packet_id>, group_id }
119118
);
120119
}
121120

122-
void ChatManager::_delete_group(ChatGroup::index_t group_id) {
121+
void ChatManager::_delete_group(ChatGroup::index_type group_id) {
123122
groups.erase(groups.begin() + group_id);
124123
group_deleted(group_id);
125124
}

src/openvic-simulation/multiplayer/ChatManager.hpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,40 @@
44
#include <cstdint>
55
#include <string_view>
66

7-
#include "openvic-simulation/multiplayer/BaseMultiplayerManager.hpp"
8-
#include "openvic-simulation/multiplayer/ClientManager.hpp"
7+
#include "openvic-simulation/multiplayer/Constants.hpp"
8+
#include "openvic-simulation/multiplayer/PacketType.hpp"
99
#include "openvic-simulation/types/Signal.hpp"
1010
#include "openvic-simulation/utility/Marshal.hpp"
1111

1212
namespace OpenVic {
1313
struct GameManager;
1414
struct ChatManager;
1515
struct ChatMessageLog;
16+
struct BaseMultiplayerManager;
17+
struct ClientManager;
1618

1719
struct ChatGroup {
18-
using index_t = size_t;
20+
using index_type = size_t;
1921

2022
ChatGroup(ChatGroup const&) = delete;
2123
ChatGroup& operator=(ChatGroup const&) = delete;
2224
ChatGroup(ChatGroup&&) = default;
2325
ChatGroup& operator=(ChatGroup&& lhs) = default;
2426

25-
std::span<const BaseMultiplayerManager::client_id_type> get_clients() const {
27+
std::span<const client_id_type> get_clients() const {
2628
return clients;
2729
}
2830

29-
operator index_t() const {
31+
operator index_type() const {
3032
return index;
3133
}
3234

3335
private:
3436
friend struct ChatManager;
35-
ChatGroup(index_t index, memory::vector<BaseMultiplayerManager::client_id_type>&& clients);
37+
ChatGroup(index_type index, memory::vector<client_id_type>&& clients);
3638

37-
memory::vector<BaseMultiplayerManager::client_id_type> clients;
38-
index_t PROPERTY(index);
39+
memory::vector<client_id_type> clients;
40+
index_type PROPERTY(index);
3941
};
4042

4143
struct ChatManager {
@@ -44,7 +46,8 @@ namespace OpenVic {
4446
struct MessageData {
4547
MessageType type = MessageType::NONE;
4648
memory::string message;
47-
uint64_t from_index = BaseMultiplayerManager::HOST_ID;
49+
// Depending on type, either client_id_type or ChatGroup::index_type
50+
uint64_t from_index = MP_HOST_ID;
4851

4952
template<std::endian Endian>
5053
size_t encode(std::span<uint8_t> span) const {
@@ -63,7 +66,8 @@ namespace OpenVic {
6366
MessageType type = utility::decode<MessageType, Endian>(span, r_decode_count);
6467
size_t offset = r_decode_count;
6568

66-
uint64_t from_index = BaseMultiplayerManager::HOST_ID;
69+
// Depending on type, either client_id_type or ChatGroup::index_type
70+
uint64_t from_index = MP_HOST_ID;
6771
if (type != MessageType::PUBLIC) {
6872
from_index = utility::decode<uint16_t>(span.subspan(offset), r_decode_count);
6973
offset += r_decode_count;
@@ -79,32 +83,32 @@ namespace OpenVic {
7983

8084
ChatManager(ClientManager* client_manager = nullptr);
8185

82-
bool send_private_message(BaseMultiplayerManager::client_id_type to, memory::string&& message);
83-
bool send_private_message(BaseMultiplayerManager::client_id_type to, std::string_view message);
86+
bool send_private_message(client_id_type to, memory::string&& message);
87+
bool send_private_message(client_id_type to, std::string_view message);
8488
bool send_public_message(memory::string&& message);
8589
bool send_public_message(std::string_view message);
8690
bool send_group_message(ChatGroup const& group, memory::string&& message);
87-
bool send_group_message(ChatGroup::index_t group_id, memory::string&& message);
91+
bool send_group_message(ChatGroup::index_type group_id, memory::string&& message);
8892
bool send_group_message(ChatGroup const& group, std::string_view message);
89-
bool send_group_message(ChatGroup::index_t group_id, std::string_view message);
93+
bool send_group_message(ChatGroup::index_type group_id, std::string_view message);
9094

9195
signal_property<ChatManager, ChatMessageLog const&> message_logged;
9296

9397
ChatMessageLog const& log_message(
94-
BaseMultiplayerManager::client_id_type from, MessageData&& message,
98+
client_id_type from, MessageData&& message,
9599
std::chrono::time_point<std::chrono::system_clock> timestamp = std::chrono::system_clock::now()
96100
);
97101
memory::vector<ChatMessageLog> const& get_message_logs() const;
98102

99103
signal_property<ChatManager, ChatGroup const&> group_created;
100104
// group, old clients
101-
signal_property<ChatManager, ChatGroup const&, memory::vector<BaseMultiplayerManager::client_id_type>&> group_modified;
102-
signal_property<ChatManager, ChatGroup::index_t> group_deleted;
105+
signal_property<ChatManager, ChatGroup const&, memory::vector<client_id_type>&> group_modified;
106+
signal_property<ChatManager, ChatGroup::index_type> group_deleted;
103107

104-
void create_group(memory::vector<BaseMultiplayerManager::client_id_type>&& clients);
105-
void set_group(ChatGroup::index_t group, memory::vector<BaseMultiplayerManager::client_id_type>&& clients);
106-
ChatGroup const& get_group(ChatGroup::index_t group_index) const;
107-
void delete_group(ChatGroup::index_t group_id);
108+
void create_group(memory::vector<client_id_type>&& clients);
109+
void set_group(ChatGroup::index_type group, memory::vector<client_id_type>&& clients);
110+
ChatGroup const& get_group(ChatGroup::index_type group_index) const;
111+
void delete_group(ChatGroup::index_type group_id);
108112

109113
private:
110114
ClientManager* PROPERTY_PTR(client_manager);
@@ -120,26 +124,26 @@ namespace OpenVic {
120124
friend bool PacketTypes::add_chat_group_process_callback( //
121125
BaseMultiplayerManager* multiplayer_manager, PacketSpan packet
122126
);
123-
void _create_group(memory::vector<BaseMultiplayerManager::client_id_type>&& clients);
127+
void _create_group(memory::vector<client_id_type>&& clients);
124128

125129
friend bool PacketTypes::modify_chat_group_process_callback( //
126130
BaseMultiplayerManager* multiplayer_manager, PacketSpan packet
127131
);
128-
void _set_group(ChatGroup::index_t group, memory::vector<BaseMultiplayerManager::client_id_type>&& clients);
132+
void _set_group(ChatGroup::index_type group, memory::vector<client_id_type>&& clients);
129133

130134
friend bool PacketTypes::delete_chat_group_process_callback( //
131135
BaseMultiplayerManager* multiplayer_manager, PacketSpan packet
132136
);
133-
void _delete_group(ChatGroup::index_t group_id);
137+
void _delete_group(ChatGroup::index_type group_id);
134138
};
135139

136140
struct ChatMessageLog {
137-
BaseMultiplayerManager::client_id_type from_id = BaseMultiplayerManager::HOST_ID;
141+
client_id_type from_id = MP_HOST_ID;
138142
ChatManager::MessageData data;
139143
int64_t timestamp = 0;
140144

141145
ChatMessageLog() = default;
142-
ChatMessageLog(BaseMultiplayerManager::client_id_type from, ChatManager::MessageData data, int64_t timestamp)
146+
ChatMessageLog(client_id_type from, ChatManager::MessageData data, int64_t timestamp)
143147
: from_id { from }, data { data }, timestamp { timestamp } {}
144148

145149
template<std::endian Endian>

src/openvic-simulation/multiplayer/ClientManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#include "ClientManager.hpp"
32

43
#include <cstdio>
@@ -75,10 +74,11 @@ int64_t ClientManager::poll() {
7574
}
7675

7776
PacketSpan span = client.packet_span();
78-
if (client_id == INVALID_CLIENT_ID) {
77+
if (player == nullptr) {
7978
OV_ERR_FAIL_COND_V(client.get_current_sequence_value() != 0, -1);
80-
client_id = span.read<decltype(client_id)>();
79+
client_id_type client_id = span.read<decltype(client_id)>();
8180
OV_ERR_FAIL_COND_V(client_id == INVALID_CLIENT_ID, -1);
81+
player = host_session.add_player(client_id, "");
8282
return poll();
8383
}
8484

src/openvic-simulation/multiplayer/ClientManager.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#pragma once
22

33
#include <future>
4-
#include <limits>
54
#include <optional>
65

76
#include "openvic-simulation/multiplayer/BaseMultiplayerManager.hpp"
7+
#include "openvic-simulation/multiplayer/Constants.hpp"
88
#include "openvic-simulation/multiplayer/lowlevel/HostnameAddress.hpp"
9-
#include "openvic-simulation/multiplayer/lowlevel/NetworkSocket.hpp"
109
#include "openvic-simulation/multiplayer/lowlevel/ReliableUdpClient.hpp"
1110
#include "openvic-simulation/multiplayer/lowlevel/TcpPacketStream.hpp"
1211
#include "openvic-simulation/utility/Containers.hpp"
@@ -17,14 +16,14 @@ namespace OpenVic {
1716
struct ClientManager final : BaseMultiplayerManager {
1817
using BaseMultiplayerManager::BaseMultiplayerManager;
1918

20-
bool connect_to(HostnameAddress const& address, NetworkSocket::port_type port);
19+
bool connect_to(HostnameAddress const& address, socket_port_type port);
2120

2221
bool broadcast_packet(PacketType const& type, PacketType::argument_type argument) override;
2322
bool send_packet(client_id_type client_id, PacketType const& type, PacketType::argument_type argument) override;
2423
int64_t poll() override;
2524
void close() override;
2625

27-
bool connect_to_resource_server(std::optional<NetworkSocket::port_type> port = std::nullopt);
26+
bool connect_to_resource_server(std::optional<socket_port_type> port = std::nullopt);
2827
std::future<void> poll_resource_server();
2928

3029
bool is_running_as_host() const;
@@ -34,12 +33,12 @@ namespace OpenVic {
3433
return type_tag;
3534
}
3635

37-
static constexpr client_id_type INVALID_CLIENT_ID = std::numeric_limits<client_id_type>::max() - 1;
36+
static constexpr client_id_type INVALID_CLIENT_ID = MP_INVALID_CLIENT_ID;
3837

3938
private:
4039
ReliableUdpClient PROPERTY_REF(client);
4140
TcpPacketStream resource_client;
42-
client_id_type PROPERTY(client_id, INVALID_CLIENT_ID);
41+
Player const* PROPERTY(player, &Player::INVALID_PLAYER);
4342

4443
friend bool PacketTypes::update_host_session_process_callback(BaseMultiplayerManager* game_manager, PacketSpan packet);
4544

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <limits>
5+
6+
namespace OpenVic {
7+
using client_id_type = uint64_t;
8+
static constexpr client_id_type MP_HOST_ID = static_cast<client_id_type>(~0);
9+
static constexpr client_id_type MP_INVALID_CLIENT_ID = std::numeric_limits<client_id_type>::max() - 1;
10+
}

src/openvic-simulation/multiplayer/HostManager.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#include "HostManager.hpp"
32

43
#include <chrono>
@@ -33,8 +32,8 @@ HostManager::~HostManager() {
3332
this->disconnect_all();
3433
}
3534

36-
void HostManager::_on_session_changed(HostSession const& session) {
37-
broadcast_packet(PacketTypes::update_host_session, &session);
35+
void HostManager::_on_session_changed() {
36+
broadcast_packet(PacketTypes::update_host_session, &host_session);
3837
}
3938

4039
bool HostManager::listen(NetworkSocket::port_type port, HostnameAddress const& bind_address) {

0 commit comments

Comments
 (0)