summaryrefslogtreecommitdiffstats
path: root/src/network/room.cpp
diff options
context:
space:
mode:
authorB3n30 <benediktthomas@gmail.com>2017-07-09 12:26:03 +0200
committerB3n30 <benediktthomas@gmail.com>2017-07-16 21:29:49 +0200
commit42e57c121896818f9fbde5ddd9b7dbb5b2a267b1 (patch)
treeb3f27a02bc2cda201297c48fb52dfbc0efba326b /src/network/room.cpp
parentNetwork: Handle the disconnect of a client (diff)
downloadyuzu-42e57c121896818f9fbde5ddd9b7dbb5b2a267b1.tar
yuzu-42e57c121896818f9fbde5ddd9b7dbb5b2a267b1.tar.gz
yuzu-42e57c121896818f9fbde5ddd9b7dbb5b2a267b1.tar.bz2
yuzu-42e57c121896818f9fbde5ddd9b7dbb5b2a267b1.tar.lz
yuzu-42e57c121896818f9fbde5ddd9b7dbb5b2a267b1.tar.xz
yuzu-42e57c121896818f9fbde5ddd9b7dbb5b2a267b1.tar.zst
yuzu-42e57c121896818f9fbde5ddd9b7dbb5b2a267b1.zip
Diffstat (limited to '')
-rw-r--r--src/network/room.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/network/room.cpp b/src/network/room.cpp
index 6dc7db341..0fdb5e68f 100644
--- a/src/network/room.cpp
+++ b/src/network/room.cpp
@@ -107,6 +107,12 @@ public:
void HandleWifiPacket(const ENetEvent* event);
/**
+ * Extracts a chat entry from a received ENet packet and adds it to the chat queue.
+ * @param event The ENet event that was received.
+ */
+ void HandleChatPacket(const ENetEvent* event);
+
+ /**
* Removes the client from the members list if it was in it and announces the change
* to all other clients.
*/
@@ -128,6 +134,9 @@ void Room::RoomImpl::ServerLoop() {
case IdWifiPacket:
HandleWifiPacket(&event);
break;
+ case IdChatMessage:
+ HandleChatPacket(&event);
+ break;
}
enet_packet_destroy(event.packet);
break;
@@ -271,6 +280,35 @@ void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) {
enet_host_flush(server);
}
+void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) {
+ Packet in_packet;
+ in_packet.Append(event->packet->data, event->packet->dataLength);
+
+ in_packet.IgnoreBytes(sizeof(MessageID));
+ std::string message;
+ in_packet >> message;
+ auto CompareNetworkAddress = [&](const Member member) -> bool {
+ return member.peer == event->peer;
+ };
+ const auto sending_member = std::find_if(members.begin(), members.end(), CompareNetworkAddress);
+ if (sending_member == members.end()) {
+ return; // Received a chat message from a unknown sender
+ }
+
+ Packet out_packet;
+ out_packet << static_cast<MessageID>(IdChatMessage);
+ out_packet << sending_member->nickname;
+ out_packet << message;
+
+ ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(),
+ ENET_PACKET_FLAG_RELIABLE);
+ for (auto it = members.begin(); it != members.end(); ++it) {
+ if (it->peer != event->peer)
+ enet_peer_send(it->peer, 0, enet_packet);
+ }
+ enet_host_flush(server);
+}
+
void Room::RoomImpl::HandleClientDisconnection(ENetPeer* client) {
// Remove the client from the members list.
members.erase(std::remove_if(members.begin(), members.end(),