Skip to content

Commit 9359c24

Browse files
committed
Update project and mirror
1 parent 254a0ea commit 9359c24

File tree

273 files changed

+9864
-8183
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

273 files changed

+9864
-8183
lines changed

Assets/Mirror/Authenticators/BasicAuthenticator.cs

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,50 @@ public class BasicAuthenticator : NetworkAuthenticator
1414
public string username;
1515
public string password;
1616

17-
public class AuthRequestMessage : MessageBase
17+
#region Messages
18+
19+
public struct AuthRequestMessage : NetworkMessage
1820
{
1921
// use whatever credentials make sense for your game
2022
// for example, you might want to pass the accessToken if using oauth
2123
public string authUsername;
2224
public string authPassword;
2325
}
2426

25-
public class AuthResponseMessage : MessageBase
27+
public struct AuthResponseMessage : NetworkMessage
2628
{
2729
public byte code;
2830
public string message;
2931
}
3032

33+
#endregion
34+
35+
#region Server
36+
37+
/// <summary>
38+
/// Called on server from StartServer to initialize the Authenticator
39+
/// <para>Server message handlers should be registered in this method.</para>
40+
/// </summary>
3141
public override void OnStartServer()
3242
{
3343
// register a handler for the authentication request we expect from client
3444
NetworkServer.RegisterHandler<AuthRequestMessage>(OnAuthRequestMessage, false);
3545
}
3646

37-
public override void OnStartClient()
38-
{
39-
// register a handler for the authentication response we expect from server
40-
NetworkClient.RegisterHandler<AuthResponseMessage>(OnAuthResponseMessage, false);
41-
}
42-
47+
/// <summary>
48+
/// Called on server from OnServerAuthenticateInternal when a client needs to authenticate
49+
/// </summary>
50+
/// <param name="conn">Connection to client.</param>
4351
public override void OnServerAuthenticate(NetworkConnection conn)
4452
{
4553
// do nothing...wait for AuthRequestMessage from client
4654
}
4755

48-
public override void OnClientAuthenticate(NetworkConnection conn)
49-
{
50-
AuthRequestMessage authRequestMessage = new AuthRequestMessage
51-
{
52-
authUsername = username,
53-
authPassword = password
54-
};
55-
56-
conn.Send(authRequestMessage);
57-
}
58-
56+
/// <summary>
57+
/// Called on server when the client's AuthRequestMessage arrives
58+
/// </summary>
59+
/// <param name="conn">Connection to client.</param>
60+
/// <param name="msg">The message payload</param>
5961
public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg)
6062
{
6163
if (logger.LogEnabled()) logger.LogFormat(LogType.Log, "Authentication Request: {0} {1}", msg.authUsername, msg.authPassword);
@@ -72,8 +74,8 @@ public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg)
7274

7375
conn.Send(authResponseMessage);
7476

75-
// Invoke the event to complete a successful authentication
76-
OnServerAuthenticated.Invoke(conn);
77+
// Accept the successful authentication
78+
ServerAccept(conn);
7779
}
7880
else
7981
{
@@ -94,31 +96,66 @@ public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg)
9496
}
9597
}
9698

97-
public IEnumerator DelayedDisconnect(NetworkConnection conn, float waitTime)
99+
IEnumerator DelayedDisconnect(NetworkConnection conn, float waitTime)
98100
{
99101
yield return new WaitForSeconds(waitTime);
100-
conn.Disconnect();
102+
103+
// Reject the unsuccessful authentication
104+
ServerReject(conn);
101105
}
102106

107+
#endregion
108+
109+
#region Client
110+
111+
/// <summary>
112+
/// Called on client from StartClient to initialize the Authenticator
113+
/// <para>Client message handlers should be registered in this method.</para>
114+
/// </summary>
115+
public override void OnStartClient()
116+
{
117+
// register a handler for the authentication response we expect from server
118+
NetworkClient.RegisterHandler<AuthResponseMessage>(OnAuthResponseMessage, false);
119+
}
120+
121+
/// <summary>
122+
/// Called on client from OnClientAuthenticateInternal when a client needs to authenticate
123+
/// </summary>
124+
/// <param name="conn">Connection of the client.</param>
125+
public override void OnClientAuthenticate(NetworkConnection conn)
126+
{
127+
AuthRequestMessage authRequestMessage = new AuthRequestMessage
128+
{
129+
authUsername = username,
130+
authPassword = password
131+
};
132+
133+
conn.Send(authRequestMessage);
134+
}
135+
136+
/// <summary>
137+
/// Called on client when the server's AuthResponseMessage arrives
138+
/// </summary>
139+
/// <param name="conn">Connection to client.</param>
140+
/// <param name="msg">The message payload</param>
103141
public void OnAuthResponseMessage(NetworkConnection conn, AuthResponseMessage msg)
104142
{
105143
if (msg.code == 100)
106144
{
107145
if (logger.LogEnabled()) logger.LogFormat(LogType.Log, "Authentication Response: {0}", msg.message);
108146

109-
// Invoke the event to complete a successful authentication
110-
OnClientAuthenticated.Invoke(conn);
147+
// Authentication has been accepted
148+
ClientAccept(conn);
111149
}
112150
else
113151
{
114152
logger.LogFormat(LogType.Error, "Authentication Response: {0}", msg.message);
115153

116-
// Set this on the client for local reference
117-
conn.isAuthenticated = false;
118-
119-
// disconnect the client
120-
conn.Disconnect();
154+
// Authentication has been rejected
155+
ClientReject(conn);
121156
}
122157
}
158+
159+
#endregion
123160
}
124161
}

