Messages.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using UnityEngine;
  3. namespace Mirror
  4. {
  5. // need to send time every sendInterval.
  6. // batching automatically includes remoteTimestamp.
  7. // all we need to do is ensure that an empty message is sent.
  8. // and react to it.
  9. // => we don't want to insert a snapshot on every batch.
  10. // => do it exactly every sendInterval on every TimeSnapshotMessage.
  11. public struct TimeSnapshotMessage : NetworkMessage {}
  12. public struct ReadyMessage : NetworkMessage {}
  13. public struct NotReadyMessage : NetworkMessage {}
  14. public struct AddPlayerMessage : NetworkMessage {}
  15. public struct SceneMessage : NetworkMessage
  16. {
  17. public string sceneName;
  18. // Normal = 0, LoadAdditive = 1, UnloadAdditive = 2
  19. public SceneOperation sceneOperation;
  20. public bool customHandling;
  21. }
  22. public enum SceneOperation : byte
  23. {
  24. Normal,
  25. LoadAdditive,
  26. UnloadAdditive
  27. }
  28. public struct CommandMessage : NetworkMessage
  29. {
  30. public uint netId;
  31. public byte componentIndex;
  32. public ushort functionHash;
  33. // the parameters for the Cmd function
  34. // -> ArraySegment to avoid unnecessary allocations
  35. public ArraySegment<byte> payload;
  36. }
  37. public struct RpcMessage : NetworkMessage
  38. {
  39. public uint netId;
  40. public byte componentIndex;
  41. public ushort functionHash;
  42. // the parameters for the Cmd function
  43. // -> ArraySegment to avoid unnecessary allocations
  44. public ArraySegment<byte> payload;
  45. }
  46. public struct SpawnMessage : NetworkMessage
  47. {
  48. // netId of new or existing object
  49. public uint netId;
  50. public bool isLocalPlayer;
  51. // Sets hasAuthority on the spawned object
  52. public bool isOwner;
  53. public ulong sceneId;
  54. // If sceneId != 0 then it is used instead of assetId
  55. public uint assetId;
  56. // Local position
  57. public Vector3 position;
  58. // Local rotation
  59. public Quaternion rotation;
  60. // Local scale
  61. public Vector3 scale;
  62. // serialized component data
  63. // ArraySegment to avoid unnecessary allocations
  64. public ArraySegment<byte> payload;
  65. }
  66. public struct ChangeOwnerMessage : NetworkMessage
  67. {
  68. public uint netId;
  69. public bool isOwner;
  70. public bool isLocalPlayer;
  71. }
  72. public struct ObjectSpawnStartedMessage : NetworkMessage {}
  73. public struct ObjectSpawnFinishedMessage : NetworkMessage {}
  74. public struct ObjectDestroyMessage : NetworkMessage
  75. {
  76. public uint netId;
  77. }
  78. public struct ObjectHideMessage : NetworkMessage
  79. {
  80. public uint netId;
  81. }
  82. public struct EntityStateMessage : NetworkMessage
  83. {
  84. public uint netId;
  85. // the serialized component data
  86. // -> ArraySegment to avoid unnecessary allocations
  87. public ArraySegment<byte> payload;
  88. }
  89. // whoever wants to measure rtt, sends this to the other end.
  90. public struct NetworkPingMessage : NetworkMessage
  91. {
  92. // local time is used to calculate round trip time,
  93. // and to calculate the predicted time offset.
  94. public double localTime;
  95. // predicted time is sent to compare the final error, for debugging only
  96. public double predictedTimeAdjusted;
  97. public NetworkPingMessage(double localTime, double predictedTimeAdjusted)
  98. {
  99. this.localTime = localTime;
  100. this.predictedTimeAdjusted = predictedTimeAdjusted;
  101. }
  102. }
  103. // the other end responds with this message.
  104. // we can use this to calculate rtt.
  105. public struct NetworkPongMessage : NetworkMessage
  106. {
  107. // local time is used to calculate round trip time.
  108. public double localTime;
  109. // predicted error is used to adjust the predicted timeline.
  110. public double predictionErrorUnadjusted;
  111. public double predictionErrorAdjusted; // for debug purposes
  112. public NetworkPongMessage(double localTime, double predictionErrorUnadjusted, double predictionErrorAdjusted)
  113. {
  114. this.localTime = localTime;
  115. this.predictionErrorUnadjusted = predictionErrorUnadjusted;
  116. this.predictionErrorAdjusted = predictionErrorAdjusted;
  117. }
  118. }
  119. }