/// <summary> /// When we get a message from a client, we handle the message here /// and perform any necessary tasks. /// </summary> private void ListenerOnNetworkReceiveEvent(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod) { try { // Parse this message bool relayOnServer = CommandReceiver.Parse(reader, peer); if (relayOnServer) { // Copy relevant message part (exclude protocol headers) byte[] data = new byte[reader.UserDataSize]; Array.Copy(reader.RawData, reader.UserDataOffset, data, 0, reader.UserDataSize); // Send this message to all other clients var peers = _netServer.ConnectedPeerList; foreach (var client in peers) { // Don't send the message back to the client that sent it. if (client.Id == peer.Id) { continue; } // Send the message so the other client can stay in sync client.Send(data, DeliveryMethod.ReliableOrdered); } } } catch (Exception ex) { ChatLogPanel.PrintGameMessage(ChatLogPanel.MessageType.Error, "Error while parsing command. See log."); _logger.Error(ex, $"Encountered an error while reading command from {peer.EndPoint.Address}:{peer.EndPoint.Port}:"); } }
/// <summary> /// When we get a message from the server, we handle the message here /// and perform any necessary tasks. /// </summary> private void ListenerOnNetworkReceiveEvent(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod) { try { CommandReceiver.Parse(reader, peer); } catch (Exception ex) { _logger.Error(ex, $"Encountered an error while reading command from {peer.EndPoint.Address}:{peer.EndPoint.Port}:"); } }