2018-06-07 13:41:08 +00:00
|
|
|
#if ENABLE_UNET
|
2018-08-06 16:50:11 +00:00
|
|
|
using System;
|
2018-06-07 13:41:08 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace UnityEngine.Networking
|
|
|
|
{
|
|
|
|
sealed class LocalClient : NetworkClient
|
|
|
|
{
|
|
|
|
struct InternalMsg
|
|
|
|
{
|
2018-08-06 16:50:11 +00:00
|
|
|
internal ushort msgType;
|
|
|
|
internal byte[] content;
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List<InternalMsg> m_InternalMsgs = new List<InternalMsg>();
|
|
|
|
List<InternalMsg> m_InternalMsgs2 = new List<InternalMsg>();
|
|
|
|
|
|
|
|
bool m_Connected;
|
|
|
|
|
|
|
|
public override void Disconnect()
|
|
|
|
{
|
|
|
|
ClientScene.HandleClientDisconnect(m_Connection);
|
|
|
|
if (m_Connected)
|
|
|
|
{
|
2018-07-19 19:03:00 +00:00
|
|
|
PostInternalMessage((short)MsgType.Disconnect);
|
2018-06-07 13:41:08 +00:00
|
|
|
m_Connected = false;
|
|
|
|
}
|
2018-08-14 09:13:02 +00:00
|
|
|
connectState = ConnectState.Disconnected;
|
2018-06-22 18:34:35 +00:00
|
|
|
NetworkServer.RemoveLocalClient(m_Connection);
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal void InternalConnectLocalServer(bool generateConnectMsg)
|
|
|
|
{
|
2018-06-22 18:34:35 +00:00
|
|
|
m_Connection = new ULocalConnectionToServer();
|
2018-06-07 13:41:08 +00:00
|
|
|
SetHandlers(m_Connection);
|
2018-06-22 18:34:35 +00:00
|
|
|
m_Connection.connectionId = NetworkServer.AddLocalClient(this);
|
2018-08-14 09:13:02 +00:00
|
|
|
connectState = ConnectState.Connected;
|
2018-06-07 13:41:08 +00:00
|
|
|
|
|
|
|
SetActive(true);
|
|
|
|
RegisterSystemHandlers(true);
|
|
|
|
|
|
|
|
if (generateConnectMsg)
|
|
|
|
{
|
2018-07-19 19:03:00 +00:00
|
|
|
PostInternalMessage((short)MsgType.Connect);
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
m_Connected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal override void Update()
|
|
|
|
{
|
|
|
|
ProcessInternalMessages();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called by the server to set the LocalClient's LocalPlayer object during NetworkServer.AddPlayer()
|
|
|
|
internal void AddLocalPlayer(PlayerController localPlayer)
|
|
|
|
{
|
|
|
|
if (LogFilter.logDev) Debug.Log("Local client AddLocalPlayer " + localPlayer.gameObject.name + " conn=" + m_Connection.connectionId);
|
|
|
|
m_Connection.isReady = true;
|
|
|
|
m_Connection.SetPlayerController(localPlayer);
|
|
|
|
var uv = localPlayer.unetView;
|
|
|
|
if (uv != null)
|
|
|
|
{
|
|
|
|
ClientScene.SetLocalObject(uv.netId, localPlayer.gameObject);
|
|
|
|
uv.SetConnectionToServer(m_Connection);
|
|
|
|
}
|
|
|
|
// there is no SystemOwnerMessage for local client. add to ClientScene here instead
|
|
|
|
ClientScene.InternalAddPlayer(uv, localPlayer.playerControllerId);
|
|
|
|
}
|
|
|
|
|
2018-08-14 20:15:34 +00:00
|
|
|
private void PostInternalMessage(short msgType, byte[] content)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
2018-06-11 08:34:32 +00:00
|
|
|
InternalMsg msg = new InternalMsg();
|
2018-08-06 16:50:11 +00:00
|
|
|
msg.msgType = (ushort)msgType;
|
|
|
|
msg.content = content;
|
2018-06-07 13:41:08 +00:00
|
|
|
m_InternalMsgs.Add(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void PostInternalMessage(short msgType)
|
|
|
|
{
|
2018-08-06 16:50:11 +00:00
|
|
|
// call PostInternalMessage with empty content array if we just want to call a message like Connect
|
2018-08-20 13:35:24 +00:00
|
|
|
// -> original NetworkTransport used empty [] and not null array for those messages too
|
2018-08-14 20:15:34 +00:00
|
|
|
PostInternalMessage(msgType, new byte[0]);
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void ProcessInternalMessages()
|
|
|
|
{
|
|
|
|
if (m_InternalMsgs.Count == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// new msgs will get put in m_InternalMsgs2
|
|
|
|
List<InternalMsg> tmp = m_InternalMsgs;
|
|
|
|
m_InternalMsgs = m_InternalMsgs2;
|
|
|
|
|
|
|
|
// iterate through existing set
|
|
|
|
for (int i = 0; i < tmp.Count; i++)
|
|
|
|
{
|
2018-08-06 16:50:11 +00:00
|
|
|
InternalMsg msg = tmp[i];
|
|
|
|
|
2018-06-12 13:34:22 +00:00
|
|
|
NetworkMessage internalMessage = new NetworkMessage();
|
2018-08-06 16:50:11 +00:00
|
|
|
internalMessage.msgType = (short)msg.msgType;
|
|
|
|
internalMessage.reader = new NetworkReader(msg.content);
|
2018-06-12 13:34:22 +00:00
|
|
|
internalMessage.conn = connection;
|
|
|
|
|
|
|
|
m_Connection.InvokeHandler(internalMessage);
|
2018-06-07 13:41:08 +00:00
|
|
|
connection.lastMessageTime = Time.time;
|
|
|
|
}
|
|
|
|
|
|
|
|
// put m_InternalMsgs back and clear it
|
|
|
|
m_InternalMsgs = tmp;
|
|
|
|
m_InternalMsgs.Clear();
|
|
|
|
|
|
|
|
// add any newly generated msgs in m_InternalMsgs2 and clear it
|
2018-06-11 08:19:52 +00:00
|
|
|
m_InternalMsgs.AddRange(m_InternalMsgs2);
|
2018-06-07 13:41:08 +00:00
|
|
|
m_InternalMsgs2.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// called by the server, to bypass network
|
2018-08-14 20:15:34 +00:00
|
|
|
internal void InvokeBytesOnClient(byte[] buffer)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
2018-08-06 16:50:11 +00:00
|
|
|
// unpack message and post to internal list for processing
|
|
|
|
ushort msgType;
|
|
|
|
byte[] content;
|
|
|
|
if (Protocol.UnpackMessage(buffer, out msgType, out content))
|
|
|
|
{
|
2018-08-14 20:15:34 +00:00
|
|
|
PostInternalMessage((short)msgType, content);
|
2018-08-06 16:50:11 +00:00
|
|
|
}
|
|
|
|
else if (LogFilter.logError) Debug.LogError("InvokeBytesOnClient failed to unpack message: " + BitConverter.ToString(buffer));
|
2018-06-07 13:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif //ENABLE_UNET
|