Assets/Mirror/Authenticators/TimeoutAuthenticator.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ public void Awake()
2323
authenticator.OnServerAuthenticated.AddListener(connection => OnServerAuthenticated.Invoke(connection));
2424
}
2525

26+
public override void OnStartClient()
27+
{
28+
authenticator.OnStartClient();
29+
}
30+
31+
public override void OnStartServer()
32+
{
33+
authenticator.OnStartServer();
34+
}
35+
2636
public override void OnClientAuthenticate(NetworkConnection conn)
2737
{
2838
authenticator.OnClientAuthenticate(conn);

Assets/Mirror/CompilerSymbols/PreprocessorDefine.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public static void AddDefineSymbols()
3030
"MIRROR_13_0_OR_NEWER",
3131
"MIRROR_14_0_OR_NEWER",
3232
"MIRROR_15_0_OR_NEWER",
33-
"MIRROR_16_0_OR_NEWER"
33+
"MIRROR_16_0_OR_NEWER",
34+
"MIRROR_17_0_OR_NEWER",
35+
"MIRROR_18_0_OR_NEWER",
36+
"MIRROR_24_0_OR_NEWER",
37+
"MIRROR_26_0_OR_NEWER"
3438
};
3539

3640
// only touch PlayerSettings if we actually modified it.

Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ namespace Mirror.Discovery
1818
[DisallowMultipleComponent]
1919
[HelpURL("https://mirror-networking.com/docs/Components/NetworkDiscovery.html")]
2020
public abstract class NetworkDiscoveryBase<Request, Response> : MonoBehaviour
21-
where Request : IMessageBase, new()
22-
where Response : IMessageBase, new()
21+
where Request : NetworkMessage
22+
where Response : NetworkMessage
2323
{
2424
public static bool SupportedOnThisPlatform { get { return Application.platform != RuntimePlatform.WebGLPlayer; } }
2525

@@ -29,7 +29,7 @@ public abstract class NetworkDiscoveryBase<Request, Response> : MonoBehaviour
2929

3030
[SerializeField]
3131
[Tooltip("The UDP port the server will listen for multi-cast messages")]
32-
int serverBroadcastListenPort = 47777;
32+
protected int serverBroadcastListenPort = 47777;
3333

3434
[SerializeField]
3535
[Tooltip("Time in seconds between multi-cast messages")]
@@ -62,11 +62,10 @@ public static long RandomLong()
6262
/// </summary>
6363
public virtual void Start()
6464
{
65-
// headless mode? then start advertising
66-
if (NetworkManager.isHeadless)
67-
{
68-
AdvertiseServer();
69-
}
65+
// Server mode? then start advertising
66+
#if UNITY_SERVER
67+
AdvertiseServer();
68+
#endif
7069
}
7170

7271
// Ensure the ports are cleared no matter when Game/Unity UI exits
@@ -166,8 +165,7 @@ async Task ReceiveRequestAsync(UdpClient udpClient)
166165
throw new ProtocolViolationException("Invalid handshake");
167166
}
168167

169-
Request request = new Request();
170-
request.Deserialize(networkReader);
168+
Request request = networkReader.Read<Request>();
171169

