Skip to content

Commit 83d16f8

Browse files
committed
WIP
1 parent 8a2be72 commit 83d16f8

20 files changed

+220
-101
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: 41 additions & 32 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,17 +46,23 @@ namespace OpenVic {
4446
struct MessageData {
4547
MessageType type = MessageType::NONE;
4648
memory::string message;
47-
uint64_t from_index = BaseMultiplayerManager::HOST_ID;
49+
union {
50+
client_id_type to_client = MP_HOST_ID;
51+
ChatGroup::index_type to_group;
52+
};
4853

4954
template<std::endian Endian>
5055
size_t encode(std::span<uint8_t> span) const {
5156
size_t offset = utility::encode(type, span, utility::endian_tag<Endian>);
5257
if (type != MessageType::PUBLIC) {
53-
offset += utility::encode<
54-
uint16_t>(from_index, !span.empty() ? span.subspan(offset) : span, utility::endian_tag<Endian>);
58+
offset += //
59+
utility::encode<uint16_t, Endian>(
60+
to_client, //
61+
span.empty() ? span : span.subspan(offset) //
62+
);
5563
}
5664

57-
return utility::encode(message, !span.empty() ? span.subspan(offset) : span, utility::endian_tag<Endian>) +
65+
return utility::encode(message, span.empty() ? span : span.subspan(offset), utility::endian_tag<Endian>) +
5866
offset;
5967
}
6068

@@ -63,14 +71,15 @@ namespace OpenVic {
6371
MessageType type = utility::decode<MessageType, Endian>(span, r_decode_count);
6472
size_t offset = r_decode_count;
6573

66-
uint64_t from_index = BaseMultiplayerManager::HOST_ID;
74+
// Depending on type, either client_id_type or ChatGroup::index_type
75+
decltype(to_client) to_index = MP_HOST_ID;
6776
if (type != MessageType::PUBLIC) {
68-
from_index = utility::decode<uint16_t>(span.subspan(offset), r_decode_count);
77+
to_index = utility::decode<uint16_t, Endian>(span.subspan(offset), r_decode_count);
6978
offset += r_decode_count;
7079
}
7180

7281
MessageData data {
73-
type, utility::decode<decltype(message), Endian>(span.subspan(offset), r_decode_count), from_index //
82+
type, utility::decode<decltype(message), Endian>(span.subspan(offset), r_decode_count), to_index //
7483
};
7584
r_decode_count += offset;
7685
return data;
@@ -79,32 +88,32 @@ namespace OpenVic {
7988

8089
ChatManager(ClientManager* client_manager = nullptr);
8190

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);
91+
bool send_private_message(client_id_type to, memory::string&& message);
92+
bool send_private_message(client_id_type to, std::string_view message);
8493
bool send_public_message(memory::string&& message);
8594
bool send_public_message(std::string_view message);
8695
bool send_group_message(ChatGroup const& group, memory::string&& message);
87-
bool send_group_message(ChatGroup::index_t group_id, memory::string&& message);
96+
bool send_group_message(ChatGroup::index_type group_id, memory::string&& message);
8897
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);
98+
bool send_group_message(ChatGroup::index_type group_id, std::string_view message);
9099

91100
signal_property<ChatManager, ChatMessageLog const&> message_logged;
92101

93102
ChatMessageLog const& log_message(
94-
BaseMultiplayerManager::client_id_type from, MessageData&& message,
103+
client_id_type from, MessageData&& message,
95104
std::chrono::time_point<std::chrono::system_clock> timestamp = std::chrono::system_clock::now()
96105
);
97106
memory::vector<ChatMessageLog> const& get_message_logs() const;
98107

99108
signal_property<ChatManager, ChatGroup const&> group_created;
100-
// 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;
109+
signal_property<ChatManager, ChatGroup const& /* group */, memory::vector<client_id_type>& /* old clients */>
110+
group_modified;
111+
signal_property<ChatManager, ChatGroup::index_type> group_deleted;
103112

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);
113+
void create_group(memory::vector<client_id_type>&& clients);
114+
void set_group(ChatGroup::index_type group, memory::vector<client_id_type>&& clients);
115+
ChatGroup const& get_group(ChatGroup::index_type group_index) const;
116+
void delete_group(ChatGroup::index_type group_id);
108117

109118
private:
110119
ClientManager* PROPERTY_PTR(client_manager);
@@ -120,26 +129,26 @@ namespace OpenVic {
120129
friend bool PacketTypes::add_chat_group_process_callback( //
121130
BaseMultiplayerManager* multiplayer_manager, PacketSpan packet
122131
);
123-
void _create_group(memory::vector<BaseMultiplayerManager::client_id_type>&& clients);
132+
void _create_group(memory::vector<client_id_type>&& clients);
124133

125134
friend bool PacketTypes::modify_chat_group_process_callback( //
126135
BaseMultiplayerManager* multiplayer_manager, PacketSpan packet
127136
);
128-
void _set_group(ChatGroup::index_t group, memory::vector<BaseMultiplayerManager::client_id_type>&& clients);
137+
void _set_group(ChatGroup::index_type group, memory::vector<client_id_type>&& clients);
129138

130139
friend bool PacketTypes::delete_chat_group_process_callback( //
131140
BaseMultiplayerManager* multiplayer_manager, PacketSpan packet
132141
);
133-
void _delete_group(ChatGroup::index_t group_id);
142+
void _delete_group(ChatGroup::index_type group_id);
134143
};
135144

136145
struct ChatMessageLog {
137-
BaseMultiplayerManager::client_id_type from_id = BaseMultiplayerManager::HOST_ID;
146+
client_id_type from_id = MP_HOST_ID;
138147
ChatManager::MessageData data;
139148
int64_t timestamp = 0;
140149

141150
ChatMessageLog() = default;
142-
ChatMessageLog(BaseMultiplayerManager::client_id_type from, ChatManager::MessageData data, int64_t timestamp)
151+
ChatMessageLog(client_id_type from, ChatManager::MessageData data, int64_t timestamp)
143152
: from_id { from }, data { data }, timestamp { timestamp } {}
144153

145154
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+
}

0 commit comments

Comments
 (0)