172170
ProcessClientRequest(request, udpReceiveResult.RemoteEndPoint);
173171
}
@@ -195,7 +193,7 @@ protected virtual void ProcessClientRequest(Request request, IPEndPoint endpoint
195193
{
196194
writer.WriteInt64(secretHandshake);
197195

198-
info.Serialize(writer);
196+
writer.Write(info);
199197

200198
ArraySegment<byte> data = writer.ToArraySegment();
201199
// signature matches
@@ -306,7 +304,7 @@ public void BroadcastDiscoveryRequest()
306304
{
307305
Request request = GetRequest();
308306

309-
request.Serialize(writer);
307+
writer.Write(request);
310308

311309
ArraySegment<byte> data = writer.ToArraySegment();
312310

@@ -326,7 +324,7 @@ public void BroadcastDiscoveryRequest()
326324
/// Override if you wish to include additional data in the discovery message
327325
/// such as desired game mode, language, difficulty, etc... </remarks>
328326
/// <returns>An instance of ServerRequest with data to be broadcasted</returns>
329-
protected virtual Request GetRequest() => new Request();
327+
protected virtual Request GetRequest() => default;
330328

331329
async Task ReceiveGameBroadcastAsync(UdpClient udpClient)
332330
{
@@ -340,8 +338,7 @@ async Task ReceiveGameBroadcastAsync(UdpClient udpClient)
340338
if (networkReader.ReadInt64() != secretHandshake)
341339
return;
342340

343-
Response response = new Response();
344-
response.Deserialize(networkReader);
341+
Response response = networkReader.Read<Response>();
345342

346343
ProcessResponse(response, udpReceiveResult.RemoteEndPoint);
347344
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
namespace Mirror.Discovery
22
{
3-
public class ServerRequest : MessageBase { }
3+
public struct ServerRequest : NetworkMessage { }
44
}

Assets/Mirror/Components/Discovery/ServerResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Mirror.Discovery
55
{
6-
public class ServerResponse : MessageBase
6+
public struct ServerResponse : NetworkMessage
77
{
88
// The server that sent this
99
// this is a property so that it is not serialized, but the
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using UnityEngine;
2+
3+
namespace Mirror.Experimental
4+
{
5+
[AddComponentMenu("Network/Experimental/NetworkLerpRigidbody")]
6+
[HelpURL("https://mirror-networking.com/docs/Components/NetworkLerpRigidbody.html")]
7+
public class NetworkLerpRigidbody : NetworkBehaviour
8+
{
9+
[Header("Settings")]
10+
[SerializeField] internal Rigidbody target = null;
11+
[Tooltip("How quickly current velocity approaches target velocity")]
12+
[SerializeField] float lerpVelocityAmount = 0.5f;
13+
[Tooltip("How quickly current position approaches target position")]
14+
[SerializeField] float lerpPositionAmount = 0.5f;
15+
16+
[Tooltip("Set to true if moves come from owner client, set to false if moves always come from server")]
17+
[SerializeField] bool clientAuthority = false;
18+
19+
float nextSyncTime;
20+
21+
22+
[SyncVar()]
23+
Vector3 targetVelocity;
24+
25+
[SyncVar()]
26+
Vector3 targetPosition;
27+
28+
/// <summary>
29+
/// Ignore value if is host or client with Authority
30+
/// </summary>
31+
/// <returns></returns>
32+
bool IgnoreSync => isServer || ClientWithAuthority;
33+
34+
bool ClientWithAuthority => clientAuthority && hasAuthority;
35+
36+
void OnValidate()
37+
{
38+
if (target == null)
39+
{
40+
target = GetComponent<Rigidbody>();
41+
}
42+
}
43+
44+
void Update()
45+
{
46+
if (isServer)
47+
{
48+
SyncToClients();
49+
}
50+
else if (ClientWithAuthority)
51+
{
52+
SendToServer();
53+
}
54+
}
55+
56+
private void SyncToClients()
57+
{
58+
targetVelocity = target.velocity;
59+
targetPosition = target.position;
60+
}
61+
62+
private void SendToServer()
63+
{
64+
float now = Time.time;
65+
if (now > nextSyncTime)
66+
{
67+
nextSyncTime = now + syncInterval;
68+
CmdSendState(target.velocity, target.position);
69+
}
70+
}
71+
72+
[Command]
73+
private void CmdSendState(Vector3 velocity, Vector3 position)
74+
{
75+
target.velocity = velocity;
76+
target.position = position;
77+
targetVelocity = velocity;
78+
targetPosition = position;
79+
}
80+
81+
void FixedUpdate()
82+
{
83+
if (IgnoreSync) { return; }
84+
85+
target.velocity = Vector3.Lerp(target.velocity, targetVelocity, lerpVelocityAmount);
86+
target.position = Vector3.Lerp(target.position, targetPosition, lerpPositionAmount);
87+
// add velocity to position as position would have moved on server at that velocity
88+
targetPosition += target.velocity * Time.fixedDeltaTime;
89+
90+
// TODO does this also need to sync acceleration so and update velocity?
91+
}
92+
}
93+
}

Assets/Mirror/Runtime/Transport/Websocket/WebsocketTransport.cs.meta renamed to Assets/Mirror/Components/Experimental/NetworkLerpRigidbody.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)